示例#1
0
 def create_genesis_block(self, wallet: Wallet):
     '''method to create and puts the genesis block into the blockchain'''
     block_reward = Transaction("Block_Reward", wallet.pubkey,
                                "5.0").to_json()
     genesis_block = Block(
         0, [block_reward],
         datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0")
     # Hash of genesis block cannot be computed directly, proof of work is needed
     genesis_block.hash = self.proof_of_work(genesis_block)
     self.chain.append(genesis_block.to_json())
示例#2
0
 def add_block(self, block: Block, proof: str) -> bool:
     '''
     method to check if a new block could be added at the end of the blockchain
     1. by checking that the new block from parameter is the next block from our last block
     2. by checking the new block and the given hash (from proof-of-work) is legit
     '''
     previous_hash = self.last_block['hash']
     if previous_hash != block.previous_hash:
         return False
     if not self.is_valid_proof(block, proof):
         return False
     block.hash = proof
     self.chain.append(block.to_json())
     return True