Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2014-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test spending coinbase transactions.7The coinbase transaction in block N can appear in block8N+100... so is valid in the mempool when the best block9height is N+99.10This test makes sure coinbase spends that will be mature11in the next block are accepted into the memory pool,12but less mature coinbase spends are NOT.13"""15from test_framework.test_framework import BitcoinTestFramework16from test_framework.util import assert_equal, assert_raises_rpc_error17from test_framework.wallet import MiniWallet20class MempoolSpendCoinbaseTest(BitcoinTestFramework):21def set_test_params(self):22self.num_nodes = 124def run_test(self):25wallet = MiniWallet(self.nodes[0])27# Invalidate two blocks, so that miniwallet has access to a coin that will mature in the next block28chain_height = 19829self.nodes[0].invalidateblock(self.nodes[0].getblockhash(chain_height + 1))30assert_equal(chain_height, self.nodes[0].getblockcount())32# Coinbase at height chain_height-100+1 ok in mempool, should33# get mined. Coinbase at height chain_height-100+2 is34# too immature to spend.35def coinbase_txid(h):36return self.nodes[0].getblock(self.nodes[0].getblockhash(h))['tx'][0]37utxo_mature = wallet.get_utxo(txid=coinbase_txid(chain_height - 100 + 1))38utxo_immature = wallet.get_utxo(txid=coinbase_txid(chain_height - 100 + 2))40spend_mature_id = wallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_mature)["txid"]42# other coinbase should be too immature to spend43immature_tx = wallet.create_self_transfer(utxo_to_spend=utxo_immature)44assert_raises_rpc_error(-26,45"bad-txns-premature-spend-of-coinbase",46lambda: self.nodes[0].sendrawtransaction(immature_tx['hex']))48# mempool should have just the mature one49assert_equal(self.nodes[0].getrawmempool(), [spend_mature_id])51# mine a block, mature one should get confirmed52self.generate(self.nodes[0], 1)53assert_equal(set(self.nodes[0].getrawmempool()), set())55# ... and now previously immature can be spent:56spend_new_id = self.nodes[0].sendrawtransaction(immature_tx['hex'])57assert_equal(self.nodes[0].getrawmempool(), [spend_new_id])60if __name__ == '__main__':61MempoolSpendCoinbaseTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.