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 sync_txindex, 16 ) 17 from test_framework.wallet import MiniWallet 18 19 20 class MerkleBlockTest(BitcoinTestFramework): 21 def set_test_params(self): 22 self.num_nodes = 2 23 self.extra_args = [ 24 [], 25 ["-txindex"], 26 ] 27 28 def run_test(self): 29 miniwallet = MiniWallet(self.nodes[0]) 30 31 chain_height = self.nodes[1].getblockcount() 32 assert_equal(chain_height, 200) 33 34 txid1 = miniwallet.send_self_transfer(from_node=self.nodes[0])['txid'] 35 txid2 = miniwallet.send_self_transfer(from_node=self.nodes[0])['txid'] 36 # This will raise an exception because the transaction is not yet in a block 37 assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1]) 38 39 self.generate(self.nodes[0], 1) 40 blockhash = self.nodes[0].getblockhash(chain_height + 1) 41 42 txlist = [] 43 blocktxn = self.nodes[0].getblock(blockhash, True)["tx"] 44 txlist.append(blocktxn[1]) 45 txlist.append(blocktxn[2]) 46 47 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1])), [txid1]) 48 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2])), txlist) 49 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2], blockhash)), txlist) 50 51 txin_spent = miniwallet.get_utxo(txid=txid2) # Get the change from txid2 52 tx3 = miniwallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=txin_spent) 53 txid3 = tx3['txid'] 54 self.generate(self.nodes[0], 1) 55 56 txid_spent = txin_spent["txid"] 57 txid_unspent = txid1 # Input was change from txid2, so txid1 should be unspent 58 59 # Invalid txids 60 assert_raises_rpc_error(-8, "txid must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["00000000000000000000000000000000"], blockhash) 61 assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["ZZZ0000000000000000000000000000000000000000000000000000000000000"], blockhash) 62 # Invalid blockhashes 63 assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "00000000000000000000000000000000") 64 assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "ZZZ0000000000000000000000000000000000000000000000000000000000000") 65 # We can't find the block from a fully-spent tx 66 assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid_spent]) 67 # We can get the proof if we specify the block 68 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_spent], blockhash)), [txid_spent]) 69 # We can't get the proof if we specify a non-existent block 70 assert_raises_rpc_error(-5, "Block not found", self.nodes[0].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000") 71 # We can't get the proof if we only have the header of the specified block 72 block = self.generateblock(self.nodes[0], output="raw(55)", transactions=[], submit=False) 73 self.nodes[0].submitheader(block["hex"]) 74 assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", self.nodes[0].gettxoutproof, [txid_spent], block['hash']) 75 # We can get the proof if the transaction is unspent 76 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_unspent])), [txid_unspent]) 77 # 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. 78 assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist)) 79 assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist)) 80 # We can always get a proof if we have a -txindex 81 sync_txindex(self, self.nodes[1]) 82 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent]) 83 # We can't get a proof if we specify transactions from different blocks 84 assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3]) 85 # Test empty list 86 assert_raises_rpc_error(-8, "Parameter 'txids' cannot be empty", self.nodes[0].gettxoutproof, []) 87 # Test duplicate txid 88 assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1]) 89 90 # Now we'll try tweaking a proof. 91 proof = self.nodes[1].gettxoutproof([txid1, txid2]) 92 assert txid1 in self.nodes[0].verifytxoutproof(proof) 93 assert txid2 in self.nodes[1].verifytxoutproof(proof) 94 95 tweaked_proof = from_hex(CMerkleBlock(), proof) 96 97 # Make sure that our serialization/deserialization is working 98 assert txid1 in self.nodes[0].verifytxoutproof(tweaked_proof.serialize().hex()) 99 100 # Check to see if we can go up the merkle tree and pass this off as a 101 # single-transaction block 102 tweaked_proof.txn.nTransactions = 1 103 tweaked_proof.txn.vHash = [tweaked_proof.header.hashMerkleRoot] 104 tweaked_proof.txn.vBits = [True] + [False]*7 105 106 for n in self.nodes: 107 assert not n.verifytxoutproof(tweaked_proof.serialize().hex()) 108 109 # TODO: try more variants, eg transactions at different depths, and 110 # verify that the proofs are invalid 111 112 if __name__ == '__main__': 113 MerkleBlockTest(__file__).main()