Bitcoin Core  0.20.99
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::State& state, 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  NodeContext node;
28  std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain(node);
29  CWallet wallet{chain.get(), WalletLocation(), CreateMockWalletDatabase()};
30  {
31  wallet.SetupLegacyScriptPubKeyMan();
32  bool first_run;
33  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
34  }
35  auto handler = chain->handleNotifications({&wallet, [](CWallet*) {}});
36 
37  const Optional<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt};
38  if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
39 
40  for (int i = 0; i < 100; ++i) {
41  generatetoaddress(test_setup.m_node, address_mine.get_value_or(ADDRESS_WATCHONLY));
42  generatetoaddress(test_setup.m_node, ADDRESS_WATCHONLY);
43  }
45 
46  auto bal = wallet.GetBalance(); // Cache
47 
48  while (state.KeepRunning()) {
49  if (set_dirty) wallet.MarkDirty();
50  bal = wallet.GetBalance();
51  if (add_mine) assert(bal.m_mine_trusted > 0);
52  if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
53  }
54 }
55 
56 static void WalletBalanceDirty(benchmark::State& state) { WalletBalance(state, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); }
57 static void WalletBalanceClean(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); }
58 static void WalletBalanceMine(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); }
59 static void WalletBalanceWatch(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); }
60 
static void WalletBalance(benchmark::State &state, const bool set_dirty, const bool add_watchonly, const bool add_mine)
bool(* handler)(const util::Ref &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:649
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static void WalletBalanceWatch(benchmark::State &state)
static const std::string REGTEST
static void WalletBalanceDirty(benchmark::State &state)
bool KeepRunning()
Definition: bench.h:69
static auto & nullopt
Substitute for C++17 std::nullopt.
Definition: optional.h:24
static UniValue generatetoaddress(const JSONRPCRequest &request)
Definition: mining.cpp:239
static void WalletBalanceClean(benchmark::State &state)
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: chain.cpp:386
NodeContext struct containing references to chain state and connection state.
Definition: context.h:34
static void WalletBalanceMine(benchmark::State &state)
std::unique_ptr< BerkeleyDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1028
BENCHMARK(WalletBalanceDirty, 2500)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:618
UniValue importaddress(const JSONRPCRequest &request)
Definition: rpcdump.cpp:220
The WalletLocation class provides wallet information.
Definition: walletutil.h:71
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
static UniValue getnewaddress(const JSONRPCRequest &request)
Definition: rpcwallet.cpp:232