Skip to content

Commit e86d71b

Browse files
committed
wallet: refactor, dedup wallet re-loading code
1 parent 1de423e commit e86d71b

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,21 +4314,20 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
43144314
std::set<fs::path> wallet_dirs;
43154315
if (success) {
43164316
// Migration successful, unload all wallets locally, then reload them.
4317-
// Reload the main wallet
4318-
wallet_dirs.insert(fs::PathFromString(local_wallet->GetDatabase().Filename()).parent_path());
4319-
success = reload_wallet(local_wallet);
4317+
// Note: We use a pointer to the shared_ptr to avoid increasing its reference count,
4318+
// as 'reload_wallet' expects to be the sole owner (use_count == 1).
4319+
for (std::shared_ptr<CWallet>* wallet_ptr : {&local_wallet, &res.watchonly_wallet, &res.solvables_wallet}) {
4320+
if (success && *wallet_ptr) {
4321+
std::shared_ptr<CWallet>& wallet = *wallet_ptr;
4322+
// Save db path and reload wallet
4323+
wallet_dirs.insert(fs::PathFromString(wallet->GetDatabase().Filename()).parent_path());
4324+
success = reload_wallet(wallet);
4325+
}
4326+
}
4327+
4328+
// Set main wallet
43204329
res.wallet = local_wallet;
43214330
res.wallet_name = wallet_name;
4322-
if (success && res.watchonly_wallet) {
4323-
// Reload watchonly
4324-
wallet_dirs.insert(fs::PathFromString(res.watchonly_wallet->GetDatabase().Filename()).parent_path());
4325-
success = reload_wallet(res.watchonly_wallet);
4326-
}
4327-
if (success && res.solvables_wallet) {
4328-
// Reload solvables
4329-
wallet_dirs.insert(fs::PathFromString(res.solvables_wallet->GetDatabase().Filename()).parent_path());
4330-
success = reload_wallet(res.solvables_wallet);
4331-
}
43324331
}
43334332
if (!success) {
43344333
// Migration failed, cleanup

0 commit comments

Comments
 (0)