LCOV - code coverage report
Current view: top level - src/test - baseindex_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.1 % 107 105
Test Date: 2026-08-25 06:16:57 Functions: 100.0 % 20 20
Branches: 51.0 % 492 251

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <addresstype.h>
       6                 :             : #include <blockfilter.h>
       7                 :             : #include <chain.h>
       8                 :             : #include <chainparams.h>
       9                 :             : #include <coins.h>
      10                 :             : #include <common/args.h>
      11                 :             : #include <consensus/validation.h>
      12                 :             : #include <index/base.h>
      13                 :             : #include <index/blockfilterindex.h>
      14                 :             : #include <index/coinstatsindex.h>
      15                 :             : #include <index/txindex.h>
      16                 :             : #include <index/txospenderindex.h>
      17                 :             : #include <interfaces/chain.h>
      18                 :             : #include <kernel/types.h>
      19                 :             : #include <key.h>
      20                 :             : #include <node/context.h>
      21                 :             : #include <primitives/block.h>
      22                 :             : #include <script/script.h>
      23                 :             : #include <sync.h>
      24                 :             : #include <test/util/mining.h>
      25                 :             : #include <test/util/setup_common.h>
      26                 :             : #include <test/util/time.h>
      27                 :             : #include <test/util/validation.h>
      28                 :             : #include <tinyformat.h>
      29                 :             : #include <util/byte_units.h>
      30                 :             : #include <util/check.h>
      31                 :             : #include <util/fs.h>
      32                 :             : #include <validation.h>
      33                 :             : 
      34                 :             : #include <boost/test/unit_test.hpp>
      35                 :             : 
      36                 :             : #include <chrono>
      37                 :             : #include <functional>
      38                 :             : #include <future>
      39                 :             : #include <memory>
      40                 :             : #include <string>
      41                 :             : #include <thread>
      42                 :             : #include <utility>
      43                 :             : #include <vector>
      44                 :             : 
      45                 :             : using kernel::ChainstateRole;
      46                 :             : 
      47                 :             : using IndexFactory = std::function<std::unique_ptr<BaseIndex>(node::NodeContext&)>;
      48                 :             : 
      49                 :             : static const std::vector<std::pair<std::string, IndexFactory>> INDEX_FACTORIES{
      50                 :           5 :     {"coinstatsindex", [](node::NodeContext& node) -> std::unique_ptr<BaseIndex> {
      51   [ +  -  -  + ]:           5 :         return std::make_unique<CoinStatsIndex>(interfaces::MakeChain(node), /*n_cache_size=*/1_MiB); }},
      52                 :           5 :     {"txindex", [](node::NodeContext& node) -> std::unique_ptr<BaseIndex> {
      53   [ +  -  -  + ]:           5 :         return std::make_unique<TxIndex>(interfaces::MakeChain(node), /*n_cache_size=*/1_MiB); }},
      54                 :           5 :     {"txospenderindex", [](node::NodeContext& node) -> std::unique_ptr<BaseIndex> {
      55   [ +  -  -  + ]:           5 :         return std::make_unique<TxoSpenderIndex>(interfaces::MakeChain(node), /*n_cache_size=*/1_MiB); }},
      56                 :           5 :     {"blockfilterindex", [](node::NodeContext& node) -> std::unique_ptr<BaseIndex> {
      57   [ +  -  -  + ]:           5 :         return std::make_unique<BlockFilterIndex>(interfaces::MakeChain(node), BlockFilterType::BASIC, /*n_cache_size=*/1_MiB); }},
      58                 :             : };
      59                 :             : 
      60                 :             : // Tests of generic BaseIndex functionality that is independent of which
      61                 :             : // concrete index is being used.
      62                 :             : BOOST_AUTO_TEST_SUITE(baseindex_tests)
      63                 :             : 
      64                 :             : // Test that the index does not commit ahead of the chainstate's last
      65                 :             : // flushed block. If it did, a subsequent unclean shutdown would corrupt
      66                 :             : // the index, because during reverting it would require blocks that were
      67                 :             : // never flushed to disk.
      68   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(baseindex_no_commit_ahead_of_flush, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      69                 :             : {
      70         [ -  + ]:           1 :     Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
      71   [ +  -  +  + ]:           5 :     for (const auto& [index_name, make_index] : INDEX_FACTORIES) {
      72         [ +  - ]:           4 :         BOOST_TEST_INFO_SCOPE(index_name);
      73   [ +  -  -  +  :          16 :         const int tip_height{WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->nHeight)};
                   +  - ]
      74                 :          16 :         auto sync_index = [&](bool do_flush, int expected_sync_height, int expected_commit_height) {
      75                 :          12 :             auto index{make_index(m_node)};
      76   [ +  -  +  -  :          24 :             BOOST_REQUIRE(index->Init());
             +  -  +  - ]
      77         [ +  - ]:          12 :             index->Sync();
      78         [ +  + ]:          12 :             if (do_flush) {
      79         [ +  - ]:           4 :                 chainstate.ForceFlushStateToDisk();
      80   [ +  -  +  - ]:           4 :                 m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
      81                 :             :             }
      82   [ +  -  +  -  :          12 :             BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, expected_sync_height);
                   +  - ]
      83         [ +  - ]:          12 :             index->Stop();
      84                 :             :             // Reload index to see which block data was actually committed.
      85   [ +  -  +  -  :          24 :             BOOST_REQUIRE(index->Init());
             +  -  +  - ]
      86   [ +  -  +  -  :          12 :             BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, expected_commit_height);
                   +  - ]
      87         [ +  - ]:          12 :             index->Stop();
      88                 :          16 :         };
      89                 :             : 
      90                 :             :         // Part 1: Sync, then "crash" (stop without flushing). Models a node that
      91                 :             :         // started up, had its index catch up, but never flushed before going down.
      92                 :             :         // The end-of-sync Commit() runs at the chain tip but m_last_flushed_block
      93                 :             :         // is null, so it is skipped.
      94         [ +  - ]:           4 :         sync_index(false, tip_height, 0);
      95                 :             : 
      96                 :             :         // Part 2: Restart cleanly. Sync, force a chainstate flush, and drain the
      97                 :             :         // validation queue so the index's ChainStateFlushed callback runs.
      98                 :             :         // Now m_last_flushed_block == tip and the index can commit.
      99         [ +  - ]:           4 :         sync_index(true, tip_height, tip_height);
     100                 :             : 
     101                 :             :         // Part 3: Connect a new block on the chain without flushing
     102                 :             :         // (m_last_flushed_block stays at tip_height). For a real node this would
     103                 :             :         // happen in parallel with Sync(). Here we do it before Sync() to make the
     104                 :             :         // race state deterministic.
     105   [ +  -  +  - ]:           8 :         CreateAndProcessBlock({}, CScript() << OP_TRUE);
     106         [ +  - ]:           4 :         sync_index(false, tip_height + 1, tip_height);
     107                 :           4 :     }
     108                 :           1 : }
     109                 :             : 
     110                 :             : // Test shutdown between BlockConnected and ChainStateFlushed notifications,
     111                 :             : // make sure index is not corrupted and reloads at the last committed height.
     112   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(index_unclean_shutdown, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     113                 :             : {
     114         [ -  + ]:           1 :     Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
     115                 :           1 :     const CChainParams& params = Params();
     116   [ -  +  +  - ]:           2 :     const int tip_height{WITH_LOCK(cs_main, return chainstate.m_chain.Height())};
     117                 :           1 :     chainstate.ForceFlushStateToDisk();
     118                 :             :     // Drain the notification before registering any index.
     119                 :           1 :     m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
     120   [ +  -  +  + ]:           5 :     for (const auto& [index_name, make_index] : INDEX_FACTORIES) {
     121         [ +  - ]:           4 :         BOOST_TEST_INFO_SCOPE(index_name);
     122                 :           4 :         {
     123         [ +  - ]:           4 :             auto index{make_index(m_node)};
     124   [ +  -  +  -  :           8 :             BOOST_REQUIRE(index->Init());
             +  -  +  - ]
     125         [ +  - ]:           4 :             index->Sync();
     126                 :           4 :             std::shared_ptr<const CBlock> new_block;
     127                 :           4 :             CBlockIndex* new_block_index = nullptr;
     128                 :           4 :             {
     129   [ +  -  +  -  :           8 :                 const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
                   +  - ]
     130         [ +  - ]:           4 :                 const CBlock block = this->CreateBlock({}, script_pub_key);
     131                 :             : 
     132   [ +  -  -  + ]:           4 :                 new_block = std::make_shared<CBlock>(block);
     133                 :             : 
     134         [ +  - ]:           4 :                 LOCK(cs_main);
     135         [ +  - ]:           4 :                 BlockValidationState state;
     136   [ +  -  +  -  :           8 :                 BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
             +  -  +  - ]
     137   [ +  -  +  -  :           8 :                 BOOST_CHECK(m_node.chainman->AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr, true));
             +  -  +  - ]
     138   [ +  -  +  - ]:           4 :                 CCoinsViewCache view(&chainstate.CoinsTip());
     139   [ +  -  +  -  :           8 :                 BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
                   +  - ]
     140         [ +  - ]:          12 :             }
     141                 :             :             // Send block connected notification, then stop the index without
     142                 :             :             // sending a chainstate flushed notification. Prior to #24138, this
     143                 :             :             // would cause the index to be corrupted and fail to reload.
     144         [ +  - ]:           4 :             ValidationInterfaceTest::BlockConnected(ChainstateRole{}, *index, new_block, new_block_index);
     145         [ +  - ]:           4 :             index->Stop();
     146                 :           4 :         }
     147                 :             : 
     148                 :           4 :         {
     149         [ +  - ]:           4 :             auto index{make_index(m_node)};
     150   [ +  -  +  -  :           8 :             BOOST_REQUIRE(index->Init());
             +  -  +  - ]
     151                 :             :             // Make sure the index reloads from the pre-crash commit.
     152   [ +  -  +  -  :           4 :             BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, tip_height);
                   +  - ]
     153   [ +  -  +  -  :           8 :             BOOST_REQUIRE(index->StartBackgroundSync());
             +  -  +  - ]
     154         [ +  - ]:           4 :             index->Stop();
     155                 :           4 :         }
     156                 :           4 :     }
     157                 :           1 : }
     158                 :             : 
     159                 :             : class IndexReorgCrash : public BaseIndex
     160                 :             : {
     161                 :             : private:
     162                 :             :     FakeNodeClock& m_clock;
     163                 :             :     std::unique_ptr<BaseIndex::DB> m_db;
     164                 :             :     std::shared_future<void> m_blocker;
     165                 :             :     int m_blocking_height;
     166                 :             : 
     167                 :             : public:
     168                 :           1 :     explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker, int blocking_height, FakeNodeClock& clock)
     169   [ +  -  +  -  :           1 :         : BaseIndex(std::move(chain), "test index", "testidx"), m_clock(clock), m_blocker(blocker), m_blocking_height(blocking_height)
                   +  - ]
     170                 :             :     {
     171         [ +  - ]:           2 :         const fs::path path = gArgs.GetDataDirNet() / "index";
     172         [ +  - ]:           1 :         fs::create_directories(path);
     173   [ +  -  +  -  :           4 :         m_db = std::make_unique<BaseIndex::DB>(path / "db", /*n_cache_size=*/0, /*f_memory=*/true, /*f_wipe=*/false);
                   +  - ]
     174         [ -  - ]:           1 :     }
     175                 :             : 
     176                 :           6 :     bool AllowPrune() const override { return false; }
     177                 :           1 :     BaseIndex::DB& GetDB() const override { return *m_db; }
     178                 :             : 
     179                 :         104 :     bool CustomAppend(const interfaces::BlockInfo& block) override
     180                 :             :     {
     181                 :             :         // Simulate a delay so new blocks can get connected during the initial sync
     182         [ +  + ]:         104 :         if (block.height == m_blocking_height) m_blocker.wait();
     183                 :             : 
     184                 :             :         // Move mock time forward so the best index gets updated only when we are not at the blocking height
     185   [ +  +  +  + ]:         104 :         if (block.height == m_blocking_height - 1 || block.height > m_blocking_height) {
     186                 :           3 :             m_clock += 31s;
     187                 :             :         }
     188                 :             : 
     189                 :         104 :         return true;
     190                 :             :     }
     191                 :             : };
     192                 :             : 
     193   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(index_reorg_crash, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     194                 :             : {
     195                 :           1 :     std::promise<void> promise;
     196   [ +  -  +  - ]:           2 :     std::shared_future<void> blocker(promise.get_future());
     197   [ +  -  -  +  :           4 :     int blocking_height = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->nHeight);
                   +  - ]
     198                 :             : 
     199   [ +  -  +  - ]:           3 :     IndexReorgCrash index{interfaces::MakeChain(m_node), blocker, blocking_height, m_clock};
     200   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.Init());
             +  -  +  - ]
     201   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.StartBackgroundSync());
             +  -  +  - ]
     202                 :             : 
     203                 :           3 :     auto func_wait_until = [&](int height, std::chrono::milliseconds timeout) {
     204                 :           2 :         auto deadline = std::chrono::steady_clock::now() + timeout;
     205         [ +  + ]:           6 :         while (index.GetSummary().best_block_height < height) {
     206         [ -  + ]:           2 :             if (std::chrono::steady_clock::now() > deadline) {
     207   [ #  #  #  # ]:           0 :                 BOOST_FAIL(strprintf("Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
     208                 :           0 :                 return;
     209                 :             :             }
     210                 :           2 :             std::this_thread::sleep_for(100ms);
     211                 :             :         }
     212                 :           1 :     };
     213                 :             : 
     214                 :             :     // Wait until the index is one block before the fork point
     215         [ +  - ]:           1 :     func_wait_until(blocking_height - 1, /*timeout=*/5s);
     216                 :             : 
     217                 :             :     // Create a fork to trigger the reorg
     218                 :           1 :     std::vector<std::shared_ptr<CBlock>> fork;
     219   [ +  -  -  +  :           4 :     const CBlockIndex* prev_tip = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->pprev);
                   +  - ]
     220   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(m_node, prev_tip, GetScriptForDestination(PKHash(GenerateRandomKey().GetPubKey())), 3, fork));
          +  -  +  -  +  
                -  +  - ]
     221                 :             : 
     222         [ +  + ]:           4 :     for (const auto& block : fork) {
     223   [ +  -  +  -  :          12 :         BOOST_REQUIRE(m_node.chainman->ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
          +  -  +  -  +  
                      - ]
     224                 :             :     }
     225                 :             : 
     226                 :             :     // Unblock the index thread so it can process the reorg
     227         [ +  - ]:           1 :     promise.set_value();
     228                 :             :     // Wait for the index to reach the new tip
     229         [ +  - ]:           1 :     func_wait_until(blocking_height + 2, 5s);
     230         [ +  - ]:           1 :     index.Stop();
     231         [ +  - ]:           2 : }
     232                 :             : 
     233                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1