2 # Copyright (c) 2014-2016 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."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import *
10 class MerkleBlockTest(BitcoinTestFramework
):
14 self
.setup_clean_chain
= True
17 def setup_network(self
):
19 # Nodes 0/1 are "wallet" nodes
20 self
.nodes
.append(start_node(0, self
.options
.tmpdir
, ["-debug"]))
21 self
.nodes
.append(start_node(1, self
.options
.tmpdir
, ["-debug"]))
22 # Nodes 2/3 are used for testing
23 self
.nodes
.append(start_node(2, self
.options
.tmpdir
, ["-debug"]))
24 self
.nodes
.append(start_node(3, self
.options
.tmpdir
, ["-debug", "-txindex"]))
25 connect_nodes(self
.nodes
[0], 1)
26 connect_nodes(self
.nodes
[0], 2)
27 connect_nodes(self
.nodes
[0], 3)
29 self
.is_network_split
= False
33 print("Mining blocks...")
34 self
.nodes
[0].generate(105)
37 chain_height
= self
.nodes
[1].getblockcount()
38 assert_equal(chain_height
, 105)
39 assert_equal(self
.nodes
[1].getbalance(), 0)
40 assert_equal(self
.nodes
[2].getbalance(), 0)
42 node0utxos
= self
.nodes
[0].listunspent(1)
43 tx1
= self
.nodes
[0].createrawtransaction([node0utxos
.pop()], {self
.nodes
[1].getnewaddress(): 49.99})
44 txid1
= self
.nodes
[0].sendrawtransaction(self
.nodes
[0].signrawtransaction(tx1
)["hex"])
45 tx2
= self
.nodes
[0].createrawtransaction([node0utxos
.pop()], {self
.nodes
[1].getnewaddress(): 49.99})
46 txid2
= self
.nodes
[0].sendrawtransaction(self
.nodes
[0].signrawtransaction(tx2
)["hex"])
47 assert_raises(JSONRPCException
, self
.nodes
[0].gettxoutproof
, [txid1
])
49 self
.nodes
[0].generate(1)
50 blockhash
= self
.nodes
[0].getblockhash(chain_height
+ 1)
54 blocktxn
= self
.nodes
[0].getblock(blockhash
, True)["tx"]
55 txlist
.append(blocktxn
[1])
56 txlist
.append(blocktxn
[2])
58 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
])), [txid1
])
59 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
, txid2
])), txlist
)
60 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
, txid2
], blockhash
)), txlist
)
62 txin_spent
= self
.nodes
[1].listunspent(1).pop()
63 tx3
= self
.nodes
[1].createrawtransaction([txin_spent
], {self
.nodes
[0].getnewaddress(): 49.98})
64 self
.nodes
[0].sendrawtransaction(self
.nodes
[1].signrawtransaction(tx3
)["hex"])
65 self
.nodes
[0].generate(1)
68 txid_spent
= txin_spent
["txid"]
69 txid_unspent
= txid1
if txin_spent
["txid"] != txid1
else txid2
71 # We can't find the block from a fully-spent tx
72 assert_raises(JSONRPCException
, self
.nodes
[2].gettxoutproof
, [txid_spent
])
73 # ...but we can if we specify the block
74 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid_spent
], blockhash
)), [txid_spent
])
75 # ...or if the first tx is not fully-spent
76 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid_unspent
])), [txid_unspent
])
78 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
, txid2
])), txlist
)
79 except JSONRPCException
:
80 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid2
, txid1
])), txlist
)
81 # ...or if we have a -txindex
82 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[3].gettxoutproof([txid_spent
])), [txid_spent
])
84 if __name__
== '__main__':
85 MerkleBlockTest().main()