Dash Core  0.12.2.1
P2P Digital Currency
merkle_blocks.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 
14 
15  def setup_chain(self):
16  print("Initializing test directory "+self.options.tmpdir)
17  initialize_chain_clean(self.options.tmpdir, 4)
18 
19  def setup_network(self):
20  self.nodes = []
21  # Nodes 0/1 are "wallet" nodes
22  self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
23  self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"]))
24  # Nodes 2/3 are used for testing
25  self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"]))
26  self.nodes.append(start_node(3, self.options.tmpdir, ["-debug", "-txindex"]))
27  connect_nodes(self.nodes[0], 1)
28  connect_nodes(self.nodes[0], 2)
29  connect_nodes(self.nodes[0], 3)
30 
31  self.is_network_split = False
32  self.sync_all()
33 
34  def run_test(self):
35  print "Mining blocks..."
36  self.nodes[0].generate(105)
37  self.sync_all()
38 
39  chain_height = self.nodes[1].getblockcount()
40  assert_equal(chain_height, 105)
41  assert_equal(self.nodes[1].getbalance(), 0)
42  assert_equal(self.nodes[2].getbalance(), 0)
43 
44  node0utxos = self.nodes[0].listunspent(1)
45  tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 500})
46  txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx1)["hex"])
47  tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 500})
48  txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransaction(tx2)["hex"])
49  assert_raises(JSONRPCException, self.nodes[0].gettxoutproof, [txid1])
50 
51  self.nodes[0].generate(1)
52  blockhash = self.nodes[0].getblockhash(chain_height + 1)
53  self.sync_all()
54 
55  txlist = []
56  blocktxn = self.nodes[0].getblock(blockhash, True)["tx"]
57  txlist.append(blocktxn[1])
58  txlist.append(blocktxn[2])
59 
60  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1])), [txid1])
61  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
62  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist)
63 
64  txin_spent = self.nodes[1].listunspent(1).pop()
65  tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 500})
66  self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransaction(tx3)["hex"])
67  self.nodes[0].generate(1)
68  self.sync_all()
69 
70  txid_spent = txin_spent["txid"]
71  txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
72 
73  # We can't find the block from a fully-spent tx
74  # Doesn't apply to Dash Core - we have txindex always on
75  # assert_raises(JSONRPCException, self.nodes[2].gettxoutproof, [txid_spent])
76  # ...but we can if we specify the block
77  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
78  # ...or if the first tx is not fully-spent
79  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
80  try:
81  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
82  except JSONRPCException:
83  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1])), txlist)
84  # ...or if we have a -txindex
85  assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
86 
87 if __name__ == '__main__':
UniValue gettxoutproof(const UniValue &params, bool fHelp)
UniValue getbalance(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:792
def assert_raises(exc, fun, args, kwds)
Definition: util.py:469
UniValue getblock(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:483
UniValue listunspent(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:2533
def connect_nodes(from_connection, node_num)
Definition: util.py:343
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
UniValue getblockhash(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:311
UniValue getblockcount(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:131
def initialize_chain_clean(test_dir, num_nodes)
Definition: util.py:252
UniValue signrawtransaction(const UniValue &params, bool fHelp)
UniValue createrawtransaction(const UniValue &params, bool fHelp)
def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None)
Definition: util.py:281
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
UniValue sendrawtransaction(const UniValue &params, bool fHelp)
UniValue verifytxoutproof(const UniValue &params, bool fHelp)
def assert_equal(thing1, thing2)
Definition: util.py:461