rpc_txoutproof.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2014-2021 The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test gettxoutproof and verifytxoutproof RPCs.""" 6 7 from test_framework.messages import ( 8 CMerkleBlock, 9 from_hex, 10 ) 11 from test_framework.test_framework import BitcoinTestFramework 12 from test_framework.util import ( 13 assert_equal, 14 assert_raises_rpc_error, 15 ) 16 from test_framework.wallet import MiniWallet 17 18 19 class MerkleBlockTest(BitcoinTestFramework): 20 def set_test_params(self): 21 self.num_nodes = 2 22 self.extra_args = [ 23 [], 24 ["-txindex"], 25 ] 26 27 def run_test(self): 28 miniwallet = MiniWallet(self.nodes[0]) 29 30 chain_height = self.nodes[1].getblockcount() 31 assert_equal(chain_height, 200) 32 33 txid1 = miniwallet.send_self_transfer(from_node=self.nodes[0])['txid'] 34 txid2 = miniwallet.send_self_transfer(from_node=self.nodes[0])['txid'] 35 # This will raise an exception because the transaction is not yet in a block 36 assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1]) 37 38 self.generate(self.nodes[0], 1) 39 blockhash = self.nodes[0].getblockhash(chain_height + 1) 40 41 txlist = [] 42 blocktxn = self.nodes[0].getblock(blockhash, True)["tx"] 43 txlist.append(blocktxn[1]) 44 txlist.append(blocktxn[2]) 45 46 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1])), [txid1]) 47 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2])), txlist) 48 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2], blockhash)), txlist) 49 50 txin_spent = miniwallet.get_utxo(txid=txid2) # Get the change from txid2 51 tx3 = miniwallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=txin_spent) 52 txid3 = tx3['txid'] 53 self.generate(self.nodes[0], 1) 54 55 txid_spent = txin_spent["txid"] 56 txid_unspent = txid1 # Input was change from txid2, so txid1 should be unspent 57 58 # Invalid txids 59 assert_raises_rpc_error(-8, "txid must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["00000000000000000000000000000000"], blockhash) 60 assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["ZZZ0000000000000000000000000000000000000000000000000000000000000"], blockhash) 61 # Invalid blockhashes 62 assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "00000000000000000000000000000000") 63 assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "ZZZ0000000000000000000000000000000000000000000000000000000000000") 64 # We can't find the block from a fully-spent tx 65 assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid_spent]) 66 # We can get the proof if we specify the block 67 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_spent], blockhash)), [txid_spent]) 68 # We can't get the proof if we specify a non-existent block 69 assert_raises_rpc_error(-5, "Block not found", self.nodes[0].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000") 70 # We can get the proof if the transaction is unspent 71 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_unspent])), [txid_unspent]) 72 # We can get the proof if we provide a list of transactions and one of them is unspent. The ordering of the list should not matter. 73 assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist)) 74 assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist)) 75 # We can always get a proof if we have a -txindex 76 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent]) 77 # We can't get a proof if we specify transactions from different blocks 78 assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3]) 79 # Test empty list 80 assert_raises_rpc_error(-8, "Parameter 'txids' cannot be empty", self.nodes[0].gettxoutproof, []) 81 # Test duplicate txid 82 assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1]) 83 84 # Now we'll try tweaking a proof. 85 proof = self.nodes[1].gettxoutproof([txid1, txid2]) 86 assert txid1 in self.nodes[0].verifytxoutproof(proof) 87 assert txid2 in self.nodes[1].verifytxoutproof(proof) 88 89 tweaked_proof = from_hex(CMerkleBlock(), proof) 90 91 # Make sure that our serialization/deserialization is working 92 assert txid1 in self.nodes[0].verifytxoutproof(tweaked_proof.serialize().hex()) 93 94 # Check to see if we can go up the merkle tree and pass this off as a 95 # single-transaction block 96 tweaked_proof.txn.nTransactions = 1 97 tweaked_proof.txn.vHash = [tweaked_proof.header.hashMerkleRoot] 98 tweaked_proof.txn.vBits = [True] + [False]*7 99 100 for n in self.nodes: 101 assert not n.verifytxoutproof(tweaked_proof.serialize().hex()) 102 103 # TODO: try more variants, eg transactions at different depths, and 104 # verify that the proofs are invalid 105 106 if __name__ == '__main__': 107 MerkleBlockTest().main()