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
):
20 def set_test_params(self
):
22 self
.extra_args
= [["-checkmempool"]]
25 chain_height
= self
.nodes
[0].getblockcount()
26 assert_equal(chain_height
, 200)
27 node0_address
= self
.nodes
[0].getnewaddress()
29 # Coinbase at height chain_height-100+1 ok in mempool, should
30 # get mined. Coinbase at height chain_height-100+2 is
31 # is too immature to spend.
32 b
= [ self
.nodes
[0].getblockhash(n
) for n
in range(101, 103) ]
33 coinbase_txids
= [ self
.nodes
[0].getblock(h
)['tx'][0] for h
in b
]
34 spends_raw
= [ create_tx(self
.nodes
[0], txid
, node0_address
, 49.99) for txid
in coinbase_txids
]
36 spend_101_id
= self
.nodes
[0].sendrawtransaction(spends_raw
[0])
38 # coinbase at height 102 should be too immature to spend
39 assert_raises_jsonrpc(-26,"bad-txns-premature-spend-of-coinbase", self
.nodes
[0].sendrawtransaction
, spends_raw
[1])
41 # mempool should have just spend_101:
42 assert_equal(self
.nodes
[0].getrawmempool(), [ spend_101_id
])
44 # mine a block, spend_101 should get confirmed
45 self
.nodes
[0].generate(1)
46 assert_equal(set(self
.nodes
[0].getrawmempool()), set())
48 # ... and now height 102 can be spent:
49 spend_102_id
= self
.nodes
[0].sendrawtransaction(spends_raw
[1])
50 assert_equal(self
.nodes
[0].getrawmempool(), [ spend_102_id
])
52 if __name__
== '__main__':
53 MempoolSpendCoinbaseTest().main()