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: 2025-08-25 05:11:47 Functions: 100.0 % 16 16
Branches: 51.0 % 800 408

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

Generated by: LCOV version 2.0-1