@@ -4174,18 +4174,10 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
41744174 std::vector<bilingual_str> warnings;
41754175 bilingual_str error;
41764176
4177- // If the wallet is still loaded, unload it so that nothing else tries to use it while we're changing it
4178- bool was_loaded = false ;
4177+ // The only kind of wallets that could be loaded are descriptor ones, which don't need to be migrated.
41794178 if (auto wallet = GetWallet (context, wallet_name)) {
4180- if (wallet->IsWalletFlagSet (WALLET_FLAG_DESCRIPTORS)) {
4181- return util::Error{_ (" Error: This wallet is already a descriptor wallet" )};
4182- }
4183-
4184- if (!RemoveWallet (context, wallet, /* load_on_start=*/ std::nullopt , warnings)) {
4185- return util::Error{_ (" Unable to unload the wallet before migrating" )};
4186- }
4187- WaitForDeleteWallet (std::move (wallet));
4188- was_loaded = true ;
4179+ assert (wallet->IsWalletFlagSet (WALLET_FLAG_DESCRIPTORS));
4180+ return util::Error{_ (" Error: This wallet is already a descriptor wallet" )};
41894181 } else {
41904182 // Check if the wallet is BDB
41914183 const auto & wallet_path = GetWalletPath (wallet_name);
@@ -4219,10 +4211,10 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
42194211 return util::Error{Untranslated (" Wallet loading failed." ) + Untranslated (" " ) + error};
42204212 }
42214213
4222- return MigrateLegacyToDescriptor (std::move (local_wallet), passphrase, context, was_loaded );
4214+ return MigrateLegacyToDescriptor (std::move (local_wallet), passphrase, context);
42234215}
42244216
4225- util::Result<MigrationResult> MigrateLegacyToDescriptor (std::shared_ptr<CWallet> local_wallet, const SecureString& passphrase, WalletContext& context, bool was_loaded )
4217+ util::Result<MigrationResult> MigrateLegacyToDescriptor (std::shared_ptr<CWallet> local_wallet, const SecureString& passphrase, WalletContext& context)
42264218{
42274219 MigrationResult res;
42284220 bilingual_str error;
@@ -4245,9 +4237,6 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
42454237
42464238 // Before anything else, check if there is something to migrate.
42474239 if (local_wallet->IsWalletFlagSet (WALLET_FLAG_DESCRIPTORS)) {
4248- if (was_loaded) {
4249- reload_wallet (local_wallet);
4250- }
42514240 return util::Error{_ (" Error: This wallet is already a descriptor wallet" )};
42524241 }
42534242
@@ -4256,9 +4245,6 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
42564245 fs::path backup_filename = fs::PathFromString (strprintf (" %s_%d.legacy.bak" , (wallet_name.empty () ? " default_wallet" : wallet_name), GetTime ()));
42574246 fs::path backup_path = this_wallet_dir / backup_filename;
42584247 if (!local_wallet->BackupWallet (fs::PathToString (backup_path))) {
4259- if (was_loaded) {
4260- reload_wallet (local_wallet);
4261- }
42624248 return util::Error{_ (" Error: Unable to make a backup of your wallet" )};
42634249 }
42644250 res.backup_path = backup_path;
@@ -4267,9 +4253,6 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
42674253
42684254 // Unlock the wallet if needed
42694255 if (local_wallet->IsLocked () && !local_wallet->Unlock (passphrase)) {
4270- if (was_loaded) {
4271- reload_wallet (local_wallet);
4272- }
42734256 if (passphrase.find (' \0 ' ) == std::string::npos) {
42744257 return util::Error{Untranslated (" Error: Wallet decryption failed, the wallet passphrase was not provided or was incorrect." )};
42754258 } else {
@@ -4379,23 +4362,19 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
43794362
43804363 // Restore the backup
43814364 // Convert the backup file to the wallet db file by renaming it and moving it into the wallet's directory.
4382- // Reload it into memory if the wallet was previously loaded.
43834365 bilingual_str restore_error;
4384- const auto & ptr_wallet = RestoreWallet (context, temp_backup_location, wallet_name, /* load_on_start=*/ std::nullopt , status, restore_error, warnings, /* load_after_restore=*/ was_loaded );
4366+ const auto & ptr_wallet = RestoreWallet (context, temp_backup_location, wallet_name, /* load_on_start=*/ std::nullopt , status, restore_error, warnings, /* load_after_restore=*/ false );
43854367 if (!restore_error.empty ()) {
43864368 error += restore_error + _ (" \n Unable to restore backup of wallet." );
43874369 return util::Error{error};
43884370 }
4371+ // Verify that the legacy wallet is not loaded after restoring from the backup.
4372+ assert (!ptr_wallet);
43894373
43904374 // The wallet directory has been restored, but just in case, copy the previously created backup to the wallet dir
43914375 fs::copy_file (temp_backup_location, backup_path, fs::copy_options::none);
43924376 fs::remove (temp_backup_location);
43934377
4394- // Verify that there is no dangling wallet: when the wallet wasn't loaded before, expect null.
4395- // This check is performed after restoration to avoid an early error before saving the backup.
4396- bool wallet_reloaded = ptr_wallet != nullptr ;
4397- assert (was_loaded == wallet_reloaded);
4398-
43994378 return util::Error{error};
44004379 }
44014380 return res;
0 commit comments