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
16 # Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
17 self
.extra_args
= [[], [], [], ["-txindex"]]
19 def setup_network(self
):
21 connect_nodes(self
.nodes
[0], 1)
22 connect_nodes(self
.nodes
[0], 2)
23 connect_nodes(self
.nodes
[0], 3)
28 self
.log
.info("Mining blocks...")
29 self
.nodes
[0].generate(105)
32 chain_height
= self
.nodes
[1].getblockcount()
33 assert_equal(chain_height
, 105)
34 assert_equal(self
.nodes
[1].getbalance(), 0)
35 assert_equal(self
.nodes
[2].getbalance(), 0)
37 node0utxos
= self
.nodes
[0].listunspent(1)
38 tx1
= self
.nodes
[0].createrawtransaction([node0utxos
.pop()], {self
.nodes
[1].getnewaddress(): 49.99})
39 txid1
= self
.nodes
[0].sendrawtransaction(self
.nodes
[0].signrawtransaction(tx1
)["hex"])
40 tx2
= self
.nodes
[0].createrawtransaction([node0utxos
.pop()], {self
.nodes
[1].getnewaddress(): 49.99})
41 txid2
= self
.nodes
[0].sendrawtransaction(self
.nodes
[0].signrawtransaction(tx2
)["hex"])
42 assert_raises(JSONRPCException
, self
.nodes
[0].gettxoutproof
, [txid1
])
44 self
.nodes
[0].generate(1)
45 blockhash
= self
.nodes
[0].getblockhash(chain_height
+ 1)
49 blocktxn
= self
.nodes
[0].getblock(blockhash
, True)["tx"]
50 txlist
.append(blocktxn
[1])
51 txlist
.append(blocktxn
[2])
53 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
])), [txid1
])
54 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
, txid2
])), txlist
)
55 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
, txid2
], blockhash
)), txlist
)
57 txin_spent
= self
.nodes
[1].listunspent(1).pop()
58 tx3
= self
.nodes
[1].createrawtransaction([txin_spent
], {self
.nodes
[0].getnewaddress(): 49.98})
59 self
.nodes
[0].sendrawtransaction(self
.nodes
[1].signrawtransaction(tx3
)["hex"])
60 self
.nodes
[0].generate(1)
63 txid_spent
= txin_spent
["txid"]
64 txid_unspent
= txid1
if txin_spent
["txid"] != txid1
else txid2
66 # We can't find the block from a fully-spent tx
67 assert_raises(JSONRPCException
, self
.nodes
[2].gettxoutproof
, [txid_spent
])
68 # ...but we can if we specify the block
69 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid_spent
], blockhash
)), [txid_spent
])
70 # ...or if the first tx is not fully-spent
71 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid_unspent
])), [txid_unspent
])
73 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid1
, txid2
])), txlist
)
74 except JSONRPCException
:
75 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[2].gettxoutproof([txid2
, txid1
])), txlist
)
76 # ...or if we have a -txindex
77 assert_equal(self
.nodes
[2].verifytxoutproof(self
.nodes
[3].gettxoutproof([txid_spent
])), [txid_spent
])
79 if __name__
== '__main__':
80 MerkleBlockTest().main()