Bitcoin Core  31.0.0
P2P Digital Currency
wallet_ismine.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022-present 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 <addresstype.h>
6 #include <bench/bench.h>
7 #include <key.h>
8 #include <key_io.h>
9 #include <script/descriptor.h>
10 #include <script/script.h>
11 #include <script/signingprovider.h>
12 #include <sync.h>
13 #include <test/util/setup_common.h>
14 #include <wallet/context.h>
15 #include <wallet/db.h>
16 #include <wallet/test/util.h>
17 #include <wallet/wallet.h>
18 #include <wallet/walletutil.h>
19 
20 #include <cassert>
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 
26 namespace wallet {
27 static void WalletIsMine(benchmark::Bench& bench, int num_combo = 0)
28 {
29  const auto test_setup = MakeNoLogFileContext<TestingSetup>();
30 
31  WalletContext context;
32  context.args = &test_setup->m_args;
33  context.chain = test_setup->m_node.chain.get();
34 
35  // Setup the wallet
36  // Loading the wallet will also create it
37  uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;
38  auto database = CreateMockableWalletDatabase();
39  auto wallet = TestCreateWallet(std::move(database), context, create_flags);
40 
41  // For a descriptor wallet, fill with num_combo combo descriptors with random keys
42  // This benchmarks a non-HD wallet migrated to descriptors
43  if (num_combo > 0) {
44  LOCK(wallet->cs_wallet);
45  for (int i = 0; i < num_combo; ++i) {
46  CKey key;
47  key.MakeNewKey(/*fCompressed=*/true);
49  std::string error;
50  std::vector<std::unique_ptr<Descriptor>> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
51  WalletDescriptor w_desc(std::move(desc.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
52  Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false));
53  }
54  }
55 
57 
58  bench.run([&] {
59  LOCK(wallet->cs_wallet);
60  bool mine = wallet->IsMine(script);
61  assert(!mine);
62  });
63 
64  TestUnloadWallet(std::move(wallet));
65 }
66 
67 static void WalletIsMineDescriptors(benchmark::Bench& bench) { WalletIsMine(bench); }
68 static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*num_combo=*/2000); }
71 } // namespace wallet
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:395
assert(!tx.IsCoinBase())
static void WalletIsMineDescriptors(benchmark::Bench &bench)
static void WalletIsMine(benchmark::Bench &bench, int num_combo=0)
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
std::shared_ptr< CWallet > TestCreateWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
#define LOCK(cs)
Definition: sync.h:258
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:162
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
BENCHMARK(WalletBalanceDirty)
Descriptor with some wallet metadata.
Definition: walletutil.h:63
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:211
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:98
WalletContext struct containing references to state shared between CWallet instances, like the reference to the chain interface, and the list of opened wallets.
Definition: context.h:36
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:31
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
An encapsulated private key.
Definition: key.h:35
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:299
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:231
ArgsManager * args
Definition: context.h:39
static void WalletIsMineMigratedDescriptors(benchmark::Bench &bench)
#define Assert(val)
Identity function.
Definition: check.h:113