Bitcoin ABC  0.24.7
P2P Digital Currency
wallet_balance.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 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 <config.h>
7 #include <interfaces/chain.h>
8 #include <node/context.h>
9 #include <validationinterface.h>
10 #include <wallet/wallet.h>
11 
12 #include <test/util/mining.h>
13 #include <test/util/setup_common.h>
14 #include <test/util/wallet.h>
15 
16 #include <optional>
17 
18 static void WalletBalance(benchmark::Bench &bench, const bool set_dirty,
19  const bool add_watchonly, const bool add_mine) {
20  TestingSetup test_setup{
22  /* extra_args */
23  {
24  "-nodebuglogfile",
25  "-nodebug",
26  },
27  };
28 
29  const auto &ADDRESS_WATCHONLY = ADDRESS_ECREG_UNSPENDABLE;
30 
31  const Config &config = GetConfig();
32 
33  NodeContext node;
34  std::unique_ptr<interfaces::Chain> chain =
35  interfaces::MakeChain(node, config.GetChainParams());
36  CWallet wallet{chain.get(), "", CreateMockWalletDatabase()};
37  {
38  wallet.SetupLegacyScriptPubKeyMan();
39  bool first_run;
40  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) {
41  assert(false);
42  }
43  }
44 
45  auto handler = chain->handleNotifications({&wallet, [](CWallet *) {}});
46 
47  const std::optional<std::string> address_mine{
48  add_mine ? std::optional<std::string>{getnewaddress(config, wallet)}
49  : std::nullopt};
50  if (add_watchonly) {
51  importaddress(wallet, ADDRESS_WATCHONLY);
52  }
53 
54  for (int i = 0; i < 100; ++i) {
55  generatetoaddress(config, test_setup.m_node,
56  address_mine.value_or(ADDRESS_WATCHONLY));
57  generatetoaddress(config, test_setup.m_node, ADDRESS_WATCHONLY);
58  }
60 
61  // Cache
62  auto bal = wallet.GetBalance();
63 
64  bench.run([&] {
65  if (set_dirty) {
66  wallet.MarkDirty();
67  }
68  bal = wallet.GetBalance();
69  if (add_mine) {
70  assert(bal.m_mine_trusted > Amount::zero());
71  }
72  if (add_watchonly) {
73  assert(bal.m_watchonly_trusted > Amount::zero());
74  }
75  });
76 }
77 
78 static void WalletBalanceDirty(benchmark::Bench &bench) {
79  WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true,
80  /* add_mine */ true);
81 }
82 static void WalletBalanceClean(benchmark::Bench &bench) {
83  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
84  /* add_mine */ true);
85 }
86 static void WalletBalanceMine(benchmark::Bench &bench) {
87  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false,
88  /* add_mine */ true);
89 }
90 static void WalletBalanceWatch(benchmark::Bench &bench) {
91  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
92  /* add_mine */ false);
93 }
94 
wallet.h
WalletBalanceClean
static void WalletBalanceClean(benchmark::Bench &bench)
Definition: wallet_balance.cpp:82
WalletBalanceMine
static void WalletBalanceMine(benchmark::Bench &bench)
Definition: wallet_balance.cpp:86
WalletBalanceWatch
static void WalletBalanceWatch(benchmark::Bench &bench)
Definition: wallet_balance.cpp:90
getnewaddress
static RPCHelpMan getnewaddress()
Definition: rpcwallet.cpp:209
validationinterface.h
generatetoaddress
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:302
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:18
chain.h
context.h
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
Config
Definition: config.h:17
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:162
BENCHMARK
BENCHMARK(WalletBalanceDirty)
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:23
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
handler
bool(* handler)(Config &config, const util::Ref &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:773
bench.h
DBErrors::LOAD_OK
@ LOAD_OK
Config::GetChainParams
virtual const CChainParams & GetChainParams() const =0
WalletBalanceDirty
static void WalletBalanceDirty(benchmark::Bench &bench)
Definition: wallet_balance.cpp:78
CreateMockWalletDatabase
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1169
CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:678
interfaces::MakeChain
std::unique_ptr< Chain > MakeChain(NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: chain.cpp:491
config.h
importaddress
RPCHelpMan importaddress()
Definition: rpcdump.cpp:256
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
GetConfig
const Config & GetConfig()
Definition: config.cpp:34