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// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.4//5#include <boost/test/unit_test.hpp>6#include <core_memusage.h>7#include <kernel/disconnected_transactions.h>8#include <test/util/setup_common.h>10BOOST_AUTO_TEST_SUITE(disconnected_transactions)12//! Tests that DisconnectedBlockTransactions limits its own memory properly13BOOST_FIXTURE_TEST_CASE(disconnectpool_memory_limits, TestChain100Setup)14{15// Use the coinbase transactions from TestChain100Setup. It doesn't matter whether these16// transactions would realistically be in a block together, they just need distinct txids and17// uniform size for this test to work.18std::vector<CTransactionRef> block_vtx(m_coinbase_txns);19BOOST_CHECK_EQUAL(block_vtx.size(), 100);21// Roughly estimate sizes to sanity check that DisconnectedBlockTransactions::DynamicMemoryUsage22// is within an expected range.24// Overhead for the hashmap depends on number of buckets25std::unordered_map<Txid, CTransaction*, SaltedTxidHasher> temp_map;26temp_map.reserve(1);27const size_t MAP_1{memusage::DynamicUsage(temp_map)};28temp_map.reserve(100);29const size_t MAP_100{memusage::DynamicUsage(temp_map)};31const size_t TX_USAGE{RecursiveDynamicUsage(block_vtx.front())};32for (const auto& tx : block_vtx)33BOOST_CHECK_EQUAL(RecursiveDynamicUsage(tx), TX_USAGE);35// Our overall formula is unordered map overhead + usage per entry.36// Implementations may vary, but we're trying to guess the usage of data structures.37const size_t ENTRY_USAGE_ESTIMATE{38TX_USAGE39// list entry: 2 pointers (next pointer and prev pointer) + element itself40+ memusage::MallocUsage((2 * sizeof(void*)) + sizeof(decltype(block_vtx)::value_type))41// unordered map: 1 pointer for the hashtable + key and value42+ memusage::MallocUsage(sizeof(void*) + sizeof(decltype(temp_map)::key_type)43+ sizeof(decltype(temp_map)::value_type))};45// DisconnectedBlockTransactions that's just big enough for 1 transaction.46{47DisconnectedBlockTransactions disconnectpool{MAP_1 + ENTRY_USAGE_ESTIMATE};48// Add just 2 (and not all 100) transactions to keep the unordered map's hashtable overhead49// to a minimum and avoid all (instead of all but 1) transactions getting evicted.50std::vector<CTransactionRef> two_txns({block_vtx.at(0), block_vtx.at(1)});51auto evicted_txns{disconnectpool.AddTransactionsFromBlock(two_txns)};52BOOST_CHECK(disconnectpool.DynamicMemoryUsage() <= MAP_1 + ENTRY_USAGE_ESTIMATE);54// Only 1 transaction can be kept55BOOST_CHECK_EQUAL(1, evicted_txns.size());56// Transactions are added from back to front and eviction is FIFO.57BOOST_CHECK_EQUAL(block_vtx.at(1), evicted_txns.front());59disconnectpool.clear();60}62// DisconnectedBlockTransactions with a comfortable maximum memory usage so that nothing is evicted.63// Record usage so we can check size limiting in the next test.64size_t usage_full{0};65{66const size_t USAGE_100_OVERESTIMATE{MAP_100 + ENTRY_USAGE_ESTIMATE * 100};67DisconnectedBlockTransactions disconnectpool{USAGE_100_OVERESTIMATE};68auto evicted_txns{disconnectpool.AddTransactionsFromBlock(block_vtx)};69BOOST_CHECK_EQUAL(evicted_txns.size(), 0);70BOOST_CHECK(disconnectpool.DynamicMemoryUsage() <= USAGE_100_OVERESTIMATE);72usage_full = disconnectpool.DynamicMemoryUsage();74disconnectpool.clear();75}77// DisconnectedBlockTransactions that's just a little too small for all of the transactions.78{79const size_t MAX_MEMUSAGE_99{usage_full - sizeof(void*)};80DisconnectedBlockTransactions disconnectpool{MAX_MEMUSAGE_99};81auto evicted_txns{disconnectpool.AddTransactionsFromBlock(block_vtx)};82BOOST_CHECK(disconnectpool.DynamicMemoryUsage() <= MAX_MEMUSAGE_99);84// Only 1 transaction needed to be evicted85BOOST_CHECK_EQUAL(1, evicted_txns.size());87// Transactions are added from back to front and eviction is FIFO.88// The last transaction of block_vtx should be the first to be evicted.89BOOST_CHECK_EQUAL(block_vtx.back(), evicted_txns.front());91disconnectpool.clear();92}93}95BOOST_AUTO_TEST_SUITE_END()
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.