Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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>
9#include <primitives/block.h>
11#include <sync.h>
12#include <test/util/mining.h>
14#include <test/util/time.h>
15#include <uint256.h>
16#include <util/check.h>
17#include <validation.h>
18#include <wallet/receive.h>
19#include <wallet/sqlite.h>
20#include <wallet/test/util.h>
21#include <wallet/wallet.h>
22#include <wallet/walletutil.h>
23
24#include <memory>
25#include <optional>
26#include <string>
27
28namespace wallet {
29static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
30{
32
34
35 // Set clock to genesis block, so the descriptors/keys creation time don't interfere with the blocks scanning process.
36 // The reason is 'generatetoaddress', which creates a chain with deterministic timestamps in the past.
37 FakeNodeClock clock{test_setup->m_node.chainman->GetParams().GenesisBlock().Time()};
38 CWallet wallet{test_setup->m_node.chain.get(), "", MakeInMemoryWalletDatabase()};
39 {
40 LOCK(wallet.cs_wallet);
41 wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
42 wallet.SetupDescriptorScriptPubKeyMans();
43 }
44 auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
45
46 const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
47
48 for (int i = 0; i < 100; ++i) {
51 }
52 // Calls SyncWithValidationInterfaceQueue
53 wallet.chain().waitForNotificationsIfTipChanged(uint256::ZERO);
54
55 auto bal = GetBalance(wallet); // Cache
56
57 bench.setup([&] {
58 if (set_dirty) wallet.MarkDirty();
59 })
60 .run([&] {
63 assert(add_mine == (bal.m_mine_trusted > 0));
64 });
65}
66
67static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/true, /*add_mine=*/true); }
68static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
69static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/false); }
70
74} // namespace wallet
#define BENCHMARK(n)
Definition bench.h:69
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:649
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:318
void doNotOptimizeAway(Arg &&arg)
Makes sure none of the given arguments are optimized away by the compiler.
Definition nanobench.h:1353
std::unique_ptr< WalletDatabase > MakeInMemoryWalletDatabase()
Definition sqlite.cpp:729
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition util.h:36
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)
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:1198
static RPCMethod generatetoaddress()
Definition mining.cpp:294
#define LOCK(cs)
Definition sync.h:268
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
FakeNodeClock clock