Skip to content

Commit 34bf079

Browse files
committed
wallet: refactor ApplyMigrationData to return util::Result<void>
1 parent aacaaaa commit 34bf079

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/wallet/wallet.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,15 +4073,14 @@ std::optional<MigrationData> CWallet::GetDescriptorsForLegacy(bilingual_str& err
40734073
return res;
40744074
}
40754075

4076-
bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
4076+
util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
40774077
{
40784078
AssertLockHeld(cs_wallet);
40794079

40804080
LegacyDataSPKM* legacy_spkm = GetLegacyDataSPKM();
40814081
if (!Assume(legacy_spkm)) {
40824082
// This shouldn't happen
4083-
error = Untranslated(STR_INTERNAL_BUG("Error: Legacy wallet data missing"));
4084-
return false;
4083+
return util::Error{Untranslated(STR_INTERNAL_BUG("Error: Legacy wallet data missing"))};
40854084
}
40864085

40874086
// Get all invalid or non-watched scripts that will not be migrated
@@ -4095,16 +4094,15 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
40954094

40964095
for (auto& desc_spkm : data.desc_spkms) {
40974096
if (m_spk_managers.count(desc_spkm->GetID()) > 0) {
4098-
error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.");
4099-
return false;
4097+
return util::Error{_("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.")};
41004098
}
41014099
uint256 id = desc_spkm->GetID();
41024100
AddScriptPubKeyMan(id, std::move(desc_spkm));
41034101
}
41044102

41054103
// Remove the LegacyScriptPubKeyMan from disk
41064104
if (!legacy_spkm->DeleteRecords()) {
4107-
return false;
4105+
return util::Error{_("Error: cannot remove legacy wallet records")};
41084106
}
41094107

41104108
// Remove the LegacyScriptPubKeyMan from memory
@@ -4127,8 +4125,7 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
41274125
// Get best block locator so that we can copy it to the watchonly and solvables
41284126
CBlockLocator best_block_locator;
41294127
if (!WalletBatch(GetDatabase()).ReadBestBlock(best_block_locator)) {
4130-
error = _("Error: Unable to read wallet's best block locator record");
4131-
return false;
4128+
return util::Error{_("Error: Unable to read wallet's best block locator record")};
41324129
}
41334130

41344131
// Check if the transactions in the wallet are still ours. Either they belong here, or they belong in the watchonly wallet.
@@ -4143,15 +4140,13 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
41434140
watchonly_batch->WriteOrderPosNext(data.watchonly_wallet->nOrderPosNext);
41444141
// Write the best block locator to avoid rescanning on reload
41454142
if (!watchonly_batch->WriteBestBlock(best_block_locator)) {
4146-
error = _("Error: Unable to write watchonly wallet best block locator record");
4147-
return false;
4143+
return util::Error{_("Error: Unable to write watchonly wallet best block locator record")};
41484144
}
41494145
}
41504146
if (data.solvable_wallet) {
41514147
// Write the best block locator to avoid rescanning on reload
41524148
if (!WalletBatch(data.solvable_wallet->GetDatabase()).WriteBestBlock(best_block_locator)) {
4153-
error = _("Error: Unable to write solvable wallet best block locator record");
4154-
return false;
4149+
return util::Error{_("Error: Unable to write solvable wallet best block locator record")};
41554150
}
41564151
}
41574152
for (const auto& [_pos, wtx] : wtxOrdered) {
@@ -4170,8 +4165,7 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
41704165
ins_wtx.CopyFrom(to_copy_wtx);
41714166
return true;
41724167
})) {
4173-
error = strprintf(_("Error: Could not add watchonly tx %s to watchonly wallet"), wtx->GetHash().GetHex());
4174-
return false;
4168+
return util::Error{strprintf(_("Error: Could not add watchonly tx %s to watchonly wallet"), wtx->GetHash().GetHex())};
41754169
}
41764170
watchonly_batch->WriteTx(data.watchonly_wallet->mapWallet.at(hash));
41774171
// Mark as to remove from the migrated wallet only if it does not also belong to it
@@ -4183,16 +4177,14 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
41834177
}
41844178
if (!is_mine) {
41854179
// Both not ours and not in the watchonly wallet
4186-
error = strprintf(_("Error: Transaction %s in wallet cannot be identified to belong to migrated wallets"), wtx->GetHash().GetHex());
4187-
return false;
4180+
return util::Error{strprintf(_("Error: Transaction %s in wallet cannot be identified to belong to migrated wallets"), wtx->GetHash().GetHex())};
41884181
}
41894182
}
41904183
watchonly_batch.reset(); // Flush
41914184
// Do the removes
41924185
if (txids_to_delete.size() > 0) {
41934186
if (auto res = RemoveTxs(txids_to_delete); !res) {
4194-
error = _("Error: Could not delete watchonly transactions. ") + util::ErrorString(res);
4195-
return false;
4187+
return util::Error{_("Error: Could not delete watchonly transactions. ") + util::ErrorString(res)};
41964188
}
41974189
}
41984190

@@ -4203,8 +4195,7 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
42034195

42044196
std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(ext_wallet->GetDatabase());
42054197
if (!batch->TxnBegin()) {
4206-
error = strprintf(_("Error: database transaction cannot be executed for wallet %s"), ext_wallet->GetName());
4207-
return false;
4198+
return util::Error{strprintf(_("Error: database transaction cannot be executed for wallet %s"), ext_wallet->GetName())};
42084199
}
42094200
wallets_vec.emplace_back(ext_wallet, std::move(batch));
42104201
}
@@ -4254,16 +4245,14 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
42544245
continue;
42554246
}
42564247

4257-
error = _("Error: Address book data in wallet cannot be identified to belong to migrated wallets");
4258-
return false;
4248+
return util::Error{_("Error: Address book data in wallet cannot be identified to belong to migrated wallets")};
42594249
}
42604250
}
42614251

42624252
// Persist external wallets address book entries
42634253
for (auto& [wallet, batch] : wallets_vec) {
42644254
if (!batch->TxnCommit()) {
4265-
error = strprintf(_("Error: address book copy failed for wallet %s"), wallet->GetName());
4266-
return false;
4255+
return util::Error{strprintf(_("Error: address book copy failed for wallet %s"), wallet->GetName())};
42674256
}
42684257
}
42694258

@@ -4273,16 +4262,15 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
42734262
if (dests_to_delete.size() > 0) {
42744263
for (const auto& dest : dests_to_delete) {
42754264
if (!DelAddressBookWithDB(local_wallet_batch, dest)) {
4276-
error = _("Error: Unable to remove watchonly address book data");
4277-
return false;
4265+
return util::Error{_("Error: Unable to remove watchonly address book data")};
42784266
}
42794267
}
42804268
}
42814269
local_wallet_batch.TxnCommit();
42824270

42834271
WalletLogPrintf("Wallet migration complete.\n");
42844272

4285-
return true;
4273+
return {}; // all good
42864274
}
42874275

42884276
bool CWallet::CanGrindR() const
@@ -4393,7 +4381,8 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error,
43934381
}
43944382

43954383
// Add the descriptors to wallet, remove LegacyScriptPubKeyMan, and cleanup txs and address book data
4396-
if (!wallet.ApplyMigrationData(*data, error)) {
4384+
if (auto res_migration = wallet.ApplyMigrationData(*data); !res_migration) {
4385+
error = util::ErrorString(res_migration);
43974386
return false;
43984387
}
43994388
return true;

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10511051

10521052
//! Adds the ScriptPubKeyMans given in MigrationData to this wallet, removes LegacyScriptPubKeyMan,
10531053
//! and where needed, moves tx and address book entries to watchonly_wallet or solvable_wallet
1054-
bool ApplyMigrationData(MigrationData& data, bilingual_str& error) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1054+
util::Result<void> ApplyMigrationData(MigrationData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10551055

10561056
//! Whether the (external) signer performs R-value signature grinding
10571057
bool CanGrindR() const;

0 commit comments

Comments
 (0)