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 % 145 135
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 5 5
Branches: 51.3 % 548 281

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2017-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <addresstype.h>
       6                 :             : #include <blockfilter.h>
       7                 :             : #include <chain.h>
       8                 :             : #include <index/blockfilterindex.h>
       9                 :             : #include <interfaces/chain.h>
      10                 :             : #include <key.h>
      11                 :             : #include <node/blockstorage.h>
      12                 :             : #include <node/context.h>
      13                 :             : #include <node/indexes.h>
      14                 :             : #include <primitives/block.h>
      15                 :             : #include <script/script.h>
      16                 :             : #include <sync.h>
      17                 :             : #include <test/util/blockfilter.h>
      18                 :             : #include <test/util/common.h>
      19                 :             : #include <test/util/mining.h>
      20                 :             : #include <test/util/setup_common.h>
      21                 :             : #include <test/util/validation.h>
      22                 :             : #include <uint256.h>
      23                 :             : #include <util/check.h>
      24                 :             : #include <validation.h>
      25                 :             : 
      26                 :             : #include <boost/test/unit_test.hpp>
      27                 :             : 
      28                 :             : #include <compare>
      29                 :             : #include <cstddef>
      30                 :             : #include <cstdint>
      31                 :             : #include <functional>
      32                 :             : #include <memory>
      33                 :             : #include <span>
      34                 :             : #include <string>
      35                 :             : #include <utility>
      36                 :             : #include <vector>
      37                 :             : 
      38                 :             : using node::BlockManager;
      39                 :             : 
      40                 :             : BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
      41                 :             : 
      42                 :         114 : static bool CheckFilterLookups(BlockFilterIndex& filter_index, const CBlockIndex* block_index,
      43                 :             :                                uint256& last_header, const BlockManager& blockman)
      44                 :             : {
      45                 :         114 :     BlockFilter expected_filter;
      46   [ +  -  -  + ]:         114 :     if (!ComputeFilter(filter_index.GetFilterType(), *block_index, expected_filter, blockman)) {
      47   [ #  #  #  # ]:           0 :         BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight);
      48                 :           0 :         return false;
      49                 :             :     }
      50                 :             : 
      51         [ +  - ]:         114 :     BlockFilter filter;
      52                 :         114 :     uint256 filter_header;
      53                 :         114 :     std::vector<BlockFilter> filters;
      54                 :         114 :     std::vector<uint256> filter_hashes;
      55                 :             : 
      56   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
      57   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
      58   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
      59   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
      60                 :             :                                                    filter_hashes));
      61                 :             : 
      62   [ +  -  -  +  :         114 :     BOOST_CHECK_EQUAL(filters.size(), 1U);
                   +  - ]
      63   [ +  -  -  +  :         114 :     BOOST_CHECK_EQUAL(filter_hashes.size(), 1U);
                   +  - ]
      64                 :             : 
      65   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter.GetHash(), expected_filter.GetHash());
             +  -  +  - ]
      66   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter_header, expected_filter.ComputeHeader(last_header));
                   +  - ]
      67   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filters[0].GetHash(), expected_filter.GetHash());
             +  -  +  - ]
      68   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash());
                   +  - ]
      69                 :             : 
      70                 :         114 :     filters.clear();
      71         [ +  - ]:         114 :     filter_hashes.clear();
      72                 :         114 :     last_header = filter_header;
      73                 :         114 :     return true;
      74                 :         228 : }
      75                 :             : 
      76   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      77                 :             : {
      78         [ +  - ]:           1 :     BlockFilterIndex filter_index(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1_MiB, true);
      79   [ +  -  +  -  :           2 :     BOOST_REQUIRE(filter_index.Init());
             +  -  +  - ]
      80                 :             : 
      81                 :           1 :     uint256 last_header;
      82                 :             : 
      83                 :             :     // Filter should not be found in the index before it is started.
      84                 :           1 :     {
      85         [ +  - ]:           1 :         LOCK(cs_main);
      86                 :             : 
      87         [ +  - ]:           1 :         BlockFilter filter;
      88                 :           1 :         uint256 filter_header;
      89                 :           1 :         std::vector<BlockFilter> filters;
      90                 :           1 :         std::vector<uint256> filter_hashes;
      91                 :             : 
      92   [ +  -  -  + ]:           1 :         for (const CBlockIndex* block_index = m_node.chainman->ActiveChain().Genesis();
      93         [ +  + ]:         102 :              block_index != nullptr;
      94         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
      95   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
      96   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
      97   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
      98   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
      99                 :             :                                                             filter_hashes));
     100                 :             :         }
     101         [ +  - ]:           1 :     }
     102                 :             : 
     103                 :             :     // BlockUntilSyncedToCurrentChain should return false before index is started.
     104   [ +  -  +  -  :           2 :     BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     105                 :             : 
     106         [ +  - ]:           1 :     filter_index.Sync();
     107                 :             : 
     108                 :             :     // Check that filter index has all blocks that were in the chain before it started.
     109                 :           1 :     {
     110         [ +  - ]:           1 :         LOCK(cs_main);
     111                 :           1 :         const CBlockIndex* block_index;
     112   [ +  -  -  + ]:           1 :         for (block_index = m_node.chainman->ActiveChain().Genesis();
     113         [ +  + ]:         102 :              block_index != nullptr;
     114         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
     115         [ +  - ]:         101 :             CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
     116                 :             :         }
     117                 :           0 :     }
     118                 :             : 
     119                 :             :     // Create two forks.
     120                 :           1 :     const CBlockIndex* tip;
     121                 :           1 :     {
     122         [ +  - ]:           1 :         LOCK(cs_main);
     123   [ +  -  -  +  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     124                 :           0 :     }
     125                 :           1 :     CKey coinbase_key_A = GenerateRandomKey();
     126                 :           1 :     CKey coinbase_key_B = GenerateRandomKey();
     127   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
                   +  - ]
     128   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
                   +  - ]
     129                 :           1 :     std::vector<std::shared_ptr<CBlock>> chainA, chainB;
     130   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(m_node, tip, coinbase_script_pub_key_A, 10, chainA));
             +  -  +  - ]
     131   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(m_node, tip, coinbase_script_pub_key_B, 10, chainB));
                   +  - ]
     132                 :             : 
     133                 :             :     // Check that new blocks on chain A get indexed.
     134                 :           1 :     uint256 chainA_last_header = last_header;
     135         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     136         [ +  - ]:           2 :         const auto& block = chainA[i];
     137   [ +  -  +  -  :           8 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     138                 :             :     }
     139         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     140         [ +  - ]:           2 :         const auto& block = chainA[i];
     141                 :           2 :         const CBlockIndex* block_index;
     142                 :           2 :         {
     143         [ +  - ]:           2 :             LOCK(cs_main);
     144   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     145                 :           0 :         }
     146                 :             : 
     147   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     148         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     149                 :             :     }
     150                 :             : 
     151                 :             :     // Reorg to chain B.
     152                 :           1 :     uint256 chainB_last_header = last_header;
     153         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     154         [ +  - ]:           3 :         const auto& block = chainB[i];
     155   [ +  -  +  -  :          12 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     156                 :             :     }
     157         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     158         [ +  - ]:           3 :         const auto& block = chainB[i];
     159                 :           3 :         const CBlockIndex* block_index;
     160                 :           3 :         {
     161         [ +  - ]:           3 :             LOCK(cs_main);
     162   [ +  -  +  -  :           3 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     163                 :           0 :         }
     164                 :             : 
     165   [ +  -  +  -  :           6 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     166         [ +  - ]:           3 :         CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     167                 :             :     }
     168                 :             : 
     169                 :             :     // Check that filters for stale blocks on A can be retrieved.
     170                 :           1 :     chainA_last_header = last_header;
     171         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     172         [ +  - ]:           2 :         const auto& block = chainA[i];
     173                 :           2 :         const CBlockIndex* block_index;
     174                 :           2 :         {
     175         [ +  - ]:           2 :             LOCK(cs_main);
     176   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     177                 :           0 :         }
     178                 :             : 
     179   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     180         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     181                 :             :     }
     182                 :             : 
     183                 :             :     // Reorg back to chain A.
     184         [ +  + ]:           3 :      for (size_t i = 2; i < 4; i++) {
     185         [ +  - ]:           2 :          const auto& block = chainA[i];
     186   [ +  -  +  -  :           8 :          BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     187                 :             :      }
     188                 :             : 
     189                 :             :      // Check that chain A and B blocks can be retrieved.
     190                 :           1 :      chainA_last_header = last_header;
     191                 :           1 :      chainB_last_header = last_header;
     192         [ +  + ]:           4 :      for (size_t i = 0; i < 3; i++) {
     193                 :           3 :          const CBlockIndex* block_index;
     194                 :             : 
     195                 :           3 :          {
     196         [ +  - ]:           3 :              LOCK(cs_main);
     197   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
                   +  - ]
     198                 :           0 :          }
     199   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     200         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     201                 :             : 
     202                 :           3 :          {
     203         [ +  - ]:           3 :              LOCK(cs_main);
     204   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
                   +  - ]
     205                 :           0 :          }
     206   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     207         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     208                 :             :      }
     209                 :             : 
     210                 :             :     // Test lookups for a range of filters/hashes.
     211                 :           1 :     std::vector<BlockFilter> filters;
     212                 :           1 :     std::vector<uint256> filter_hashes;
     213                 :             : 
     214                 :           1 :     {
     215         [ +  - ]:           1 :         LOCK(cs_main);
     216   [ +  -  -  +  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     217                 :           0 :     }
     218   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterRange(0, tip, filters));
             +  -  +  - ]
     219   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterHashRange(0, tip, filter_hashes));
             +  -  -  + ]
     220                 :             : 
     221         [ -  + ]:           1 :     assert(tip->nHeight >= 0);
     222   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(filters.size(), tip->nHeight + 1U);
                   +  - ]
     223   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(filter_hashes.size(), tip->nHeight + 1U);
                   +  - ]
     224                 :             : 
     225                 :           1 :     filters.clear();
     226         [ +  - ]:           1 :     filter_hashes.clear();
     227                 :             : 
     228         [ +  - ]:           1 :     filter_index.Interrupt();
     229         [ +  - ]:           1 :     StopIndex(filter_index, m_node);
     230                 :           1 : }
     231                 :             : 
     232   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_lookup, BasicTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     233                 :             : {
     234   [ +  -  +  - ]:           2 :     BOOST_CHECK(node::GetBlockFilterIndex(m_node, BlockFilterType::BASIC) == nullptr);
     235                 :             : 
     236         [ +  - ]:           1 :     m_node.block_filter_indexes.emplace_back(
     237         [ +  - ]:           2 :         node::MakeIndex<BlockFilterIndex>(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true));
     238                 :             : 
     239                 :           1 :     BlockFilterIndex* filter_index{node::GetBlockFilterIndex(m_node, BlockFilterType::BASIC)};
     240         [ +  - ]:           2 :     BOOST_REQUIRE(filter_index != nullptr);
     241         [ +  - ]:           2 :     BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
     242                 :             : 
     243                 :           1 :     m_node.block_filter_indexes.clear();
     244   [ +  -  +  - ]:           2 :     BOOST_CHECK(node::GetBlockFilterIndex(m_node, BlockFilterType::BASIC) == nullptr);
     245                 :           1 : }
     246                 :             : 
     247                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1