Files
bitcoin/src/test/coinstatsindex_tests.cpp
Martin Zumsande 8b959f4c6a test: move unclean_shutdown test to baseindex_tests
It tests generic functionality, nothing specific to the
coinststatsindex.
Mostly move-only, can be reviewed with
--color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
2026-07-30 15:43:32 +02:00

79 lines
2.4 KiB
C++

// Copyright (c) 2020-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chain.h>
#include <index/coinstatsindex.h>
#include <interfaces/chain.h>
#include <kernel/coinstats.h>
#include <key.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <script/script.h>
#include <sync.h>
#include <test/util/setup_common.h>
#include <util/check.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <optional>
#include <span>
#include <vector>
BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
{
CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1_MiB, true};
BOOST_REQUIRE(coin_stats_index.Init());
const CBlockIndex* block_index;
{
LOCK(cs_main);
block_index = m_node.chainman->ActiveChain().Tip();
}
// CoinStatsIndex should not be found before it is started.
BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
// BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
// is started.
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
coin_stats_index.Sync();
// Check that CoinStatsIndex works for genesis block.
const CBlockIndex* genesis_block_index;
{
LOCK(cs_main);
genesis_block_index = m_node.chainman->ActiveChain().Genesis();
}
BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
// Check that CoinStatsIndex updates with new blocks.
BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
std::vector<CMutableTransaction> noTxns;
CreateAndProcessBlock(noTxns, script_pub_key);
// Let the CoinStatsIndex to catch up again.
BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
const CBlockIndex* new_block_index;
{
LOCK(cs_main);
new_block_index = m_node.chainman->ActiveChain().Tip();
}
BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
BOOST_CHECK(block_index != new_block_index);
// Shutdown sequence (c.f. Shutdown() in init.cpp)
coin_stats_index.Stop();
}
BOOST_AUTO_TEST_SUITE_END()