From 3a411c281992d3669941913c5caf43c870ef3fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSpekulaas=E2=80=9D?= <“ryan@aterve.nl”> Date: Fri, 22 Sep 2023 15:57:57 +0200 Subject: [PATCH] mistake ex3 --- .../405_HW3_Tamper-Proof_Chain/BlockChain.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/period_1/04-blockchain_class/405_HW3_Tamper-Proof_Chain/BlockChain.py b/period_1/04-blockchain_class/405_HW3_Tamper-Proof_Chain/BlockChain.py index e2344fc..fd1287c 100644 --- a/period_1/04-blockchain_class/405_HW3_Tamper-Proof_Chain/BlockChain.py +++ b/period_1/04-blockchain_class/405_HW3_Tamper-Proof_Chain/BlockChain.py @@ -63,6 +63,12 @@ class CBlock: # Make sure to compute the hash value of the current block and store it properly def mine(self, leading_zeros): self.nonce = 0 + + if self.previousBlock: + self.previousHash = self.previousBlock.blockHash + else: + self.previousHash = None + while True: digest = self.computeHash() @@ -72,15 +78,23 @@ class CBlock: break self.nonce += 1 - + # TODO 4: Check if the current block contains valid hash digest values # Make sure to distinguish between the genesis block and other blocks # Make sure to compare both hash digest values: # The computed digest of the current block # The stored digest of the previous block # return the result of all comparisons as a boolean value + # def is_valid_hash(self): + # if(self.previousBlock == None): + # return True + + # if(self.computeHash() != self.blockHash or self.previousBlock.blockHash != self.previousBlock.computeHash()): + # return False + # return self.previousBlock.is_valid_hash() + def is_valid_hash(self): - if self.previousBlock == None: - return True - + if self.previousBlock: + self.previousHash = self.previousBlock.computeHash() + return self.computeHash() == self.blockHash \ No newline at end of file