Skip to content

Commit 0f86da3

Browse files
committed
wallet: remove dead code in legacy wallet migration
A discussion on a previous PR 32481 related to legacy wallet dead code removal made me realize that checking if the legacy wallet was loaded prior to the start of the migration is not required ever since legacy wallets can't be loaded in the first place. I also verified that the `load_on_start` persistent setting can also not cause the legacy wallets to be loaded, which further makes the case for removal of the above mentioned checks during migration. The current test coverage also shows these lines uncovered.
1 parent 49d5f1f commit 0f86da3

File tree

3 files changed

+10
-31
lines changed

3 files changed

+10
-31
lines changed

src/bench/wallet_migration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void WalletMigration(benchmark::Bench& bench)
6666

6767
bench.epochs(/*numEpochs=*/1).epochIterations(/*numIters=*/1) // run the migration exactly once
6868
.run([&] {
69-
auto res{MigrateLegacyToDescriptor(std::move(wallet), /*passphrase=*/"", *loader->context(), /*was_loaded=*/false)};
69+
auto res{MigrateLegacyToDescriptor(std::move(wallet), /*passphrase=*/"", *loader->context())};
7070
assert(res);
7171
assert(res->wallet);
7272
assert(res->watchonly_wallet);

src/wallet/wallet.cpp

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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 + _("\nUnable 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;

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ struct MigrationResult {
11371137
//! Do all steps to migrate a legacy wallet to a descriptor wallet
11381138
[[nodiscard]] util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& wallet_name, const SecureString& passphrase, WalletContext& context);
11391139
//! Requirement: The wallet provided to this function must be isolated, with no attachment to the node's context.
1140-
[[nodiscard]] util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet> local_wallet, const SecureString& passphrase, WalletContext& context, bool was_loaded);
1140+
[[nodiscard]] util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet> local_wallet, const SecureString& passphrase, WalletContext& context);
11411141
} // namespace wallet
11421142

11431143
#endif // BITCOIN_WALLET_WALLET_H

0 commit comments

Comments
 (0)