Skip to content

Commit b276825

Browse files
committed
bench: Add a benchmark for ismine
1 parent 0b76874 commit b276825

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Makefile.bench.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ bench_bench_bitcoin_SOURCES += bench/wallet_balance.cpp
8888
bench_bench_bitcoin_SOURCES += bench/wallet_create.cpp
8989
bench_bench_bitcoin_SOURCES += bench/wallet_loading.cpp
9090
bench_bench_bitcoin_SOURCES += bench/wallet_create_tx.cpp
91+
bench_bench_bitcoin_SOURCES += bench/wallet_ismine.cpp
92+
9193
bench_bench_bitcoin_LDADD += $(BDB_LIBS) $(SQLITE_LIBS)
9294
endif
9395

src/bench/wallet_ismine.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
#include <interfaces/chain.h>
7+
#include <key.h>
8+
#include <key_io.h>
9+
#include <node/context.h>
10+
#include <test/util/setup_common.h>
11+
#include <util/translation.h>
12+
#include <validationinterface.h>
13+
#include <wallet/context.h>
14+
#include <wallet/wallet.h>
15+
#include <wallet/walletutil.h>
16+
#include <wallet/test/util.h>
17+
18+
namespace wallet {
19+
static void WalletIsMine(benchmark::Bench& bench, bool legacy_wallet, int num_combo = 0)
20+
{
21+
const auto test_setup = MakeNoLogFileContext<TestingSetup>();
22+
23+
WalletContext context;
24+
context.args = &test_setup->m_args;
25+
context.chain = test_setup->m_node.chain.get();
26+
27+
// Setup the wallet
28+
// Loading the wallet will also create it
29+
uint64_t create_flags = 0;
30+
if (!legacy_wallet) {
31+
create_flags = WALLET_FLAG_DESCRIPTORS;
32+
}
33+
auto database = CreateMockableWalletDatabase();
34+
auto wallet = TestLoadWallet(std::move(database), context, create_flags);
35+
36+
// For a descriptor wallet, fill with num_combo combo descriptors with random keys
37+
// This benchmarks a non-HD wallet migrated to descriptors
38+
if (!legacy_wallet && num_combo > 0) {
39+
LOCK(wallet->cs_wallet);
40+
for (int i = 0; i < num_combo; ++i) {
41+
CKey key;
42+
key.MakeNewKey(/*fCompressed=*/true);
43+
FlatSigningProvider keys;
44+
std::string error;
45+
std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
46+
WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
47+
auto spkm = wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false);
48+
assert(spkm);
49+
}
50+
}
51+
52+
const CScript script = GetScriptForDestination(DecodeDestination(ADDRESS_BCRT1_UNSPENDABLE));
53+
54+
bench.run([&] {
55+
LOCK(wallet->cs_wallet);
56+
isminetype mine = wallet->IsMine(script);
57+
assert(mine == ISMINE_NO);
58+
});
59+
60+
TestUnloadWallet(std::move(wallet));
61+
}
62+
63+
#ifdef USE_BDB
64+
static void WalletIsMineLegacy(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/true); }
65+
BENCHMARK(WalletIsMineLegacy, benchmark::PriorityLevel::LOW);
66+
#endif
67+
68+
#ifdef USE_SQLITE
69+
static void WalletIsMineDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false); }
70+
static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false, /*num_combo=*/2000); }
71+
BENCHMARK(WalletIsMineDescriptors, benchmark::PriorityLevel::LOW);
72+
BENCHMARK(WalletIsMineMigratedDescriptors, benchmark::PriorityLevel::LOW);
73+
#endif
74+
} // namespace wallet

0 commit comments

Comments
 (0)