wallet_ismine.cpp raw

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