def get_transaction_proof(self, tx_hash: Hash32): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") block_prover = BlockProver.new(block.header.version, block.body.transactions, BlockProverType.Transaction) return block_prover.get_proof(tx_hash)
def prove_transaction(self, tx_hash: Hash32, proof: list): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") block_prover = BlockProver.new(block.header.version, None, BlockProverType.Transaction) # Do not need txs return block_prover.prove(tx_hash, block.header.transaction_hash, proof)
def prove_receipt(self, tx_hash: Hash32, proof: list): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") tx_result = tx_info["result"] block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") block_prover = BlockProver.new(block.header.version, None, BlockProverType.Receipt) # Do not need receipts receipt_hash = block_prover.to_hash32(tx_result) return block_prover.prove(receipt_hash, block.header.receipt_hash, proof)
def get_receipt_proof(self, tx_hash: Hash32): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") tx_result = tx_info["result"] block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") tx_results = (self.find_tx_info(tx_hash)["result"] for tx_hash in block.body.transactions) block_prover = BlockProver.new(block.header.version, tx_results, BlockProverType.Receipt) receipt_hash = block_prover.to_hash32(tx_result) return block_prover.get_proof(receipt_hash)
def test_block_v0_3(self): private_auth = test_util.create_default_peer_auth() tx_versioner = TransactionVersioner() dummy_receipts = {} block_builder = BlockBuilder.new("0.3", tx_versioner) for i in range(1000): tx_builder = TransactionBuilder.new("0x3", tx_versioner) tx_builder.private_key = private_auth.private_key tx_builder.to_address = ExternalAddress.new() tx_builder.step_limit = random.randint(0, 10000) tx_builder.value = random.randint(0, 10000) tx_builder.nid = 2 tx = tx_builder.build() tx_serializer = TransactionSerializer.new(tx.version, tx_versioner) block_builder.transactions[tx.hash] = tx dummy_receipts[tx.hash.hex()] = { "dummy_receipt": "dummy", "tx_dumped": tx_serializer.to_full_data(tx) } block_builder.peer_private_key = private_auth.private_key block_builder.height = 0 block_builder.state_hash = Hash32(bytes(Hash32.size)) block_builder.receipts = dummy_receipts block_builder.reps = [ ExternalAddress.fromhex_address(private_auth.address) ] block_builder.next_leader = ExternalAddress.fromhex( "hx00112233445566778899aabbccddeeff00112233") block = block_builder.build() block_verifier = BlockVerifier.new("0.3", tx_versioner) block_verifier.invoke_func = lambda b: (block, dummy_receipts) block_verifier.verify(block, None, None, block.header.peer_id, reps=block_builder.reps) block_serializer = BlockSerializer.new("0.3", tx_versioner) block_serialized = block_serializer.serialize(block) block_deserialized = block_serializer.deserialize(block_serialized) assert block.header == block_deserialized.header # FIXME : confirm_prev_block not serialized # assert block.body == block_deserialized.body tx_hashes = list(block.body.transactions) tx_index = random.randrange(0, len(tx_hashes)) block_prover = BlockProver.new(block.header.version, tx_hashes, BlockProverType.Transaction) tx_proof = block_prover.get_proof(tx_index) assert block_prover.prove(tx_hashes[tx_index], block.header.transaction_hash, tx_proof) block_prover = BlockProver.new(block.header.version, block_builder.receipts, BlockProverType.Receipt) receipt_proof = block_prover.get_proof(tx_index) receipt_hash = block_prover.to_hash32(block_builder.receipts[tx_index]) assert block_prover.prove(receipt_hash, block.header.receipt_hash, receipt_proof)