1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2021 The Limenka 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 LimenkaTestFramework
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(LimenkaTestFramework):
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, 2)["tx"]
43 txlist.append(blocktxn[1]['txid'])
44 txlist.append(blocktxn[2]['txid'])
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 chain_height += 1
51 expected_proven = {
52 'blockhash': blockhash,
53 'blockheight': chain_height,
54 'tx': [
55 {
56 'txid': blocktxn[1]['txid'],
57 'wtxid': blocktxn[1]['hash'],
58 'blockindex': 1,
59 }, {
60 'txid': blocktxn[2]['txid'],
61 'wtxid': blocktxn[2]['hash'],
62 'blockindex': 2,
63 },
64 ],
65 }
66 proofres = self.nodes[0].gettxoutproof([txid1, txid2], prove_witness=True)
67 assert_equal(proofres['proven'], expected_proven)
68 expected_proven['confirmations'] = 1
69 del expected_proven['tx'][0]['txid']
70 del expected_proven['tx'][1]['txid']
71 assert_equal(self.nodes[0].verifytxoutproof(proofres['proof'], verify_witness=True), expected_proven)
72 73 txin_spent = miniwallet.get_utxo(txid=txid2) # Get the change from txid2
74 tx3 = miniwallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=txin_spent)
75 txid3 = tx3['txid']
76 self.generate(self.nodes[0], 1)
77 78 txid_spent = txin_spent["txid"]
79 txid_unspent = txid1 # Input was change from txid2, so txid1 should be unspent
80 81 # Invalid txids
82 assert_raises_rpc_error(-8, "txid must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["00000000000000000000000000000000"], blockhash)
83 assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["ZZZ0000000000000000000000000000000000000000000000000000000000000"], blockhash)
84 # Invalid blockhashes
85 assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "00000000000000000000000000000000")
86 assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "ZZZ0000000000000000000000000000000000000000000000000000000000000")
87 # We can't find the block from a fully-spent tx
88 assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid_spent])
89 # We can get the proof if we specify the block
90 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_spent], blockhash)), [txid_spent])
91 # We can't get the proof if we specify a non-existent block
92 assert_raises_rpc_error(-5, "Block not found", self.nodes[0].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000")
93 # We can't get the proof if we only have the header of the specified block
94 block = self.generateblock(self.nodes[0], output="raw(55)", transactions=[], submit=False)
95 self.nodes[0].submitheader(block["hex"])
96 assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", self.nodes[0].gettxoutproof, [txid_spent], block['hash'])
97 # We can get the proof if the transaction is unspent
98 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_unspent])), [txid_unspent])
99 # 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.
100 assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist))
101 assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist))
102 # We can always get a proof if we have a -txindex
103 assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent])
104 # We can't get a proof if we specify transactions from different blocks
105 assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
106 # Test empty list
107 assert_raises_rpc_error(-8, "Parameter 'txids' cannot be empty", self.nodes[0].gettxoutproof, [])
108 # Test duplicate txid
109 assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1])
110 111 # Now we'll try tweaking a proof.
112 proof = self.nodes[1].gettxoutproof([txid1, txid2])
113 assert txid1 in self.nodes[0].verifytxoutproof(proof)
114 assert txid2 in self.nodes[1].verifytxoutproof(proof)
115 116 tweaked_proof = from_hex(CMerkleBlock(), proof)
117 118 # Make sure that our serialization/deserialization is working
119 assert txid1 in self.nodes[0].verifytxoutproof(tweaked_proof.serialize().hex())
120 121 # Check to see if we can go up the merkle tree and pass this off as a
122 # single-transaction block
123 tweaked_proof.txn.nTransactions = 1
124 tweaked_proof.txn.vHash = [tweaked_proof.header.hashMerkleRoot]
125 tweaked_proof.txn.vBits = [True] + [False]*7
126 127 for n in self.nodes:
128 assert not n.verifytxoutproof(tweaked_proof.serialize().hex())
129 130 # TODO: try more variants, eg transactions at different depths, and
131 # verify that the proofs are invalid
132 133 if __name__ == '__main__':
134 MerkleBlockTest(__file__).main()
135