Skip to content

Commit 589ed1a

Browse files
furszyachow101
andcommitted
wallet: migration, avoid loading wallet after failure when it wasn't loaded before
During migration failure, only load wallet back into memory when the wallet was loaded prior to migration. This fixes the case where BDB is not supported, which implies that no legacy wallet can be loaded into memory due to the lack of db writing functionality. This commit also improves migration backup related comments to better document the current workflow. Co-authored-by: Ava Chow <[email protected]>
1 parent 35000e3 commit 589ed1a

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,9 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
490490
return wallet;
491491
}
492492

493-
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
493+
// Re-creates wallet from the backup file by renaming and moving it into the wallet's directory.
494+
// If 'load_after_restore=true', the wallet object will be fully initialized and appended to the context.
495+
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings, bool load_after_restore)
494496
{
495497
DatabaseOptions options;
496498
ReadDatabaseArgs(*context.args, options);
@@ -515,13 +517,17 @@ std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& b
515517

516518
fs::copy_file(backup_file, wallet_file, fs::copy_options::none);
517519

518-
wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
520+
if (load_after_restore) {
521+
wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
522+
}
519523
} catch (const std::exception& e) {
520524
assert(!wallet);
521525
if (!error.empty()) error += Untranslated("\n");
522526
error += Untranslated(strprintf("Unexpected exception: %s", e.what()));
523527
}
524-
if (!wallet) {
528+
529+
// Remove created wallet path only when loading fails
530+
if (load_after_restore && !wallet) {
525531
fs::remove_all(wallet_path);
526532
}
527533

@@ -4523,7 +4529,7 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
45234529
}
45244530
if (!success) {
45254531
// Migration failed, cleanup
4526-
// Copy the backup to the actual wallet dir
4532+
// Before deleting the wallet's directory, copy the backup file to the top-level wallets dir
45274533
fs::path temp_backup_location = fsbridge::AbsPathJoin(GetWalletDir(), backup_filename);
45284534
fs::copy_file(backup_path, temp_backup_location, fs::copy_options::none);
45294535

@@ -4560,17 +4566,24 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
45604566
}
45614567

45624568
// Restore the backup
4563-
DatabaseStatus status;
4564-
std::vector<bilingual_str> warnings;
4565-
if (!RestoreWallet(context, temp_backup_location, wallet_name, /*load_on_start=*/std::nullopt, status, error, warnings)) {
4566-
error += _("\nUnable to restore backup of wallet.");
4569+
// Convert the backup file to the wallet db file by renaming it and moving it into the wallet's directory.
4570+
// Reload it into memory if the wallet was previously loaded.
4571+
bilingual_str restore_error;
4572+
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);
4573+
if (!restore_error.empty()) {
4574+
error += restore_error + _("\nUnable to restore backup of wallet.");
45674575
return util::Error{error};
45684576
}
45694577

4570-
// Move the backup to the wallet dir
4578+
// The wallet directory has been restored, but just in case, copy the previously created backup to the wallet dir
45714579
fs::copy_file(temp_backup_location, backup_path, fs::copy_options::none);
45724580
fs::remove(temp_backup_location);
45734581

4582+
// Verify that there is no dangling wallet: when the wallet wasn't loaded before, expect null.
4583+
// This check is performed after restoration to avoid an early error before saving the backup.
4584+
bool wallet_reloaded = ptr_wallet != nullptr;
4585+
assert(was_loaded == wallet_reloaded);
4586+
45744587
return util::Error{error};
45754588
}
45764589
return res;

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count)
9595
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);
9696
std::shared_ptr<CWallet> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
9797
std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
98-
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
98+
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings, bool load_after_restore = true);
9999
std::unique_ptr<interfaces::Handler> HandleLoadWallet(WalletContext& context, LoadWalletFn load_wallet);
100100
void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr<CWallet>& wallet);
101101
std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);

test/functional/wallet_migration.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,7 @@ def test_failed_migration_cleanup(self):
894894
shutil.copytree(self.old_node.wallets_path / "failed", self.master_node.wallets_path / "failed")
895895
assert_raises_rpc_error(-4, "Failed to create database", self.master_node.migratewallet, "failed")
896896

897-
assert "failed" in self.master_node.listwallets()
898-
assert "failed_watchonly" not in self.master_node.listwallets()
899-
assert "failed_solvables" not in self.master_node.listwallets()
897+
assert all(wallet not in self.master_node.listwallets() for wallet in ["failed", "failed_watchonly", "failed_solvables"])
900898

901899
assert not (self.master_node.wallets_path / "failed_watchonly").exists()
902900
# Since the file in failed_solvables is one that we put there, migration shouldn't touch it
@@ -910,6 +908,22 @@ def test_failed_migration_cleanup(self):
910908
_, _, magic = struct.unpack("QII", data)
911909
assert_equal(magic, BTREE_MAGIC)
912910

911+
####################################################
912+
# Perform the same test with a loaded legacy wallet.
913+
# The wallet should remain loaded after the failure.
914+
#
915+
# This applies only when BDB is enabled, as the user
916+
# cannot interact with the legacy wallet database
917+
# without BDB support.
918+
if self.is_bdb_compiled() is not None:
919+
# Advance time to generate a different backup name
920+
self.master_node.setmocktime(self.master_node.getblockheader(self.master_node.getbestblockhash())['time'] + 100)
921+
assert "failed" not in self.master_node.listwallets()
922+
self.master_node.loadwallet("failed")
923+
assert_raises_rpc_error(-4, "Failed to create database", self.master_node.migratewallet, "failed")
924+
wallets = self.master_node.listwallets()
925+
assert "failed" in wallets and all(wallet not in wallets for wallet in ["failed_watchonly", "failed_solvables"])
926+
913927
def test_blank(self):
914928
self.log.info("Test that a blank wallet is migrated")
915929
wallet = self.create_legacy_wallet("blank", blank=True)

0 commit comments

Comments
 (0)