FAQ: Build your own Mini-Blockchain! - Checking for a Broken Chain

This community-built FAQ covers the “Checking for a Broken Chain” exercise from the lesson “Build your own Mini-Blockchain!”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Introduction to Blockchain

FAQs on the exercise Checking for a Broken Chain

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

  def validate_chain(self):
    for i in range(1, len(self.chain)):
      current = self.chain[i]
      previous = self.chain[i-1]
      if current.hash != current.generate_hash():
        return False
      if previous.hash != previous.generate_hash():
        return False
      return True

In another class:

class Block:
    def __init__(self, transactions, previous_hash):
        self.time_stamp = datetime.datetime.now()
        self.transactions = transactions
        self.previous_hash = previous_hash
        self.nonce = 0
        self.hash = self.generate_hash()

    def generate_hash(self):
        block_header = str(self.time_stamp) + str(self.transactions) +str(self.previous_hash) + str(self.nonce)
        block_hash = sha256(block_header.encode())
        return block_hash.hexdigest()

According to instruction the function of validate_chain() method is:

In order to validate the entire blockchain, we must loop through each of the blocks stored inside the blockchain itself. Then, we will check through each of the blocks to ensure that the previous hash value matches with the hash value inside our previous block.

My question is:
What is the difference between using .hash and .generate_hash() ?

.hash, this is a variable that can be changed. .generate_hash () returns a .hash that cannot be changed.

I believe hash is an instance variable that is created at when the block is instantiated. The method “generate_hash” is called during the comparison and we would expect that these two would not be equivalent, as for one thing the timestamp would be different.

I hope this helps