Bitcoin ABC  0.22.13
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 <interfaces/chain.h>
8 #include <node/context.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,
16  const bool add_watchonly, const bool add_mine) {
17  TestingSetup test_setup{
19  /* extra_args */
20  {
21  "-nodebuglogfile",
22  "-nodebug",
23  },
24  };
25 
26  const auto &ADDRESS_WATCHONLY = ADDRESS_BCHREG_UNSPENDABLE;
27 
28  const Config &config = GetConfig();
29 
30  NodeContext node;
31  std::unique_ptr<interfaces::Chain> chain =
32  interfaces::MakeChain(node, config.GetChainParams());
33  CWallet wallet{chain.get(), WalletLocation(), WalletDatabase::CreateMock()};
34  {
35  wallet.SetupLegacyScriptPubKeyMan();
36  bool first_run;
37  if (wallet.LoadWallet(first_run) != DBErrors::LOAD_OK) {
38  assert(false);
39  }
40  }
41 
42  auto handler = chain->handleNotifications({&wallet, [](CWallet *) {}});
43 
44  const std::optional<std::string> address_mine{
45  add_mine ? std::optional<std::string>{getnewaddress(config, wallet)}
46  : std::nullopt};
47  if (add_watchonly) {
48  importaddress(wallet, ADDRESS_WATCHONLY);
49  }
50 
51  for (int i = 0; i < 100; ++i) {
52  generatetoaddress(config, test_setup.m_node,
53  address_mine.value_or(ADDRESS_WATCHONLY));
54  generatetoaddress(config, test_setup.m_node, ADDRESS_WATCHONLY);
55  }
57 
58  // Cache
59  auto bal = wallet.GetBalance();
60 
61  while (state.KeepRunning()) {
62  if (set_dirty) {
63  wallet.MarkDirty();
64  }
65  bal = wallet.GetBalance();
66  if (add_mine) {
67  assert(bal.m_mine_trusted > Amount::zero());
68  }
69  if (add_watchonly) {
70  assert(bal.m_watchonly_trusted > Amount::zero());
71  }
72  }
73 }
74 
75 static void WalletBalanceDirty(benchmark::State &state) {
76  WalletBalance(state, /* set_dirty */ true, /* add_watchonly */ true,
77  /* add_mine */ true);
78 }
79 static void WalletBalanceClean(benchmark::State &state) {
80  WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true,
81  /* add_mine */ true);
82 }
83 static void WalletBalanceMine(benchmark::State &state) {
84  WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ false,
85  /* add_mine */ true);
86 }
87 static void WalletBalanceWatch(benchmark::State &state) {
88  WalletBalance(state, /* set_dirty */ false, /* add_watchonly */ true,
89  /* add_mine */ false);
90 }
91 
static void WalletBalance(benchmark::State &state, const bool set_dirty, const bool add_watchonly, const bool add_mine)
static UniValue getnewaddress(const Config &config, const JSONRPCRequest &request)
Definition: rpcwallet.cpp:210
static std::unique_ptr< BerkeleyDatabase > CreateMock()
Return object for accessing temporary in-memory database.
Definition: bdb.h:142
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static constexpr Amount zero()
Definition: amount.h:35
static UniValue generatetoaddress(const Config &config, const JSONRPCRequest &request)
Definition: mining.cpp:280
static void WalletBalanceWatch(benchmark::State &state)
UniValue importaddress(const Config &config, const JSONRPCRequest &request)
Definition: rpcdump.cpp:249
static const std::string REGTEST
static void WalletBalanceDirty(benchmark::State &state)
bool KeepRunning()
Definition: bench.h:71
static void WalletBalanceClean(benchmark::State &state)
virtual const CChainParams & GetChainParams() const =0
Definition: config.h:19
NodeContext struct containing references to chain state and connection state.
Definition: context.h:35
const Config & GetConfig()
Definition: config.cpp:34
static void WalletBalanceMine(benchmark::State &state)
bool(* handler)(Config &config, const util::Ref &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:756
BENCHMARK(WalletBalanceDirty, 2500)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:679
The WalletLocation class provides wallet information.
Definition: walletutil.h:80
std::unique_ptr< Chain > MakeChain(NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: chain.cpp:448