block_chains.append(Block(block_chains[i-1].hash, transaction , datetime.datetime.now())) # 前一个区块的哈希,交易信息,时间戳 print("Block {} is created, and the transaction is: {} transfer {} BTC to {}".format(i,input_sender,input_amount,input_receiver)) print("The hash of block {} is: {}".format(i,block_chains[i].hash)) i += 1 return block_chains,i
defis_valid(chain): for i inrange(1, len(chain)): if chain[i].get_hash() == chain[i].hash: if chain[i].prev_hash != chain[i-1].hash: returnFalse else: returnFalse returnTrue
创建主函数
1 2 3 4 5 6 7
if __name__ == '__main__': print("Welcome") i = 1 block_chains = [Block.create_genesis_block()] print("Genesis Block is created.Hash is: ",block_chains[0].hash) whileTrue: block_chains,i = create_blocks(block_chains,i)