Bitcoin Core  29.1.0
P2P Digital Currency
wallet_migration.cpp
Go to the documentation of this file.
1 // Copyright (c) 2024 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #include <bitcoin-build-config.h> // IWYU pragma: keep
6 
7 #include <bench/bench.h>
8 #include <kernel/chain.h>
9 #include <interfaces/chain.h>
10 #include <node/context.h>
11 #include <test/util/mining.h>
12 #include <test/util/setup_common.h>
13 #include <wallet/test/util.h>
14 #include <wallet/context.h>
15 #include <wallet/receive.h>
16 #include <wallet/wallet.h>
17 
18 #include <optional>
19 
20 #if defined(USE_SQLITE) // only enable benchmark when sqlite is enabled
21 
22 namespace wallet{
23 
24 static void WalletMigration(benchmark::Bench& bench)
25 {
26  const auto test_setup = MakeNoLogFileContext<TestingSetup>();
27 
28  WalletContext context;
29  context.args = &test_setup->m_args;
30  context.chain = test_setup->m_node.chain.get();
31 
32  // Number of imported watch only addresses
33  int NUM_WATCH_ONLY_ADDR = 20;
34 
35  // Setup legacy wallet
36  std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(test_setup->m_node.chain.get(), "", CreateMockableWalletDatabase());
37  wallet->chainStateFlushed(ChainstateRole::NORMAL, CBlockLocator{});
38  LegacyDataSPKM* legacy_spkm = wallet->GetOrCreateLegacyDataSPKM();
39 
40  // Add watch-only addresses
41  std::vector<CScript> scripts_watch_only;
42  for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
43  CKey key = GenerateRandomKey();
44  LOCK(wallet->cs_wallet);
45  const auto& dest = GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY);
46  const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(dest));
47  assert(legacy_spkm->LoadWatchOnly(script));
48  assert(wallet->SetAddressBook(dest, strprintf("watch_%d", w), /*purpose=*/std::nullopt));
49  }
50 
51  // Generate transactions and local addresses
52  for (int j = 0; j < 500; ++j) {
53  CKey key = GenerateRandomKey();
54  CPubKey pubkey = key.GetPubKey();
55  // Load key, scripts and create address book record
56  Assert(legacy_spkm->LoadKey(key, pubkey));
57  CTxDestination dest{PKHash(pubkey)};
58  Assert(wallet->SetAddressBook(dest, strprintf("legacy_%d", j), /*purpose=*/std::nullopt));
59 
61  mtx.vout.emplace_back(COIN, GetScriptForDestination(dest));
62  mtx.vout.emplace_back(COIN, scripts_watch_only.at(j % NUM_WATCH_ONLY_ADDR));
63  mtx.vin.resize(2);
64  wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{}, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, /*rescanning_old_block=*/true);
65  }
66 
67  bench.epochs(/*numEpochs=*/1).run([&context, &wallet] {
68  util::Result<MigrationResult> res = MigrateLegacyToDescriptor(std::move(wallet), /*passphrase=*/"", context, /*was_loaded=*/false);
69  assert(res);
70  assert(res->wallet);
71  assert(res->watchonly_wallet);
72  });
73 }
74 
76 
77 } // namespace wallet
78 
79 #endif // end USE_SQLITE && USE_BDB
assert(!tx.IsCoinBase())
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:123
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
#define LOCK(cs)
Definition: sync.h:257
An encapsulated public key.
Definition: pubkey.h:33
std::vector< CTxOut > vout
Definition: transaction.h:380
util::Result< MigrationResult > MigrateLegacyToDescriptor(const std::string &wallet_name, const SecureString &passphrase, WalletContext &context)
Do all steps to migrate a legacy wallet to a descriptor wallet.
Definition: wallet.cpp:4413
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:186
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
Definition: outputtype.cpp:50
A mutable version of CTransaction.
Definition: transaction.h:377
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
An encapsulated private key.
Definition: key.h:34
#define Assert(val)
Identity function.
Definition: check.h:85
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15