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.5#include <test/util/validation.h>7#include <coins.h>8#include <consensus/consensus.h>9#include <node/blockstorage.h>10#include <node/mining_types.h>11#include <test/util/mining.h>12#include <test/util/script.h>13#include <test/util/setup_common.h>14#include <test/util/time.h>15#include <test/util/txmempool.h>16#include <txmempool.h>17#include <util/check.h>18#include <util/time.h>19#include <validation.h>20#include <validationinterface.h>22#include <memory>23#include <utility>24#include <vector>26using kernel::ChainstateRole;28void TestBlockManager::CleanupForFuzzing()29{30m_dirty_blockindex.clear();31m_dirty_fileinfo.clear();32m_blockfile_info.resize(1);33}35void TestChainstateManager::DisableNextWrite()36{37struct TestChainstate : public Chainstate {38void ResetNextWrite() { m_next_write = NodeClock::time_point::max() - 1s; }39};40LOCK(::cs_main);41for (const auto& cs : m_chainstates) {42static_cast<TestChainstate&>(*cs).ResetNextWrite();43}44}46void TestChainstateManager::ResetIbd()47{48m_cached_is_ibd = true;49assert(IsInitialBlockDownload());50}52void TestChainstateManager::JumpOutOfIbd()53{54Assert(IsInitialBlockDownload());55m_cached_is_ibd = false;56Assert(!IsInitialBlockDownload());57}59void ValidationInterfaceTest::BlockConnected(60const ChainstateRole& role,61CValidationInterface& obj,62const std::shared_ptr<const CBlock>& block,63const CBlockIndex* pindex)64{65obj.BlockConnected(role, block, pindex);66}67void TestChainstateManager::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)68{69struct TestChainstate : public Chainstate {70void CallInvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)71{72InvalidBlockFound(pindex, state);73}74};76static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidBlockFound(pindex, state);77}79void TestChainstateManager::InvalidChainFound(CBlockIndex* pindexNew)80{81struct TestChainstate : public Chainstate {82void CallInvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)83{84InvalidChainFound(pindexNew);85}86};88static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidChainFound(pindexNew);89}91CBlockIndex* TestChainstateManager::FindMostWorkChain()92{93struct TestChainstate : public Chainstate {94CBlockIndex* CallFindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)95{96return FindMostWorkChain();97}98};100return static_cast<TestChainstate*>(&ActiveChainstate())->CallFindMostWorkChain();101}103void TestChainstateManager::ResetBestInvalid()104{105m_best_invalid = nullptr;106}108std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup, FakeNodeClock& node_clock)109{110node_clock.set(setup.m_node.chainman->GetParams().GenesisBlock().Time());112bilingual_str error{};113setup.m_node.mempool.reset();114setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);115Assert(error.empty());117setup.m_node.chainman.reset();118setup.m_make_chainman();119setup.LoadVerifyActivateChainstate();121node::BlockCreateOptions options;122options.coinbase_output_script = P2WSH_OP_TRUE;124std::vector<std::pair<COutPoint, CAmount>> mature_coinbase;125for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {126COutPoint prevout{MineBlock(setup.m_node, options)};127if (i < COINBASE_MATURITY) {128LOCK(cs_main);129CAmount subsidy{setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};130mature_coinbase.emplace_back(prevout, subsidy);131}132}133return mature_coinbase;134}
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.