Bitcoin Core 28.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 <bitcoin-build-config.h> // IWYU pragma: keep
6
7#include <bench/bench.h>
8#include <interfaces/chain.h>
9#include <node/context.h>
10#include <test/util/mining.h>
12#include <wallet/test/util.h>
13#include <wallet/context.h>
14#include <wallet/receive.h>
15#include <wallet/wallet.h>
16
17#include <optional>
18
19#if defined(USE_BDB) && defined(USE_SQLITE) // only enable benchmark when bdb and sqlite are enabled
20
21namespace wallet{
22
23static void WalletMigration(benchmark::Bench& bench)
24{
25 const auto test_setup = MakeNoLogFileContext<TestingSetup>();
26
27 WalletContext context;
28 context.args = &test_setup->m_args;
29 context.chain = test_setup->m_node.chain.get();
30
31 // Number of imported watch only addresses
32 int NUM_WATCH_ONLY_ADDR = 20;
33
34 // Setup legacy wallet
35 DatabaseOptions options;
36 options.use_unsafe_sync = true;
37 options.verify = false;
38 DatabaseStatus status;
39 bilingual_str error;
40 auto database = MakeWalletDatabase(fs::PathToString(test_setup->m_path_root / "legacy"), options, status, error);
41 uint64_t create_flags = 0;
42 auto wallet = TestLoadWallet(std::move(database), context, create_flags);
43
44 // Add watch-only addresses
45 std::vector<CScript> scripts_watch_only;
46 for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
47 CKey key = GenerateRandomKey();
48 LOCK(wallet->cs_wallet);
49 const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY)));
50 bool res = wallet->ImportScriptPubKeys(strprintf("watch_%d", w), {script},
51 /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1);
52 assert(res);
53 }
54
55 // Generate transactions and local addresses
56 for (int j = 0; j < 400; ++j) {
58 mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::BECH32, strprintf("bench_%d", j)))));
59 mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::LEGACY, strprintf("legacy_%d", j)))));
60 mtx.vout.emplace_back(COIN, scripts_watch_only.at(j % NUM_WATCH_ONLY_ADDR));
61 mtx.vin.resize(2);
62 wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{}, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, /*rescanning_old_block=*/true);
63 }
64
65 // Unload so the migration process loads it
66 TestUnloadWallet(std::move(wallet));
67
68 bench.epochs(/*numEpochs=*/1).run([&] {
69 util::Result<MigrationResult> res = MigrateLegacyToDescriptor(fs::PathToString(test_setup->m_path_root / "legacy"), "", context);
70 assert(res);
71 assert(res->wallet);
72 assert(res->watchonly_wallet);
73 });
74}
75
77
78} // namespace wallet
79
80#endif // end USE_SQLITE && USE_BDB
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
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
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.
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:352
@ LOW
Definition: bench.h:47
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
BENCHMARK(WalletBalanceDirty, benchmark::PriorityLevel::HIGH)
std::shared_ptr< CWallet > TestLoadWallet(std::unique_ptr< WalletDatabase > database, WalletContext &context, uint64_t create_flags)
Definition: util.cpp:50
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:4385
void TestUnloadWallet(std::shared_ptr< CWallet > &&wallet)
Definition: util.cpp:73
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition: wallet.cpp:2972
DatabaseStatus
Definition: db.h:205
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
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
Bilingual messages:
Definition: translation.h:21
#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:1165
assert(!tx.IsCoinBase())