wallet_balance.cpp
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> 7 #include <kernel/chainparams.h> 8 #include <primitives/block.h> 9 #include <primitives/transaction.h> 10 #include <sync.h> 11 #include <test/util/mining.h> 12 #include <test/util/setup_common.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 27 namespace wallet { 28 static 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 63 static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/true, /*add_mine=*/true); } 64 static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/true); } 65 static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /*set_dirty=*/false, /*add_mine=*/false); } 66 67 BENCHMARK(WalletBalanceDirty); 68 BENCHMARK(WalletBalanceClean); 69 BENCHMARK(WalletBalanceWatch); 70 } // namespace wallet