LCOV - code coverage report
Current view: top level - src/test - blockfilter_index_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 93.1 % 189 176
Test Date: 2025-06-01 05:54:06 Functions: 100.0 % 7 7
Branches: 51.0 % 606 309

             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/index.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <validation.h>
      18                 :             : 
      19                 :             : #include <boost/test/unit_test.hpp>
      20                 :             : 
      21                 :             : using node::BlockAssembler;
      22                 :             : using node::BlockManager;
      23                 :             : using node::CBlockTemplate;
      24                 :             : 
      25                 :             : BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
      26                 :             : 
      27                 :           2 : 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                 :          20 : CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
      67                 :             :     const std::vector<CMutableTransaction>& txns,
      68                 :             :     const CScript& scriptPubKey)
      69                 :             : {
      70                 :          20 :     BlockAssembler::Options options;
      71                 :          20 :     options.coinbase_output_script = scriptPubKey;
      72   [ +  -  +  -  :          20 :     std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock();
                   +  - ]
      73                 :          20 :     CBlock& block = pblocktemplate->block;
      74                 :          20 :     block.hashPrevBlock = prev->GetBlockHash();
      75                 :          20 :     block.nTime = prev->nTime + 1;
      76                 :             : 
      77                 :             :     // Replace mempool-selected txns with just coinbase plus passed-in txns:
      78         [ +  - ]:          20 :     block.vtx.resize(1);
      79         [ -  + ]:          20 :     for (const CMutableTransaction& tx : txns) {
      80   [ #  #  #  #  :           0 :         block.vtx.push_back(MakeTransactionRef(tx));
                   #  # ]
      81                 :             :     }
      82                 :          20 :     {
      83   [ +  -  +  - ]:          20 :         CMutableTransaction tx_coinbase{*block.vtx.at(0)};
      84                 :          20 :         tx_coinbase.nLockTime = static_cast<uint32_t>(prev->nHeight);
      85   [ +  -  +  - ]:          20 :         tx_coinbase.vin.at(0).scriptSig = CScript{} << prev->nHeight + 1;
      86   [ +  -  +  -  :          40 :         block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
                   -  + ]
      87         [ +  - ]:          20 :         block.hashMerkleRoot = BlockMerkleRoot(block);
      88                 :           0 :     }
      89                 :             : 
      90   [ +  -  +  -  :          40 :     while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
                   +  + ]
      91                 :             : 
      92         [ +  - ]:          20 :     return block;
      93                 :          20 : }
      94                 :             : 
      95                 :           2 : 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                 :           2 :     std::vector<CMutableTransaction> no_txns;
     101                 :             : 
     102         [ +  - ]:           2 :     chain.resize(length);
     103         [ +  + ]:          22 :     for (auto& block : chain) {
     104   [ +  -  -  + ]:          40 :         block = std::make_shared<CBlock>(CreateBlock(pindex, no_txns, coinbase_script_pub_key));
     105                 :          20 :         CBlockHeader header = block->GetBlockHeader();
     106                 :             : 
     107         [ +  - ]:          20 :         BlockValidationState state;
     108   [ +  -  +  -  :          20 :         if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{header}}, true, state, &pindex)) {
                   -  + ]
     109                 :           0 :             return false;
     110                 :             :         }
     111                 :          20 :     }
     112                 :             : 
     113                 :             :     return true;
     114                 :           2 : }
     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   [ +  -  +  -  :           2 :     BOOST_REQUIRE(filter_index.StartBackgroundSync());
             +  -  +  - ]
     147                 :             : 
     148                 :             :     // Allow filter index to catch up with the block index.
     149   [ +  -  +  - ]:           1 :     IndexWaitSynced(filter_index, *Assert(m_node.shutdown_signal));
     150                 :             : 
     151                 :             :     // Check that filter index has all blocks that were in the chain before it started.
     152                 :           1 :     {
     153         [ +  - ]:           1 :         LOCK(cs_main);
     154                 :           1 :         const CBlockIndex* block_index;
     155   [ +  -  +  - ]:           1 :         for (block_index = m_node.chainman->ActiveChain().Genesis();
     156         [ +  + ]:         102 :              block_index != nullptr;
     157         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(block_index)) {
     158         [ +  - ]:         101 :             CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
     159                 :             :         }
     160                 :           0 :     }
     161                 :             : 
     162                 :             :     // Create two forks.
     163                 :           1 :     const CBlockIndex* tip;
     164                 :           1 :     {
     165         [ +  - ]:           1 :         LOCK(cs_main);
     166   [ +  -  +  -  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     167                 :           0 :     }
     168                 :           1 :     CKey coinbase_key_A = GenerateRandomKey();
     169                 :           1 :     CKey coinbase_key_B = GenerateRandomKey();
     170   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
                   +  - ]
     171   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
                   +  - ]
     172                 :           1 :     std::vector<std::shared_ptr<CBlock>> chainA, chainB;
     173   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
             +  -  +  - ]
     174   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
                   +  - ]
     175                 :             : 
     176                 :             :     // Check that new blocks on chain A get indexed.
     177                 :           1 :     uint256 chainA_last_header = last_header;
     178         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     179         [ +  - ]:           2 :         const auto& block = chainA[i];
     180   [ +  -  +  -  :           8 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     181                 :             :     }
     182         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     183         [ +  - ]:           2 :         const auto& block = chainA[i];
     184                 :           2 :         const CBlockIndex* block_index;
     185                 :           2 :         {
     186         [ +  - ]:           2 :             LOCK(cs_main);
     187   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     188                 :           0 :         }
     189                 :             : 
     190   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     191         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     192                 :             :     }
     193                 :             : 
     194                 :             :     // Reorg to chain B.
     195                 :           1 :     uint256 chainB_last_header = last_header;
     196         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     197         [ +  - ]:           3 :         const auto& block = chainB[i];
     198   [ +  -  +  -  :          12 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     199                 :             :     }
     200         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     201         [ +  - ]:           3 :         const auto& block = chainB[i];
     202                 :           3 :         const CBlockIndex* block_index;
     203                 :           3 :         {
     204         [ +  - ]:           3 :             LOCK(cs_main);
     205   [ +  -  +  -  :           3 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     206                 :           0 :         }
     207                 :             : 
     208   [ +  -  +  -  :           6 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     209         [ +  - ]:           3 :         CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     210                 :             :     }
     211                 :             : 
     212                 :             :     // Check that filters for stale blocks on A can be retrieved.
     213                 :           1 :     chainA_last_header = last_header;
     214         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     215         [ +  - ]:           2 :         const auto& block = chainA[i];
     216                 :           2 :         const CBlockIndex* block_index;
     217                 :           2 :         {
     218         [ +  - ]:           2 :             LOCK(cs_main);
     219   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     220                 :           0 :         }
     221                 :             : 
     222   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     223         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     224                 :             :     }
     225                 :             : 
     226                 :             :     // Reorg back to chain A.
     227         [ +  + ]:           3 :      for (size_t i = 2; i < 4; i++) {
     228         [ +  - ]:           2 :          const auto& block = chainA[i];
     229   [ +  -  +  -  :           8 :          BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     230                 :             :      }
     231                 :             : 
     232                 :             :      // Check that chain A and B blocks can be retrieved.
     233                 :           1 :      chainA_last_header = last_header;
     234                 :           1 :      chainB_last_header = last_header;
     235         [ +  + ]:           4 :      for (size_t i = 0; i < 3; i++) {
     236                 :           3 :          const CBlockIndex* block_index;
     237                 :             : 
     238                 :           3 :          {
     239         [ +  - ]:           3 :              LOCK(cs_main);
     240   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
                   +  - ]
     241                 :           0 :          }
     242   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     243         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     244                 :             : 
     245                 :           3 :          {
     246         [ +  - ]:           3 :              LOCK(cs_main);
     247   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
                   +  - ]
     248                 :           0 :          }
     249   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     250         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     251                 :             :      }
     252                 :             : 
     253                 :             :     // Test lookups for a range of filters/hashes.
     254                 :           1 :     std::vector<BlockFilter> filters;
     255                 :           1 :     std::vector<uint256> filter_hashes;
     256                 :             : 
     257                 :           1 :     {
     258         [ +  - ]:           1 :         LOCK(cs_main);
     259   [ +  -  +  -  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     260                 :           0 :     }
     261   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterRange(0, tip, filters));
             +  -  +  - ]
     262   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterHashRange(0, tip, filter_hashes));
             +  -  -  + ]
     263                 :             : 
     264         [ -  + ]:           1 :     assert(tip->nHeight >= 0);
     265   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(filters.size(), tip->nHeight + 1U);
     266   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(filter_hashes.size(), tip->nHeight + 1U);
     267                 :             : 
     268                 :           1 :     filters.clear();
     269         [ +  - ]:           1 :     filter_hashes.clear();
     270                 :             : 
     271         [ +  - ]:           1 :     filter_index.Interrupt();
     272         [ +  - ]:           1 :     filter_index.Stop();
     273                 :           1 : }
     274                 :             : 
     275   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     276                 :             : {
     277                 :           1 :     BlockFilterIndex* filter_index;
     278                 :             : 
     279                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     280         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     281                 :             : 
     282   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
     283                 :             : 
     284                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     285         [ +  - ]:           2 :     BOOST_CHECK(filter_index != nullptr);
     286         [ +  - ]:           2 :     BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
     287                 :             : 
     288                 :             :     // Initialize returns false if index already exists.
     289   [ +  -  +  -  :           3 :     BOOST_CHECK(!InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
                   +  - ]
     290                 :             : 
     291                 :           1 :     int iter_count = 0;
     292         [ +  - ]:           2 :     ForEachBlockFilterIndex([&iter_count](BlockFilterIndex& _index) { iter_count++; });
     293         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(iter_count, 1);
     294                 :             : 
     295   [ +  -  +  - ]:           2 :     BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));
     296                 :             : 
     297                 :             :     // Destroy returns false because index was already destroyed.
     298   [ +  -  +  - ]:           2 :     BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));
     299                 :             : 
     300                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     301         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     302                 :             : 
     303                 :             :     // Reinitialize index.
     304   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
     305                 :             : 
     306                 :           1 :     DestroyAllBlockFilterIndexes();
     307                 :             : 
     308                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     309         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     310                 :           1 : }
     311                 :             : 
     312                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1