Repository: bitcoin/bitcoin
File: src/bench/wallet_migration.cpp (C++)

Code before:
    DatabaseOptions options;
    options.use_unsafe_sync = true;
    options.verify = false;
    DatabaseStatus status;
    bilingual_str error;
    auto database = MakeWalletDatabase(fs::PathToString(test_setup->m_path_root / "legacy"), options, status, error);
    uint64_t create_flags = 0;
    auto wallet = TestLoadWallet(std::move(database), context, create_flags);

    // Add watch-only addresses
    std::vector<CScript> scripts_watch_only;
    for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
        CKey key = GenerateRandomKey();
        LOCK(wallet->cs_wallet);
        const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY)));
        bool res = wallet->ImportScriptPubKeys(strprintf("watch_%d", w), {script},
                                    /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1);
        assert(res);
    }

    // Generate transactions and local addresses
    for (int j = 0; j < 400; ++j) {
        CMutableTransaction mtx;
        mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::BECH32, strprintf("bench_%d", j)))));
        mtx.vout.emplace_back(COIN, GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::LEGACY, strprintf("legacy_%d", j)))));
        mtx.vout.emplace_back(COIN, scripts_watch_only.at(j % NUM_WATCH_ONLY_ADDR));
        mtx.vin.resize(2);
        wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{}, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, /*rescanning_old_block=*/true);
    }

    // Unload so the migration process loads it
    TestUnloadWallet(std::move(wallet));

    bench.epochs(/*numEpochs=*/1).run([&] {
        util::Result<MigrationResult> res = MigrateLegacyToDescriptor(fs::PathToString(test_setup->m_path_root / "legacy"), "", context);
        assert(res);
        assert(res->wallet);
        assert(res->watchonly_wallet);
    });
}

BENCHMARK(WalletMigration, benchmark::PriorityLevel::LOW);

} // namespace wallet

#endif // end USE_SQLITE && USE_BDB


Review comment (suggestion):
This `LoadCScript` is unnecessary since the spks for public keys is either added via `ImplicitlyLearnRelatedKeyScripts` or is not in the wallet explicitly.

Code after:
    // Setup legacy wallet
    std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(test_setup->m_node.chain.get(), "", CreateMockableWalletDatabase());
    wallet->chainStateFlushed(ChainstateRole::NORMAL, CBlockLocator{});
    LegacyDataSPKM* legacy_spkm = wallet->GetOrCreateLegacyDataSPKM();

    // Add watch-only addresses
    std::vector<CScript> scripts_watch_only;
    for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) {
        CKey key = GenerateRandomKey();
        LOCK(wallet->cs_wallet);
        const auto& dest = GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY);
        const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(dest));
        assert(legacy_spkm->LoadWatchOnly(script));
        assert(wallet->SetAddressBook(dest, strprintf("watch_%d", w), /*purpose=*/std::nullopt));
    }

    // Generate transactions and local addresses
    for (int j = 0; j < 500; ++j) {
        CKey key = GenerateRandomKey();
        CPubKey pubkey = key.GetPubKey();
        // Load key, scripts and create address book record
        Assert(legacy_spkm->LoadKey(key, pubkey));
        CTxDestination dest{PKHash(pubkey)};
        Assert(wallet->SetAddressBook(dest, strprintf("legacy_%d", j), /*purpose=*/std::nullopt));

        CMutableTransaction mtx;
        mtx.vout.emplace_back(COIN, GetScriptForDestination(dest));
        mtx.vout.emplace_back(COIN, scripts_watch_only.at(j % NUM_WATCH_ONLY_ADDR));
        mtx.vin.resize(2);
        wallet->AddToWallet(MakeTransactionRef(mtx), TxStateInactive{}, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, /*rescanning_old_block=*/true);
    }

    bench.epochs(/*numEpochs=*/1).run([&context, &wallet] {
        util::Result<MigrationResult> res = MigrateLegacyToDescriptor(std::move(wallet), /*passphrase=*/"", context, /*was_loaded=*/false);
        assert(res);
        assert(res->wallet);
        assert(res->watchonly_wallet);
    });
}

BENCHMARK(WalletMigration, benchmark::PriorityLevel::LOW);

} // namespace wallet

#endif // end USE_SQLITE && USE_BDB