-
Notifications
You must be signed in to change notification settings - Fork 35.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wallet: Reload watchonly and solvables wallets after migration #28609
Changes from all commits
9af87cf
118f2d7
d616d30
4814e40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3908,6 +3908,14 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error) | |
// Check if the transactions in the wallet are still ours. Either they belong here, or they belong in the watchonly wallet. | ||
// We need to go through these in the tx insertion order so that lookups to spends works. | ||
std::vector<uint256> txids_to_delete; | ||
std::unique_ptr<WalletBatch> watchonly_batch; | ||
if (data.watchonly_wallet) { | ||
watchonly_batch = std::make_unique<WalletBatch>(data.watchonly_wallet->GetDatabase()); | ||
// Copy the next tx order pos to the watchonly wallet | ||
LOCK(data.watchonly_wallet->cs_wallet); | ||
data.watchonly_wallet->nOrderPosNext = nOrderPosNext; | ||
watchonly_batch->WriteOrderPosNext(data.watchonly_wallet->nOrderPosNext); | ||
} | ||
for (const auto& [_pos, wtx] : wtxOrdered) { | ||
if (!IsMine(*wtx->tx) && !IsFromMe(*wtx->tx)) { | ||
// Check it is the watchonly wallet's | ||
|
@@ -3916,12 +3924,20 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error) | |
LOCK(data.watchonly_wallet->cs_wallet); | ||
if (data.watchonly_wallet->IsMine(*wtx->tx) || data.watchonly_wallet->IsFromMe(*wtx->tx)) { | ||
// Add to watchonly wallet | ||
if (!data.watchonly_wallet->AddToWallet(wtx->tx, wtx->m_state)) { | ||
error = _("Error: Could not add watchonly tx to watchonly wallet"); | ||
const uint256& hash = wtx->GetHash(); | ||
const CWalletTx& to_copy_wtx = *wtx; | ||
if (!data.watchonly_wallet->LoadToWallet(hash, [&](CWalletTx& ins_wtx, bool new_tx) EXCLUSIVE_LOCKS_REQUIRED(data.watchonly_wallet->cs_wallet) { | ||
achow101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!new_tx) return false; | ||
ins_wtx.SetTx(to_copy_wtx.tx); | ||
ins_wtx.CopyFrom(to_copy_wtx); | ||
return true; | ||
})) { | ||
error = strprintf(_("Error: Could not add watchonly tx %s to watchonly wallet"), wtx->GetHash().GetHex()); | ||
return false; | ||
} | ||
watchonly_batch->WriteTx(data.watchonly_wallet->mapWallet.at(hash)); | ||
// Mark as to remove from this wallet | ||
txids_to_delete.push_back(wtx->GetHash()); | ||
txids_to_delete.push_back(hash); | ||
continue; | ||
} | ||
} | ||
|
@@ -3930,6 +3946,7 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error) | |
return false; | ||
} | ||
} | ||
watchonly_batch.reset(); // Flush | ||
// Do the removes | ||
if (txids_to_delete.size() > 0) { | ||
std::vector<uint256> deleted_txids; | ||
|
@@ -4066,6 +4083,10 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error, | |
DatabaseOptions options; | ||
options.require_existing = false; | ||
options.require_create = true; | ||
options.require_format = DatabaseFormat::SQLITE; | ||
|
||
WalletContext empty_context; | ||
empty_context.args = context.args; | ||
|
||
// Make the wallets | ||
options.create_flags = WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET | WALLET_FLAG_DESCRIPTORS; | ||
|
@@ -4081,8 +4102,14 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error, | |
DatabaseStatus status; | ||
std::vector<bilingual_str> warnings; | ||
std::string wallet_name = wallet.GetName() + "_watchonly"; | ||
data->watchonly_wallet = CreateWallet(context, wallet_name, std::nullopt, options, status, error, warnings); | ||
if (status != DatabaseStatus::SUCCESS) { | ||
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(wallet_name, options, status, error); | ||
if (!database) { | ||
error = strprintf(_("Wallet file creation failed: %s"), error); | ||
return false; | ||
} | ||
|
||
data->watchonly_wallet = CWallet::Create(empty_context, wallet_name, std::move(database), options.create_flags, error, warnings); | ||
if (!data->watchonly_wallet) { | ||
error = _("Error: Failed to create new watchonly wallet"); | ||
return false; | ||
} | ||
|
@@ -4112,8 +4139,14 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error, | |
DatabaseStatus status; | ||
std::vector<bilingual_str> warnings; | ||
std::string wallet_name = wallet.GetName() + "_solvables"; | ||
data->solvable_wallet = CreateWallet(context, wallet_name, std::nullopt, options, status, error, warnings); | ||
if (status != DatabaseStatus::SUCCESS) { | ||
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(wallet_name, options, status, error); | ||
if (!database) { | ||
error = strprintf(_("Wallet file creation failed: %s"), error); | ||
return false; | ||
} | ||
|
||
data->solvable_wallet = CWallet::Create(empty_context, wallet_name, std::move(database), options.create_flags, error, warnings); | ||
if (!data->solvable_wallet) { | ||
error = _("Error: Failed to create new watchonly wallet"); | ||
return false; | ||
} | ||
|
@@ -4216,47 +4249,69 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle | |
success = DoMigration(*local_wallet, context, error, res); | ||
} | ||
|
||
// In case of reloading failure, we need to remember the wallet dirs to remove | ||
// Set is used as it may be populated with the same wallet directory paths multiple times, | ||
// both before and after reloading. This ensures the set is complete even if one of the wallets | ||
// fails to reload. | ||
std::set<fs::path> wallet_dirs; | ||
achow101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (success) { | ||
// Migration successful, unload the wallet locally, then reload it. | ||
assert(local_wallet.use_count() == 1); | ||
local_wallet.reset(); | ||
res.wallet = LoadWallet(context, wallet_name, /*load_on_start=*/std::nullopt, options, status, error, warnings); | ||
// Migration successful, unload all wallets locally, then reload them. | ||
const auto& reload_wallet = [&](std::shared_ptr<CWallet>& to_reload) { | ||
assert(to_reload.use_count() == 1); | ||
std::string name = to_reload->GetName(); | ||
wallet_dirs.insert(fs::PathFromString(to_reload->GetDatabase().Filename()).parent_path()); | ||
to_reload.reset(); | ||
to_reload = LoadWallet(context, name, /*load_on_start=*/std::nullopt, options, status, error, warnings); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In commit "wallet: Reload watchonly and solvables wallets after migration" (b57218e) Just a thought, not for this PR, but maybe it would be good to load the new wallet on startup if the previous wallet loaded on startup. Also it would be good to improve error reporting here. It seems like errors/warnings just get discarded which might make problems hard to debug. Though I guess we don't expect failure loading a newly creating wallet. |
||
return to_reload != nullptr; | ||
}; | ||
// Reload the main wallet | ||
success = reload_wallet(local_wallet); | ||
res.wallet = local_wallet; | ||
res.wallet_name = wallet_name; | ||
} else { | ||
if (success && res.watchonly_wallet) { | ||
// Reload watchonly | ||
success = reload_wallet(res.watchonly_wallet); | ||
} | ||
if (success && res.solvables_wallet) { | ||
// Reload solvables | ||
success = reload_wallet(res.solvables_wallet); | ||
} | ||
} | ||
if (!success) { | ||
// Migration failed, cleanup | ||
// Copy the backup to the actual wallet dir | ||
fs::path temp_backup_location = fsbridge::AbsPathJoin(GetWalletDir(), backup_filename); | ||
fs::copy_file(backup_path, temp_backup_location, fs::copy_options::none); | ||
|
||
// Remember this wallet's walletdir to remove after unloading | ||
std::vector<fs::path> wallet_dirs; | ||
wallet_dirs.emplace_back(fs::PathFromString(local_wallet->GetDatabase().Filename()).parent_path()); | ||
|
||
// Unload the wallet locally | ||
assert(local_wallet.use_count() == 1); | ||
local_wallet.reset(); | ||
|
||
// Make list of wallets to cleanup | ||
std::vector<std::shared_ptr<CWallet>> created_wallets; | ||
if (local_wallet) created_wallets.push_back(std::move(local_wallet)); | ||
if (res.watchonly_wallet) created_wallets.push_back(std::move(res.watchonly_wallet)); | ||
if (res.solvables_wallet) created_wallets.push_back(std::move(res.solvables_wallet)); | ||
|
||
// Get the directories to remove after unloading | ||
for (std::shared_ptr<CWallet>& w : created_wallets) { | ||
wallet_dirs.emplace_back(fs::PathFromString(w->GetDatabase().Filename()).parent_path()); | ||
wallet_dirs.emplace(fs::PathFromString(w->GetDatabase().Filename()).parent_path()); | ||
} | ||
|
||
// Unload the wallets | ||
for (std::shared_ptr<CWallet>& w : created_wallets) { | ||
if (!RemoveWallet(context, w, /*load_on_start=*/false)) { | ||
error += _("\nUnable to cleanup failed migration"); | ||
return util::Error{error}; | ||
if (w->HaveChain()) { | ||
// Unloading for wallets that were loaded for normal use | ||
if (!RemoveWallet(context, w, /*load_on_start=*/false)) { | ||
error += _("\nUnable to cleanup failed migration"); | ||
return util::Error{error}; | ||
} | ||
UnloadWallet(std::move(w)); | ||
} else { | ||
// Unloading for wallets in local context | ||
assert(w.use_count() == 1); | ||
w.reset(); | ||
} | ||
UnloadWallet(std::move(w)); | ||
} | ||
|
||
// Delete the wallet directories | ||
for (fs::path& dir : wallet_dirs) { | ||
for (const fs::path& dir : wallet_dirs) { | ||
fs::remove_all(dir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In commit "wallet: Reload watchonly and solvables wallets after migration" (b57218e) Not directly related to this PR, but this remove_all seems not great because if there were any non-wallet files in the wallet directory they will be recursively removed. I think it would be preferable to only delete files that we know about and leave files that we do not recognize alone. This would also avoid the need to copy the backup file from the wallet directory to the wallet parent directory, and then copy it back in again when the wallet directory is recreated. A lot of this logic seems pretty fragile and possible to simplify (Maybe furszy already has a PR to do it?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not in a PR, but I do have something related in progress. Still, we should move forward with your suggestion; it is much more scoped than what I'm doing. I'm rewriting the process, so the bdb->sqlite migration, the removal of "no longer ours" database records, and the backup creation steps are not needed. This would indirectly solve the issue you mentioned because, in case of a failure, the process would only need to delete the newly created wallet directories and nothing else. |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the comment above be updated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's still valid when these are
private
.