scripted-diff: Use the C++11 keyword nullptr to denote the pointer literal instead...
[bitcoinplatinum.git] / test / functional / mempool_reorg.py
blob937bf4bab5781624eaca85936723a9882ffba0b5
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2016 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 """Test mempool re-org scenarios.
7 Test re-org scenarios with a mempool that contains transactions
8 that spend (directly or indirectly) coinbase transactions.
9 """
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.util import *
14 # Create one-input, one-output, no-fee transaction:
15 class MempoolCoinbaseTest(BitcoinTestFramework):
16 def __init__(self):
17 super().__init__()
18 self.num_nodes = 2
19 self.setup_clean_chain = False
20 self.extra_args = [["-checkmempool"]] * 2
22 alert_filename = None # Set by setup_network
24 def run_test(self):
25 # Start with a 200 block chain
26 assert_equal(self.nodes[0].getblockcount(), 200)
28 # Mine four blocks. After this, nodes[0] blocks
29 # 101, 102, and 103 are spend-able.
30 new_blocks = self.nodes[1].generate(4)
31 self.sync_all()
33 node0_address = self.nodes[0].getnewaddress()
34 node1_address = self.nodes[1].getnewaddress()
36 # Three scenarios for re-orging coinbase spends in the memory pool:
37 # 1. Direct coinbase spend : spend_101
38 # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
39 # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
40 # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
41 # and make sure the mempool code behaves correctly.
42 b = [ self.nodes[0].getblockhash(n) for n in range(101, 105) ]
43 coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
44 spend_101_raw = create_tx(self.nodes[0], coinbase_txids[1], node1_address, 49.99)
45 spend_102_raw = create_tx(self.nodes[0], coinbase_txids[2], node0_address, 49.99)
46 spend_103_raw = create_tx(self.nodes[0], coinbase_txids[3], node0_address, 49.99)
48 # Create a transaction which is time-locked to two blocks in the future
49 timelock_tx = self.nodes[0].createrawtransaction([{"txid": coinbase_txids[0], "vout": 0}], {node0_address: 49.99})
50 # Set the time lock
51 timelock_tx = timelock_tx.replace("ffffffff", "11111191", 1)
52 timelock_tx = timelock_tx[:-8] + hex(self.nodes[0].getblockcount() + 2)[2:] + "000000"
53 timelock_tx = self.nodes[0].signrawtransaction(timelock_tx)["hex"]
54 # This will raise an exception because the timelock transaction is too immature to spend
55 assert_raises_jsonrpc(-26, "non-final", self.nodes[0].sendrawtransaction, timelock_tx)
57 # Broadcast and mine spend_102 and 103:
58 spend_102_id = self.nodes[0].sendrawtransaction(spend_102_raw)
59 spend_103_id = self.nodes[0].sendrawtransaction(spend_103_raw)
60 self.nodes[0].generate(1)
61 # Time-locked transaction is still too immature to spend
62 assert_raises_jsonrpc(-26,'non-final', self.nodes[0].sendrawtransaction, timelock_tx)
64 # Create 102_1 and 103_1:
65 spend_102_1_raw = create_tx(self.nodes[0], spend_102_id, node1_address, 49.98)
66 spend_103_1_raw = create_tx(self.nodes[0], spend_103_id, node1_address, 49.98)
68 # Broadcast and mine 103_1:
69 spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
70 last_block = self.nodes[0].generate(1)
71 # Time-locked transaction can now be spent
72 timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx)
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)
78 self.sync_all()
80 assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, timelock_tx_id})
82 for node in self.nodes:
83 node.invalidateblock(last_block[0])
84 # Time-locked transaction is now too immature and has been removed from the mempool
85 # spend_103_1 has been re-orged out of the chain and is back in the mempool
86 assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, spend_103_1_id})
88 # Use invalidateblock to re-org back and make all those coinbase spends
89 # immature/invalid:
90 for node in self.nodes:
91 node.invalidateblock(new_blocks[0])
93 self.sync_all()
95 # mempool should be empty.
96 assert_equal(set(self.nodes[0].getrawmempool()), set())
98 if __name__ == '__main__':
99 MempoolCoinbaseTest().main()