def generate_genesis_block(self): # add genesis block if len(self.chain) == 0: print('Mining Block #0...') genesis = Block([], 'genesis') genesis.mine(self.difficulty) self.chain.append(genesis.serialized)
def add_block(self, miner_address): # We reward those who mine with 1 coin, so we append a new transaction with this reward self.pending_transactions.append( Transaction('blockchain', miner_address, 1).serialized) # from blockchain, to miner, 1 coin # construct a new block new_block = Block(self.pending_transactions, self.chain[-1]['hash']) print(f'Mining block #{len(self.chain)}...') # mine it new_block.mine(self.difficulty) # and append to chain, cleaning the pending transaction ('cos we've already written them in the block) self.chain.append(new_block.serialized) self.pending_transactions = []