Merge pull request #11 from 2gotgrossman/master
Modified valid_proof and proof_of_work to include the hash of last block
This commit is contained in:
commit
ffc0075964
|
@ -48,7 +48,7 @@ class Blockchain:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Check that the Proof of Work is correct
|
# 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
|
return False
|
||||||
|
|
||||||
last_block = block
|
last_block = block
|
||||||
|
@ -146,30 +146,39 @@ class Blockchain:
|
||||||
block_string = json.dumps(block, sort_keys=True).encode()
|
block_string = json.dumps(block, sort_keys=True).encode()
|
||||||
return hashlib.sha256(block_string).hexdigest()
|
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:
|
Simple Proof of Work Algorithm:
|
||||||
|
|
||||||
- Find a number p' such that hash(pp') contains leading 4 zeroes
|
- Find a number p' such that hash(pp') contains leading 4 zeroes
|
||||||
- Where p is the previous proof, and p' is the new proof
|
- Where p is the previous proof, and p' is the new proof
|
||||||
|
|
||||||
|
:param last_block: <dict> last Block
|
||||||
|
:return: <int>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
last_proof = last_block['proof']
|
||||||
|
last_hash = self.hash(last_block)
|
||||||
|
|
||||||
proof = 0
|
proof = 0
|
||||||
while self.valid_proof(last_proof, proof) is False:
|
while self.valid_proof(last_proof, proof, last_hash) is False:
|
||||||
proof += 1
|
proof += 1
|
||||||
|
|
||||||
return proof
|
return proof
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def valid_proof(last_proof, proof):
|
def valid_proof(last_proof, proof, last_hash):
|
||||||
"""
|
"""
|
||||||
Validates the Proof
|
Validates the Proof
|
||||||
|
|
||||||
:param last_proof: Previous Proof
|
:param last_proof: <int> Previous Proof
|
||||||
:param proof: Current Proof
|
:param proof: <int> Current Proof
|
||||||
:return: True if correct, False if not.
|
:param last_hash: <str> The hash of the Previous Block
|
||||||
|
:return: <bool> 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()
|
guess_hash = hashlib.sha256(guess).hexdigest()
|
||||||
return guess_hash[:4] == "0000"
|
return guess_hash[:4] == "0000"
|
||||||
|
|
||||||
|
@ -188,8 +197,7 @@ blockchain = Blockchain()
|
||||||
def mine():
|
def mine():
|
||||||
# We run the proof of work algorithm to get the next proof...
|
# We run the proof of work algorithm to get the next proof...
|
||||||
last_block = blockchain.last_block
|
last_block = blockchain.last_block
|
||||||
last_proof = last_block['proof']
|
proof = blockchain.proof_of_work(last_block)
|
||||||
proof = blockchain.proof_of_work(last_proof)
|
|
||||||
|
|
||||||
# We must receive a reward for finding the proof.
|
# We must receive a reward for finding the proof.
|
||||||
# The sender is "0" to signify that this node has mined a new coin.
|
# The sender is "0" to signify that this node has mined a new coin.
|
||||||
|
|
Loading…
Reference in New Issue