Bitcoin Core  27.99.0
P2P Digital Currency
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 #if defined(HAVE_CONFIG_H)
7 #endif // HAVE_CONFIG_H
8 #include <bench/bench.h>
9 #include <interfaces/chain.h>
10 #include <key.h>
11 #include <key_io.h>
12 #include <node/context.h>
13 #include <test/util/setup_common.h>
14 #include <util/translation.h>
15 #include <validationinterface.h>
16 #include <wallet/context.h>
17 #include <wallet/test/util.h>
18 #include <wallet/wallet.h>
19 #include <wallet/walletutil.h>
20 
21 namespace wallet {
22 static void WalletIsMine(benchmark::Bench& bench, bool legacy_wallet, int num_combo = 0)
23 {
24  const auto test_setup = MakeNoLogFileContext<TestingSetup>();
25 
26  WalletContext context;
27  context.args = &test_setup->m_args;
28  context.chain = test_setup->m_node.chain.get();
29 
30  // Setup the wallet
31  // Loading the wallet will also create it
32  uint64_t create_flags = 0;
33  if (!legacy_wallet) {
34  create_flags = WALLET_FLAG_DESCRIPTORS;
35  }
36  auto database = CreateMockableWalletDatabase();
37  auto wallet = TestLoadWallet(std::move(database), context, create_flags);
38 
39  // For a descriptor wallet, fill with num_combo combo descriptors with random keys
40  // This benchmarks a non-HD wallet migrated to descriptors
41  if (!legacy_wallet && num_combo > 0) {
42  LOCK(wallet->cs_wallet);
43  for (int i = 0; i < num_combo; ++i) {
44  CKey key;
45  key.MakeNewKey(/*fCompressed=*/true);
47  std::string error;
48  std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
49  WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
50  auto spkm = wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false);
51  assert(spkm);
52  }
53  }
54 
56 
57  bench.run([&] {
58  LOCK(wallet->cs_wallet);
59  isminetype mine = wallet->IsMine(script);
60  assert(mine == ISMINE_NO);
61  });
62 
63  TestUnloadWallet(std::move(wallet));
64 }
65 
66 #ifdef USE_BDB
67 static void WalletIsMineLegacy(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/true); }
68 BENCHMARK(WalletIsMineLegacy, benchmark::PriorityLevel::LOW);
69 #endif
70 
71 #ifdef USE_SQLITE
72 static void WalletIsMineDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false); }
73 static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*legacy_wallet=*/false, /*num_combo=*/2000); }
74 BENCHMARK(WalletIsMineDescriptors, benchmark::PriorityLevel::LOW);
75 BENCHMARK(WalletIsMineMigratedDescriptors, benchmark::PriorityLevel::LOW);
76 #endif
77 } // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
An encapsulated private key.
Definition: key.h:33
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:318
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:292
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:227
@ LOW
Definition: bench.h:46
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:39
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:190
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition: types.h:40
@ ISMINE_NO
Definition: types.h:41
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:74
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())