diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ca73486aeee..f61cc2e7167 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4226,15 +4226,6 @@ util::Result MigrateLegacyToDescriptor(std::shared_ptr const std::string wallet_name = local_wallet->GetName(); - // Helper to reload as normal for some of our exit scenarios - const auto& reload_wallet = [&](std::shared_ptr& to_reload) { - assert(to_reload.use_count() == 1); - std::string name = to_reload->GetName(); - to_reload.reset(); - to_reload = LoadWallet(context, name, /*load_on_start=*/std::nullopt, options, status, error, warnings); - return to_reload != nullptr; - }; - // Before anything else, check if there is something to migrate. if (local_wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) { return util::Error{_("Error: This wallet is already a descriptor wallet")}; @@ -4289,10 +4280,10 @@ util::Result MigrateLegacyToDescriptor(std::shared_ptr } } - // In case of reloading failure, we need to remember the wallet dirs to remove - // Set is used as it may be populated with the same wallet directory paths multiple times, - // both before and after reloading. This ensures the set is complete even if one of the wallets - // fails to reload. + // In case of loading failure, we need to remember the wallet dirs to remove. + // A `set` is used as it may be populated with the same wallet directory paths multiple times, + // both before and after loading. This ensures the set is complete even if one of the wallets + // fails to load. std::set wallet_dirs; if (success) { Assume(!res.wallet); // We will set it here. @@ -4304,15 +4295,17 @@ util::Result MigrateLegacyToDescriptor(std::shared_ptr for (const auto& path_to_remove : paths_to_remove) fs::remove_all(path_to_remove); } - // Migration successful, unload all wallets locally, then reload them. - // Note: We use a pointer to the shared_ptr to avoid increasing its reference count, - // as 'reload_wallet' expects to be the sole owner (use_count == 1). + // Migration successful, load all the migrated wallets. for (std::shared_ptr* wallet_ptr : {&local_wallet, &res.watchonly_wallet, &res.solvables_wallet}) { if (success && *wallet_ptr) { std::shared_ptr& wallet = *wallet_ptr; - // Save db path and reload wallet + // Save db path and load wallet wallet_dirs.insert(fs::PathFromString(wallet->GetDatabase().Filename()).parent_path()); - success = reload_wallet(wallet); + assert(wallet.use_count() == 1); + std::string wallet_name = wallet->GetName(); + wallet.reset(); + wallet = LoadWallet(context, wallet_name, /*load_on_start=*/std::nullopt, options, status, error, warnings); + success = (wallet != nullptr); // When no wallet is set, set the main wallet. if (!res.wallet) {