wallet_ismine.cpp raw

   1  // Copyright (c) 2022-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 <bench/bench.h>
   7  #include <key.h>
   8  #include <key_io.h>
   9  #include <script/descriptor.h>
  10  #include <script/script.h>
  11  #include <script/signingprovider.h>
  12  #include <sync.h>
  13  #include <test/util/setup_common.h>
  14  #include <util/check.h>
  15  #include <wallet/context.h>
  16  #include <wallet/db.h>
  17  #include <wallet/sqlite.h>
  18  #include <wallet/test/util.h>
  19  #include <wallet/wallet.h>
  20  #include <wallet/walletutil.h>
  21  
  22  #include <cstdint>
  23  #include <functional>
  24  #include <memory>
  25  #include <string>
  26  #include <utility>
  27  #include <vector>
  28  
  29  namespace wallet {
  30  static void WalletIsMine(benchmark::Bench& bench, int num_combo = 0)
  31  {
  32      const auto test_setup = MakeNoLogFileContext<TestingSetup>();
  33  
  34      WalletContext context;
  35      context.args = &test_setup->m_args;
  36      context.chain = test_setup->m_node.chain.get();
  37  
  38      // Setup the wallet
  39      // Loading the wallet will also create it
  40      uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;
  41      auto database = MakeInMemoryWalletDatabase();
  42      auto wallet = TestCreateWallet(std::move(database), context, create_flags);
  43  
  44      // For a descriptor wallet, fill with num_combo combo descriptors with random keys
  45      // This benchmarks a non-HD wallet migrated to descriptors
  46      if (num_combo > 0) {
  47          LOCK(wallet->cs_wallet);
  48          for (int i = 0; i < num_combo; ++i) {
  49              CKey key;
  50              key.MakeNewKey(/*fCompressed=*/true);
  51              FlatSigningProvider keys;
  52              std::string error;
  53              std::vector<std::unique_ptr<Descriptor>> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
  54              WalletDescriptor w_desc(std::move(desc.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
  55              Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false));
  56          }
  57      }
  58  
  59      const CScript script = GetScriptForDestination(DecodeDestination(ADDRESS_BCRT1_UNSPENDABLE));
  60  
  61      bench.run([&] {
  62          LOCK(wallet->cs_wallet);
  63          bool mine = wallet->IsMine(script);
  64          assert(!mine);
  65      });
  66  
  67      TestUnloadWallet(std::move(wallet));
  68  }
  69  
  70  static void WalletIsMineDescriptors(benchmark::Bench& bench) { WalletIsMine(bench); }
  71  static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*num_combo=*/2000); }
  72  BENCHMARK(WalletIsMineDescriptors);
  73  BENCHMARK(WalletIsMineMigratedDescriptors);
  74  } // namespace wallet
  75