Bitcoin Core  24.99.0
P2P Digital Currency
wallet_balance.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2021 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 <test/util/wallet.h>
11 #include <validationinterface.h>
12 #include <wallet/receive.h>
13 #include <wallet/wallet.h>
14 
15 #include <optional>
16 
17 using wallet::CWallet;
19 using wallet::DBErrors;
20 using wallet::GetBalance;
22 
23 static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
24 {
25  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
26 
27  const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
28 
29  CWallet wallet{test_setup->m_node.chain.get(), "", gArgs, CreateMockWalletDatabase()};
30  {
31  LOCK(wallet.cs_wallet);
32  wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
33  wallet.SetupDescriptorScriptPubKeyMans();
34  if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
35  }
36  auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
37 
38  const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
39 
40  for (int i = 0; i < 100; ++i) {
41  generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
42  generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);
43  }
45 
46  auto bal = GetBalance(wallet); // Cache
47 
48  bench.run([&] {
49  if (set_dirty) wallet.MarkDirty();
50  bal = GetBalance(wallet);
51  if (add_mine) assert(bal.m_mine_trusted > 0);
52  });
53 }
54 
55 static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/true, /*add_mine=*/true); }
56 static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
57 static void WalletBalanceMine(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
58 static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/false); }
59 
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:236
Definition: node.h:39
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:46
Balance GetBalance(const CWallet &wallet, const int min_depth, bool avoid_reuse)
Definition: receive.cpp:293
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase(DatabaseOptions &options)
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1242
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:66
std::shared_ptr< CWallet > wallet
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:931
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:244
#define LOCK(cs)
Definition: sync.h:261
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: wallet.cpp:18
std::string getnewaddress(wallet::CWallet &w)
Returns a new address from the wallet.
ArgsManager gArgs
Definition: system.cpp:86
assert(!tx.IsCoinBase())
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static void WalletBalanceClean(benchmark::Bench &bench)
static void WalletBalance(benchmark::Bench &bench, const bool set_dirty, const bool add_mine)
static void WalletBalanceMine(benchmark::Bench &bench)
BENCHMARK(WalletBalanceDirty)
static void WalletBalanceWatch(benchmark::Bench &bench)
static void WalletBalanceDirty(benchmark::Bench &bench)