Dash Core  0.12.2.1
P2P Digital Currency
mempool_reorg.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 re-org scenarios with a mempool that contains transactions
8 # that spend (directly or indirectly) coinbase transactions.
9 #
10 
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.util import *
13 
14 # Create one-input, one-output, no-fee transaction:
16 
17  alert_filename = None # Set by setup_network
18 
19  def setup_network(self):
20  args = ["-checkmempool", "-debug=mempool"]
21  self.nodes = []
22  self.nodes.append(start_node(0, self.options.tmpdir, args))
23  self.nodes.append(start_node(1, self.options.tmpdir, args))
24  connect_nodes(self.nodes[1], 0)
25  self.is_network_split = False
26  self.sync_all()
27 
28  def run_test(self):
29  start_count = self.nodes[0].getblockcount()
30 
31  # Mine three blocks. After this, nodes[0] blocks
32  # 101, 102, and 103 are spend-able.
33  new_blocks = self.nodes[1].generate(4)
34  self.sync_all()
35 
36  node0_address = self.nodes[0].getnewaddress()
37  node1_address = self.nodes[1].getnewaddress()
38 
39  # Three scenarios for re-orging coinbase spends in the memory pool:
40  # 1. Direct coinbase spend : spend_101
41  # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
42  # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
43  # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
44  # and make sure the mempool code behaves correctly.
45  b = [ self.nodes[0].getblockhash(n) for n in range(101, 105) ]
46  coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
47  spend_101_raw = create_tx(self.nodes[0], coinbase_txids[1], node1_address, 500)
48  spend_102_raw = create_tx(self.nodes[0], coinbase_txids[2], node0_address, 500)
49  spend_103_raw = create_tx(self.nodes[0], coinbase_txids[3], node0_address, 500)
50 
51  # Create a block-height-locked transaction which will be invalid after reorg
52  timelock_tx = self.nodes[0].createrawtransaction([{"txid": coinbase_txids[0], "vout": 0}], {node0_address: 500})
53  # Set the time lock
54  timelock_tx = timelock_tx.replace("ffffffff", "11111111", 1)
55  timelock_tx = timelock_tx[:-8] + hex(self.nodes[0].getblockcount() + 2)[2:] + "000000"
56  timelock_tx = self.nodes[0].signrawtransaction(timelock_tx)["hex"]
57  assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, timelock_tx)
58 
59  # Broadcast and mine spend_102 and 103:
60  spend_102_id = self.nodes[0].sendrawtransaction(spend_102_raw)
61  spend_103_id = self.nodes[0].sendrawtransaction(spend_103_raw)
62  self.nodes[0].generate(1)
63  assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, timelock_tx)
64 
65  # Create 102_1 and 103_1:
66  spend_102_1_raw = create_tx(self.nodes[0], spend_102_id, node1_address, 500)
67  spend_103_1_raw = create_tx(self.nodes[0], spend_103_id, node1_address, 500)
68 
69  # Broadcast and mine 103_1:
70  spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
71  last_block = self.nodes[0].generate(1)
72  timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx)
73 
74  # ... now put spend_101 and spend_102_1 in memory pools:
75  spend_101_id = self.nodes[0].sendrawtransaction(spend_101_raw)
76  spend_102_1_id = self.nodes[0].sendrawtransaction(spend_102_1_raw)
77 
78  self.sync_all()
79 
80  assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, timelock_tx_id})
81 
82  for node in self.nodes:
83  node.invalidateblock(last_block[0])
84  assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, spend_103_1_id})
85 
86  # Use invalidateblock to re-org back and make all those coinbase spends
87  # immature/invalid:
88  for node in self.nodes:
89  node.invalidateblock(new_blocks[0])
90 
91  self.sync_all()
92 
93  # mempool should be empty.
94  assert_equal(set(self.nodes[0].getrawmempool()), set())
95 
96 if __name__ == '__main__':
def assert_raises(exc, fun, args, kwds)
Definition: util.py:469
UniValue getblock(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:483
def connect_nodes(from_connection, node_num)
Definition: util.py:343
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def create_tx(dashd, fromaddresses, toaddress, amount, fee)
Definition: spendfrom.py:142
UniValue getblockhash(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:311
UniValue getblockcount(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:131
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 getrawmempool(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:234
def assert_equal(thing1, thing2)
Definition: util.py:461