Skip to content

Commit c98fc36

Browse files
committed
wallet: migration, consolidate external wallets db writes
Perform a single db write operation for each external wallet (watch-only and solvables) for the entire migration procedure.
1 parent 7c9076a commit c98fc36

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,7 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
41294129
std::unique_ptr<WalletBatch> watchonly_batch;
41304130
if (data.watchonly_wallet) {
41314131
watchonly_batch = std::make_unique<WalletBatch>(data.watchonly_wallet->GetDatabase());
4132+
if (!watchonly_batch->TxnBegin()) return util::Error{strprintf(_("Error: database transaction cannot be executed for wallet %s"), data.watchonly_wallet->GetName())};
41324133
// Copy the next tx order pos to the watchonly wallet
41334134
LOCK(data.watchonly_wallet->cs_wallet);
41344135
data.watchonly_wallet->nOrderPosNext = nOrderPosNext;
@@ -4138,9 +4139,12 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
41384139
return util::Error{_("Error: Unable to write watchonly wallet best block locator record")};
41394140
}
41404141
}
4142+
std::unique_ptr<WalletBatch> solvables_batch;
41414143
if (data.solvable_wallet) {
4144+
solvables_batch = std::make_unique<WalletBatch>(data.solvable_wallet->GetDatabase());
4145+
if (!solvables_batch->TxnBegin()) return util::Error{strprintf(_("Error: database transaction cannot be executed for wallet %s"), data.solvable_wallet->GetName())};
41424146
// Write the best block locator to avoid rescanning on reload
4143-
if (!WalletBatch(data.solvable_wallet->GetDatabase()).WriteBestBlock(best_block_locator)) {
4147+
if (!solvables_batch->WriteBestBlock(best_block_locator)) {
41444148
return util::Error{_("Error: Unable to write solvable wallet best block locator record")};
41454149
}
41464150
}
@@ -4175,7 +4179,7 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
41754179
return util::Error{strprintf(_("Error: Transaction %s in wallet cannot be identified to belong to migrated wallets"), wtx->GetHash().GetHex())};
41764180
}
41774181
}
4178-
watchonly_batch.reset(); // Flush
4182+
41794183
// Do the removes
41804184
if (txids_to_delete.size() > 0) {
41814185
if (auto res = RemoveTxs(local_wallet_batch, txids_to_delete); !res) {
@@ -4185,15 +4189,8 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
41854189

41864190
// Pair external wallets with their corresponding db handler
41874191
std::vector<std::pair<std::shared_ptr<CWallet>, std::unique_ptr<WalletBatch>>> wallets_vec;
4188-
for (const auto& ext_wallet : {data.watchonly_wallet, data.solvable_wallet}) {
4189-
if (!ext_wallet) continue;
4190-
4191-
std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(ext_wallet->GetDatabase());
4192-
if (!batch->TxnBegin()) {
4193-
return util::Error{strprintf(_("Error: database transaction cannot be executed for wallet %s"), ext_wallet->GetName())};
4194-
}
4195-
wallets_vec.emplace_back(ext_wallet, std::move(batch));
4196-
}
4192+
if (data.watchonly_wallet) wallets_vec.emplace_back(data.watchonly_wallet, std::move(watchonly_batch));
4193+
if (data.solvable_wallet) wallets_vec.emplace_back(data.solvable_wallet, std::move(solvables_batch));
41974194

41984195
// Write address book entry to disk
41994196
auto func_store_addr = [](WalletBatch& batch, const CTxDestination& dest, const CAddressBookData& entry) {
@@ -4247,7 +4244,7 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
42474244
// Persist external wallets address book entries
42484245
for (auto& [wallet, batch] : wallets_vec) {
42494246
if (!batch->TxnCommit()) {
4250-
return util::Error{strprintf(_("Error: address book copy failed for wallet %s"), wallet->GetName())};
4247+
return util::Error{strprintf(_("Error: Unable to write data to disk for wallet %s"), wallet->GetName())};
42514248
}
42524249
}
42534250

0 commit comments

Comments
 (0)