Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
wallet_migration.cpp
Go to the documentation of this file.
1// Copyright (c) 2024-present 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 <interfaces/chain.h>
7#include <interfaces/wallet.h>
8#include <kernel/chain.h>
9#include <kernel/types.h>
10#include <node/context.h>
11#include <test/util/mining.h>
13#include <wallet/context.h>
14#include <wallet/receive.h>
15#include <wallet/test/util.h>
16#include <wallet/wallet.h>
17
18#include <optional>
19
20namespace wallet{
21
23{
25 const auto loader{MakeWalletLoader(*test_setup->m_node.chain, test_setup->m_args)};
26
27 // Number of imported watch only addresses
28 int NUM_WATCH_ONLY_ADDR = 20;
29
30 // Setup legacy wallet
31 std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(test_setup->m_node.chain.get(), "", CreateMockableWalletDatabase());
32 LegacyDataSPKM* legacy_spkm = wallet->GetOrCreateLegacyDataSPKM();
33 WalletBatch batch{wallet->GetDatabase()};
34
35 // Write a best block record as migration expects one to exist
37 batch.WriteBestBlock(loc);
38
39 // Add watch-only addresses
40 std::vector<CScript> scripts_watch_only;
41 for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
42 CKey key = GenerateRandomKey();
43 LOCK(wallet->cs_wallet);
44 const PKHash dest{key.GetPubKey()};
45 const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(dest));
46 assert(legacy_spkm->LoadWatchOnly(script));
47 assert(wallet->SetAddressBook(dest, strprintf("watch_%d", w), /*purpose=*/std::nullopt));
48 batch.WriteWatchOnly(script, CKeyMetadata());
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, /*rescanning_old_block=*/true);
65 batch.WriteKey(pubkey, key.GetPrivKey(), CKeyMetadata());
66 }
67
68 bench.epochs(/*numEpochs=*/1).epochIterations(/*numIters=*/1) // run the migration exactly once
69 .run([&] {
70 auto res{MigrateLegacyToDescriptor(std::move(wallet), /*passphrase=*/"", *loader->context())};
71 assert(res);
72 assert(res->wallet);
73 assert(res->watchonly_wallet);
74 });
75}
76
78
79} // 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.
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition amount.h:15
#define BENCHMARK(n)
Definition bench.h:68
#define Assert(val)
Identity function.
Definition check.h:113
An encapsulated private key.
Definition key.h:36
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition key.cpp:170
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition key.cpp:183
An encapsulated public key.
Definition pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition script.h:405
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:627
Access to the wallet database.
Definition walletdb.h:193
is a home for simple enum and struct type definitions that can be used internally by functions in the...
CKey GenerateRandomKey(bool compressed) noexcept
Definition key.cpp:475
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:4250
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition block.h:117
A mutable version of CTransaction.
std::vector< CTxOut > vout
State of transaction not confirmed or conflicting with a known block and not in the mempool.
Definition transaction.h:59
#define LOCK(cs)
Definition sync.h:258
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
assert(!tx.IsCoinBase())