Merge #11726: Cleanups + nit fixes for walletdir PR
[bitcoinplatinum.git] / test / functional / mempool_reorg.py
blob2803371f5b603892fc549e15284f01b98ec73883
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 set_test_params(self):
17 self.num_nodes = 2
18 self.extra_args = [["-checkmempool"]] * 2
20 alert_filename = None # Set by setup_network
22 def run_test(self):
23 # Start with a 200 block chain
24 assert_equal(self.nodes[0].getblockcount(), 200)
26 # Mine four blocks. After this, nodes[0] blocks
27 # 101, 102, and 103 are spend-able.
28 new_blocks = self.nodes[1].generate(4)
29 self.sync_all()
31 node0_address = self.nodes[0].getnewaddress()
32 node1_address = self.nodes[1].getnewaddress()
34 # Three scenarios for re-orging coinbase spends in the memory pool:
35 # 1. Direct coinbase spend : spend_101
36 # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
37 # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
38 # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
39 # and make sure the mempool code behaves correctly.
40 b = [ self.nodes[0].getblockhash(n) for n in range(101, 105) ]
41 coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
42 spend_101_raw = create_tx(self.nodes[0], coinbase_txids[1], node1_address, 49.99)
43 spend_102_raw = create_tx(self.nodes[0], coinbase_txids[2], node0_address, 49.99)
44 spend_103_raw = create_tx(self.nodes[0], coinbase_txids[3], node0_address, 49.99)
46 # Create a transaction which is time-locked to two blocks in the future
47 timelock_tx = self.nodes[0].createrawtransaction([{"txid": coinbase_txids[0], "vout": 0}], {node0_address: 49.99})
48 # Set the time lock
49 timelock_tx = timelock_tx.replace("ffffffff", "11111191", 1)
50 timelock_tx = timelock_tx[:-8] + hex(self.nodes[0].getblockcount() + 2)[2:] + "000000"
51 timelock_tx = self.nodes[0].signrawtransaction(timelock_tx)["hex"]
52 # This will raise an exception because the timelock transaction is too immature to spend
53 assert_raises_rpc_error(-26, "non-final", self.nodes[0].sendrawtransaction, timelock_tx)
55 # Broadcast and mine spend_102 and 103:
56 spend_102_id = self.nodes[0].sendrawtransaction(spend_102_raw)
57 spend_103_id = self.nodes[0].sendrawtransaction(spend_103_raw)
58 self.nodes[0].generate(1)
59 # Time-locked transaction is still too immature to spend
60 assert_raises_rpc_error(-26,'non-final', self.nodes[0].sendrawtransaction, timelock_tx)
62 # Create 102_1 and 103_1:
63 spend_102_1_raw = create_tx(self.nodes[0], spend_102_id, node1_address, 49.98)
64 spend_103_1_raw = create_tx(self.nodes[0], spend_103_id, node1_address, 49.98)
66 # Broadcast and mine 103_1:
67 spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
68 last_block = self.nodes[0].generate(1)
69 # Time-locked transaction can now be spent
70 timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx)
72 # ... now put spend_101 and spend_102_1 in memory pools:
73 spend_101_id = self.nodes[0].sendrawtransaction(spend_101_raw)
74 spend_102_1_id = self.nodes[0].sendrawtransaction(spend_102_1_raw)
76 self.sync_all()
78 assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, timelock_tx_id})
80 for node in self.nodes:
81 node.invalidateblock(last_block[0])
82 # Time-locked transaction is now too immature and has been removed from the mempool
83 # spend_103_1 has been re-orged out of the chain and is back in the mempool
84 assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, spend_103_1_id})
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])
91 self.sync_all()
93 # mempool should be empty.
94 assert_equal(set(self.nodes[0].getrawmempool()), set())
96 if __name__ == '__main__':
97 MempoolCoinbaseTest().main()