Bitcoin Core  0.19.99
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 <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  const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
18 
19  NodeContext node;
20  std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain(node);
21  CWallet wallet{chain.get(), WalletLocation(), WalletDatabase::CreateMock()};
22  {
23  wallet.SetupLegacyScriptPubKeyMan();
24  bool first_run;
25  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) assert(false);
26  }
27  auto handler = chain->handleNotifications({ &wallet, [](CWallet*) {} });
28 
29  const Optional<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt};
30  if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
31 
32  for (int i = 0; i < 100; ++i) {
33  generatetoaddress(g_testing_setup->m_node, address_mine.get_value_or(ADDRESS_WATCHONLY));
34  generatetoaddress(g_testing_setup->m_node, ADDRESS_WATCHONLY);
35  }
37 
38  auto bal = wallet.GetBalance(); // Cache
39 
40  while (state.KeepRunning()) {
41  if (set_dirty) wallet.MarkDirty();
42  bal = wallet.GetBalance();
43  if (add_mine) assert(bal.m_mine_trusted > 0);
44  if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
45  }
46 }
47 
48 static void WalletBalanceDirty(benchmark::State& state) { WalletBalance(state, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); }
49 static void WalletBalanceClean(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); }
50 static void WalletBalanceMine(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); }
51 static void WalletBalanceWatch(benchmark::State& state) { WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); }
52 
static void WalletBalance(benchmark::State &state, const bool set_dirty, const bool add_watchonly, const bool add_mine)
static std::unique_ptr< BerkeleyDatabase > CreateMock()
Return object for accessing temporary in-memory database.
Definition: db.h:149
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:651
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 void WalletBalanceDirty(benchmark::State &state)
bool KeepRunning()
Definition: bench.h:72
static auto & nullopt
Substitute for C++17 std::nullopt.
Definition: optional.h:24
static UniValue generatetoaddress(const JSONRPCRequest &request)
Definition: mining.cpp:192
static void WalletBalanceClean(benchmark::State &state)
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: chain.cpp:386
const RegTestingSetup * g_testing_setup
A pointer to the current testing setup.
Definition: bench.cpp:18
NodeContext struct containing references to chain state and connection state.
Definition: context.h:31
static void WalletBalanceMine(benchmark::State &state)
BENCHMARK(WalletBalanceDirty, 2500)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:609
UniValue importaddress(const JSONRPCRequest &request)
Definition: rpcdump.cpp:222
The WalletLocation class provides wallet information.
Definition: walletutil.h:67
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
static UniValue getnewaddress(const JSONRPCRequest &request)
Definition: rpcwallet.cpp:190