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) 2019-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.5#include <sync.h>6#include <test/util/coins.h>7#include <test/util/random.h>8#include <test/util/common.h>9#include <test/util/setup_common.h>10#include <validation.h>12#include <boost/test/unit_test.hpp>14BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, TestingSetup)16//! Verify that Chainstate::GetCoinsCacheSizeState() switches from OK→LARGE→CRITICAL17//! at the expected utilization thresholds, first with *no* mempool head-room,18//! then with additional mempool head-room.19BOOST_AUTO_TEST_CASE(getcoinscachesizestate)20{21constexpr uint64_t MAX_COINS_BYTES{8_MiB};22constexpr uint64_t MAX_MEMPOOL_BYTES{4_MiB};23constexpr uint64_t MAX_ATTEMPTS{50'000};24Chainstate& chainstate{m_node.chainman->ActiveChainstate()};26LOCK(::cs_main);27BOOST_REQUIRE(chainstate.ResizeCoinsCaches(MAX_COINS_BYTES, /*coinsdb_size=*/1_MiB));28CCoinsViewCache& view{chainstate.CoinsTip()};30// Sanity: an empty cache should be ≲ 1 chunk (~ 256 KiB).31BOOST_CHECK_LT(view.DynamicMemoryUsage() / (256 * 1024.0), 1.1);33// Run the same growth-path twice: first with 0 head-room, then with extra head-room34for (uint64_t max_mempool_size_bytes : {uint64_t{0}, MAX_MEMPOOL_BYTES}) {35const int64_t full_cap{int64_t(MAX_COINS_BYTES + max_mempool_size_bytes)};36const int64_t large_cap{LargeCoinsCacheThreshold(full_cap)};38// OK → LARGE39auto state{chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes)};40for (uint64_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= large_cap; ++i) {41BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::OK);42AddTestCoin(m_rng, view);43state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);44}46// LARGE → CRITICAL47for (uint64_t i{0}; i < MAX_ATTEMPTS && int64_t(view.DynamicMemoryUsage()) <= full_cap; ++i) {48BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::LARGE);49AddTestCoin(m_rng, view);50state = chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, max_mempool_size_bytes);51}52BOOST_CHECK_EQUAL(state, CoinsCacheSizeState::CRITICAL);53}55// Unused mempool space permits many more coins.56for (int i{0}; i < 1'000; ++i) {57AddTestCoin(m_rng, view);58BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(), CoinsCacheSizeState::OK);59}61// CRITICAL → OK via Flush62BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::CRITICAL);63BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(), CoinsCacheSizeState::OK);64view.SetBestBlock(m_rng.rand256());65view.Flush();66BOOST_CHECK_EQUAL(chainstate.GetCoinsCacheSizeState(MAX_COINS_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::OK);67}69BOOST_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.