Bitcoin ABC  0.26.3
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 <consensus/amount.h>
8 #include <interfaces/chain.h>
9 #include <node/context.h>
10 #include <validationinterface.h>
11 #include <wallet/receive.h>
12 #include <wallet/wallet.h>
13 
14 #include <test/util/mining.h>
15 #include <test/util/setup_common.h>
16 #include <test/util/wallet.h>
17 
18 #include <optional>
19 
20 static void WalletBalance(benchmark::Bench &bench, const bool set_dirty,
21  const bool add_watchonly, const bool add_mine) {
22  TestingSetup test_setup{
24  /* extra_args */
25  {
26  "-nodebuglogfile",
27  "-nodebug",
28  },
29  };
30 
31  const auto &ADDRESS_WATCHONLY = ADDRESS_ECREG_UNSPENDABLE;
32 
33  const Config &config = GetConfig();
34 
35  CWallet wallet{test_setup.m_node.chain.get(), "",
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 = test_setup.m_node.chain->handleNotifications(
46  {&wallet, [](CWallet *) {}});
47 
48  const std::optional<std::string> address_mine{
49  add_mine ? std::optional<std::string>{getnewaddress(config, wallet)}
50  : std::nullopt};
51  if (add_watchonly) {
52  importaddress(wallet, ADDRESS_WATCHONLY);
53  }
54 
55  for (int i = 0; i < 100; ++i) {
56  generatetoaddress(config, test_setup.m_node,
57  address_mine.value_or(ADDRESS_WATCHONLY));
58  generatetoaddress(config, test_setup.m_node, ADDRESS_WATCHONLY);
59  }
61 
62  // Cache
63  auto bal = GetBalance(wallet);
64 
65  bench.run([&] {
66  if (set_dirty) {
67  wallet.MarkDirty();
68  }
69  bal = GetBalance(wallet);
70  if (add_mine) {
71  assert(bal.m_mine_trusted > Amount::zero());
72  }
73  if (add_watchonly) {
74  assert(bal.m_watchonly_trusted > Amount::zero());
75  }
76  });
77 }
78 
79 static void WalletBalanceDirty(benchmark::Bench &bench) {
80  WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true,
81  /* add_mine */ true);
82 }
83 static void WalletBalanceClean(benchmark::Bench &bench) {
84  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
85  /* add_mine */ true);
86 }
87 static void WalletBalanceMine(benchmark::Bench &bench) {
88  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false,
89  /* add_mine */ true);
90 }
91 static void WalletBalanceWatch(benchmark::Bench &bench) {
92  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
93  /* add_mine */ false);
94 }
95 
RPCHelpMan importaddress()
Definition: backup.cpp:260
static const std::string REGTEST
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:253
Definition: config.h:17
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
const Config & GetConfig()
Definition: config.cpp:34
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:310
Balance GetBalance(const CWallet &wallet, const int min_depth, bool avoid_reuse)
Definition: receive.cpp:384
bool(* handler)(Config &config, const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:822
static RPCHelpMan getnewaddress()
Definition: rpcwallet.cpp:94
static constexpr Amount zero()
Definition: amount.h:42
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 WalletBalanceMine(benchmark::Bench &bench)
BENCHMARK(WalletBalanceDirty)
static void WalletBalanceWatch(benchmark::Bench &bench)
static void WalletBalanceDirty(benchmark::Bench &bench)
static void WalletBalance(benchmark::Bench &bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1169