Created
May 18, 2020 19:19
-
-
Save bajcmartinez/c9791875556190d993cdb1b2af9bd4dd to your computer and use it in GitHub Desktop.
From Zero to Blockchain in Python - Part 1 - Add Blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_block(self, block): | |
""" | |
Creates a new block and passes it to the chain | |
:param block: <Block> Block to add to the chain | |
:return: <bool> True if the block is added to the chain, False if not. | |
""" | |
if self.validate_block(block, self.last_block): | |
self.__chain.append(block) | |
# Remove transactions from the list | |
self.__current_transactions = [] | |
return True | |
return False | |
def validate_block(self, current_block, previous_block): | |
""" | |
Validates a block with reference to its previous | |
:param current_block: | |
:param previous_block: | |
:return: | |
""" | |
# Check the block index | |
if current_block.index != previous_block.index + 1: | |
return False | |
if current_block.previous_hash != previous_block.hash: | |
return False | |
if current_block.hash != current_block.hash_block(): | |
return False | |
if not self.validate_proof_of_work(previous_block.nonce, previous_block.hash, current_block.nonce): | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment