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 % 227 211
Test Date: 2026-01-16 05:29:45 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 <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   [ +  -  +  -  :          48 :     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                 :             : 
     106         [ -  + ]:          23 :         BlockValidationState state;
     107   [ -  +  +  -  :          23 :         if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{*block}}, true, state, &pindex)) {
                   -  + ]
     108                 :           0 :             return false;
     109                 :             :         }
     110                 :          23 :     }
     111                 :             : 
     112                 :             :     return true;
     113                 :           3 : }
     114                 :             : 
     115   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     116                 :             : {
     117         [ +  - ]:           1 :     BlockFilterIndex filter_index(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1 << 20, true);
     118   [ +  -  +  -  :           2 :     BOOST_REQUIRE(filter_index.Init());
             +  -  +  - ]
     119                 :             : 
     120                 :           1 :     uint256 last_header;
     121                 :             : 
     122                 :             :     // Filter should not be found in the index before it is started.
     123                 :           1 :     {
     124         [ +  - ]:           1 :         LOCK(cs_main);
     125                 :             : 
     126         [ +  - ]:           1 :         BlockFilter filter;
     127                 :           1 :         uint256 filter_header;
     128                 :           1 :         std::vector<BlockFilter> filters;
     129                 :           1 :         std::vector<uint256> filter_hashes;
     130                 :             : 
     131   [ +  -  -  + ]:           1 :         for (const CBlockIndex* block_index = m_node.chainman->ActiveChain().Genesis();
     132         [ +  + ]:         102 :              block_index != nullptr;
     133         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(block_index)) {
     134   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
     135   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
     136   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
     137   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
     138                 :             :                                                             filter_hashes));
     139                 :             :         }
     140         [ +  - ]:           1 :     }
     141                 :             : 
     142                 :             :     // BlockUntilSyncedToCurrentChain should return false before index is started.
     143   [ +  -  +  -  :           2 :     BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     144                 :             : 
     145         [ +  - ]:           1 :     filter_index.Sync();
     146                 :             : 
     147                 :             :     // Check that filter index has all blocks that were in the chain before it started.
     148                 :           1 :     {
     149         [ +  - ]:           1 :         LOCK(cs_main);
     150                 :           1 :         const CBlockIndex* block_index;
     151   [ +  -  -  + ]:           1 :         for (block_index = m_node.chainman->ActiveChain().Genesis();
     152         [ +  + ]:         102 :              block_index != nullptr;
     153         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(block_index)) {
     154         [ +  - ]:         101 :             CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
     155                 :             :         }
     156                 :           0 :     }
     157                 :             : 
     158                 :             :     // Create two forks.
     159                 :           1 :     const CBlockIndex* tip;
     160                 :           1 :     {
     161         [ +  - ]:           1 :         LOCK(cs_main);
     162   [ +  -  -  +  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     163                 :           0 :     }
     164                 :           1 :     CKey coinbase_key_A = GenerateRandomKey();
     165                 :           1 :     CKey coinbase_key_B = GenerateRandomKey();
     166   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
                   +  - ]
     167   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
                   +  - ]
     168                 :           1 :     std::vector<std::shared_ptr<CBlock>> chainA, chainB;
     169   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
             +  -  +  - ]
     170   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
                   +  - ]
     171                 :             : 
     172                 :             :     // Check that new blocks on chain A get indexed.
     173                 :           1 :     uint256 chainA_last_header = last_header;
     174         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     175         [ +  - ]:           2 :         const auto& block = chainA[i];
     176   [ +  -  -  +  :           8 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     177                 :             :     }
     178         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     179         [ +  - ]:           2 :         const auto& block = chainA[i];
     180                 :           2 :         const CBlockIndex* block_index;
     181                 :           2 :         {
     182         [ +  - ]:           2 :             LOCK(cs_main);
     183   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     184                 :           0 :         }
     185                 :             : 
     186   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     187         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     188                 :             :     }
     189                 :             : 
     190                 :             :     // Reorg to chain B.
     191                 :           1 :     uint256 chainB_last_header = last_header;
     192         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     193         [ +  - ]:           3 :         const auto& block = chainB[i];
     194   [ +  -  -  +  :          12 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     195                 :             :     }
     196         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     197         [ +  - ]:           3 :         const auto& block = chainB[i];
     198                 :           3 :         const CBlockIndex* block_index;
     199                 :           3 :         {
     200         [ +  - ]:           3 :             LOCK(cs_main);
     201   [ +  -  +  -  :           3 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     202                 :           0 :         }
     203                 :             : 
     204   [ +  -  +  -  :           6 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     205         [ +  - ]:           3 :         CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     206                 :             :     }
     207                 :             : 
     208                 :             :     // Check that filters for stale blocks on A can be retrieved.
     209                 :           1 :     chainA_last_header = last_header;
     210         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     211         [ +  - ]:           2 :         const auto& block = chainA[i];
     212                 :           2 :         const CBlockIndex* block_index;
     213                 :           2 :         {
     214         [ +  - ]:           2 :             LOCK(cs_main);
     215   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     216                 :           0 :         }
     217                 :             : 
     218   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     219         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     220                 :             :     }
     221                 :             : 
     222                 :             :     // Reorg back to chain A.
     223         [ +  + ]:           3 :      for (size_t i = 2; i < 4; i++) {
     224         [ +  - ]:           2 :          const auto& block = chainA[i];
     225   [ +  -  -  +  :           8 :          BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     226                 :             :      }
     227                 :             : 
     228                 :             :      // Check that chain A and B blocks can be retrieved.
     229                 :           1 :      chainA_last_header = last_header;
     230                 :           1 :      chainB_last_header = last_header;
     231         [ +  + ]:           4 :      for (size_t i = 0; i < 3; i++) {
     232                 :           3 :          const CBlockIndex* block_index;
     233                 :             : 
     234                 :           3 :          {
     235         [ +  - ]:           3 :              LOCK(cs_main);
     236   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
                   +  - ]
     237                 :           0 :          }
     238   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     239         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     240                 :             : 
     241                 :           3 :          {
     242         [ +  - ]:           3 :              LOCK(cs_main);
     243   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
                   +  - ]
     244                 :           0 :          }
     245   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     246         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     247                 :             :      }
     248                 :             : 
     249                 :             :     // Test lookups for a range of filters/hashes.
     250                 :           1 :     std::vector<BlockFilter> filters;
     251                 :           1 :     std::vector<uint256> filter_hashes;
     252                 :             : 
     253                 :           1 :     {
     254         [ +  - ]:           1 :         LOCK(cs_main);
     255   [ +  -  -  +  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     256                 :           0 :     }
     257   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterRange(0, tip, filters));
             +  -  +  - ]
     258   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterHashRange(0, tip, filter_hashes));
             +  -  -  + ]
     259                 :             : 
     260         [ -  + ]:           1 :     assert(tip->nHeight >= 0);
     261   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(filters.size(), tip->nHeight + 1U);
                   +  - ]
     262   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(filter_hashes.size(), tip->nHeight + 1U);
                   +  - ]
     263                 :             : 
     264                 :           1 :     filters.clear();
     265         [ +  - ]:           1 :     filter_hashes.clear();
     266                 :             : 
     267         [ +  - ]:           1 :     filter_index.Interrupt();
     268         [ +  - ]:           1 :     filter_index.Stop();
     269                 :           1 : }
     270                 :             : 
     271   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     272                 :             : {
     273                 :           1 :     BlockFilterIndex* filter_index;
     274                 :             : 
     275                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     276         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     277                 :             : 
     278   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
     279                 :             : 
     280                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     281         [ +  - ]:           2 :     BOOST_CHECK(filter_index != nullptr);
     282         [ +  - ]:           2 :     BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
     283                 :             : 
     284                 :             :     // Initialize returns false if index already exists.
     285   [ +  -  +  -  :           3 :     BOOST_CHECK(!InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
                   +  - ]
     286                 :             : 
     287                 :           1 :     int iter_count = 0;
     288         [ +  - ]:           2 :     ForEachBlockFilterIndex([&iter_count](BlockFilterIndex& _index) { iter_count++; });
     289         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(iter_count, 1);
     290                 :             : 
     291   [ +  -  +  - ]:           2 :     BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));
     292                 :             : 
     293                 :             :     // Destroy returns false because index was already destroyed.
     294   [ +  -  +  - ]:           2 :     BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));
     295                 :             : 
     296                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     297         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     298                 :             : 
     299                 :             :     // Reinitialize index.
     300   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
     301                 :             : 
     302                 :           1 :     DestroyAllBlockFilterIndexes();
     303                 :             : 
     304                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     305         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     306                 :           1 : }
     307                 :             : 
     308                 :             : class IndexReorgCrash : public BaseIndex
     309                 :             : {
     310                 :             : private:
     311                 :             :     std::unique_ptr<BaseIndex::DB> m_db;
     312                 :             :     std::shared_future<void> m_blocker;
     313                 :             :     int m_blocking_height;
     314                 :             : 
     315                 :             : public:
     316                 :           1 :     explicit IndexReorgCrash(std::unique_ptr<interfaces::Chain> chain, std::shared_future<void> blocker,
     317                 :           0 :                              int blocking_height) : BaseIndex(std::move(chain), "test index"), m_blocker(blocker),
     318   [ +  -  +  - ]:           1 :                                                     m_blocking_height(blocking_height)
     319                 :             :     {
     320   [ +  -  +  - ]:           2 :         const fs::path path = gArgs.GetDataDirNet() / "index";
     321         [ +  - ]:           1 :         fs::create_directories(path);
     322   [ +  -  +  -  :           4 :         m_db = std::make_unique<BaseIndex::DB>(path / "db", /*n_cache_size=*/0, /*f_memory=*/true, /*f_wipe=*/false);
                   +  - ]
     323         [ -  - ]:           1 :     }
     324                 :             : 
     325                 :           6 :     bool AllowPrune() const override { return false; }
     326                 :          13 :     BaseIndex::DB& GetDB() const override { return *m_db; }
     327                 :             : 
     328                 :         104 :     bool CustomAppend(const interfaces::BlockInfo& block) override
     329                 :             :     {
     330                 :             :         // Simulate a delay so new blocks can get connected during the initial sync
     331         [ +  + ]:         104 :         if (block.height == m_blocking_height) m_blocker.wait();
     332                 :             : 
     333                 :             :         // Move mock time forward so the best index gets updated only when we are not at the blocking height
     334   [ +  +  +  + ]:         104 :         if (block.height == m_blocking_height - 1 || block.height > m_blocking_height) {
     335                 :           3 :             SetMockTime(GetTime<std::chrono::seconds>() + 31s);
     336                 :             :         }
     337                 :             : 
     338                 :         104 :         return true;
     339                 :             :     }
     340                 :             : };
     341                 :             : 
     342   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(index_reorg_crash, BuildChainTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     343                 :             : {
     344                 :             :     // Enable mock time
     345                 :           1 :     SetMockTime(GetTime<std::chrono::minutes>());
     346                 :             : 
     347                 :           1 :     std::promise<void> promise;
     348   [ +  -  +  - ]:           2 :     std::shared_future<void> blocker(promise.get_future());
     349   [ +  -  -  +  :           4 :     int blocking_height = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->nHeight);
                   +  - ]
     350                 :             : 
     351   [ +  -  +  -  :           2 :     IndexReorgCrash index(interfaces::MakeChain(m_node), blocker, blocking_height);
             +  -  +  - ]
     352   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.Init());
             +  -  +  - ]
     353   [ +  -  +  -  :           2 :     BOOST_REQUIRE(index.StartBackgroundSync());
             +  -  +  - ]
     354                 :             : 
     355                 :           3 :     auto func_wait_until = [&](int height, std::chrono::milliseconds timeout) {
     356                 :           2 :         auto deadline = std::chrono::steady_clock::now() + timeout;
     357         [ +  + ]:           6 :         while (index.GetSummary().best_block_height < height) {
     358         [ -  + ]:           2 :             if (std::chrono::steady_clock::now() > deadline) {
     359   [ #  #  #  # ]:           0 :                 BOOST_FAIL(strprintf("Timeout waiting for index height %d (current: %d)", height, index.GetSummary().best_block_height));
     360                 :           0 :                 return;
     361                 :             :             }
     362                 :           2 :             std::this_thread::sleep_for(100ms);
     363                 :             :         }
     364                 :           1 :     };
     365                 :             : 
     366                 :             :     // Wait until the index is one block before the fork point
     367         [ +  - ]:           1 :     func_wait_until(blocking_height - 1, /*timeout=*/5s);
     368                 :             : 
     369                 :             :     // Create a fork to trigger the reorg
     370                 :           1 :     std::vector<std::shared_ptr<CBlock>> fork;
     371   [ +  -  -  +  :           4 :     const CBlockIndex* prev_tip = WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->pprev);
                   +  - ]
     372   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(prev_tip, GetScriptForDestination(PKHash(GenerateRandomKey().GetPubKey())), 3, fork));
          +  -  +  -  +  
                -  +  - ]
     373                 :             : 
     374         [ +  + ]:           4 :     for (const auto& block : fork) {
     375   [ +  -  +  -  :          12 :         BOOST_REQUIRE(m_node.chainman->ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
          +  -  +  -  +  
                      - ]
     376                 :             :     }
     377                 :             : 
     378                 :             :     // Unblock the index thread so it can process the reorg
     379         [ +  - ]:           1 :     promise.set_value();
     380                 :             :     // Wait for the index to reach the new tip
     381         [ +  - ]:           1 :     func_wait_until(blocking_height + 2, 5s);
     382         [ +  - ]:           1 :     index.Stop();
     383         [ +  - ]:           2 : }
     384                 :             : 
     385                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1