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"]
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 with a 200 block chain
34 assert_equal(self
.nodes
[0].getblockcount(), 200)
36 # Mine four blocks. After this, nodes[0] blocks
37 # 101, 102, and 103 are spend-able.
38 new_blocks
= self
.nodes
[1].generate(4)
41 node0_address
= self
.nodes
[0].getnewaddress()
42 node1_address
= self
.nodes
[1].getnewaddress()
44 # Three scenarios for re-orging coinbase spends in the memory pool:
45 # 1. Direct coinbase spend : spend_101
46 # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
47 # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
48 # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
49 # and make sure the mempool code behaves correctly.
50 b
= [ self
.nodes
[0].getblockhash(n
) for n
in range(101, 105) ]
51 coinbase_txids
= [ self
.nodes
[0].getblock(h
)['tx'][0] for h
in b
]
52 spend_101_raw
= create_tx(self
.nodes
[0], coinbase_txids
[1], node1_address
, 49.99)
53 spend_102_raw
= create_tx(self
.nodes
[0], coinbase_txids
[2], node0_address
, 49.99)
54 spend_103_raw
= create_tx(self
.nodes
[0], coinbase_txids
[3], node0_address
, 49.99)
56 # Create a transaction which is time-locked to two blocks in the future
57 timelock_tx
= self
.nodes
[0].createrawtransaction([{"txid": coinbase_txids
[0], "vout": 0}], {node0_address
: 49.99})
59 timelock_tx
= timelock_tx
.replace("ffffffff", "11111191", 1)
60 timelock_tx
= timelock_tx
[:-8] + hex(self
.nodes
[0].getblockcount() + 2)[2:] + "000000"
61 timelock_tx
= self
.nodes
[0].signrawtransaction(timelock_tx
)["hex"]
62 # This will raise an exception because the timelock transaction is too immature to spend
63 assert_raises_jsonrpc(-26, "non-final", self
.nodes
[0].sendrawtransaction
, timelock_tx
)
65 # Broadcast and mine spend_102 and 103:
66 spend_102_id
= self
.nodes
[0].sendrawtransaction(spend_102_raw
)
67 spend_103_id
= self
.nodes
[0].sendrawtransaction(spend_103_raw
)
68 self
.nodes
[0].generate(1)
69 # Time-locked transaction is still too immature to spend
70 assert_raises_jsonrpc(-26,'non-final', self
.nodes
[0].sendrawtransaction
, timelock_tx
)
72 # Create 102_1 and 103_1:
73 spend_102_1_raw
= create_tx(self
.nodes
[0], spend_102_id
, node1_address
, 49.98)
74 spend_103_1_raw
= create_tx(self
.nodes
[0], spend_103_id
, node1_address
, 49.98)
76 # Broadcast and mine 103_1:
77 spend_103_1_id
= self
.nodes
[0].sendrawtransaction(spend_103_1_raw
)
78 last_block
= self
.nodes
[0].generate(1)
79 # Time-locked transaction can now be spent
80 timelock_tx_id
= self
.nodes
[0].sendrawtransaction(timelock_tx
)
82 # ... now put spend_101 and spend_102_1 in memory pools:
83 spend_101_id
= self
.nodes
[0].sendrawtransaction(spend_101_raw
)
84 spend_102_1_id
= self
.nodes
[0].sendrawtransaction(spend_102_1_raw
)
88 assert_equal(set(self
.nodes
[0].getrawmempool()), {spend_101_id
, spend_102_1_id
, timelock_tx_id
})
90 for node
in self
.nodes
:
91 node
.invalidateblock(last_block
[0])
92 # Time-locked transaction is now too immature and has been removed from the mempool
93 # spend_103_1 has been re-orged out of the chain and is back in the mempool
94 assert_equal(set(self
.nodes
[0].getrawmempool()), {spend_101_id
, spend_102_1_id
, spend_103_1_id
})
96 # Use invalidateblock to re-org back and make all those coinbase spends
98 for node
in self
.nodes
:
99 node
.invalidateblock(new_blocks
[0])
103 # mempool should be empty.
104 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
106 if __name__
== '__main__':
107 MempoolCoinbaseTest().main()