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 <chain.h>6#include <index/coinstatsindex.h>7#include <interfaces/chain.h>8#include <kernel/coinstats.h>9#include <key.h>10#include <primitives/block.h>11#include <primitives/transaction.h>12#include <script/script.h>13#include <sync.h>14#include <test/util/setup_common.h>15#include <util/check.h>16#include <validation.h>18#include <boost/test/unit_test.hpp>20#include <memory>21#include <optional>22#include <span>23#include <vector>25BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)27BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)28{29CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1_MiB, true};30BOOST_REQUIRE(coin_stats_index.Init());32const CBlockIndex* block_index;33{34LOCK(cs_main);35block_index = m_node.chainman->ActiveChain().Tip();36}38// CoinStatsIndex should not be found before it is started.39BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));41// BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex42// is started.43BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());45coin_stats_index.Sync();47// Check that CoinStatsIndex works for genesis block.48const CBlockIndex* genesis_block_index;49{50LOCK(cs_main);51genesis_block_index = m_node.chainman->ActiveChain().Genesis();52}53BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));55// Check that CoinStatsIndex updates with new blocks.56BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));58const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};59std::vector<CMutableTransaction> noTxns;60CreateAndProcessBlock(noTxns, script_pub_key);62// Let the CoinStatsIndex to catch up again.63BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());65const CBlockIndex* new_block_index;66{67LOCK(cs_main);68new_block_index = m_node.chainman->ActiveChain().Tip();69}70BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));72BOOST_CHECK(block_index != new_block_index);74// Shutdown sequence (c.f. Shutdown() in init.cpp)75coin_stats_index.Stop();76}78BOOST_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.