Bitcoin Core  25.99.0
P2P Digital Currency
wallet_loading.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022 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 <node/context.h>
8 #include <test/util/mining.h>
10 #include <wallet/test/util.h>
11 #include <util/translation.h>
12 #include <validationinterface.h>
13 #include <wallet/context.h>
14 #include <wallet/receive.h>
15 #include <wallet/wallet.h>
16 
17 #include <optional>
18 
19 namespace wallet{
20 static void AddTx(CWallet& wallet)
21 {
23  mtx.vout.push_back({COIN, GetScriptForDestination(*Assert(wallet.GetNewDestination(OutputType::BECH32, "")))});
24  mtx.vin.push_back(CTxIn());
25 
26  wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});
27 }
28 
29 static void WalletLoading(benchmark::Bench& bench, bool legacy_wallet)
30 {
31  const auto test_setup = MakeNoLogFileContext<TestingSetup>();
32 
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  // Generate a bunch of transactions and addresses to put into the wallet
47  for (int i = 0; i < 1000; ++i) {
48  AddTx(*wallet);
49  }
50 
51  database = DuplicateMockDatabase(wallet->GetDatabase());
52 
53  // reload the wallet for the actual benchmark
54  TestUnloadWallet(std::move(wallet));
55 
56  bench.epochs(5).run([&] {
57  wallet = TestLoadWallet(std::move(database), context, create_flags);
58 
59  // Cleanup
60  database = DuplicateMockDatabase(wallet->GetDatabase());
61  TestUnloadWallet(std::move(wallet));
62  });
63 }
64 
65 #ifdef USE_BDB
66 static void WalletLoadingLegacy(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/true); }
67 BENCHMARK(WalletLoadingLegacy, benchmark::PriorityLevel::HIGH);
68 #endif
69 
70 #ifdef USE_SQLITE
71 static void WalletLoadingDescriptors(benchmark::Bench& bench) { WalletLoading(bench, /*legacy_wallet=*/false); }
72 BENCHMARK(WalletLoadingDescriptors, benchmark::PriorityLevel::HIGH);
73 #endif
74 } // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
#define Assert(val)
Identity function.
Definition: check.h:73
An input of a transaction.
Definition: transaction.h:75
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:623
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1230
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:301
@ HIGH
Definition: bench.h:47
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:73
static void AddTx(CWallet &wallet)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:190
std::unique_ptr< WalletDatabase > DuplicateMockDatabase(WalletDatabase &database)
Definition: util.cpp:80
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:74
static void WalletLoading(benchmark::Bench &bench, bool legacy_wallet)
WalletContext context
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:422
A mutable version of CTransaction.
Definition: transaction.h:380
std::vector< CTxOut > vout
Definition: transaction.h:382
std::vector< CTxIn > vin
Definition: transaction.h:381
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:50
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35
interfaces::Chain * chain
Definition: context.h:36
ArgsManager * args
Definition: context.h:37