Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
bench
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.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
>
24
25
#include <cstdint>
26
#include <memory>
27
#include <string>
28
#include <utility>
29
#include <vector>
30
31
namespace
wallet
{
32
static
void
WalletEncrypt
(
benchmark::Bench
& bench,
unsigned
int
key_count)
33
{
34
auto
test_setup =
MakeNoLogFileContext<TestingSetup>
();
35
FastRandomContext
rand{
/*fDeterministic=*/
true
};
36
37
auto
password{rand.
randbytes
(20)};
38
SecureString
secure_pass{password.begin(), password.end()};
39
40
WalletContext
context;
41
context.
args
= &test_setup->m_args;
42
context.
chain
= test_setup->m_node.chain.get();
43
44
uint64_t create_flags =
WALLET_FLAG_DESCRIPTORS
;
45
46
std::vector<std::pair<WalletDescriptor, FlatSigningProvider>> descs;
47
descs.reserve(key_count);
48
for
(
unsigned
int
i = 0; i < key_count; i++) {
49
CKey
key =
GenerateRandomKey
();
50
FlatSigningProvider
keys
;
51
std::string error;
52
std::vector<std::unique_ptr<Descriptor>> desc =
Parse
(
"combo("
+
EncodeSecret
(key) +
")"
,
keys
, error,
/*require_checksum=*/
false
);
53
WalletDescriptor
w_desc(std::move(desc.at(0)),
/*creation_time=*/
0,
/*range_start=*/
0,
/*range_end=*/
0,
/*next_index=*/
0);
54
descs.emplace_back(w_desc,
keys
);
55
}
56
57
// Setting a mock time is necessary to force default derive iteration count during
58
// wallet encryption.
59
FakeNodeClock
clock{1s};
60
61
std::unique_ptr<WalletDatabase> database;
62
std::shared_ptr<CWallet>
wallet
;
63
bench.
batch
(key_count).
unit
(
"key"
).
setup
([&] {
64
if
(
wallet
) {
65
TestUnloadWallet
(std::move(
wallet
));
66
}
67
68
std::unique_ptr<WalletDatabase> database =
MakeInMemoryWalletDatabase
();
69
wallet
=
TestCreateWallet
(std::move(database), context, create_flags);
70
71
{
72
LOCK
(
wallet
->cs_wallet);
73
for
(
auto
& [desc,
keys
] : descs) {
74
Assert
(
wallet
->AddWalletDescriptor(desc,
keys
,
/*label=*/
""
,
/*internal=*/
false
));
75
}
76
}
77
})
78
.run([&] {
79
wallet
->EncryptWallet(secure_pass);
80
81
for
(
const
auto
& [
_
, key] :
wallet
->mapMasterKeys){
82
assert
(key.nDeriveIterations ==
CMasterKey::DEFAULT_DERIVE_ITERATIONS
);
83
}
84
});
85
TestUnloadWallet
(std::move(
wallet
));
86
}
87
88
constexpr
unsigned
int
KEY_COUNT
= 2000;
89
90
static
void
WalletEncryptDescriptors
(
benchmark::Bench
& bench) {
WalletEncrypt
(bench,
/*key_count=*/
KEY_COUNT
); }
91
BENCHMARK
(
WalletEncryptDescriptors
);
92
93
}
// namespace wallet
bench.h
check.h
Assert
#define Assert(val)
Identity function.
Definition
check.h:116
CKey
An encapsulated private key.
Definition
key.h:40
FakeNodeClock
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition
time.h:54
FastRandomContext
Fast randomness source.
Definition
random.h:386
RandomMixin::randbytes
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
Definition
random.h:297
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:649
ankerl::nanobench::Bench::batch
Bench & batch(T b) noexcept
Sets the batch size.
Definition
nanobench.h:1332
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
ankerl::nanobench::Bench::setup
detail::SetupRunner< SetupOp > setup(SetupOp setupOp)
Configure an untimed setup step per epoch (forces single-iteration epochs).
Definition
nanobench.h:1302
wallet::CMasterKey::DEFAULT_DERIVE_ITERATIONS
static constexpr unsigned int DEFAULT_DERIVE_ITERATIONS
Default/minimum number of key derivation rounds.
Definition
crypter.h:48
wallet::WalletDescriptor
Descriptor with some wallet metadata.
Definition
walletutil.h:64
crypter.h
db.h
GenerateRandomKey
CKey GenerateRandomKey(bool compressed) noexcept
Definition
key.cpp:354
key.h
EncodeSecret
std::string EncodeSecret(const CKey &key)
Definition
key_io.cpp:232
key_io.h
wallet
Definition
coin_selection.cpp:31
wallet::MakeInMemoryWalletDatabase
std::unique_ptr< WalletDatabase > MakeInMemoryWalletDatabase()
Definition
sqlite.cpp:729
wallet::KEY_COUNT
constexpr unsigned int KEY_COUNT
Definition
wallet_encrypt.cpp:88
wallet::WalletEncryptDescriptors
static void WalletEncryptDescriptors(benchmark::Bench &bench)
Definition
wallet_encrypt.cpp:90
wallet::TestUnloadWallet
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition
util.cpp:97
wallet::WalletEncrypt
static void WalletEncrypt(benchmark::Bench &bench, unsigned int key_count)
Definition
wallet_encrypt.cpp:32
wallet::BENCHMARK
BENCHMARK(CoinSelection)
wallet::TestCreateWallet
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition
util.cpp:51
wallet::WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition
walletutil.h:53
Parse
std::vector< std::unique_ptr< Descriptor > > Parse(std::string_view descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Definition
descriptor.cpp:2994
descriptor.h
secure.h
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition
secure.h:53
setup_common.h
MakeNoLogFileContext
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Definition
setup_common.h:241
signingprovider.h
sqlite.h
FlatSigningProvider
Definition
signingprovider.h:235
wallet::WalletContext
Definition
context.h:36
wallet::WalletContext::chain
interfaces::Chain * chain
Definition
context.h:37
wallet::WalletContext::args
ArgsManager * args
Definition
context.h:39
sync.h
LOCK
#define LOCK(cs)
Definition
sync.h:268
keys
std::vector< uint16_t > keys
Definition
dbwrapper.cpp:376
time.h
_
consteval auto _(util::TranslatedLiteral str)
Definition
translation.h:79
assert
assert(!tx.IsCoinBase())
context.h
util.h
wallet.h
walletutil.h
Generated on
for Bitcoin Core by
1.18.0