LCOV - code coverage report
Current view: top level - src/test - blockfilter_index_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 93.0 % 228 212
Test Date: 2026-04-27 06:58:15 Functions: 100.0 % 16 16
Branches: 51.0 % 798 407

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

Generated by: LCOV version 2.0-1