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) 2025-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://www.opensource.org/licenses/mit-license.php.5#include <bench/bench.h>6#include <key.h>7#include <key_io.h>8#include <random.h>9#include <script/descriptor.h>10#include <script/signingprovider.h>11#include <support/allocators/secure.h>12#include <sync.h>13#include <test/util/setup_common.h>14#include <test/util/time.h>15#include <util/check.h>16#include <util/time.h>17#include <wallet/context.h>18#include <wallet/crypter.h>19#include <wallet/db.h>20#include <wallet/sqlite.h>21#include <wallet/test/util.h>22#include <wallet/wallet.h>23#include <wallet/walletutil.h>25#include <cstdint>26#include <memory>27#include <string>28#include <utility>29#include <vector>31namespace wallet {32static void WalletEncrypt(benchmark::Bench& bench, unsigned int key_count)33{34auto test_setup = MakeNoLogFileContext<TestingSetup>();35FastRandomContext rand{/*fDeterministic=*/true};37auto password{rand.randbytes(20)};38SecureString secure_pass{password.begin(), password.end()};40WalletContext context;41context.args = &test_setup->m_args;42context.chain = test_setup->m_node.chain.get();44uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;46std::vector<std::pair<WalletDescriptor, FlatSigningProvider>> descs;47descs.reserve(key_count);48for (unsigned int i = 0; i < key_count; i++) {49CKey key = GenerateRandomKey();50FlatSigningProvider keys;51std::string error;52std::vector<std::unique_ptr<Descriptor>> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);53WalletDescriptor w_desc(std::move(desc.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);54descs.emplace_back(w_desc, keys);55}57// Setting a mock time is necessary to force default derive iteration count during58// wallet encryption.59FakeNodeClock clock{1s};61std::unique_ptr<WalletDatabase> database;62std::shared_ptr<CWallet> wallet;63bench.batch(key_count).unit("key").setup([&] {64if (wallet) {65TestUnloadWallet(std::move(wallet));66}68std::unique_ptr<WalletDatabase> database = MakeInMemoryWalletDatabase();69wallet = TestCreateWallet(std::move(database), context, create_flags);71{72LOCK(wallet->cs_wallet);73for (auto& [desc, keys] : descs) {74Assert(wallet->AddWalletDescriptor(desc, keys, /*label=*/"", /*internal=*/false));75}76}77})78.run([&] {79wallet->EncryptWallet(secure_pass);81for (const auto& [_, key] : wallet->mapMasterKeys){82assert(key.nDeriveIterations == CMasterKey::DEFAULT_DERIVE_ITERATIONS);83}84});85TestUnloadWallet(std::move(wallet));86}88constexpr unsigned int KEY_COUNT = 2000;90static void WalletEncryptDescriptors(benchmark::Bench& bench) { WalletEncrypt(bench, /*key_count=*/KEY_COUNT); }91BENCHMARK(WalletEncryptDescriptors);93} // 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.