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 <validation.h>
11 #include <validationinterface.h>
12 #include <wallet/receive.h>
13 #include <wallet/wallet.h>
14 
15 #include <test/util/mining.h>
16 #include <test/util/setup_common.h>
17 #include <test/util/wallet.h>
18 
19 #include <optional>
20 
21 static void WalletBalance(benchmark::Bench &bench, const bool set_dirty,
22  const bool add_watchonly, const bool add_mine) {
23  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
24 
25  const auto &ADDRESS_WATCHONLY = ADDRESS_ECREG_UNSPENDABLE;
26 
27  const Config &config = test_setup->m_node.chainman->GetConfig();
28 
29  CWallet wallet{test_setup->m_node.chain.get(), "",
31  {
32  wallet.SetupLegacyScriptPubKeyMan();
33  bool first_run;
34  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) {
35  assert(false);
36  }
37  }
38 
39  auto handler = test_setup->m_node.chain->handleNotifications(
40  {&wallet, [](CWallet *) {}});
41 
42  const std::optional<std::string> address_mine{
43  add_mine ? std::optional<std::string>{getnewaddress(config, wallet)}
44  : std::nullopt};
45  if (add_watchonly) {
46  importaddress(wallet, ADDRESS_WATCHONLY);
47  }
48 
49  for (int i = 0; i < 100; ++i) {
50  generatetoaddress(config, test_setup->m_node,
51  address_mine.value_or(ADDRESS_WATCHONLY));
52  generatetoaddress(config, test_setup->m_node, ADDRESS_WATCHONLY);
53  }
55 
56  // Cache
57  auto bal = GetBalance(wallet);
58 
59  bench.run([&] {
60  if (set_dirty) {
61  wallet.MarkDirty();
62  }
63  bal = GetBalance(wallet);
64  if (add_mine) {
65  assert(bal.m_mine_trusted > Amount::zero());
66  }
67  if (add_watchonly) {
68  assert(bal.m_watchonly_trusted > Amount::zero());
69  }
70  });
71 }
72 
73 static void WalletBalanceDirty(benchmark::Bench &bench) {
74  WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true,
75  /* add_mine */ true);
76 }
77 static void WalletBalanceClean(benchmark::Bench &bench) {
78  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
79  /* add_mine */ true);
80 }
81 static void WalletBalanceMine(benchmark::Bench &bench) {
82  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false,
83  /* add_mine */ true);
84 }
85 static void WalletBalanceWatch(benchmark::Bench &bench) {
86  WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
87  /* add_mine */ false);
88 }
89 
RPCHelpMan importaddress()
Definition: backup.cpp:266
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:254
Definition: config.h:19
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
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:305
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:818
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