# 如果找到满足条件的哈希值,就创建一个新区块并广播给其他节点 ifhash <= target: new_block = Block(index, prev_hash, transactions, nonce, hash) print(f"{self.name} created a new block: \n{new_block}") # 这里是让所有的节点都收到这个新创建的区块 for node in nodes: node.receive_block(new_block)
# 验证新区块的前一个区块是否是自己的区块链的最后一个区块 # 这里为了简化,如果发现不是就会直接拒绝掉, # 实际上并不一定直接拒绝,如果他的链更长可能会承认他的链 prev_block = self.chain[-1] if prev_block.index != index - 1or prev_block.hash != prev_hash: print(f"{self.name} rejected block {index} because the previous block does not match.") return
# 验证新区块的哈希值是否满足目标值 target = "0000" + "f" * 60 message = str(index) + prev_hash + str(transactions) + str(nonce) # 如果发现计算出来的hash值和这个区块的hash值不一致 # 或者计算出来的hash > target # 就拒绝这个新区块 if hashlib.sha256(message.encode()).hexdigest() != hashorhash > target: print(f"{self.name} rejected block {index} because the hash value is invalid.") return
# 如果新区块有效,就添加到自己的区块链中,并从交易池中删除已确认的交易,实际上这里只是删除创建区块的节点的自己的交易池中的交易,因为上面假设交易并没有真正广播出去 self.chain.append(new_block) for transaction in transactions: if transaction inself.pool: self.pool.remove(transaction) print(f"{self.name} accepted block {index} and updated its chain and pool.")
# 如果找到满足条件的哈希值,就创建一个新区块并广播给其他节点 ifhash <= target: new_block = Block(index, prev_hash, transactions, nonce, hash) print(f"{self.name} created a new block: \n{new_block}") # 这里是让所有的节点都收到这个新创建的区块 for node in nodes: node.receive_block(new_block)
# 验证新区块的前一个区块是否是自己的区块链的最后一个区块 # 这里为了简化,如果发现不是就会直接拒绝掉, # 实际上并不一定直接拒绝,如果他的链更长可能会承认他的链 prev_block = self.chain[-1] if prev_block.index != index - 1or prev_block.hash != prev_hash: print(f"{self.name} rejected block {index} because the previous block does not match.") return
# 验证新区块的哈希值是否满足目标值 target = "0000" + "f" * 60 message = str(index) + prev_hash + str(transactions) + str(nonce) # 如果发现计算出来的hash值和这个区块的hash值不一致 # 或者计算出来的hash > target # 就拒绝这个新区块 if hashlib.sha256(message.encode()).hexdigest() != hashorhash > target: print(f"{self.name} rejected block {index} because the hash value is invalid.") return
# 如果新区块有效,就添加到自己的区块链中,并从交易池中删除已确认的交易 self.chain.append(new_block) for transaction in transactions: if transaction inself.pool: self.pool.remove(transaction) print(f"{self.name} accepted block {index} and updated its chain and pool.")
Alice created the genesis block: Block 0: Prev_hash:0000000000000000000000000000000000000000000000000000000000000000 Transactions: ['Hello, Bitcoin!'] Nonce:0 Hash:0000000000000000000000000000000000000000000000000000000000000000 Bob created the genesis block: Block 0: Prev_hash:0000000000000000000000000000000000000000000000000000000000000000 Transactions: ['Hello, Bitcoin!'] Nonce:0 Hash:0000000000000000000000000000000000000000000000000000000000000000 Charlie created the genesis block: Block 0: Prev_hash:0000000000000000000000000000000000000000000000000000000000000000 Transactions: ['Hello, Bitcoin!'] Nonce:0 Hash:0000000000000000000000000000000000000000000000000000000000000000 David created the genesis block: Block 0: Prev_hash:0000000000000000000000000000000000000000000000000000000000000000 Transactions: ['Hello, Bitcoin!'] Nonce:0 Hash:0000000000000000000000000000000000000000000000000000000000000000 Alice created a transaction: Alice sent 8BTC to Alice Bob created a new block: Block 1: Prev_hash:0000000000000000000000000000000000000000000000000000000000000000 Transactions: [] Nonce:22844 Hash:0000cbc7a7c49dacc4a0b61f51c10cbc1c806ccb48db8f0e1ada6ac0f8f0e7b4 Alice accepted block 1and updated its chain and pool. Bob accepted block 1and updated its chain and pool. Charlie accepted block 1and updated its chain and pool. David accepted block 1and updated its chain and pool. Charlie created a new block: Block 2: Prev_hash:0000cbc7a7c49dacc4a0b61f51c10cbc1c806ccb48db8f0e1ada6ac0f8f0e7b4 Transactions: [] Nonce:38441 Hash:00007abd6de3c175698cd372254801fdd6550ebb761f540bc25558e1ee1f8fb0 Alice accepted block 2and updated its chain and pool. Bob accepted block 2and updated its chain and pool. Charlie accepted block 2and updated its chain and pool. David accepted block 2and updated its chain and pool. David created a transaction: David sent 4BTC to David Alice created a transaction: Alice sent 2BTC to Bob Bob created a new block: Block 3: Prev_hash:00007abd6de3c175698cd372254801fdd6550ebb761f540bc25558e1ee1f8fb0 Transactions: [] Nonce:34800 Hash:0000c6dbf042a301da5fed4bb83b00d637a02a804f3959c7f3e63335ea10d6ff Alice accepted block 3and updated its chain and pool. Bob accepted block 3and updated its chain and pool. Charlie accepted block 3and updated its chain and pool. David accepted block 3and updated its chain and pool. Charlie created a transaction: Charlie sent 3BTC to Charlie David created a transaction: David sent 10BTC to Alice Alice created a new block: Block 4: Prev_hash:0000c6dbf042a301da5fed4bb83b00d637a02a804f3959c7f3e63335ea10d6ff Transactions: ['Alice sent 8 BTC to Alice', 'Alice sent 2 BTC to Bob'] Nonce:101424 Hash:0000c6b36c5336c2d83c411509ab5216fdd20719c6c9d9682a0e1e035358c0c6 Alice accepted block 4and updated its chain and pool. Bob accepted block 4and updated its chain and pool. Charlie accepted block 4and updated its chain and pool. David accepted block 4and updated its chain and pool. Bob created a transaction: Bob sent 6BTC to David