index_blockfilter.cpp raw

   1  // Copyright (c) 2023-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or https://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <addresstype.h>
   6  #include <bench/bench.h>
   7  #include <blockfilter.h>
   8  #include <chain.h>
   9  #include <index/base.h>
  10  #include <index/blockfilterindex.h>
  11  #include <interfaces/chain.h>
  12  #include <primitives/block.h>
  13  #include <primitives/transaction.h>
  14  #include <pubkey.h>
  15  #include <script/script.h>
  16  #include <sync.h>
  17  #include <test/util/setup_common.h>
  18  #include <test/util/time.h>
  19  #include <uint256.h>
  20  #include <util/check.h>
  21  #include <util/strencodings.h>
  22  #include <validation.h>
  23  
  24  #include <memory>
  25  #include <span>
  26  #include <vector>
  27  
  28  using namespace util::hex_literals;
  29  
  30  // Very simple block filter index sync benchmark, only using coinbase outputs.
  31  static void BlockFilterIndexSync(benchmark::Bench& bench)
  32  {
  33      const auto test_setup = MakeNoLogFileContext<TestChain100Setup>();
  34  
  35      // Create more blocks
  36      int CHAIN_SIZE = 600;
  37      CPubKey pubkey{"02ed26169896db86ced4cbb7b3ecef9859b5952825adbeab998fb5b307e54949c9"_hex_u8};
  38      CScript script = GetScriptForDestination(WitnessV0KeyHash(pubkey));
  39      std::vector<CMutableTransaction> noTxns;
  40      for (int i = 0; i < CHAIN_SIZE - 100; i++) {
  41          test_setup->CreateAndProcessBlock(noTxns, script);
  42          test_setup->m_clock += 1s;
  43      }
  44      assert(WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveHeight() == CHAIN_SIZE));
  45  
  46      bench.minEpochIterations(5).run([&] {
  47          BlockFilterIndex filter_index(interfaces::MakeChain(test_setup->m_node), BlockFilterType::BASIC,
  48                                        /*n_cache_size=*/0, /*f_memory=*/false, /*f_wipe=*/true);
  49          assert(filter_index.Init());
  50          assert(!filter_index.BlockUntilSyncedToCurrentChain());
  51          filter_index.Sync();
  52  
  53          IndexSummary summary = filter_index.GetSummary();
  54          assert(summary.synced);
  55          assert(summary.best_block_hash == WITH_LOCK(::cs_main, return test_setup->m_node.chainman->ActiveTip()->GetBlockHash()));
  56  
  57          // Shutdown sequence (c.f. Shutdown() in init.cpp)
  58          filter_index.Stop();
  59      });
  60  }
  61  
  62  BENCHMARK(BlockFilterIndexSync);
  63