2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2015-02-04 02:59:41 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test gettxoutproof and verifytxoutproof RPCs."""
|
2015-02-04 02:59:41 +01:00
|
|
|
|
2015-05-02 12:53:35 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
2015-02-04 02:59:41 +01:00
|
|
|
|
|
|
|
class MerkleBlockTest(BitcoinTestFramework):
|
|
|
|
|
2016-05-14 13:01:31 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 4
|
2015-02-04 02:59:41 +01:00
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
self.nodes = []
|
|
|
|
# Nodes 0/1 are "wallet" nodes
|
2017-02-24 18:39:33 +01:00
|
|
|
self.nodes.append(start_node(0, self.options.tmpdir))
|
|
|
|
self.nodes.append(start_node(1, self.options.tmpdir))
|
2015-02-04 02:59:41 +01:00
|
|
|
# Nodes 2/3 are used for testing
|
2017-02-24 18:39:33 +01:00
|
|
|
self.nodes.append(start_node(2, self.options.tmpdir))
|
|
|
|
self.nodes.append(start_node(3, self.options.tmpdir, ["-txindex"]))
|
2015-02-04 02:59:41 +01:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
|
|
|
connect_nodes(self.nodes[0], 3)
|
|
|
|
|
|
|
|
self.is_network_split = False
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
def run_test(self):
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Mining blocks...")
|
2015-02-04 02:59:41 +01:00
|
|
|
self.nodes[0].generate(105)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
chain_height = self.nodes[1].getblockcount()
|
|
|
|
assert_equal(chain_height, 105)
|
|
|
|
assert_equal(self.nodes[1].getbalance(), 0)
|
|
|
|
assert_equal(self.nodes[2].getbalance(), 0)
|
|
|
|
|
|
|
|
node0utxos = self.nodes[0].listunspent(1)
|
2015-12-06 20:24:02 +01:00
|
|
|
tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
|
2015-02-04 02:59:41 +01:00
|
|
|
txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx1)["hex"])
|
2015-12-06 20:24:02 +01:00
|
|
|
tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
|
2015-02-04 02:59:41 +01:00
|
|
|
txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx2)["hex"])
|
|
|
|
assert_raises(JSONRPCException, self.nodes[0].gettxoutproof, [txid1])
|
|
|
|
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
blockhash = self.nodes[0].getblockhash(chain_height + 1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
txlist = []
|
|
|
|
blocktxn = self.nodes[0].getblock(blockhash, True)["tx"]
|
|
|
|
txlist.append(blocktxn[1])
|
|
|
|
txlist.append(blocktxn[2])
|
|
|
|
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1])), [txid1])
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist)
|
|
|
|
|
|
|
|
txin_spent = self.nodes[1].listunspent(1).pop()
|
2015-12-06 20:24:02 +01:00
|
|
|
tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98})
|
2015-02-04 02:59:41 +01:00
|
|
|
self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransaction(tx3)["hex"])
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
txid_spent = txin_spent["txid"]
|
|
|
|
txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
|
|
|
|
|
2016-01-17 12:03:56 +01:00
|
|
|
# We can't find the block from a fully-spent tx
|
2015-02-04 02:59:41 +01:00
|
|
|
assert_raises(JSONRPCException, self.nodes[2].gettxoutproof, [txid_spent])
|
|
|
|
# ...but we can if we specify the block
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
|
|
|
|
# ...or if the first tx is not fully-spent
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
|
|
|
|
try:
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
|
|
|
|
except JSONRPCException:
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1])), txlist)
|
|
|
|
# ...or if we have a -txindex
|
|
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
MerkleBlockTest().main()
|