LCOV - code coverage report
Current view: top level - src/test - blockfilter_index_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 92.5 % 228 211
Test Date: 2026-06-05 07:17:20 Functions: 100.0 % 16 16
Branches: 51.0 % 800 408

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-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 <consensus/merkle.h>
       9                 :             : #include <consensus/validation.h>
      10                 :             : #include <index/base.h>
      11                 :             : #include <index/blockfilterindex.h>
      12                 :             : #include <interfaces/chain.h>
      13                 :             : #include <interfaces/mining.h>
      14                 :             : #include <key.h>
      15                 :             : #include <node/blockstorage.h>
      16                 :             : #include <pow.h>
      17                 :             : #include <primitives/block.h>
      18                 :             : #include <primitives/transaction.h>
      19                 :             : #include <script/script.h>
      20                 :             : #include <sync.h>
      21                 :             : #include <test/util/blockfilter.h>
      22                 :             : #include <test/util/common.h>
      23                 :             : #include <test/util/setup_common.h>
      24                 :             : #include <tinyformat.h>
      25                 :             : #include <uint256.h>
      26                 :             : #include <util/check.h>
      27                 :             : #include <util/fs.h>
      28                 :             : #include <util/time.h>
      29                 :             : #include <validation.h>
      30                 :             : 
      31                 :             : #include <boost/test/unit_test.hpp>
      32                 :             : 
      33                 :             : #include <compare>
      34                 :             : #include <cstddef>
      35                 :             : #include <cstdint>
      36                 :             : #include <functional>
      37                 :             : #include <future>
      38                 :             : #include <memory>
      39                 :             : #include <span>
      40                 :             : #include <string>
      41                 :             : #include <thread>
      42                 :             : #include <utility>
      43                 :             : #include <vector>
      44                 :             : 
      45                 :             : using node::BlockManager;
      46                 :             : 
      47                 :             : BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
      48                 :             : 
      49                 :           4 : struct BuildChainTestingSetup : public TestChain100Setup {
      50                 :             :     CBlock CreateBlock(const CBlockIndex* prev, const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey);
      51                 :             :     bool BuildChain(const CBlockIndex* pindex, const CScript& coinbase_script_pub_key, size_t length, std::vector<std::shared_ptr<CBlock>>& chain);
      52                 :             : };
      53                 :             : 
      54                 :         114 : static bool CheckFilterLookups(BlockFilterIndex& filter_index, const CBlockIndex* block_index,
      55                 :             :                                uint256& last_header, const BlockManager& blockman)
      56                 :             : {
      57                 :         114 :     BlockFilter expected_filter;
      58   [ +  -  -  + ]:         114 :     if (!ComputeFilter(filter_index.GetFilterType(), *block_index, expected_filter, blockman)) {
      59   [ #  #  #  # ]:           0 :         BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight);
      60                 :           0 :         return false;
      61                 :             :     }
      62                 :             : 
      63         [ +  - ]:         114 :     BlockFilter filter;
      64                 :         114 :     uint256 filter_header;
      65                 :         114 :     std::vector<BlockFilter> filters;
      66                 :         114 :     std::vector<uint256> filter_hashes;
      67                 :             : 
      68   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
      69   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
      70   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
      71   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
      72                 :             :                                                    filter_hashes));
      73                 :             : 
      74   [ +  -  -  +  :         114 :     BOOST_CHECK_EQUAL(filters.size(), 1U);
                   +  - ]
      75   [ +  -  -  +  :         114 :     BOOST_CHECK_EQUAL(filter_hashes.size(), 1U);
                   +  - ]
      76                 :             : 
      77   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter.GetHash(), expected_filter.GetHash());
             +  -  +  - ]
      78   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter_header, expected_filter.ComputeHeader(last_header));
                   +  - ]
      79   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filters[0].GetHash(), expected_filter.GetHash());
             +  -  +  - ]
      80   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash());
                   +  - ]
      81                 :             : 
      82                 :         114 :     filters.clear();
      83         [ +  - ]:         114 :     filter_hashes.clear();
      84                 :         114 :     last_header = filter_header;
      85                 :         114 :     return true;
      86                 :         228 : }
      87                 :             : 
      88                 :          23 : CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
      89                 :             :     const std::vector<CMutableTransaction>& txns,
      90                 :             :     const CScript& scriptPubKey)
      91                 :             : {
      92                 :          23 :     auto mining{interfaces::MakeMining(m_node)};
      93                 :          23 :     auto block_template{mining->createNewBlock({
      94                 :             :         .coinbase_output_script = scriptPubKey,
      95                 :           0 :     }, /*cooldown=*/false)};
      96   [ +  -  +  -  :          46 :     BOOST_REQUIRE(block_template);
                   +  - ]
      97         [ +  - ]:          23 :     CBlock block{block_template->getBlock()};
      98                 :          23 :     block.hashPrevBlock = prev->GetBlockHash();
      99                 :          23 :     block.nTime = prev->nTime + 1;
     100                 :             : 
     101                 :             :     // Replace mempool-selected txns with just coinbase plus passed-in txns:
     102         [ +  - ]:          23 :     block.vtx.resize(1);
     103         [ -  + ]:          23 :     for (const CMutableTransaction& tx : txns) {
     104   [ #  #  #  #  :           0 :         block.vtx.push_back(MakeTransactionRef(tx));
                   #  # ]
     105                 :             :     }
     106                 :          23 :     {
     107   [ +  -  +  - ]:          23 :         CMutableTransaction tx_coinbase{*block.vtx.at(0)};
     108                 :          23 :         tx_coinbase.nLockTime = static_cast<uint32_t>(prev->nHeight);
     109   [ +  -  +  - ]:          23 :         tx_coinbase.vin.at(0).scriptSig = CScript{} << prev->nHeight + 1;
     110   [ +  -  +  -  :          46 :         block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
                   -  + ]
     111         [ +  - ]:          23 :         block.hashMerkleRoot = BlockMerkleRoot(block);
     112                 :           0 :     }
     113                 :             : 
     114   [ +  -  +  -  :          49 :     while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
                   +  + ]
     115                 :             : 
     116                 :          23 :     return block;
     117         [ +  - ]:          46 : }
     118                 :             : 
     119                 :           3 : bool BuildChainTestingSetup::BuildChain(const CBlockIndex* pindex,
     120                 :             :     const CScript& coinbase_script_pub_key,
     121                 :             :     size_t length,
     122                 :             :     std::vector<std::shared_ptr<CBlock>>& chain)
     123                 :             : {
     124                 :           3 :     std::vector<CMutableTransaction> no_txns;
     125                 :             : 
     126         [ +  - ]:           3 :     chain.resize(length);
     127         [ +  + ]:          26 :     for (auto& block : chain) {
     128   [ +  -  -  + ]:          46 :         block = std::make_shared<CBlock>(CreateBlock(pindex, no_txns, coinbase_script_pub_key));
     129                 :             : 
     130         [ -  + ]:          23 :         BlockValidationState state;
     131   [ -  +  +  -  :          23 :         if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{*block}}, true, state, &pindex)) {
                   -  + ]
     132                 :           0 :             return false;
     133                 :             :         }
     134                 :          23 :     }
     135                 :             : 
     136                 :             :     return true;
     137                 :           3 : }
     138                 :             : 
     139   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     140                 :             : {
     141         [ +  - ]:           1 :     BlockFilterIndex filter_index(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1_MiB, true);
     142   [ +  -  +  -  :           2 :     BOOST_REQUIRE(filter_index.Init());
             +  -  +  - ]
     143                 :             : 
     144                 :           1 :     uint256 last_header;
     145                 :             : 
     146                 :             :     // Filter should not be found in the index before it is started.
     147                 :           1 :     {
     148         [ +  - ]:           1 :         LOCK(cs_main);
     149                 :             : 
     150         [ +  - ]:           1 :         BlockFilter filter;
     151                 :           1 :         uint256 filter_header;
     152                 :           1 :         std::vector<BlockFilter> filters;
     153                 :           1 :         std::vector<uint256> filter_hashes;
     154                 :             : 
     155   [ +  -  -  + ]:           1 :         for (const CBlockIndex* block_index = m_node.chainman->ActiveChain().Genesis();
     156         [ +  + ]:         102 :              block_index != nullptr;
     157         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
     158   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
     159   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
     160   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
     161   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
     162                 :             :                                                             filter_hashes));
     163                 :             :         }
     164         [ +  - ]:           1 :     }
     165                 :             : 
     166                 :             :     // BlockUntilSyncedToCurrentChain should return false before index is started.
     167   [ +  -  +  -  :           2 :     BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     168                 :             : 
     169         [ +  - ]:           1 :     filter_index.Sync();
     170                 :             : 
     171                 :             :     // Check that filter index has all blocks that were in the chain before it started.
     172                 :           1 :     {
     173         [ +  - ]:           1 :         LOCK(cs_main);
     174                 :           1 :         const CBlockIndex* block_index;
     175   [ +  -  -  + ]:           1 :         for (block_index = m_node.chainman->ActiveChain().Genesis();
     176         [ +  + ]:         102 :              block_index != nullptr;
     177         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
     178         [ +  - ]:         101 :             CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
     179                 :             :         }
     180                 :           0 :     }
     181                 :             : 
     182                 :             :     // Create two forks.
     183                 :           1 :     const CBlockIndex* tip;
     184                 :           1 :     {
     185         [ +  - ]:           1 :         LOCK(cs_main);
     186   [ +  -  -  +  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     187                 :           0 :     }
     188                 :           1 :     CKey coinbase_key_A = GenerateRandomKey();
     189                 :           1 :     CKey coinbase_key_B = GenerateRandomKey();
     190   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
                   +  - ]
     191   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
                   +  - ]
     192                 :           1 :     std::vector<std::shared_ptr<CBlock>> chainA, chainB;
     193   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
             +  -  +  - ]
     194   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
                   +  - ]
     195                 :             : 
     196                 :             :     // Check that new blocks on chain A get indexed.
     197                 :           1 :     uint256 chainA_last_header = last_header;
     198         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     199         [ +  - ]:           2 :         const auto& block = chainA[i];
     200   [ +  -  -  +  :           8 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     201                 :             :     }
     202         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     203         [ +  - ]:           2 :         const auto& block = chainA[i];
     204                 :           2 :         const CBlockIndex* block_index;
     205                 :           2 :         {
     206         [ +  - ]:           2 :             LOCK(cs_main);
     207   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     208                 :           0 :         }
     209                 :             : 
     210   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     211         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     212                 :             :     }
     213                 :             : 
     214                 :             :     // Reorg to chain B.
     215                 :           1 :     uint256 chainB_last_header = last_header;
     216         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     217         [ +  - ]:           3 :         const auto& block = chainB[i];
     218   [ +  -  -  +  :          12 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     219                 :             :     }
     220         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     221         [ +  - ]:           3 :         const auto& block = chainB[i];
     222                 :           3 :         const CBlockIndex* block_index;
     223                 :           3 :         {
     224         [ +  - ]:           3 :             LOCK(cs_main);
     225   [ +  -  +  -  :           3 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     226                 :           0 :         }
     227                 :             : 
     228   [ +  -  +  -  :           6 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     229         [ +  - ]:           3 :         CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     230                 :             :     }
     231                 :             : 
     232                 :             :     // Check that filters for stale blocks on A can be retrieved.
     233                 :           1 :     chainA_last_header = last_header;
     234         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     235         [ +  - ]:           2 :         const auto& block = chainA[i];
     236                 :           2 :         const CBlockIndex* block_index;
     237                 :           2 :         {
     238         [ +  - ]:           2 :             LOCK(cs_main);
     239   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     240                 :           0 :         }
     241                 :             : 
     242   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     243         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     244                 :             :     }
     245                 :             : 
     246                 :             :     // Reorg back to chain A.
     247         [ +  + ]:           3 :      for (size_t i = 2; i < 4; i++) {
     248         [ +  - ]:           2 :          const auto& block = chainA[i];
     249   [ +  -  -  +  :           8 :          BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     250                 :             :      }
     251                 :             : 
     252                 :             :      // Check that chain A and B blocks can be retrieved.
     253                 :           1 :      chainA_last_header = last_header;
     254                 :           1 :      chainB_last_header = last_header;
     255         [ +  + ]:           4 :      for (size_t i = 0; i < 3; i++) {
     256                 :           3 :          const CBlockIndex* block_index;
     257                 :             : 
     258                 :           3 :          {
     259         [ +  - ]:           3 :              LOCK(cs_main);
     260   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
                   +  - ]
     261                 :           0 :          }
     262   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     263         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     264                 :             : 
     265                 :           3 :          {
     266         [ +  - ]:           3 :              LOCK(cs_main);
     267   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
                   +  - ]
     268                 :           0 :          }
     269   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     270         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     271                 :             :      }
     272                 :             : 
     273                 :             :     // Test lookups for a range of filters/hashes.
     274                 :           1 :     std::vector<BlockFilter> filters;
     275                 :           1 :     std::vector<uint256> filter_hashes;
     276                 :             : 
     277                 :           1 :     {
     278         [ +  - ]:           1 :         LOCK(cs_main);
     279   [ +  -  -  +  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     280                 :           0 :     }
     281   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterRange(0, tip, filters));
             +  -  +  - ]
     282   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterHashRange(0, tip, filter_hashes));
             +  -  -  + ]
     283                 :             : 
     284         [ -  + ]:           1 :     assert(tip->nHeight >= 0);
     285   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(filters.size(), tip->nHeight + 1U);
                   +  - ]
     286   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(filter_hashes.size(), tip->nHeight + 1U);
                   +  - ]
     287                 :             : 
     288                 :           1 :     filters.clear();
     289         [ +  - ]:           1 :     filter_hashes.clear();
     290                 :             : 
     291         [ +  - ]:           1 :     filter_index.Interrupt();
     292         [ +  - ]:           1 :     filter_index.Stop();
     293                 :           1 : }
     294                 :             : 
     295   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     296                 :             : {
     297                 :           1 :     BlockFilterIndex* filter_index;
     298                 :             : 
     299                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     300         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     301                 :             : 
     302   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, true, false));
     303                 :             : 
     304                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     305         [ +  - ]:           2 :     BOOST_CHECK(filter_index != nullptr);
     306         [ +  - ]:           2 :     BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
     307                 :             : 
     308                 :             :     // Initialize returns false if index already exists.
     309   [ +  -  +  -  :           3 :     BOOST_CHECK(!InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, true, false));
                   +  - ]
     310                 :             : 
     311                 :           1 :     int iter_count = 0;
     312         [ +  - ]:           2 :     ForEachBlockFilterIndex([&iter_count](BlockFilterIndex& _index) { iter_count++; });
     313         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(iter_count, 1);
     314                 :             : 
     315   [ +  -  +  - ]:           2 :     BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));
     316                 :             : 
     317                 :             :     // Destroy returns false because index was already destroyed.
     318   [ +  -  +  - ]:           2 :     BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));
     319                 :             : 
     320                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     321         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     322                 :             : 
     323                 :             :     // Reinitialize index.
     324   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1_MiB, true, false));
     325                 :             : 
     326                 :           1 :     DestroyAllBlockFilterIndexes();
     327                 :             : 
     328                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     329         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     330                 :           1 : }
     331                 :             : 
     332                 :             : class IndexReorgCrash : public BaseIndex
     333                 :             : {
     334                 :             : private:
     335                 :             :     std::unique_ptr<BaseIndex::DB> m_db;
     336                 :             :     std::shared_future<void> m_blocker;
     337                 :             :     int m_blocking_height;
     338                 :             : 
     339                 :             : public:
     340                 :           1 :     explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker,
     341                 :           0 :                              int blocking_height) : BaseIndex(std::move(chain), "test index"), m_blocker(blocker),
     342   [ +  -  +  - ]:           1 :                                                     m_blocking_height(blocking_height)
     343                 :             :     {
     344         [ +  - ]:           2 :         const fs::path path = gArgs.GetDataDirNet() / "index";
     345         [ +  - ]:           1 :         fs::create_directories(path);
     346   [ +  -  +  -  :           4 :         m_db = std::make_unique<BaseIndex::DB>(path / "db", /*n_cache_size=*/0, /*f_memory=*/true, /*f_wipe=*/false);
                   +  - ]
     347         [ -  - ]:           1 :     }
     348                 :             : 
     349                 :           6 :     bool AllowPrune() const override { return false; }
     350                 :          13 :     BaseIndex::DB& GetDB() const override { return *m_db; }
     351                 :             : 
     352                 :         104 :     bool CustomAppend(const interfaces::BlockInfo& block) override
     353                 :             :     {
     354                 :             :         // Simulate a delay so new blocks can get connected during the initial sync
     355         [ +  + ]:         104 :         if (block.height == m_blocking_height) m_blocker.wait();
     356                 :             : 
     357                 :             :         // Move mock time forward so the best index gets updated only when we are not at the blocking height
     358   [ +  +  +  + ]:         104 :         if (block.height == m_blocking_height - 1 || block.height > m_blocking_height) {
     359                 :           3 :             SetMockTime(GetTime<std::chrono::seconds>() + 31s);
     360                 :             :         }
     361                 :             : 
     362                 :         104 :         return true;
     363                 :             :     }
     364                 :             : };
     365                 :             : 
     366   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(index_reorg_crash, BuildChainTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     367                 :             : {
     368                 :             :     // Enable mock time
     369                 :           1 :     SetMockTime(GetTime<std::chrono::minutes>());
     370                 :             : 
     371                 :           1 :     std::promise<void> promise;
     372   [ +  -  +  - ]:           2 :     std::shared_future<void> blocker(promise.get_future());
     373   [ +  -  -  +  :           4 :     int blocking_height = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->nHeight);
                   +  - ]
     374                 :             : 
     375   [ +  -  +  -  :           2 :     IndexReorgCrash index(interfaces::MakeChain(m_node), blocker, blocking_height);
             +  -  +  - ]
     376   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.Init());
             +  -  +  - ]
     377   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.StartBackgroundSync());
             +  -  +  - ]
     378                 :             : 
     379                 :           3 :     auto func_wait_until = [&](int height, std::chrono::milliseconds timeout) {
     380                 :           2 :         auto deadline = std::chrono::steady_clock::now() + timeout;
     381         [ +  + ]:           6 :         while (index.GetSummary().best_block_height < height) {
     382         [ -  + ]:           2 :             if (std::chrono::steady_clock::now() > deadline) {
     383   [ #  #  #  # ]:           0 :                 BOOST_FAIL(strprintf("Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
     384                 :           0 :                 return;
     385                 :             :             }
     386                 :           2 :             std::this_thread::sleep_for(100ms);
     387                 :             :         }
     388                 :           1 :     };
     389                 :             : 
     390                 :             :     // Wait until the index is one block before the fork point
     391         [ +  - ]:           1 :     func_wait_until(blocking_height - 1, /*timeout=*/5s);
     392                 :             : 
     393                 :             :     // Create a fork to trigger the reorg
     394                 :           1 :     std::vector<std::shared_ptr<CBlock>> fork;
     395   [ +  -  -  +  :           4 :     const CBlockIndex* prev_tip = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->pprev);
                   +  - ]
     396   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(prev_tip, GetScriptForDestination(PKHash(GenerateRandomKey().GetPubKey())), 3, fork));
          +  -  +  -  +  
                -  +  - ]
     397                 :             : 
     398         [ +  + ]:           4 :     for (const auto& block : fork) {
     399   [ +  -  +  -  :          12 :         BOOST_REQUIRE(m_node.chainman->ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
          +  -  +  -  +  
                      - ]
     400                 :             :     }
     401                 :             : 
     402                 :             :     // Unblock the index thread so it can process the reorg
     403         [ +  - ]:           1 :     promise.set_value();
     404                 :             :     // Wait for the index to reach the new tip
     405         [ +  - ]:           1 :     func_wait_until(blocking_height + 2, 5s);
     406         [ +  - ]:           1 :     index.Stop();
     407         [ +  - ]:           2 : }
     408                 :             : 
     409                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1