From 029350c7e60ab0ef937a3a6fd2b5e0ecd34b7324 Mon Sep 17 00:00:00 2001 From: David W Grossman <2gotgrossman@gmail.com> Date: Sun, 8 Oct 2017 11:59:36 -0400 Subject: [PATCH 1/2] Modified valid_proof and proof_of_work to include the hash of the previous block. --- blockchain.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/blockchain.py b/blockchain.py index 53c7b78..5f57a1b 100644 --- a/blockchain.py +++ b/blockchain.py @@ -49,7 +49,7 @@ class Blockchain(object): return False # Check that the Proof of Work is correct - if not self.valid_proof(last_block['proof'], block['proof']): + if not self.valid_proof(last_block['proof'], block['proof'], last_block['previous_hash']): return False last_block = block @@ -148,33 +148,37 @@ class Blockchain(object): block_string = json.dumps(block, sort_keys=True).encode() return hashlib.sha256(block_string).hexdigest() - def proof_of_work(self, last_proof): + def proof_of_work(self, last_block): """ Simple Proof of Work Algorithm: - - Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p' - - p is the previous proof, and p' is the new proof + - Find a number p' such that hash(pp'h) contains leading 4 zeroes, where p is the previous p' + - p is the previous proof, p' is the new proof, and h is the hash of the last block - :param last_proof: + :param last_block: last Block :return: """ + last_proof = last_block['proof'] + last_hash = last_block['previous_hash'] + proof = 0 - while self.valid_proof(last_proof, proof) is False: + while self.valid_proof(last_proof, proof, last_hash) is False: proof += 1 return proof @staticmethod - def valid_proof(last_proof, proof): + def valid_proof(last_proof, proof, last_hash): """ Validates the Proof :param last_proof: Previous Proof :param proof: Current Proof + :param last_hash: The hash of the Previous Block :return: True if correct, False if not. """ - guess = f'{last_proof}{proof}'.encode() + guess = f'{last_proof}{proof}{last_hash}'.encode() guess_hash = hashlib.sha256(guess).hexdigest() return guess_hash[:4] == "0000" @@ -193,8 +197,7 @@ blockchain = Blockchain() def mine(): # We run the proof of work algorithm to get the next proof... last_block = blockchain.last_block - last_proof = last_block['proof'] - proof = blockchain.proof_of_work(last_proof) + proof = blockchain.proof_of_work(last_block) # We must receive a reward for finding the proof. # The sender is "0" to signify that this node has mined a new coin. From d9f8529cef0ade658d5f66a937fed4733d0ceca6 Mon Sep 17 00:00:00 2001 From: David W Grossman <2gotgrossman@gmail.com> Date: Sun, 8 Oct 2017 17:15:52 -0400 Subject: [PATCH 2/2] Corrected bug in proof_of_work: last_hash does not depend on the previous hash of last_block, but instead just on the hash of last_block. Thank you ashleyniemerg --- blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index 5f57a1b..d84d642 100644 --- a/blockchain.py +++ b/blockchain.py @@ -159,7 +159,7 @@ class Blockchain(object): """ last_proof = last_block['proof'] - last_hash = last_block['previous_hash'] + last_hash = self.hash(last_block) proof = 0 while self.valid_proof(last_proof, proof, last_hash) is False: