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.0 % 187 174
Test Date: 2024-11-04 04:45:35 Functions: 100.0 % 7 7
Branches: 51.0 % 602 307

             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 :     std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(scriptPubKey);
      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.vin.at(0).scriptSig = CScript{} << prev->nHeight + 1;
      84   [ +  -  +  -  :          40 :         block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
                   -  + ]
      85         [ +  - ]:          20 :         block.hashMerkleRoot = BlockMerkleRoot(block);
      86                 :           0 :     }
      87                 :             : 
      88   [ +  -  +  -  :          43 :     while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
                   +  + ]
      89                 :             : 
      90         [ +  - ]:          40 :     return block;
      91                 :          20 : }
      92                 :             : 
      93                 :           2 : bool BuildChainTestingSetup::BuildChain(const CBlockIndex* pindex,
      94                 :             :     const CScript& coinbase_script_pub_key,
      95                 :             :     size_t length,
      96                 :             :     std::vector<std::shared_ptr<CBlock>>& chain)
      97                 :             : {
      98                 :           2 :     std::vector<CMutableTransaction> no_txns;
      99                 :             : 
     100         [ +  - ]:           2 :     chain.resize(length);
     101         [ +  + ]:          22 :     for (auto& block : chain) {
     102   [ +  -  -  + ]:          40 :         block = std::make_shared<CBlock>(CreateBlock(pindex, no_txns, coinbase_script_pub_key));
     103                 :          20 :         CBlockHeader header = block->GetBlockHeader();
     104                 :             : 
     105         [ +  - ]:          20 :         BlockValidationState state;
     106   [ +  -  +  -  :          20 :         if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{header}}, true, state, &pindex)) {
                   -  + ]
     107                 :           0 :             return false;
     108                 :             :         }
     109                 :          20 :     }
     110                 :             : 
     111                 :             :     return true;
     112                 :           2 : }
     113                 :             : 
     114   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     115                 :             : {
     116         [ +  - ]:           1 :     BlockFilterIndex filter_index(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1 << 20, true);
     117   [ +  -  +  -  :           2 :     BOOST_REQUIRE(filter_index.Init());
             +  -  +  - ]
     118                 :             : 
     119                 :           1 :     uint256 last_header;
     120                 :             : 
     121                 :             :     // Filter should not be found in the index before it is started.
     122                 :           1 :     {
     123         [ +  - ]:           1 :         LOCK(cs_main);
     124                 :             : 
     125         [ +  - ]:           1 :         BlockFilter filter;
     126                 :           1 :         uint256 filter_header;
     127                 :           1 :         std::vector<BlockFilter> filters;
     128                 :           1 :         std::vector<uint256> filter_hashes;
     129                 :             : 
     130   [ +  -  +  - ]:           1 :         for (const CBlockIndex* block_index = m_node.chainman->ActiveChain().Genesis();
     131         [ +  + ]:         102 :              block_index != nullptr;
     132         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(block_index)) {
     133   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
             +  -  +  - ]
     134   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
             +  -  +  - ]
     135   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
             +  -  +  - ]
     136   [ +  -  +  -  :         202 :             BOOST_CHECK(!filter_index.LookupFilterHashRange(block_index->nHeight, block_index,
             +  -  +  - ]
     137                 :             :                                                             filter_hashes));
     138                 :             :         }
     139         [ +  - ]:           1 :     }
     140                 :             : 
     141                 :             :     // BlockUntilSyncedToCurrentChain should return false before index is started.
     142   [ +  -  +  -  :           2 :     BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     143                 :             : 
     144   [ +  -  +  -  :           2 :     BOOST_REQUIRE(filter_index.StartBackgroundSync());
             +  -  +  - ]
     145                 :             : 
     146                 :             :     // Allow filter index to catch up with the block index.
     147   [ +  -  +  - ]:           1 :     IndexWaitSynced(filter_index, *Assert(m_node.shutdown_signal));
     148                 :             : 
     149                 :             :     // Check that filter index has all blocks that were in the chain before it started.
     150                 :           1 :     {
     151         [ +  - ]:           1 :         LOCK(cs_main);
     152                 :           1 :         const CBlockIndex* block_index;
     153   [ +  -  +  - ]:           1 :         for (block_index = m_node.chainman->ActiveChain().Genesis();
     154         [ +  + ]:         102 :              block_index != nullptr;
     155         [ +  - ]:         101 :              block_index = m_node.chainman->ActiveChain().Next(block_index)) {
     156         [ +  - ]:         101 :             CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
     157                 :             :         }
     158                 :           0 :     }
     159                 :             : 
     160                 :             :     // Create two forks.
     161                 :           1 :     const CBlockIndex* tip;
     162                 :           1 :     {
     163         [ +  - ]:           1 :         LOCK(cs_main);
     164   [ +  -  +  -  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     165                 :           0 :     }
     166                 :           1 :     CKey coinbase_key_A = GenerateRandomKey();
     167                 :           1 :     CKey coinbase_key_B = GenerateRandomKey();
     168   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_A = GetScriptForDestination(PKHash(coinbase_key_A.GetPubKey()));
                   +  - ]
     169   [ +  -  +  -  :           1 :     CScript coinbase_script_pub_key_B = GetScriptForDestination(PKHash(coinbase_key_B.GetPubKey()));
                   +  - ]
     170                 :           1 :     std::vector<std::shared_ptr<CBlock>> chainA, chainB;
     171   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_A, 10, chainA));
             +  -  +  - ]
     172   [ +  -  +  -  :           2 :     BOOST_REQUIRE(BuildChain(tip, coinbase_script_pub_key_B, 10, chainB));
                   +  - ]
     173                 :             : 
     174                 :             :     // Check that new blocks on chain A get indexed.
     175                 :           1 :     uint256 chainA_last_header = last_header;
     176         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     177         [ +  - ]:           2 :         const auto& block = chainA[i];
     178   [ +  -  +  -  :           8 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     179                 :             :     }
     180         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     181         [ +  - ]:           2 :         const auto& block = chainA[i];
     182                 :           2 :         const CBlockIndex* block_index;
     183                 :           2 :         {
     184         [ +  - ]:           2 :             LOCK(cs_main);
     185   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     186                 :           0 :         }
     187                 :             : 
     188   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     189         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     190                 :             :     }
     191                 :             : 
     192                 :             :     // Reorg to chain B.
     193                 :           1 :     uint256 chainB_last_header = last_header;
     194         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     195         [ +  - ]:           3 :         const auto& block = chainB[i];
     196   [ +  -  +  -  :          12 :         BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     197                 :             :     }
     198         [ +  + ]:           4 :     for (size_t i = 0; i < 3; i++) {
     199         [ +  - ]:           3 :         const auto& block = chainB[i];
     200                 :           3 :         const CBlockIndex* block_index;
     201                 :           3 :         {
     202         [ +  - ]:           3 :             LOCK(cs_main);
     203   [ +  -  +  -  :           3 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     204                 :           0 :         }
     205                 :             : 
     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                 :             :     // Check that filters for stale blocks on A can be retrieved.
     211                 :           1 :     chainA_last_header = last_header;
     212         [ +  + ]:           3 :     for (size_t i = 0; i < 2; i++) {
     213         [ +  - ]:           2 :         const auto& block = chainA[i];
     214                 :           2 :         const CBlockIndex* block_index;
     215                 :           2 :         {
     216         [ +  - ]:           2 :             LOCK(cs_main);
     217   [ +  -  +  -  :           2 :             block_index = m_node.chainman->m_blockman.LookupBlockIndex(block->GetHash());
                   +  - ]
     218                 :           0 :         }
     219                 :             : 
     220   [ +  -  +  -  :           4 :         BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     221         [ +  - ]:           2 :         CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     222                 :             :     }
     223                 :             : 
     224                 :             :     // Reorg back to chain A.
     225         [ +  + ]:           3 :      for (size_t i = 2; i < 4; i++) {
     226         [ +  - ]:           2 :          const auto& block = chainA[i];
     227   [ +  -  +  -  :           8 :          BOOST_REQUIRE(Assert(m_node.chainman)->ProcessNewBlock(block, true, true, nullptr));
          +  -  +  -  +  
                -  +  - ]
     228                 :             :      }
     229                 :             : 
     230                 :             :      // Check that chain A and B blocks can be retrieved.
     231                 :           1 :      chainA_last_header = last_header;
     232                 :           1 :      chainB_last_header = last_header;
     233         [ +  + ]:           4 :      for (size_t i = 0; i < 3; i++) {
     234                 :           3 :          const CBlockIndex* block_index;
     235                 :             : 
     236                 :           3 :          {
     237         [ +  - ]:           3 :              LOCK(cs_main);
     238   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainA[i]->GetHash());
                   +  - ]
     239                 :           0 :          }
     240   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     241         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainA_last_header, m_node.chainman->m_blockman);
     242                 :             : 
     243                 :           3 :          {
     244         [ +  - ]:           3 :              LOCK(cs_main);
     245   [ +  -  +  -  :           3 :              block_index = m_node.chainman->m_blockman.LookupBlockIndex(chainB[i]->GetHash());
                   +  - ]
     246                 :           0 :          }
     247   [ +  -  +  -  :           6 :          BOOST_CHECK(filter_index.BlockUntilSyncedToCurrentChain());
             +  -  +  - ]
     248         [ +  - ]:           3 :          CheckFilterLookups(filter_index, block_index, chainB_last_header, m_node.chainman->m_blockman);
     249                 :             :      }
     250                 :             : 
     251                 :             :     // Test lookups for a range of filters/hashes.
     252                 :           1 :     std::vector<BlockFilter> filters;
     253                 :           1 :     std::vector<uint256> filter_hashes;
     254                 :             : 
     255                 :           1 :     {
     256         [ +  - ]:           1 :         LOCK(cs_main);
     257   [ +  -  +  -  :           2 :         tip = m_node.chainman->ActiveChain().Tip();
                   +  - ]
     258                 :           0 :     }
     259   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterRange(0, tip, filters));
             +  -  +  - ]
     260   [ +  -  +  -  :           2 :     BOOST_CHECK(filter_index.LookupFilterHashRange(0, tip, filter_hashes));
             +  -  -  + ]
     261                 :             : 
     262         [ -  + ]:           1 :     assert(tip->nHeight >= 0);
     263   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(filters.size(), tip->nHeight + 1U);
     264   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(filter_hashes.size(), tip->nHeight + 1U);
     265                 :             : 
     266                 :           1 :     filters.clear();
     267         [ +  - ]:           1 :     filter_hashes.clear();
     268                 :             : 
     269         [ +  - ]:           1 :     filter_index.Interrupt();
     270         [ +  - ]:           1 :     filter_index.Stop();
     271                 :           1 : }
     272                 :             : 
     273   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockfilter_index_init_destroy, BasicTestingSetup)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     274                 :             : {
     275                 :           1 :     BlockFilterIndex* filter_index;
     276                 :             : 
     277                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     278         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     279                 :             : 
     280   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
     281                 :             : 
     282                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     283         [ +  - ]:           2 :     BOOST_CHECK(filter_index != nullptr);
     284         [ +  - ]:           2 :     BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
     285                 :             : 
     286                 :             :     // Initialize returns false if index already exists.
     287   [ +  -  +  -  :           3 :     BOOST_CHECK(!InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
                   +  - ]
     288                 :             : 
     289                 :           1 :     int iter_count = 0;
     290         [ +  - ]:           2 :     ForEachBlockFilterIndex([&iter_count](BlockFilterIndex& _index) { iter_count++; });
     291         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(iter_count, 1);
     292                 :             : 
     293   [ +  -  +  - ]:           2 :     BOOST_CHECK(DestroyBlockFilterIndex(BlockFilterType::BASIC));
     294                 :             : 
     295                 :             :     // Destroy returns false because index was already destroyed.
     296   [ +  -  +  - ]:           2 :     BOOST_CHECK(!DestroyBlockFilterIndex(BlockFilterType::BASIC));
     297                 :             : 
     298                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     299         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     300                 :             : 
     301                 :             :     // Reinitialize index.
     302   [ +  -  +  - ]:           3 :     BOOST_CHECK(InitBlockFilterIndex([&]{ return interfaces::MakeChain(m_node); }, BlockFilterType::BASIC, 1 << 20, true, false));
     303                 :             : 
     304                 :           1 :     DestroyAllBlockFilterIndexes();
     305                 :             : 
     306                 :           1 :     filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
     307         [ +  - ]:           2 :     BOOST_CHECK(filter_index == nullptr);
     308                 :           1 : }
     309                 :             : 
     310                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1