Exemple #1
0
 def verify_new_block(cls, block: Block) -> bool:
     """验证一个新块"""
     if not cls.verify_block_depth(block):
         return False
     # pre_hash的值要正确
     if block.get_prehash() != BlockChain.get_instance().get_top_block(
     ).get_hash():
         return False
     # 验证每笔交易的有效性
     inputs = []
     for trans in block.get_user_transactions():
         if not cls.verify_new_transaction(trans):
             return False
         inputs.extend(trans.get_inputs())
     # 不同的交易不能有相同的输入
     if len(set(inputs)) != len(inputs):
         return False
     return True
Exemple #2
0
 def verify_block_depth(cls, block: Block) -> bool:
     """深入验证区块(包括区块中的交易)"""
     if not cls.verify_block(block):
         return False
     # 验证用户的交易
     for trans in block.get_user_transactions():
         if not cls.verify_transaction(trans):
             return False
     # 验证第一笔交易
     trans = block.get_head_transaction()
     # 计算交易费
     fee = BlockChain.get_instance().compute_block_fee(block)
     # 计算矿工奖励
     mini_fee = trans.compute_outputs_btcs() - fee
     # 计算这个区块时的矿工奖励
     mini_btcs = block.get_now_ming_btcs()
     # 验证矿工奖励合法性
     if mini_fee != mini_btcs:
         return False
     return True
Exemple #3
0
 def accept_block(self, block: Block) -> None:
     """胜利者已经产生(新块已加入区块链)"""
     self.suspend_mining()
     self.remove_trans(*block.get_user_transactions())