wallet_balance.cpp raw

   1  // Copyright (c) 2012-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 <bench/bench.h>
   6  #include <interfaces/chain.h>
   7  #include <interfaces/handler.h>
   8  #include <kernel/chainparams.h>
   9  #include <primitives/block.h>
  10  #include <primitives/transaction.h>
  11  #include <sync.h>
  12  #include <test/util/mining.h>
  13  #include <test/util/setup_common.h>
  14  #include <test/util/time.h>
  15  #include <uint256.h>
  16  #include <util/check.h>
  17  #include <validation.h>
  18  #include <wallet/db.h>
  19  #include <wallet/receive.h>
  20  #include <wallet/sqlite.h>
  21  #include <wallet/test/util.h>
  22  #include <wallet/wallet.h>
  23  #include <wallet/walletutil.h>
  24  
  25  #include <memory>
  26  #include <optional>
  27  #include <string>
  28  
  29  namespace wallet {
  30  static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
  31  {
  32      const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
  33  
  34      const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
  35  
  36      // Set clock to genesis block, so the descriptors/keys creation time don't interfere with the blocks scanning process.
  37      // The reason is 'generatetoaddress', which creates a chain with deterministic timestamps in the past.
  38      FakeNodeClock clock{test_setup->m_node.chainman->GetParams().GenesisBlock().Time()};
  39      CWallet wallet{test_setup->m_node.chain.get(), "", MakeInMemoryWalletDatabase()};
  40      {
  41          LOCK(wallet.cs_wallet);
  42          wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
  43          wallet.SetupDescriptorScriptPubKeyMans();
  44      }
  45      auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
  46  
  47      const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
  48  
  49      for (int i = 0; i < 100; ++i) {
  50          generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
  51          generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);
  52      }
  53      // Calls SyncWithValidationInterfaceQueue
  54      wallet.chain().waitForNotificationsIfTipChanged(uint256::ZERO);
  55  
  56      auto bal = GetBalance(wallet); // Cache
  57  
  58      bench.setup([&] {
  59              if (set_dirty) wallet.MarkDirty();
  60          })
  61          .run([&] {
  62              bal = GetBalance(wallet);
  63              ankerl::nanobench::doNotOptimizeAway(bal);
  64              assert(add_mine == (bal.m_mine_trusted > 0));
  65          });
  66  }
  67  
  68  static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/true, /*add_mine=*/true); }
  69  static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
  70  static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/false); }
  71  
  72  BENCHMARK(WalletBalanceDirty);
  73  BENCHMARK(WalletBalanceClean);
  74  BENCHMARK(WalletBalanceWatch);
  75  } // namespace wallet
  76