You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
4.3 KiB
84 lines
4.3 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2014-2017 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test gettxoutproof and verifytxoutproof RPCs."""
|
||
10 years ago
|
|
||
10 years ago
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
10 years ago
|
|
||
|
class MerkleBlockTest(BitcoinTestFramework):
|
||
8 years ago
|
def set_test_params(self):
|
||
7 years ago
|
self.num_nodes = 4
|
||
9 years ago
|
self.setup_clean_chain = True
|
||
8 years ago
|
# Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
|
||
|
self.extra_args = [[], [], [], ["-txindex"]]
|
||
10 years ago
|
|
||
|
def setup_network(self):
|
||
8 years ago
|
self.setup_nodes()
|
||
10 years ago
|
connect_nodes(self.nodes[0], 1)
|
||
|
connect_nodes(self.nodes[0], 2)
|
||
|
connect_nodes(self.nodes[0], 3)
|
||
|
|
||
|
self.sync_all()
|
||
|
|
||
|
def run_test(self):
|
||
8 years ago
|
self.log.info("Mining blocks...")
|
||
10 years ago
|
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)
|
||
9 years ago
|
tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
|
||
10 years ago
|
txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx1)["hex"])
|
||
9 years ago
|
tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
|
||
10 years ago
|
txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx2)["hex"])
|
||
8 years ago
|
# This will raise an exception because the transaction is not yet in a block
|
||
8 years ago
|
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1])
|
||
10 years ago
|
|
||
|
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()
|
||
9 years ago
|
tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98})
|
||
8 years ago
|
txid3 = self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransaction(tx3)["hex"])
|
||
10 years ago
|
self.nodes[0].generate(1)
|
||
|
self.sync_all()
|
||
|
|
||
|
txid_spent = txin_spent["txid"]
|
||
|
txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
|
||
|
|
||
9 years ago
|
# We can't find the block from a fully-spent tx
|
||
8 years ago
|
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[2].gettxoutproof, [txid_spent])
|
||
8 years ago
|
# We can get the proof if we specify the block
|
||
10 years ago
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
|
||
8 years ago
|
# We can't get the proof if we specify a non-existent block
|
||
8 years ago
|
assert_raises_rpc_error(-5, "Block not found", self.nodes[2].gettxoutproof, [txid_spent], "00000000000000000000000000000000")
|
||
8 years ago
|
# We can get the proof if the transaction is unspent
|
||
10 years ago
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
|
||
8 years ago
|
# 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.
|
||
|
assert_equal(sorted(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2]))), sorted(txlist))
|
||
|
assert_equal(sorted(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1]))), sorted(txlist))
|
||
|
# We can always get a proof if we have a -txindex
|
||
10 years ago
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
|
||
8 years ago
|
# We can't get a proof if we specify transactions from different blocks
|
||
8 years ago
|
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[2].gettxoutproof, [txid1, txid3])
|
||
8 years ago
|
|
||
10 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
MerkleBlockTest().main()
|