Bitcoin Core  21.99.0
P2P Digital Currency
wallet_balance.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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>
9 #include <test/util/setup_common.h>
10 #include <test/util/wallet.h>
11 #include <validationinterface.h>
12 #include <wallet/wallet.h>
13 
14 #include <optional>
15 
16 static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
17 {
18  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
19 
20  const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
21 
22  CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockWalletDatabase()};
23  {
24  wallet.SetupLegacyScriptPubKeyMan();
25  bool first_run;
26  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
27  }
28  auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
29 
30  const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
31  if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
32 
33  for (int i = 0; i < 100; ++i) {
34  generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
35  generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);
36  }
38 
39  auto bal = wallet.GetBalance(); // Cache
40 
41  bench.run([&] {
42  if (set_dirty) wallet.MarkDirty();
43  bal = wallet.GetBalance();
44  if (add_mine) assert(bal.m_mine_trusted > 0);
45  if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
46  });
47 }
48 
49 static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); }
50 static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); }
51 static void WalletBalanceMine(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); }
52 static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); }
53 
wallet.h
WalletBalanceClean
static void WalletBalanceClean(benchmark::Bench &bench)
Definition: wallet_balance.cpp:50
WalletBalanceMine
static void WalletBalanceMine(benchmark::Bench &bench)
Definition: wallet_balance.cpp:51
handler
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:675
WalletBalanceWatch
static void WalletBalanceWatch(benchmark::Bench &bench)
Definition: wallet_balance.cpp:52
getnewaddress
static RPCHelpMan getnewaddress()
Definition: rpcwallet.cpp:233
validationinterface.h
generatetoaddress
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:249
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
WalletBalance
static void WalletBalance(benchmark::Bench &bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
Definition: wallet_balance.cpp:16
wallet
Definition: interfaces.cpp:48
chain.h
context.h
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:160
BENCHMARK
BENCHMARK(WalletBalanceDirty)
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
bench.h
DBErrors::LOAD_OK
@ LOAD_OK
WalletBalanceDirty
static void WalletBalanceDirty(benchmark::Bench &bench)
Definition: wallet_balance.cpp:49
CreateMockWalletDatabase
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1094
CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:638
if
if(expired !=0)
Definition: validation.cpp:349
importaddress
RPCHelpMan importaddress()
Definition: rpcdump.cpp:222
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))