|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2014 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 | + |
| 6 | +# |
| 7 | +# Test merkleblock fetch/validation |
| 8 | +# |
| 9 | + |
| 10 | +from test_framework import BitcoinTestFramework |
| 11 | +from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException |
| 12 | +from util import * |
| 13 | +import os |
| 14 | +import shutil |
| 15 | + |
| 16 | +class MerkleBlockTest(BitcoinTestFramework): |
| 17 | + |
| 18 | + def setup_chain(self): |
| 19 | + print("Initializing test directory "+self.options.tmpdir) |
| 20 | + initialize_chain_clean(self.options.tmpdir, 4) |
| 21 | + |
| 22 | + def setup_network(self): |
| 23 | + self.nodes = [] |
| 24 | + # Nodes 0/1 are "wallet" nodes |
| 25 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"])) |
| 26 | + self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"])) |
| 27 | + # Nodes 2/3 are used for testing |
| 28 | + self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"])) |
| 29 | + self.nodes.append(start_node(3, self.options.tmpdir, ["-debug", "-txindex"])) |
| 30 | + connect_nodes(self.nodes[0], 1) |
| 31 | + connect_nodes(self.nodes[0], 2) |
| 32 | + connect_nodes(self.nodes[0], 3) |
| 33 | + |
| 34 | + self.is_network_split = False |
| 35 | + self.sync_all() |
| 36 | + |
| 37 | + def run_test(self): |
| 38 | + print "Mining blocks..." |
| 39 | + self.nodes[0].generate(105) |
| 40 | + self.sync_all() |
| 41 | + |
| 42 | + chain_height = self.nodes[1].getblockcount() |
| 43 | + assert_equal(chain_height, 105) |
| 44 | + assert_equal(self.nodes[1].getbalance(), 0) |
| 45 | + assert_equal(self.nodes[2].getbalance(), 0) |
| 46 | + |
| 47 | + node0utxos = self.nodes[0].listunspent(1) |
| 48 | + tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 50}) |
| 49 | + txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx1)["hex"]) |
| 50 | + tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 50}) |
| 51 | + txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx2)["hex"]) |
| 52 | + assert_raises(JSONRPCException, self.nodes[0].gettxoutproof, [txid1]) |
| 53 | + |
| 54 | + self.nodes[0].generate(1) |
| 55 | + blockhash = self.nodes[0].getblockhash(chain_height + 1) |
| 56 | + self.sync_all() |
| 57 | + |
| 58 | + txlist = [] |
| 59 | + blocktxn = self.nodes[0].getblock(blockhash, True)["tx"] |
| 60 | + txlist.append(blocktxn[1]) |
| 61 | + txlist.append(blocktxn[2]) |
| 62 | + |
| 63 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1])), [txid1]) |
| 64 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist) |
| 65 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist) |
| 66 | + |
| 67 | + txin_spent = self.nodes[1].listunspent(1).pop() |
| 68 | + tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 50}) |
| 69 | + self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransaction(tx3)["hex"]) |
| 70 | + self.nodes[0].generate(1) |
| 71 | + self.sync_all() |
| 72 | + |
| 73 | + txid_spent = txin_spent["txid"] |
| 74 | + txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2 |
| 75 | + |
| 76 | + # We cant find the block from a fully-spent tx |
| 77 | + assert_raises(JSONRPCException, self.nodes[2].gettxoutproof, [txid_spent]) |
| 78 | + # ...but we can if we specify the block |
| 79 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent]) |
| 80 | + # ...or if the first tx is not fully-spent |
| 81 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent]) |
| 82 | + try: |
| 83 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist) |
| 84 | + except JSONRPCException: |
| 85 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1])), txlist) |
| 86 | + # ...or if we have a -txindex |
| 87 | + assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent]) |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + MerkleBlockTest().main() |
0 commit comments