|
| 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> |
| 11 | +#include <test/util/setup_common.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 | + |
| 21 | +namespace wallet{ |
| 22 | + |
| 23 | +static 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) { |
| 57 | + CMutableTransaction mtx; |
| 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 | + |
| 76 | +BENCHMARK(WalletMigration, benchmark::PriorityLevel::LOW); |
| 77 | + |
| 78 | +} // namespace wallet |
| 79 | + |
| 80 | +#endif // end USE_SQLITE && USE_BDB |
0 commit comments