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 <optional.h>
9 #include <test/util/mining.h>
10 #include <test/util/setup_common.h>
11 #include <test/util/wallet.h>
12 #include <validationinterface.h>
13 #include <wallet/wallet.h>
14 
15 static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
16 {
17  TestingSetup test_setup{
19  /* extra_args */ {
20  "-nodebuglogfile",
21  "-nodebug",
22  },
23  };
24 
25  const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
26 
27  CWallet wallet{test_setup.m_node.chain.get(), "", CreateMockWalletDatabase()};
28  {
29  wallet.SetupLegacyScriptPubKeyMan();
30  bool first_run;
31  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
32  }
33  auto handler = test_setup.m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
34 
35  const Optional<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt};
36  if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
37 
38  for (int i = 0; i < 100; ++i) {
39  generatetoaddress(test_setup.m_node, address_mine.value_or(ADDRESS_WATCHONLY));
40  generatetoaddress(test_setup.m_node, ADDRESS_WATCHONLY);
41  }
43 
44  auto bal = wallet.GetBalance(); // Cache
45 
46  bench.run([&] {
47  if (set_dirty) wallet.MarkDirty();
48  bal = wallet.GetBalance();
49  if (add_mine) assert(bal.m_mine_trusted > 0);
50  if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
51  });
52 }
53 
54 static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); }
55 static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); }
56 static void WalletBalanceMine(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); }
57 static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); }
58 
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1089
bool(* handler)(const util::Ref &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:671
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
assert(!tx.IsCoinBase())
static const std::string REGTEST
static void WalletBalanceDirty(benchmark::Bench &bench)
static auto & nullopt
Substitute for C++17 std::nullopt DEPRECATED use std::nullopt in new code.
Definition: optional.h:18
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:249
BENCHMARK(WalletBalanceDirty)
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
static void WalletBalanceClean(benchmark::Bench &bench)
RPCHelpMan importaddress()
Definition: rpcdump.cpp:224
static void WalletBalanceMine(benchmark::Bench &bench)
static void WalletBalance(benchmark::Bench &bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
static void WalletBalanceWatch(benchmark::Bench &bench)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:622
static RPCHelpMan getnewaddress()
Definition: rpcwallet.cpp:233
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
std::optional< T > Optional
Substitute for C++17 std::optional DEPRECATED use std::optional in new code.
Definition: optional.h:14