Bitcoin Core 31.99.0
P2P Digital Currency
wallet_balance.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-present 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>
8#include <primitives/block.h>
10#include <sync.h>
11#include <test/util/mining.h>
13#include <test/util/time.h>
14#include <uint256.h>
15#include <util/time.h>
16#include <validation.h>
17#include <wallet/receive.h>
18#include <wallet/test/util.h>
19#include <wallet/wallet.h>
20#include <wallet/walletutil.h>
21
22#include <cassert>
23#include <memory>
24#include <optional>
25#include <string>
26
27namespace wallet {
28static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
29{
30 const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
31
32 const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
33
34 // Set clock to genesis block, so the descriptors/keys creation time don't interfere with the blocks scanning process.
35 // The reason is 'generatetoaddress', which creates a chain with deterministic timestamps in the past.
36 NodeClockContext clock_ctx{test_setup->m_node.chainman->GetParams().GenesisBlock().Time()};
37 CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockableWalletDatabase()};
38 {
39 LOCK(wallet.cs_wallet);
40 wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
41 wallet.SetupDescriptorScriptPubKeyMans();
42 }
43 auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
44
45 const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
46
47 for (int i = 0; i < 100; ++i) {
48 generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
49 generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);
50 }
51 // Calls SyncWithValidationInterfaceQueue
52 wallet.chain().waitForNotificationsIfTipChanged(uint256::ZERO);
53
54 auto bal = GetBalance(wallet); // Cache
55
56 bench.run([&] {
57 if (set_dirty) wallet.MarkDirty();
58 bal = GetBalance(wallet);
59 if (add_mine) assert(bal.m_mine_trusted > 0);
60 });
61}
62
63static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/true, /*add_mine=*/true); }
64static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
65static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/false); }
66
70} // namespace wallet
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:633
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1292
static const uint256 ZERO
Definition: uint256.h:204
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:309
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition: util.cpp:144
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:32
static void WalletBalanceDirty(benchmark::Bench &bench)
static void WalletBalance(benchmark::Bench &bench, const bool set_dirty, const bool add_mine)
static void WalletBalanceWatch(benchmark::Bench &bench)
BENCHMARK(CoinSelection)
Balance GetBalance(const CWallet &wallet, const int min_depth, bool avoid_reuse, bool include_nonmempool)
Definition: receive.cpp:245
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
RPCMethod getnewaddress()
Definition: addresses.cpp:21
static void WalletBalanceClean(benchmark::Bench &bench)
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1143
static RPCMethod generatetoaddress()
Definition: mining.cpp:264
#define LOCK(cs)
Definition: sync.h:268
assert(!tx.IsCoinBase())