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