Bitcoin Core 29.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#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>
12#include <sync.h>
14#include <wallet/context.h>
15#include <wallet/db.h>
16#include <wallet/test/util.h>
17#include <wallet/types.h>
18#include <wallet/wallet.h>
19#include <wallet/walletutil.h>
20
21#include <cassert>
22#include <cstdint>
23#include <memory>
24#include <string>
25#include <utility>
26
27namespace wallet {
28static void WalletIsMine(benchmark::Bench& bench, int num_combo = 0)
29{
30 const auto test_setup = MakeNoLogFileContext<TestingSetup>();
31
32 WalletContext context;
33 context.args = &test_setup->m_args;
34 context.chain = test_setup->m_node.chain.get();
35
36 // Setup the wallet
37 // Loading the wallet will also create it
38 uint64_t create_flags = WALLET_FLAG_DESCRIPTORS;
39 auto database = CreateMockableWalletDatabase();
40 auto wallet = TestLoadWallet(std::move(database), context, create_flags);
41
42 // For a descriptor wallet, fill with num_combo combo descriptors with random keys
43 // This benchmarks a non-HD wallet migrated to descriptors
44 if (num_combo > 0) {
45 LOCK(wallet->cs_wallet);
46 for (int i = 0; i < num_combo; ++i) {
47 CKey key;
48 key.MakeNewKey(/*fCompressed=*/true);
50 std::string error;
51 std::vector<std::unique_ptr<Descriptor>> desc = Parse("combo(" + EncodeSecret(key) + ")", keys, error, /*require_checksum=*/false);
52 WalletDescriptor w_desc(std::move(desc.at(0)), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/0, /*next_index=*/0);
53 auto spk_manager = *Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", /*internal=*/false));
54 assert(spk_manager);
55 }
56 }
57
59
60 bench.run([&] {
61 LOCK(wallet->cs_wallet);
62 isminetype mine = wallet->IsMine(script);
63 assert(mine == ISMINE_NO);
64 });
65
66 TestUnloadWallet(std::move(wallet));
67}
68
70static void WalletIsMineMigratedDescriptors(benchmark::Bench& bench) { WalletIsMine(bench, /*num_combo=*/2000); }
73} // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
#define Assert(val)
Identity function.
Definition: check.h:106
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:415
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:317
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
@ LOW
Definition: bench.h:47
static void WalletIsMineDescriptors(benchmark::Bench &bench)
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
const std::string ADDRESS_BCRT1_UNSPENDABLE
Definition: util.h:31
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:51
static void WalletIsMine(benchmark::Bench &bench, int num_combo=0)
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:74
static void WalletIsMineMigratedDescriptors(benchmark::Bench &bench)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:187
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition: types.h:41
@ ISMINE_NO
Definition: types.h:42
@ 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())
is a home for public enum and struct type definitions that are used by internally by wallet code,...