Bitcoin Core 29.99.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 <bench/bench.h>
6#include <kernel/chain.h>
7#include <interfaces/chain.h>
8#include <node/context.h>
9#include <test/util/mining.h>
11#include <wallet/test/util.h>
12#include <wallet/context.h>
13#include <wallet/receive.h>
14#include <wallet/wallet.h>
15
16#include <optional>
17
18namespace wallet{
19
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 // Number of imported watch only addresses
29 int NUM_WATCH_ONLY_ADDR = 20;
30
31 // Setup legacy wallet
32 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(test_setup->m_node.chain.get(), "", CreateMockableWalletDatabase());
33 wallet->chainStateFlushed(ChainstateRole::NORMAL, CBlockLocator{});
34 LegacyDataSPKM* legacy_spkm = wallet->GetOrCreateLegacyDataSPKM();
35
36 // Add watch-only addresses
37 std::vector<CScript> scripts_watch_only;
38 for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
39 CKey key = GenerateRandomKey();
40 LOCK(wallet->cs_wallet);
41 const auto& dest = GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY);
42 const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(dest));
43 assert(legacy_spkm->LoadWatchOnly(script));
44 assert(wallet->SetAddressBook(dest, strprintf("watch_%d", w), /*purpose=*/std::nullopt));
45 }
46
47 // Generate transactions and local addresses
48 for (int j = 0; j < 500; ++j) {
49 CKey key = GenerateRandomKey();
50 CPubKey pubkey = key.GetPubKey();
51 // Load key, scripts and create address book record
52 Assert(legacy_spkm->LoadKey(key, pubkey));
53 CTxDestination dest{PKHash(pubkey)};
54 Assert(wallet->SetAddressBook(dest, strprintf("legacy_%d", j), /*purpose=*/std::nullopt));
55
57 mtx.vout.emplace_back(COIN, GetScriptForDestination(dest));
58 mtx.vout.emplace_back(COIN, scripts_watch_only.at(j % NUM_WATCH_ONLY_ADDR));
59 mtx.vin.resize(2);
60 wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{}, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, /*rescanning_old_block=*/true);
61 }
62
63 bench.epochs(/*numEpochs=*/1).run([&context, &wallet] {
64 util::Result<MigrationResult> res = MigrateLegacyToDescriptor(std::move(wallet), /*passphrase=*/"", context, /*was_loaded=*/false);
65 assert(res);
66 assert(res->wallet);
67 assert(res->watchonly_wallet);
68 });
69}
70
72
73} // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
#define Assert(val)
Identity function.
Definition: check.h:85
An encapsulated private key.
Definition: key.h:35
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
An encapsulated public key.
Definition: pubkey.h:34
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
Bench & epochs(size_t numEpochs) noexcept
Controls number of epochs, the number of measurements to perform.
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadWatchOnly(const CScript &dest)
Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
@ LOW
Definition: bench.h:47
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
static void WalletMigration(benchmark::Bench &bench)
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:4415
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:186
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
Definition: outputtype.cpp:50
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:124
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition: transaction.h:58
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
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
assert(!tx.IsCoinBase())