Files
bitcoin/src/bench/wallet_encrypt.cpp
Pablo Martin 7508ac319d bench: replace CreateMockableWalletDatabase with MakeInMemoryWalletDatabase
Benchmarks don't need mock-specific behaviour (overridden Filename(),
Format(), or the exposed batch-level WriteKey()). Replace
CreateMockableWalletDatabase() with MakeInMemoryWalletDatabase() across
6 call sites in src/bench/ (5 files), using the same in-memory SQLite
path that production code uses.

wallet_migration.cpp is excluded: it calls GetOrCreateLegacyDataSPKM()
which asserts Format() == "sqlite-mock", a signal used to allow legacy
SPKM setup in test/bench contexts. MockableSQLiteDatabase is still
correct there.

For coin_selection.cpp, which had no other dependencies on
wallet/test/util.h, also switch the include to <wallet/sqlite.h>.

Follow-up suggested in #35655.
2026-07-13 01:06:26 -03:00

94 lines
3.1 KiB
C++

// Copyright (c) 2025-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
#include <bench/bench.h>
#include <key.h>
#include <key_io.h>
#include <random.h>
#include <script/descriptor.h>
#include <script/signingprovider.h>
#include <support/allocators/secure.h>
#include <sync.h>
#include <test/util/setup_common.h>
#include <test/util/time.h>
#include <util/check.h>
#include <wallet/context.h>
#include <wallet/crypter.h>
#include <wallet/db.h>
#include <wallet/sqlite.h>
#include <wallet/test/util.h>
#include <wallet/wallet.h>
#include <wallet/walletutil.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace wallet {
static void WalletEncrypt(benchmark::Bench& bench, unsigned int key_count)
{
auto test_setup = MakeNoLogFileContext<TestingSetup>();
FastRandomContext rand{/*fDeterministic=*/true};
auto password{rand.randbytes(20)};
SecureString secure_pass{password.begin(), password.end()};
WalletContext context;
context.args = &test_setup->m_args;
context.chain = test_setup->m_node.chain.get();
uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;
std::vector<std::pair<WalletDescriptor, FlatSigningProvider>> descs;
descs.reserve(key_count);
for (unsigned int i = 0; i < key_count; i++) {
CKey key = GenerateRandomKey();
FlatSigningProvider keys;
std::string error;
std::vector<std::unique_ptr<Descriptor>> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
WalletDescriptor w_desc(std::move(desc.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
descs.emplace_back(w_desc, keys);
}
// Setting a mock time is necessary to force default derive iteration count during
// wallet encryption.
FakeNodeClock clock{1s};
std::unique_ptr<WalletDatabase> database;
std::shared_ptr<CWallet> wallet;
bench.batch(key_count).unit("key").setup([&] {
if (wallet) {
TestUnloadWallet(std::move(wallet));
}
std::unique_ptr<WalletDatabase> database = MakeInMemoryWalletDatabase();
wallet = TestCreateWallet(std::move(database), context, create_flags);
{
LOCK(wallet->cs_wallet);
for (auto& [desc, keys] : descs) {
Assert(wallet->AddWalletDescriptor(desc, keys, /*label=*/"", /*internal=*/false));
}
}
})
.run([&] {
wallet->EncryptWallet(secure_pass);
for (const auto& [_, key] : wallet->mapMasterKeys){
assert(key.nDeriveIterations == CMasterKey::DEFAULT_DERIVE_ITERATIONS);
}
});
TestUnloadWallet(std::move(wallet));
}
constexpr unsigned int KEY_COUNT = 2000;
static void WalletEncryptDescriptors(benchmark::Bench& bench) { WalletEncrypt(bench, /*key_count=*/KEY_COUNT); }
BENCHMARK(WalletEncryptDescriptors);
} // namespace wallet