Example #1
0
 def __mining(self) -> Optional[Block]:
     """挖矿"""
     trans_list = self.__get_mining_trans()
     with FullBlockChain.safe_use_blockchain() as blc:
         block = Block(blc.get_height() + 1, blc.get_top_hash())
     # 计算总交易费
     fee = Btc("0")
     for trans in trans_list:
         fee += blc.compute_transaction_fee(trans)
         block.add_transaction(trans)  # 添加交易到block中
     # 加上矿工奖励
     fee += block.get_now_ming_btcs()
     # 构造创块交易
     head_trans = Transaction()
     head_trans.add_output(TransOutput(address=self.address, btcs=fee))
     block.set_head_transaction(head_trans)
     # 正式开始挖矿
     while not block.veri_hash():
         if self.mine_flag:
             block.randnum += MINING_ADD_NUM
             block.timestap = time.time()
         else:  # 中止挖矿(失败了)
             self.add_trans(*trans_list)  # 把交易放回交易池
             return None
     return block
Example #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