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) 2020-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"""Tests that a mempool transaction expires after a given timeout and that its6children are removed as well.8Both the default expiry timeout defined by DEFAULT_MEMPOOL_EXPIRY_HOURS and a user9definable expiry timeout via the '-mempoolexpiry=<n>' command line argument10(<n> is the timeout in hours) are tested.11"""13from datetime import timedelta15from test_framework.messages import (16COIN,17DEFAULT_MEMPOOL_EXPIRY_HOURS,18)19from test_framework.test_framework import BitcoinTestFramework20from test_framework.util import (21assert_equal,22assert_raises_rpc_error,23)24from test_framework.wallet import MiniWallet26CUSTOM_MEMPOOL_EXPIRY = 10 # hours29class MempoolExpiryTest(BitcoinTestFramework):30def set_test_params(self):31self.num_nodes = 133def test_transaction_expiry(self, timeout):34"""Tests that a transaction expires after the expiry timeout and its35children are removed as well."""36node = self.nodes[0]38# Send a parent transaction that will expire.39parent = self.wallet.send_self_transfer(from_node=node)40parent_txid = parent["txid"]41parent_utxo = self.wallet.get_utxo(txid=parent_txid)42independent_utxo = self.wallet.get_utxo()44# Add prioritisation to this transaction to check that it persists after the expiry45node.prioritisetransaction(parent_txid, 0, COIN)46assert_equal(node.getprioritisedtransactions()[parent_txid], { "fee_delta" : COIN, "in_mempool" : True, "modified_fee": COIN + COIN * parent["fee"] })48# Ensure the transactions we send to trigger the mempool check spend utxos that are independent of49# the transactions being tested for expiration.50trigger_utxo1 = self.wallet.get_utxo()51trigger_utxo2 = self.wallet.get_utxo()53# Set the mocktime to the arrival time of the parent transaction.54entry_time = node.getmempoolentry(parent_txid)['time']55node.setmocktime(entry_time)57# Let half of the timeout elapse and broadcast the child transaction spending the parent transaction.58half_expiry_time = entry_time + int(60 * 60 * timeout/2)59node.setmocktime(half_expiry_time)60child_txid = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=parent_utxo)['txid']61assert_equal(parent_txid, node.getmempoolentry(child_txid)['depends'][0])62self.log.info('Broadcast child transaction after {} hours.'.format(63timedelta(seconds=(half_expiry_time-entry_time))))65# Broadcast another (independent) transaction.66independent_txid = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=independent_utxo)['txid']68# Let most of the timeout elapse and check that the parent tx is still69# in the mempool.70nearly_expiry_time = entry_time + 60 * 60 * timeout - 571node.setmocktime(nearly_expiry_time)72# Broadcast a transaction as the expiry of transactions in the mempool is only checked73# when a new transaction is added to the mempool.74self.wallet.send_self_transfer(from_node=node, utxo_to_spend=trigger_utxo1)75self.log.info('Test parent tx not expired after {} hours.'.format(76timedelta(seconds=(nearly_expiry_time-entry_time))))77assert_equal(entry_time, node.getmempoolentry(parent_txid)['time'])79# Transaction should be evicted from the mempool after the expiry time80# has passed.81expiry_time = entry_time + 60 * 60 * timeout + 582node.setmocktime(expiry_time)83# Again, broadcast a transaction so the expiry of transactions in the mempool is checked.84self.wallet.send_self_transfer(from_node=node, utxo_to_spend=trigger_utxo2)85self.log.info('Test parent tx expiry after {} hours.'.format(86timedelta(seconds=(expiry_time-entry_time))))87assert_raises_rpc_error(-5, 'Transaction not in mempool',88node.getmempoolentry, parent_txid)90# Prioritisation does not disappear when transaction expires91assert_equal(node.getprioritisedtransactions()[parent_txid], { "fee_delta" : COIN, "in_mempool" : False})93# The child transaction should be removed from the mempool as well.94self.log.info('Test child tx is evicted as well.')95assert_raises_rpc_error(-5, 'Transaction not in mempool',96node.getmempoolentry, child_txid)98# Check that the independent tx is still in the mempool.99self.log.info('Test the independent tx not expired after {} hours.'.format(100timedelta(seconds=(expiry_time-half_expiry_time))))101assert_equal(half_expiry_time, node.getmempoolentry(independent_txid)['time'])103def run_test(self):104self.wallet = MiniWallet(self.nodes[0])106self.log.info('Test default mempool expiry timeout of %d hours.' %107DEFAULT_MEMPOOL_EXPIRY_HOURS)108self.test_transaction_expiry(DEFAULT_MEMPOOL_EXPIRY_HOURS)110self.log.info('Test custom mempool expiry timeout of %d hours.' %111CUSTOM_MEMPOOL_EXPIRY)112self.restart_node(0, ['-mempoolexpiry=%d' % CUSTOM_MEMPOOL_EXPIRY])113self.test_transaction_expiry(CUSTOM_MEMPOOL_EXPIRY)116if __name__ == '__main__':117MempoolExpiryTest(__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.