Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
wallet_ismine.cpp
Go to the documentation of this file.
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 <config/bitcoin-config.h> // IWYU pragma: keep
6#include <bench/bench.h>
7#include <interfaces/chain.h>
8#include <key.h>
9#include <key_io.h>
10#include <node/context.h>
12#include <util/translation.h>
13#include <validationinterface.h>
14#include <wallet/context.h>
15#include <wallet/test/util.h>
16#include <wallet/wallet.h>
17#include <wallet/walletutil.h>
18
19namespace wallet {
20static void WalletIsMine(benchmark::Bench& bench, bool legacy_wallet, int num_combo = 0)
21{
22 const auto test_setup = MakeNoLogFileContext<TestingSetup>();
23
24 WalletContext context;
25 context.args = &test_setup->m_args;
26 context.chain = test_setup->m_node.chain.get();
27
28 // Setup the wallet
29 // Loading the wallet will also create it
30 uint64_t create_flags = 0;
31 if (!legacy_wallet) {
32 create_flags = WALLET_FLAG_DESCRIPTORS;
33 }
34 auto database = CreateMockableWalletDatabase();
35 auto wallet = TestLoadWallet(std::move(database), context, create_flags);
36
37 // For a descriptor wallet, fill with num_combo combo descriptors with random keys
38 // This benchmarks a non-HD wallet migrated to descriptors
39 if (!legacy_wallet && num_combo > 0) {
40 LOCK(wallet->cs_wallet);
41 for (int i = 0; i < num_combo; ++i) {
42 CKey key;
43 key.MakeNewKey(/*fCompressed=*/true);
45 std::string error;
46 std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
47 WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
48 auto spkm = wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false);
49 assert(spkm);
50 }
51 }
52
54
55 bench.run([&] {
56 LOCK(wallet->cs_wallet);
57 isminetype mine = wallet->IsMine(script);
58 assert(mine == ISMINE_NO);
59 });
60
61 TestUnloadWallet(std::move(wallet));
62}
63
64#ifdef USE_BDB
65static void WalletIsMineLegacy(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/true); }
66BENCHMARK(WalletIsMineLegacy, benchmark::PriorityLevel::LOW);
67#endif
68
69#ifdef USE_SQLITE
70static void WalletIsMineDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false); }
71static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false, /*num_combo=*/2000); }
72BENCHMARK(WalletIsMineDescriptors, benchmark::PriorityLevel::LOW);
73BENCHMARK(WalletIsMineMigratedDescriptors, benchmark::PriorityLevel::LOW);
74#endif
75} // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
#define BENCHMARK(n, priority_level)
Definition bench.h:79
An encapsulated private key.
Definition key.h:35
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition key.cpp:161
Serialized script, used inside transaction inputs and outputs.
Definition script.h:414
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition nanobench.h:1234
Descriptor with some wallet metadata.
Definition walletutil.h:85
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition client.cpp:321
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition key_io.cpp:296
std::string EncodeSecret(const CKey &key)
Definition key_io.cpp:231
@ LOW
Definition bench.h:46
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition util.h:37
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition util.cpp:49
static void WalletIsMine(benchmark::Bench &bench, bool legacy_wallet, int num_combo=0)
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition util.cpp:72
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition util.cpp:185
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition types.h:41
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition walletutil.h:74
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.
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:257
assert(!tx.IsCoinBase())