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 spending coinbase transactions.
7 The coinbase transaction in block N can appear in block
8 N+100... so is valid in the mempool when the best block
10 This test makes sure coinbase spends that will be mature
11 in the next block are accepted into the memory pool,
12 but less mature coinbase spends are NOT.
15 from test_framework
.test_framework
import BitcoinTestFramework
16 from test_framework
.util
import *
18 # Create one-input, one-output, no-fee transaction:
19 class MempoolSpendCoinbaseTest(BitcoinTestFramework
):
24 self
.setup_clean_chain
= False
26 def setup_network(self
):
27 # Just need one node for this test
28 args
= ["-checkmempool", "-debug=mempool"]
30 self
.nodes
.append(start_node(0, self
.options
.tmpdir
, args
))
31 self
.is_network_split
= False
34 chain_height
= self
.nodes
[0].getblockcount()
35 assert_equal(chain_height
, 200)
36 node0_address
= self
.nodes
[0].getnewaddress()
38 # Coinbase at height chain_height-100+1 ok in mempool, should
39 # get mined. Coinbase at height chain_height-100+2 is
40 # is too immature to spend.
41 b
= [ self
.nodes
[0].getblockhash(n
) for n
in range(101, 103) ]
42 coinbase_txids
= [ self
.nodes
[0].getblock(h
)['tx'][0] for h
in b
]
43 spends_raw
= [ create_tx(self
.nodes
[0], txid
, node0_address
, 49.99) for txid
in coinbase_txids
]
45 spend_101_id
= self
.nodes
[0].sendrawtransaction(spends_raw
[0])
47 # coinbase at height 102 should be too immature to spend
48 assert_raises(JSONRPCException
, self
.nodes
[0].sendrawtransaction
, spends_raw
[1])
50 # mempool should have just spend_101:
51 assert_equal(self
.nodes
[0].getrawmempool(), [ spend_101_id
])
53 # mine a block, spend_101 should get confirmed
54 self
.nodes
[0].generate(1)
55 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
57 # ... and now height 102 can be spent:
58 spend_102_id
= self
.nodes
[0].sendrawtransaction(spends_raw
[1])
59 assert_equal(self
.nodes
[0].getrawmempool(), [ spend_102_id
])
61 if __name__
== '__main__':
62 MempoolSpendCoinbaseTest().main()