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 resurrection of mined transactions when the blockchain is re-organized."""
7 from test_framework
.test_framework
import BitcoinTestFramework
8 from test_framework
.util
import *
10 # Create one-input, one-output, no-fee transaction:
11 class MempoolCoinbaseTest(BitcoinTestFramework
):
12 def set_test_params(self
):
14 self
.extra_args
= [["-checkmempool"]]
17 node0_address
= self
.nodes
[0].getnewaddress()
18 # Spend block 1/2/3's coinbase transactions
20 # Create three more transactions, spending the spends
22 # ... make sure all the transactions are confirmed
23 # Invalidate both blocks
24 # ... make sure all the transactions are put back in the mempool
26 # ... make sure all the transactions are confirmed again.
28 b
= [ self
.nodes
[0].getblockhash(n
) for n
in range(1, 4) ]
29 coinbase_txids
= [ self
.nodes
[0].getblock(h
)['tx'][0] for h
in b
]
30 spends1_raw
= [ create_tx(self
.nodes
[0], txid
, node0_address
, 49.99) for txid
in coinbase_txids
]
31 spends1_id
= [ self
.nodes
[0].sendrawtransaction(tx
) for tx
in spends1_raw
]
34 blocks
.extend(self
.nodes
[0].generate(1))
36 spends2_raw
= [ create_tx(self
.nodes
[0], txid
, node0_address
, 49.98) for txid
in spends1_id
]
37 spends2_id
= [ self
.nodes
[0].sendrawtransaction(tx
) for tx
in spends2_raw
]
39 blocks
.extend(self
.nodes
[0].generate(1))
41 # mempool should be empty, all txns confirmed
42 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
43 for txid
in spends1_id
+spends2_id
:
44 tx
= self
.nodes
[0].gettransaction(txid
)
45 assert(tx
["confirmations"] > 0)
47 # Use invalidateblock to re-org back; all transactions should
48 # end up unconfirmed and back in the mempool
49 for node
in self
.nodes
:
50 node
.invalidateblock(blocks
[0])
52 # mempool should be empty, all txns confirmed
53 assert_equal(set(self
.nodes
[0].getrawmempool()), set(spends1_id
+spends2_id
))
54 for txid
in spends1_id
+spends2_id
:
55 tx
= self
.nodes
[0].gettransaction(txid
)
56 assert(tx
["confirmations"] == 0)
58 # Generate another block, they should all get mined
59 self
.nodes
[0].generate(1)
60 # mempool should be empty, all txns confirmed
61 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
62 for txid
in spends1_id
+spends2_id
:
63 tx
= self
.nodes
[0].gettransaction(txid
)
64 assert(tx
["confirmations"] > 0)
67 if __name__
== '__main__':
68 MempoolCoinbaseTest().main()