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
):
16 self
.setup_clean_chain
= False
17 # Just need one node for this test
18 self
.extra_args
= [["-checkmempool"]]
21 node0_address
= self
.nodes
[0].getnewaddress()
22 # Spend block 1/2/3's coinbase transactions
24 # Create three more transactions, spending the spends
26 # ... make sure all the transactions are confirmed
27 # Invalidate both blocks
28 # ... make sure all the transactions are put back in the mempool
30 # ... make sure all the transactions are confirmed again.
32 b
= [ self
.nodes
[0].getblockhash(n
) for n
in range(1, 4) ]
33 coinbase_txids
= [ self
.nodes
[0].getblock(h
)['tx'][0] for h
in b
]
34 spends1_raw
= [ create_tx(self
.nodes
[0], txid
, node0_address
, 49.99) for txid
in coinbase_txids
]
35 spends1_id
= [ self
.nodes
[0].sendrawtransaction(tx
) for tx
in spends1_raw
]
38 blocks
.extend(self
.nodes
[0].generate(1))
40 spends2_raw
= [ create_tx(self
.nodes
[0], txid
, node0_address
, 49.98) for txid
in spends1_id
]
41 spends2_id
= [ self
.nodes
[0].sendrawtransaction(tx
) for tx
in spends2_raw
]
43 blocks
.extend(self
.nodes
[0].generate(1))
45 # mempool should be empty, all txns confirmed
46 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
47 for txid
in spends1_id
+spends2_id
:
48 tx
= self
.nodes
[0].gettransaction(txid
)
49 assert(tx
["confirmations"] > 0)
51 # Use invalidateblock to re-org back; all transactions should
52 # end up unconfirmed and back in the mempool
53 for node
in self
.nodes
:
54 node
.invalidateblock(blocks
[0])
56 # mempool should be empty, all txns confirmed
57 assert_equal(set(self
.nodes
[0].getrawmempool()), set(spends1_id
+spends2_id
))
58 for txid
in spends1_id
+spends2_id
:
59 tx
= self
.nodes
[0].gettransaction(txid
)
60 assert(tx
["confirmations"] == 0)
62 # Generate another block, they should all get mined
63 self
.nodes
[0].generate(1)
64 # mempool should be empty, all txns confirmed
65 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
66 for txid
in spends1_id
+spends2_id
:
67 tx
= self
.nodes
[0].gettransaction(txid
)
68 assert(tx
["confirmations"] > 0)
71 if __name__
== '__main__':
72 MempoolCoinbaseTest().main()