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 % 188 175
Test Date: 2025-06-25 04:50:20 Functions: 100.0 % 7 7
Branches: 51.0 % 596 304

             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                 :             : 
      20                 :             : using node::BlockAssembler;
      21                 :             : using node::BlockManager;
      22                 :             : using node::CBlockTemplate;
      23                 :             : 
      24                 :             : BOOST_AUTO_TEST_SUITE(blockfilter_index_tests)
      25                 :             : 
      26                 :           2 : struct BuildChainTestingSetup : public TestChain100Setup {
      27                 :             :     CBlock CreateBlock(const CBlockIndex* prev, const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey);
      28                 :             :     bool BuildChain(const CBlockIndex* pindex, const CScript& coinbase_script_pub_key, size_t length, std::vector<std::shared_ptr<CBlock>>& chain);
      29                 :             : };
      30                 :             : 
      31                 :         114 : static bool CheckFilterLookups(BlockFilterIndex& filter_index, const CBlockIndex* block_index,
      32                 :             :                                uint256& last_header, const BlockManager& blockman)
      33                 :             : {
      34                 :         114 :     BlockFilter expected_filter;
      35   [ +  -  -  + ]:         114 :     if (!ComputeFilter(filter_index.GetFilterType(), *block_index, expected_filter, blockman)) {
      36   [ #  #  #  # ]:           0 :         BOOST_ERROR("ComputeFilter failed on block " << block_index->nHeight);
      37                 :           0 :         return false;
      38                 :             :     }
      39                 :             : 
      40         [ +  - ]:         114 :     BlockFilter filter;
      41                 :         114 :     uint256 filter_header;
      42                 :         114 :     std::vector<BlockFilter> filters;
      43                 :         114 :     std::vector<uint256> filter_hashes;
      44                 :             : 
      45   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
      46   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
      47   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
      48   [ +  -  +  -  :         228 :     BOOST_CHECK(filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
      49                 :             :                                                    filter_hashes));
      50                 :             : 
      51   [ +  -  +  - ]:         114 :     BOOST_CHECK_EQUAL(filters.size(), 1U);
      52   [ +  -  +  - ]:         114 :     BOOST_CHECK_EQUAL(filter_hashes.size(), 1U);
      53                 :             : 
      54   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter.GetHash(), expected_filter.GetHash());
             +  -  +  - ]
      55   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter_header, expected_filter.ComputeHeader(last_header));
                   +  - ]
      56   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filters[0].GetHash(), expected_filter.GetHash());
             +  -  +  - ]
      57   [ +  -  +  -  :         114 :     BOOST_CHECK_EQUAL(filter_hashes[0], expected_filter.GetHash());
                   +  - ]
      58                 :             : 
      59                 :         114 :     filters.clear();
      60         [ +  - ]:         114 :     filter_hashes.clear();
      61                 :         114 :     last_header = filter_header;
      62                 :         114 :     return true;
      63                 :         228 : }
      64                 :             : 
      65                 :          20 : CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
      66                 :             :     const std::vector<CMutableTransaction>& txns,
      67                 :             :     const CScript& scriptPubKey)
      68                 :             : {
      69                 :          20 :     BlockAssembler::Options options;
      70                 :          20 :     options.coinbase_output_script = scriptPubKey;
      71   [ +  -  +  -  :          20 :     std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock();
                   +  - ]
      72                 :          20 :     CBlock& block = pblocktemplate->block;
      73                 :          20 :     block.hashPrevBlock = prev->GetBlockHash();
      74                 :          20 :     block.nTime = prev->nTime + 1;
      75                 :             : 
      76                 :             :     // Replace mempool-selected txns with just coinbase plus passed-in txns:
      77         [ +  - ]:          20 :     block.vtx.resize(1);
      78         [ -  + ]:          20 :     for (const CMutableTransaction& tx : txns) {
      79   [ #  #  #  #  :           0 :         block.vtx.push_back(MakeTransactionRef(tx));
                   #  # ]
      80                 :             :     }
      81                 :          20 :     {
      82   [ +  -  +  - ]:          20 :         CMutableTransaction tx_coinbase{*block.vtx.at(0)};
      83                 :          20 :         tx_coinbase.nLockTime = static_cast<uint32_t>(prev->nHeight);
      84   [ +  -  +  - ]:          20 :         tx_coinbase.vin.at(0).scriptSig = CScript{} << prev->nHeight + 1;
      85   [ +  -  +  -  :          40 :         block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
                   -  + ]
      86         [ +  - ]:          20 :         block.hashMerkleRoot = BlockMerkleRoot(block);
      87                 :           0 :     }
      88                 :             : 
      89   [ +  -  +  -  :          37 :     while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
                   +  + ]
      90                 :             : 
      91         [ +  - ]:          20 :     return block;
      92                 :          20 : }
      93                 :             : 
      94                 :           2 : bool BuildChainTestingSetup::BuildChain(const CBlockIndex* pindex,
      95                 :             :     const CScript& coinbase_script_pub_key,
      96                 :             :     size_t length,
      97                 :             :     std::vector<std::shared_ptr<CBlock>>& chain)
      98                 :             : {
      99                 :           2 :     std::vector<CMutableTransaction> no_txns;
     100                 :             : 
     101         [ +  - ]:           2 :     chain.resize(length);
     102         [ +  + ]:          22 :     for (auto& block : chain) {
     103   [ +  -  -  + ]:          40 :         block = std::make_shared<CBlock>(CreateBlock(pindex, no_txns, coinbase_script_pub_key));
     104                 :          20 :         CBlockHeader header = block->GetBlockHeader();
     105                 :             : 
     106         [ +  - ]:          20 :         BlockValidationState state;
     107   [ +  -  +  -  :          20 :         if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{header}}, true, state, &pindex)) {
                   -  + ]
     108                 :           0 :             return false;
     109                 :             :         }
     110                 :          20 :     }
     111                 :             : 
     112                 :             :     return true;
     113                 :           2 : }
     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                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1