Bitcoin ABC 0.31.1
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
21static 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 if (wallet.LoadWallet() != DBErrors::LOAD_OK) {
34 assert(false);
35 }
36 }
37
38 auto handler = test_setup->m_node.chain->handleNotifications(
39 {&wallet, [](CWallet *) {}});
40
41 const std::optional<std::string> address_mine{
42 add_mine ? std::optional<std::string>{getnewaddress(config, wallet)}
43 : std::nullopt};
44 if (add_watchonly) {
45 importaddress(wallet, ADDRESS_WATCHONLY);
46 }
47
48 for (int i = 0; i < 100; ++i) {
49 generatetoaddress(config, test_setup->m_node,
50 address_mine.value_or(ADDRESS_WATCHONLY));
51 generatetoaddress(config, test_setup->m_node, ADDRESS_WATCHONLY);
52 }
54
55 // Cache
56 auto bal = GetBalance(wallet);
57
58 bench.run([&] {
59 if (set_dirty) {
60 wallet.MarkDirty();
61 }
62 bal = GetBalance(wallet);
63 if (add_mine) {
64 assert(bal.m_mine_trusted > Amount::zero());
65 }
66 if (add_watchonly) {
67 assert(bal.m_watchonly_trusted > Amount::zero());
68 }
69 });
70}
71
73 WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true,
74 /* add_mine */ true);
75}
77 WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
78 /* add_mine */ true);
79}
81 WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false,
82 /* add_mine */ true);
83}
85 WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true,
86 /* add_mine */ false);
87}
88
RPCHelpMan importaddress()
Definition: backup.cpp:290
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:308
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