Bitcoin Core  27.99.0
P2P Digital Currency
wallet_balance.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 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/chainstate.h>
8 #include <node/context.h>
9 #include <test/util/mining.h>
10 #include <test/util/setup_common.h>
11 #include <wallet/test/util.h>
12 #include <validationinterface.h>
13 #include <wallet/receive.h>
14 #include <wallet/wallet.h>
15 
16 #include <optional>
17 
18 namespace wallet {
19 static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
20 {
21  const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
22 
23  const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
24 
25  // Set clock to genesis block, so the descriptors/keys creation time don't interfere with the blocks scanning process.
26  // The reason is 'generatetoaddress', which creates a chain with deterministic timestamps in the past.
27  SetMockTime(test_setup->m_node.chainman->GetParams().GenesisBlock().nTime);
28  CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockableWalletDatabase()};
29  {
30  LOCK(wallet.cs_wallet);
31  wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
32  wallet.SetupDescriptorScriptPubKeyMans();
33  }
34  auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
35 
36  const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
37 
38  for (int i = 0; i < 100; ++i) {
39  generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
40  generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);
41  }
42  // Calls SyncWithValidationInterfaceQueue
43  wallet.chain().waitForNotificationsIfTipChanged(uint256::ZERO);
44 
45  auto bal = GetBalance(wallet); // Cache
46 
47  bench.run([&] {
48  if (set_dirty) wallet.MarkDirty();
49  bal = GetBalance(wallet);
50  if (add_mine) assert(bal.m_mine_trusted > 0);
51  });
52 }
53 
54 static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/true, /*add_mine=*/true); }
55 static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
56 static void WalletBalanceMine(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); }
57 static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/false); }
58 
63 } // namespace wallet
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
static const uint256 ZERO
Definition: uint256.h:111
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:301
@ HIGH
Definition: bench.h:47
Balance GetBalance(const CWallet &wallet, const int min_depth, bool avoid_reuse)
Definition: receive.cpp:293
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:39
static void WalletBalanceDirty(benchmark::Bench &bench)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:190
static void WalletBalance(benchmark::Bench &bench, const bool set_dirty, const bool add_mine)
static void WalletBalanceWatch(benchmark::Bench &bench)
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:74
RPCHelpMan getnewaddress()
Definition: addresses.cpp:23
static void WalletBalanceMine(benchmark::Bench &bench)
static void WalletBalanceClean(benchmark::Bench &bench)
std::shared_ptr< CWallet > wallet
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1008
static RPCHelpMan generatetoaddress()
Definition: mining.cpp:257
#define LOCK(cs)
Definition: sync.h:257
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:32
assert(!tx.IsCoinBase())