Bitcoin ABC  0.29.2
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  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
23 
24  const auto &ADDRESS_WATCHONLY = ADDRESS_ECREG_UNSPENDABLE;
25 
26  const Config &config = GetConfig();
27 
28  CWallet wallet{test_setup->m_node.chain.get(), "",
30  {
31  wallet.SetupLegacyScriptPubKeyMan();
32  bool first_run;
33  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) {
34  assert(false);
35  }
36  }
37 
38  auto handler = test_setup->m_node.chain->handleNotifications(
39  {&wallet, [](CWallet *) {}});
40 
41  const std::optional<std::string> address_mine{
42  add_mine ? std::optional<std::string>{getnewaddress(config, wallet)}
43  : std::nullopt};
44  if (add_watchonly) {
45  importaddress(wallet, ADDRESS_WATCHONLY);
46  }
47 
48  for (int i = 0; i < 100; ++i) {
49  generatetoaddress(config, test_setup->m_node,
50  address_mine.value_or(ADDRESS_WATCHONLY));
51  generatetoaddress(config, test_setup->m_node, ADDRESS_WATCHONLY);
52  }
54 
55  // Cache
56  auto bal = GetBalance(wallet);
57 
58  bench.run([&] {
59  if (set_dirty) {
60  wallet.MarkDirty();
61  }
62  bal = GetBalance(wallet);
63  if (add_mine) {
64  assert(bal.m_mine_trusted > Amount::zero());
65  }
66  if (add_watchonly) {
67  assert(bal.m_watchonly_trusted > Amount::zero());
68  }
69  });
70 }
71 
72 static void WalletBalanceDirty(benchmark::Bench &bench) {
73  WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true,
74  /* add_mine */ true);
75 }
76 static void WalletBalanceClean(benchmark::Bench &bench) {
77  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
78  /* add_mine */ true);
79 }
80 static void WalletBalanceMine(benchmark::Bench &bench) {
81  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false,
82  /* add_mine */ true);
83 }
84 static void WalletBalanceWatch(benchmark::Bench &bench) {
85  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
86  /* add_mine */ false);
87 }
88 
RPCHelpMan importaddress()
Definition: backup.cpp:265
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:300
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:820
static RPCHelpMan getnewaddress()
Definition: rpcwallet.cpp:95
static constexpr Amount zero() noexcept
Definition: amount.h:32
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:1175