Bitcoin Core 31.99.0
P2P Digital Currency
wallet_encrypt.cpp
Go to the documentation of this file.
1// Copyright (c) 2025-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4
5#include <bench/bench.h>
6#include <key_io.h>
7#include <outputtype.h>
8#include <random.h>
11#include <test/util/time.h>
12#include <util/time.h>
13#include <wallet/context.h>
14#include <wallet/test/util.h>
15#include <wallet/wallet.h>
16#include <wallet/walletutil.h>
17
18#include <cassert>
19
20namespace wallet {
21static void WalletEncrypt(benchmark::Bench& bench, unsigned int key_count)
22{
23 auto test_setup = MakeNoLogFileContext<TestingSetup>();
24 FastRandomContext rand{/*fDeterministic=*/true};
25
26 auto password{rand.randbytes(20)};
27 SecureString secure_pass{password.begin(), password.end()};
28
29 WalletContext context;
30 context.args = &test_setup->m_args;
31 context.chain = test_setup->m_node.chain.get();
32
33 uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;
34
35 std::vector<std::pair<WalletDescriptor, FlatSigningProvider>> descs;
36 descs.reserve(key_count);
37 for (unsigned int i = 0; i < key_count; i++) {
38 CKey key = GenerateRandomKey();
40 std::string error;
41 std::vector<std::unique_ptr<Descriptor>> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
42 WalletDescriptor w_desc(std::move(desc.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
43 descs.emplace_back(w_desc, keys);
44 }
45
46 // Setting a mock time is necessary to force default derive iteration count during
47 // wallet encryption.
48 FakeNodeClock clock{1s};
49
50 std::unique_ptr<WalletDatabase> database;
51 std::shared_ptr<CWallet> wallet;
52 bench.batch(key_count).unit("key").setup([&] {
53 if (wallet) {
54 TestUnloadWallet(std::move(wallet));
55 }
56
57 std::unique_ptr<WalletDatabase> database = CreateMockableWalletDatabase();
58 wallet = TestCreateWallet(std::move(database), context, create_flags);
59
60 {
61 LOCK(wallet->cs_wallet);
62 for (auto& [desc, keys] : descs) {
63 Assert(wallet->AddWalletDescriptor(desc, keys, /*label=*/"", /*internal=*/false));
64 }
65 }
66 })
67 .run([&] {
68 wallet->EncryptWallet(secure_pass);
69
70 for (const auto& [_, key] : wallet->mapMasterKeys){
71 assert(key.nDeriveIterations == CMasterKey::DEFAULT_DERIVE_ITERATIONS);
72 }
73 });
74 TestUnloadWallet(std::move(wallet));
75}
76
77constexpr unsigned int KEY_COUNT = 2000;
78
79static void WalletEncryptDescriptors(benchmark::Bench& bench) { WalletEncrypt(bench, /*key_count=*/KEY_COUNT); }
81
82} // namespace wallet
#define Assert(val)
Identity function.
Definition: check.h:116
An encapsulated private key.
Definition: key.h:37
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:54
Fast randomness source.
Definition: random.h:386
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
Definition: random.h:297
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:633
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1316
Bench & unit(char const *unit)
Sets the operation unit.
detail::SetupRunner< SetupOp > setup(SetupOp setupOp)
Configure an untimed setup step per epoch (forces single-iteration epochs).
Definition: nanobench.h:1286
static constexpr unsigned int DEFAULT_DERIVE_ITERATIONS
Default/minimum number of key derivation rounds.
Definition: crypter.h:48
Descriptor with some wallet metadata.
Definition: walletutil.h:64
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:400
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:232
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition: util.cpp:122
constexpr unsigned int KEY_COUNT
static void WalletEncryptDescriptors(benchmark::Bench &bench)
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:99
static void WalletEncrypt(benchmark::Bench &bench, unsigned int key_count)
BENCHMARK(CoinSelection)
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:51
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:53
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:36
interfaces::Chain * chain
Definition: context.h:37
ArgsManager * args
Definition: context.h:39
#define LOCK(cs)
Definition: sync.h:268
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
assert(!tx.IsCoinBase())