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) 2020-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 <chain.h>6#include <chainparams.h>7#include <coins.h>8#include <consensus/amount.h>9#include <consensus/validation.h>10#include <node/blockstorage.h>11#include <node/kernel_notifications.h>12#include <primitives/block.h>13#include <primitives/transaction.h>14#include <random.h>15#include <script/script.h>16#include <sync.h>17#include <test/util/chainstate.h>18#include <test/util/coins.h>19#include <test/util/common.h>20#include <test/util/setup_common.h>21#include <tinyformat.h>22#include <uint256.h>23#include <util/check.h>24#include <validation.h>26#include <boost/test/unit_test.hpp>28#include <memory>29#include <optional>30#include <vector>32class CTxMemPool;34BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, ChainTestingSetup)36//! Test resizing coins-related Chainstate caches during runtime.37//!38BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)39{40ChainstateManager& manager = *Assert(m_node.chainman);41CTxMemPool& mempool = *Assert(m_node.mempool);42Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));43c1.InitCoinsDB(44/*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);45WITH_LOCK(::cs_main, c1.InitCoinsCache(8_MiB));46BOOST_REQUIRE(manager.LoadGenesisBlock()); // Need at least one block loaded to be able to flush caches48// Add a coin to the in-memory cache, upsize once, then downsize.49{50LOCK(::cs_main);51const auto outpoint = AddTestCoin(m_rng, c1.CoinsTip());53// Set a meaningless bestblock value in the coinsview cache - otherwise we won't54// flush during ResizecoinsCaches() and will subsequently hit an assertion.55c1.CoinsTip().SetBestBlock(m_rng.rand256());57BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));59c1.ResizeCoinsCaches(6016_MiB, // upsizing the coinsview cache614_MiB // downsizing the coinsdb cache62);64// View should still have the coin cached, since we haven't destructed the cache on upsize.65BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));67c1.ResizeCoinsCaches(684_MiB, // downsizing the coinsview cache698_MiB // upsizing the coinsdb cache70);72// The view cache should be empty since we had to destruct to downsize.73BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint));74}75}77BOOST_FIXTURE_TEST_CASE(connect_tip_does_not_cache_inputs_on_failed_connect, TestChain100Setup)78{79Chainstate& chainstate{Assert(m_node.chainman)->ActiveChainstate()};81COutPoint outpoint;82{83LOCK(cs_main);84outpoint = AddTestCoin(m_rng, chainstate.CoinsTip());85chainstate.CoinsTip().Flush(/*reallocate_cache=*/false);86}88CMutableTransaction tx;89tx.vin.emplace_back(outpoint);90tx.vout.emplace_back(MAX_MONEY, CScript{} << OP_TRUE);92const auto tip{WITH_LOCK(cs_main, return chainstate.m_chain.Tip()->GetBlockHash())};93const CBlock block{CreateBlock({tx}, CScript{} << OP_TRUE)};94BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(std::make_shared<CBlock>(block), true, true, nullptr));96LOCK(cs_main);97BOOST_CHECK_EQUAL(tip, chainstate.m_chain.Tip()->GetBlockHash()); // block rejected98BOOST_CHECK(!chainstate.CoinsTip().HaveCoinInCache(outpoint)); // input not cached99}101//! Test UpdateTip behavior for both active and background chainstates.102//!103//! When run on the background chainstate, UpdateTip should do a subset104//! of what it does for the active chainstate.105BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)106{107ChainstateManager& chainman = *Assert(m_node.chainman);108const auto get_notify_tip{[&]() {109LOCK(m_node.notifications->m_tip_block_mutex);110BOOST_REQUIRE(m_node.notifications->TipBlock());111return *m_node.notifications->TipBlock();112}};113uint256 curr_tip = get_notify_tip();115// Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can116// be found.117mineBlocks(10);119// After adding some blocks to the tip, best block should have changed.120BOOST_CHECK(get_notify_tip() != curr_tip);122// Grab block 1 from disk; we'll add it to the background chain later.123std::shared_ptr<CBlock> pblockone = std::make_shared<CBlock>();124{125LOCK(::cs_main);126chainman.m_blockman.ReadBlock(*pblockone, *chainman.ActiveChain()[1]);127}129BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(130this, NoMalleation, /*reset_chainstate=*/ true));132// Ensure our active chain is the snapshot chainstate.133BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));135curr_tip = get_notify_tip();137// Mine a new block on top of the activated snapshot chainstate.138mineBlocks(1); // Defined in TestChain100Setup.140// After adding some blocks to the snapshot tip, best block should have changed.141BOOST_CHECK(get_notify_tip() != curr_tip);143curr_tip = get_notify_tip();145Chainstate& background_cs{*Assert(WITH_LOCK(::cs_main, return chainman.HistoricalChainstate()))};147// Append the first block to the background chain.148BlockValidationState state;149CBlockIndex* pindex = nullptr;150const CChainParams& chainparams = Params();151bool newblock = false;153// TODO: much of this is inlined from ProcessNewBlock(); just reuse PNB()154// once it is changed to support multiple chainstates.155{156LOCK(::cs_main);157bool checked = CheckBlock(*pblockone, state, chainparams.GetConsensus());158BOOST_CHECK(checked);159bool accepted = chainman.AcceptBlock(160pblockone, state, &pindex, true, nullptr, &newblock, true);161BOOST_CHECK(accepted);162}164// UpdateTip is called here165bool block_added = background_cs.ActivateBestChain(state, pblockone);167// Ensure tip is as expected168BOOST_CHECK_EQUAL(background_cs.m_chain.Tip()->GetBlockHash(), pblockone->GetHash());170// get_notify_tip() should be unchanged after adding a block to the background171// validation chain.172BOOST_CHECK(block_added);173BOOST_CHECK_EQUAL(curr_tip, get_notify_tip());174}176BOOST_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.