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.
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
):
19 self
.setup_clean_chain
= False
21 alert_filename
= None # Set by setup_network
23 def setup_network(self
):
24 args
= ["-checkmempool", "-debug=mempool"]
26 self
.nodes
.append(start_node(0, self
.options
.tmpdir
, args
))
27 self
.nodes
.append(start_node(1, self
.options
.tmpdir
, args
))
28 connect_nodes(self
.nodes
[1], 0)
29 self
.is_network_split
= False
33 start_count
= self
.nodes
[0].getblockcount()
35 # Mine three blocks. After this, nodes[0] blocks
36 # 101, 102, and 103 are spend-able.
37 new_blocks
= self
.nodes
[1].generate(4)
40 node0_address
= self
.nodes
[0].getnewaddress()
41 node1_address
= self
.nodes
[1].getnewaddress()
43 # Three scenarios for re-orging coinbase spends in the memory pool:
44 # 1. Direct coinbase spend : spend_101
45 # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
46 # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
47 # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
48 # and make sure the mempool code behaves correctly.
49 b
= [ self
.nodes
[0].getblockhash(n
) for n
in range(101, 105) ]
50 coinbase_txids
= [ self
.nodes
[0].getblock(h
)['tx'][0] for h
in b
]
51 spend_101_raw
= create_tx(self
.nodes
[0], coinbase_txids
[1], node1_address
, 49.99)
52 spend_102_raw
= create_tx(self
.nodes
[0], coinbase_txids
[2], node0_address
, 49.99)
53 spend_103_raw
= create_tx(self
.nodes
[0], coinbase_txids
[3], node0_address
, 49.99)
55 # Create a block-height-locked transaction which will be invalid after reorg
56 timelock_tx
= self
.nodes
[0].createrawtransaction([{"txid": coinbase_txids
[0], "vout": 0}], {node0_address
: 49.99})
58 timelock_tx
= timelock_tx
.replace("ffffffff", "11111191", 1)
59 timelock_tx
= timelock_tx
[:-8] + hex(self
.nodes
[0].getblockcount() + 2)[2:] + "000000"
60 timelock_tx
= self
.nodes
[0].signrawtransaction(timelock_tx
)["hex"]
61 assert_raises(JSONRPCException
, self
.nodes
[0].sendrawtransaction
, timelock_tx
)
63 # Broadcast and mine spend_102 and 103:
64 spend_102_id
= self
.nodes
[0].sendrawtransaction(spend_102_raw
)
65 spend_103_id
= self
.nodes
[0].sendrawtransaction(spend_103_raw
)
66 self
.nodes
[0].generate(1)
67 assert_raises(JSONRPCException
, self
.nodes
[0].sendrawtransaction
, timelock_tx
)
69 # Create 102_1 and 103_1:
70 spend_102_1_raw
= create_tx(self
.nodes
[0], spend_102_id
, node1_address
, 49.98)
71 spend_103_1_raw
= create_tx(self
.nodes
[0], spend_103_id
, node1_address
, 49.98)
73 # Broadcast and mine 103_1:
74 spend_103_1_id
= self
.nodes
[0].sendrawtransaction(spend_103_1_raw
)
75 last_block
= self
.nodes
[0].generate(1)
76 timelock_tx_id
= self
.nodes
[0].sendrawtransaction(timelock_tx
)
78 # ... now put spend_101 and spend_102_1 in memory pools:
79 spend_101_id
= self
.nodes
[0].sendrawtransaction(spend_101_raw
)
80 spend_102_1_id
= self
.nodes
[0].sendrawtransaction(spend_102_1_raw
)
84 assert_equal(set(self
.nodes
[0].getrawmempool()), {spend_101_id
, spend_102_1_id
, timelock_tx_id
})
86 for node
in self
.nodes
:
87 node
.invalidateblock(last_block
[0])
88 assert_equal(set(self
.nodes
[0].getrawmempool()), {spend_101_id
, spend_102_1_id
, spend_103_1_id
})
90 # Use invalidateblock to re-org back and make all those coinbase spends
92 for node
in self
.nodes
:
93 node
.invalidateblock(new_blocks
[0])
97 # mempool should be empty.
98 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
100 if __name__
== '__main__':
101 MempoolCoinbaseTest().main()