wallet_create.cpp raw

   1  // Copyright (c) 2023-present The Limenka 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 <bench/bench.h>
   6  #include <limenka-build-config.h> // IWYU pragma: keep
   7  #include <random.h>
   8  #include <support/allocators/secure.h>
   9  #include <test/util/setup_common.h>
  10  #include <uint256.h>
  11  #include <util/fs.h>
  12  #include <util/translation.h>
  13  #include <wallet/context.h>
  14  #include <wallet/db.h>
  15  #include <wallet/wallet.h>
  16  #include <wallet/walletutil.h>
  17  
  18  #include <cassert>
  19  #include <memory>
  20  #include <optional>
  21  #include <string>
  22  #include <utility>
  23  #include <vector>
  24  
  25  namespace wallet {
  26  static void WalletCreate(benchmark::Bench& bench, bool encrypted)
  27  {
  28      auto test_setup = MakeNoLogFileContext<TestingSetup>();
  29      FastRandomContext random;
  30  
  31      WalletContext context;
  32      context.args = &test_setup->m_args;
  33      context.chain = test_setup->m_node.chain.get();
  34  
  35      DatabaseOptions options;
  36      options.require_format = DatabaseFormat::SQLITE;
  37      options.require_create = true;
  38      options.create_flags = WALLET_FLAG_DESCRIPTORS;
  39  
  40      if (encrypted) {
  41          options.create_passphrase = random.rand256().ToString();
  42      }
  43  
  44      DatabaseStatus status;
  45      bilingual_str error_string;
  46      std::vector<bilingual_str> warnings;
  47  
  48      auto wallet_path = fs::PathToString(test_setup->m_path_root / "test_wallet");
  49      bench.run([&] {
  50          auto wallet = CreateWallet(context, wallet_path, /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
  51          assert(status == DatabaseStatus::SUCCESS);
  52          assert(wallet != nullptr);
  53  
  54          // Release wallet
  55          RemoveWallet(context, wallet, /*load_on_start=*/ std::nullopt);
  56          WaitForDeleteWallet(std::move(wallet));
  57          fs::remove_all(wallet_path);
  58      });
  59  }
  60  
  61  static void WalletCreatePlain(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/false); }
  62  static void WalletCreateEncrypted(benchmark::Bench& bench) { WalletCreate(bench, /*encrypted=*/true); }
  63  
  64  #ifdef USE_SQLITE
  65  BENCHMARK(WalletCreatePlain, benchmark::PriorityLevel::LOW);
  66  BENCHMARK(WalletCreateEncrypted, benchmark::PriorityLevel::LOW);
  67  #endif
  68  
  69  } // namespace wallet
  70