@@ -4174,18 +4174,10 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
4174
4174
std::vector<bilingual_str> warnings;
4175
4175
bilingual_str error;
4176
4176
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.
4179
4178
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" )};
4189
4181
} else {
4190
4182
// Check if the wallet is BDB
4191
4183
const auto & wallet_path = GetWalletPath (wallet_name);
@@ -4219,10 +4211,10 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
4219
4211
return util::Error{Untranslated (" Wallet loading failed." ) + Untranslated (" " ) + error};
4220
4212
}
4221
4213
4222
- return MigrateLegacyToDescriptor (std::move (local_wallet), passphrase, context, was_loaded );
4214
+ return MigrateLegacyToDescriptor (std::move (local_wallet), passphrase, context);
4223
4215
}
4224
4216
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)
4226
4218
{
4227
4219
MigrationResult res;
4228
4220
bilingual_str error;
@@ -4245,9 +4237,6 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
4245
4237
4246
4238
// Before anything else, check if there is something to migrate.
4247
4239
if (local_wallet->IsWalletFlagSet (WALLET_FLAG_DESCRIPTORS)) {
4248
- if (was_loaded) {
4249
- reload_wallet (local_wallet);
4250
- }
4251
4240
return util::Error{_ (" Error: This wallet is already a descriptor wallet" )};
4252
4241
}
4253
4242
@@ -4256,9 +4245,6 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
4256
4245
fs::path backup_filename = fs::PathFromString (strprintf (" %s_%d.legacy.bak" , (wallet_name.empty () ? " default_wallet" : wallet_name), GetTime ()));
4257
4246
fs::path backup_path = this_wallet_dir / backup_filename;
4258
4247
if (!local_wallet->BackupWallet (fs::PathToString (backup_path))) {
4259
- if (was_loaded) {
4260
- reload_wallet (local_wallet);
4261
- }
4262
4248
return util::Error{_ (" Error: Unable to make a backup of your wallet" )};
4263
4249
}
4264
4250
res.backup_path = backup_path;
@@ -4267,9 +4253,6 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
4267
4253
4268
4254
// Unlock the wallet if needed
4269
4255
if (local_wallet->IsLocked () && !local_wallet->Unlock (passphrase)) {
4270
- if (was_loaded) {
4271
- reload_wallet (local_wallet);
4272
- }
4273
4256
if (passphrase.find (' \0 ' ) == std::string::npos) {
4274
4257
return util::Error{Untranslated (" Error: Wallet decryption failed, the wallet passphrase was not provided or was incorrect." )};
4275
4258
} else {
@@ -4379,23 +4362,19 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
4379
4362
4380
4363
// Restore the backup
4381
4364
// 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.
4383
4365
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 );
4385
4367
if (!restore_error.empty ()) {
4386
4368
error += restore_error + _ (" \n Unable to restore backup of wallet." );
4387
4369
return util::Error{error};
4388
4370
}
4371
+ // Verify that the legacy wallet is not loaded after restoring from the backup.
4372
+ assert (!ptr_wallet);
4389
4373
4390
4374
// The wallet directory has been restored, but just in case, copy the previously created backup to the wallet dir
4391
4375
fs::copy_file (temp_backup_location, backup_path, fs::copy_options::none);
4392
4376
fs::remove (temp_backup_location);
4393
4377
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
-
4399
4378
return util::Error{error};
4400
4379
}
4401
4380
return res;
0 commit comments