def verify_blockchain_depth(cls) -> bool: """深入验证区块链(区块、交易)""" if not cls.verify_blockchain(): return False for block in BlockChain.get_instance().get_blocks()[1:]: # 第一个区块不作检查 if not cls.verify_block_depth(block): return False return True
def verify_new_transaction(cls, trans: Transaction) -> bool: """验证一笔新交易""" if not cls.verify_transaction(trans): return False # 验证输入的有效性 for inp in trans.get_inputs(): if not BlockChain.get_instance().verify_utxo( inp.block, inp.trans, inp.output): return False return True
def is_ok(self) -> bool: blc = BlockChain.get_instance() for block in blc.get_blocks(): # 区块的时间戳肯定比区块链要后 if block.get_timestap() < blc.get_start_time(): return False # 区块的时间戳肯定比现在前 if block.get_timestap() > time.localtime(): return False return True
def is_ok(self) -> bool: address_list = [key.get_address() for key in self.trans.get_pub_keys()] # 签名必须有效 if not self.trans.verify_transaction(): return False # 每一个输入都为本人的utxo for inp in self.trans.get_inputs(): outp = BlockChain.get_instance().input_to_output(inp) if outp.address not in address_list: return False return True
def is_ok(self) -> bool: pre_hash = FIREST_BLOCK_PREHASH for i, block in enumerate(BlockChain.get_instance().get_blocks()): # 索引值index应合法 if block.get_index() != i + 1: return False # pre_hash值应为上一个区块 if pre_hash != block.get_prehash(): return False pre_hash = block.get_hash() return True
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
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
from key import UserKey from chain import Btc, TransOutput, TransInput, Transaction, Block, BlockChain from config import MINING_BTCS from verify import Verify if __name__ == "__main__": print(BlockChain.__qualname__, BlockChain.get_instance.__qualname__) # 创建区块链 bc = BlockChain.get_instance() print(bc.get_start_time()) # 初始的两个用户 key1 = UserKey() key2 = UserKey() # 初始区块的创币交易(只有输出,没有输入) t1 = Transaction() t1.add_output(TransOutput(Btc("5000"), key1.get_address())) t1.add_output(TransOutput(Btc("5000"), key2.get_address())) # 创世区块 b1 = Block() b1.add_transaction(t1) # 添加矿工奖励交易 mt1 = Transaction() mt1.add_output(TransOutput(Btc(MINING_BTCS), key1.get_address())) b1.set_head_transaction(mt1) b1.set_index(1) # 挖矿 b1.find_randnum() # 添加区块 bc.add_block(b1) # key1向key2转账 t2 = Transaction()
def is_ok(self) -> bool: # 交易费必须大于0 blc = BlockChain.get_instance() return blc.compute_transaction_fee(self.trans) > Btc("0")
def __import_blc_from_db(self) -> None: """从sqlite数据库导入区块链""" for block in self.sqlite_driver.get_blocks(): BlockChain.get_instance().add_block(block)
def __blc(self) -> BlockChain: return BlockChain.get_instance()