Dash Core  0.12.2.1
P2P Digital Currency
mempool_resurrect_test.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 resurrection of mined transactions when
8 # the blockchain is re-organized.
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  def setup_network(self):
18  # Just need one node for this test
19  args = ["-checkmempool", "-debug=mempool"]
20  self.nodes = []
21  self.nodes.append(start_node(0, self.options.tmpdir, args))
22  self.is_network_split = False
23 
24  def run_test(self):
25  node0_address = self.nodes[0].getnewaddress()
26  # Spend block 1/2/3's coinbase transactions
27  # Mine a block.
28  # Create three more transactions, spending the spends
29  # Mine another block.
30  # ... make sure all the transactions are confirmed
31  # Invalidate both blocks
32  # ... make sure all the transactions are put back in the mempool
33  # Mine a new block
34  # ... make sure all the transactions are confirmed again.
35 
36  b = [ self.nodes[0].getblockhash(n) for n in range(1, 4) ]
37  coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
38  spends1_raw = [ create_tx(self.nodes[0], txid, node0_address, 500) for txid in coinbase_txids ]
39  spends1_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw ]
40 
41  blocks = []
42  blocks.extend(self.nodes[0].generate(1))
43 
44  spends2_raw = [ create_tx(self.nodes[0], txid, node0_address, 499.99) for txid in spends1_id ]
45  spends2_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw ]
46 
47  blocks.extend(self.nodes[0].generate(1))
48 
49  # mempool should be empty, all txns confirmed
50  assert_equal(set(self.nodes[0].getrawmempool()), set())
51  for txid in spends1_id+spends2_id:
52  tx = self.nodes[0].gettransaction(txid)
53  assert(tx["confirmations"] > 0)
54 
55  # Use invalidateblock to re-org back; all transactions should
56  # end up unconfirmed and back in the mempool
57  for node in self.nodes:
58  node.invalidateblock(blocks[0])
59 
60  # mempool should be empty, all txns confirmed
61  assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id))
62  for txid in spends1_id+spends2_id:
63  tx = self.nodes[0].gettransaction(txid)
64  assert(tx["confirmations"] == 0)
65 
66  # Generate another block, they should all get mined
67  self.nodes[0].generate(1)
68  # mempool should be empty, all txns confirmed
69  assert_equal(set(self.nodes[0].getrawmempool()), set())
70  for txid in spends1_id+spends2_id:
71  tx = self.nodes[0].gettransaction(txid)
72  assert(tx["confirmations"] > 0)
73 
74 
75 if __name__ == '__main__':
UniValue getblock(const UniValue &params, bool fHelp)
Definition: blockchain.cpp:483
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
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
UniValue gettransaction(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1821