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 resurrection of mined transactions when the blockchain is re-organized."""7from test_framework.test_framework import BitcoinTestFramework8from test_framework.util import assert_equal9from test_framework.wallet import MiniWallet10from test_framework.blocktools import create_empty_fork12class MempoolCoinbaseTest(BitcoinTestFramework):13def set_test_params(self):14self.num_nodes = 116def trigger_reorg(self, fork_blocks):17"""Trigger reorg of the fork blocks."""18for block in fork_blocks:19self.nodes[0].submitblock(block.serialize().hex())20assert_equal(self.nodes[0].getbestblockhash(), fork_blocks[-1].hash_hex)22def run_test(self):23node = self.nodes[0]24wallet = MiniWallet(node)26# Spend block 1/2/3's coinbase transactions27# Mine a block28# Create three more transactions, spending the spends29# Mine another block30# ... make sure all the transactions are confirmed31# Invalidate both blocks32# ... make sure all the transactions are put back in the mempool33# Mine a new block34# ... make sure all the transactions are confirmed again36# Prep for fork37fork_blocks = create_empty_fork(self.nodes[0])38blocks = []39spends1_ids = [wallet.send_self_transfer(from_node=node)['txid'] for _ in range(3)]40blocks.extend(self.generate(node, 1))41spends2_ids = [wallet.send_self_transfer(from_node=node)['txid'] for _ in range(3)]42blocks.extend(self.generate(node, 1))44spends_ids = set(spends1_ids + spends2_ids)46# mempool should be empty, all txns confirmed47assert_equal(set(node.getrawmempool()), set())48confirmed_txns = set(node.getblock(blocks[0])['tx'] + node.getblock(blocks[1])['tx'])49# Checks that all spend txns are contained in the mined blocks50assert spends_ids < confirmed_txns52# Trigger reorg53self.trigger_reorg(fork_blocks)55# All txns should be back in mempool with 0 confirmations56assert_equal(set(node.getrawmempool()), spends_ids)58# Generate another block, they should all get mined59blocks = self.generate(node, 1)60# mempool should be empty, all txns confirmed61assert_equal(set(node.getrawmempool()), set())62confirmed_txns = set(node.getblock(blocks[0])['tx'])63assert spends_ids < confirmed_txns66if __name__ == '__main__':67MempoolCoinbaseTest(__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.