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 % 108 106
Test Date: 2026-10-11 07:59:32 Functions: 100.0 % 20 20
Branches: 51.0 % 500 255

             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                 :             : 
      88         [ +  - ]:          12 :             StopIndex(*index, m_node);
      89                 :          16 :         };
      90                 :             : 
      91                 :             :         // Part 1: Sync, then "crash" (stop without flushing). Models a node that
      92                 :             :         // started up, had its index catch up, but never flushed before going down.
      93                 :             :         // The end-of-sync Commit() runs at the chain tip but m_last_flushed_block
      94                 :             :         // is null, so it is skipped.
      95         [ +  - ]:           4 :         sync_index(false, tip_height, 0);
      96                 :             : 
      97                 :             :         // Part 2: Restart cleanly. Sync, force a chainstate flush, and drain the
      98                 :             :         // validation queue so the index's ChainStateFlushed callback runs.
      99                 :             :         // Now m_last_flushed_block == tip and the index can commit.
     100         [ +  - ]:           4 :         sync_index(true, tip_height, tip_height);
     101                 :             : 
     102                 :             :         // Part 3: Connect a new block on the chain without flushing
     103                 :             :         // (m_last_flushed_block stays at tip_height). For a real node this would
     104                 :             :         // happen in parallel with Sync(). Here we do it before Sync() to make the
     105                 :             :         // race state deterministic.
     106   [ +  -  +  - ]:           8 :         CreateAndProcessBlock({}, CScript() << OP_TRUE);
     107         [ +  - ]:           4 :         sync_index(false, tip_height + 1, tip_height);
     108                 :           4 :     }
     109                 :           1 : }
     110                 :             : 
     111                 :             : // Test shutdown between BlockConnected and ChainStateFlushed notifications,
     112                 :             : // make sure index is not corrupted and reloads at the last committed height.
     113   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(index_unclean_shutdown, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     114                 :             : {
     115         [ -  + ]:           1 :     Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
     116                 :           1 :     const CChainParams& params = Params();
     117   [ -  +  +  - ]:           2 :     const int tip_height{WITH_LOCK(cs_main, return chainstate.m_chain.Height())};
     118                 :           1 :     chainstate.ForceFlushStateToDisk();
     119                 :             :     // Drain the notification before registering any index.
     120                 :           1 :     m_node.chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
     121   [ +  -  +  + ]:           5 :     for (const auto& [index_name, make_index] : INDEX_FACTORIES) {
     122         [ +  - ]:           4 :         BOOST_TEST_INFO_SCOPE(index_name);
     123                 :           4 :         {
     124         [ +  - ]:           4 :             auto index{make_index(m_node)};
     125   [ +  -  +  -  :           8 :             BOOST_REQUIRE(index->Init());
             +  -  +  - ]
     126         [ +  - ]:           4 :             index->Sync();
     127                 :           4 :             std::shared_ptr<const CBlock> new_block;
     128                 :           4 :             CBlockIndex* new_block_index = nullptr;
     129                 :           4 :             {
     130   [ +  -  +  -  :           8 :                 const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
                   +  - ]
     131         [ +  - ]:           4 :                 const CBlock block = this->CreateBlock({}, script_pub_key);
     132                 :             : 
     133   [ +  -  -  + ]:           4 :                 new_block = std::make_shared<CBlock>(block);
     134                 :             : 
     135         [ +  - ]:           4 :                 LOCK(cs_main);
     136         [ +  - ]:           4 :                 BlockValidationState state;
     137   [ +  -  +  -  :           8 :                 BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
             +  -  +  - ]
     138   [ +  -  +  -  :           8 :                 BOOST_CHECK(m_node.chainman->AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr, true));
             +  -  +  - ]
     139   [ +  -  +  - ]:           4 :                 CCoinsViewCache view(&chainstate.CoinsTip());
     140   [ +  -  +  -  :           8 :                 BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
                   +  - ]
     141         [ +  - ]:          12 :             }
     142                 :             :             // Send block connected notification, then stop the index without
     143                 :             :             // sending a chainstate flushed notification. Prior to #24138, this
     144                 :             :             // would cause the index to be corrupted and fail to reload.
     145         [ +  - ]:           4 :             ValidationInterfaceTest::BlockConnected(ChainstateRole{}, *index, new_block, new_block_index);
     146         [ +  - ]:           4 :             index->Stop();
     147                 :           4 :         }
     148                 :             : 
     149                 :           4 :         {
     150         [ +  - ]:           4 :             auto index{make_index(m_node)};
     151   [ +  -  +  -  :           8 :             BOOST_REQUIRE(index->Init());
             +  -  +  - ]
     152                 :             :             // Make sure the index reloads from the pre-crash commit.
     153   [ +  -  +  -  :           4 :             BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, tip_height);
                   +  - ]
     154   [ +  -  +  -  :           8 :             BOOST_REQUIRE(index->StartBackgroundSync());
             +  -  +  - ]
     155         [ +  - ]:           4 :             index->Stop();
     156                 :           4 :         }
     157                 :           4 :     }
     158                 :           1 : }
     159                 :             : 
     160                 :             : class IndexReorgCrash : public BaseIndex
     161                 :             : {
     162                 :             : private:
     163                 :             :     FakeNodeClock& m_clock;
     164                 :             :     std::unique_ptr<BaseIndex::DB> m_db;
     165                 :             :     std::shared_future<void> m_blocker;
     166                 :             :     int m_blocking_height;
     167                 :             : 
     168                 :             : public:
     169                 :           1 :     explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker, int blocking_height, FakeNodeClock& clock)
     170   [ +  -  +  -  :           1 :         : BaseIndex(std::move(chain), "test index", "testidx"), m_clock(clock), m_blocker(blocker), m_blocking_height(blocking_height)
                   +  - ]
     171                 :             :     {
     172         [ +  - ]:           2 :         const fs::path path = gArgs.GetDataDirNet() / "index";
     173         [ +  - ]:           1 :         fs::create_directories(path);
     174   [ +  -  +  -  :           4 :         m_db = std::make_unique<BaseIndex::DB>(path / "db", /*n_cache_size=*/0, /*f_memory=*/true, /*f_wipe=*/false);
                   +  - ]
     175         [ -  - ]:           1 :     }
     176                 :             : 
     177                 :           6 :     bool AllowPrune() const override { return false; }
     178                 :           1 :     BaseIndex::DB& GetDB() const override { return *m_db; }
     179                 :             : 
     180                 :         104 :     bool CustomAppend(const interfaces::BlockInfo& block) override
     181                 :             :     {
     182                 :             :         // Simulate a delay so new blocks can get connected during the initial sync
     183         [ +  + ]:         104 :         if (block.height == m_blocking_height) m_blocker.wait();
     184                 :             : 
     185                 :             :         // Move mock time forward so the best index gets updated only when we are not at the blocking height
     186   [ +  +  +  + ]:         104 :         if (block.height == m_blocking_height - 1 || block.height > m_blocking_height) {
     187                 :           3 :             m_clock += 31s;
     188                 :             :         }
     189                 :             : 
     190                 :         104 :         return true;
     191                 :             :     }
     192                 :             : };
     193                 :             : 
     194   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(index_reorg_crash, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     195                 :             : {
     196                 :           1 :     std::promise<void> promise;
     197   [ +  -  +  - ]:           2 :     std::shared_future<void> blocker(promise.get_future());
     198   [ +  -  -  +  :           4 :     int blocking_height = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->nHeight);
                   +  - ]
     199                 :             : 
     200   [ +  -  +  - ]:           3 :     IndexReorgCrash index{interfaces::MakeChain(m_node), blocker, blocking_height, m_clock};
     201   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.Init());
             +  -  +  - ]
     202   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.StartBackgroundSync());
             +  -  +  - ]
     203                 :             : 
     204                 :           3 :     auto func_wait_until = [&](int height, std::chrono::milliseconds timeout) {
     205                 :           2 :         auto deadline = std::chrono::steady_clock::now() + timeout;
     206         [ +  + ]:           5 :         while (index.GetSummary().best_block_height < height) {
     207         [ -  + ]:           1 :             if (std::chrono::steady_clock::now() > deadline) {
     208   [ #  #  #  # ]:           0 :                 BOOST_FAIL(strprintf("Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
     209                 :           0 :                 return;
     210                 :             :             }
     211                 :           1 :             std::this_thread::sleep_for(100ms);
     212                 :             :         }
     213                 :           1 :     };
     214                 :             : 
     215                 :             :     // Wait until the index is one block before the fork point
     216         [ +  - ]:           1 :     func_wait_until(blocking_height - 1, /*timeout=*/5s);
     217                 :             : 
     218                 :             :     // Create a fork to trigger the reorg
     219                 :           1 :     std::vector<std::shared_ptr<CBlock>> fork;
     220   [ +  -  -  +  :           4 :     const CBlockIndex* prev_tip = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->pprev);
                   +  - ]
     221   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(m_node, prev_tip, GetScriptForDestination(PKHash(GenerateRandomKey().GetPubKey())), 3, fork));
          +  -  +  -  +  
                -  +  - ]
     222                 :             : 
     223         [ +  + ]:           4 :     for (const auto& block : fork) {
     224   [ +  -  +  -  :          12 :         BOOST_REQUIRE(m_node.chainman->ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
          +  -  +  -  +  
                      - ]
     225                 :             :     }
     226                 :             : 
     227                 :             :     // The index thread is blocked and not done
     228   [ +  -  +  -  :           2 :     BOOST_CHECK(!index.GetSummary().synced);
             +  -  +  - ]
     229                 :             : 
     230                 :             :     // Unblock the index thread so it can process the reorg
     231         [ +  - ]:           1 :     promise.set_value();
     232                 :             :     // Wait for the index to reach the new tip
     233         [ +  - ]:           1 :     func_wait_until(blocking_height + 2, 5s);
     234                 :             : 
     235         [ +  - ]:           1 :     StopIndex(index, m_node);
     236         [ +  - ]:           2 : }
     237                 :             : 
     238                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1