Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2012-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <bench/bench.h>6#include <interfaces/chain.h>7#include <interfaces/handler.h>8#include <kernel/chainparams.h>9#include <primitives/block.h>10#include <primitives/transaction.h>11#include <sync.h>12#include <test/util/mining.h>13#include <test/util/setup_common.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>24#include <memory>25#include <optional>26#include <string>28namespace wallet {29static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)30{31const auto test_setup = MakeNoLogFileContext<const TestingSetup>();33const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;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.37FakeNodeClock clock{test_setup->m_node.chainman->GetParams().GenesisBlock().Time()};38CWallet wallet{test_setup->m_node.chain.get(), "", MakeInMemoryWalletDatabase()};39{40LOCK(wallet.cs_wallet);41wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);42wallet.SetupDescriptorScriptPubKeyMans();43}44auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});46const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};48for (int i = 0; i < 100; ++i) {49generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));50generatetoaddress(test_setup->m_node, ADDRESS_WATCHONLY);51}52// Calls SyncWithValidationInterfaceQueue53wallet.chain().waitForNotificationsIfTipChanged(uint256::ZERO);55auto bal = GetBalance(wallet); // Cache57bench.setup([&] {58if (set_dirty) wallet.MarkDirty();59})60.run([&] {61bal = GetBalance(wallet);62ankerl::nanobench::doNotOptimizeAway(bal);63assert(add_mine == (bal.m_mine_trusted > 0));64});65}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); }71BENCHMARK(WalletBalanceDirty);72BENCHMARK(WalletBalanceClean);73BENCHMARK(WalletBalanceWatch);74} // namespace wallet
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.