Skip to content

Commit 7c9076a

Browse files
committed
wallet: migration, consolidate main wallet db writes
Perform a single db write operation for the entire migration procedure.
1 parent 9ef20e8 commit 7c9076a

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/wallet/wallet.cpp

+13-16
Original file line numberDiff line numberDiff line change
@@ -4068,7 +4068,7 @@ std::optional<MigrationData> CWallet::GetDescriptorsForLegacy(bilingual_str& err
40684068
return res;
40694069
}
40704070

4071-
util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
4071+
util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch, MigrationData& data)
40724072
{
40734073
AssertLockHeld(cs_wallet);
40744074

@@ -4096,7 +4096,7 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
40964096
}
40974097

40984098
// Remove the LegacyScriptPubKeyMan from disk
4099-
if (!legacy_spkm->DeleteRecords()) {
4099+
if (!legacy_spkm->DeleteRecordsWithDB(local_wallet_batch)) {
41004100
return util::Error{_("Error: cannot remove legacy wallet records")};
41014101
}
41024102

@@ -4106,9 +4106,8 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
41064106
m_internal_spk_managers.clear();
41074107

41084108
// Setup new descriptors
4109-
SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
4109+
SetWalletFlagWithDB(local_wallet_batch, WALLET_FLAG_DESCRIPTORS);
41104110
if (!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
4111-
WalletBatch local_wallet_batch(GetDatabase());
41124111
// Use the existing master key if we have it
41134112
if (data.master_key.key.IsValid()) {
41144113
SetupDescriptorScriptPubKeyMans(local_wallet_batch, data.master_key);
@@ -4120,7 +4119,7 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
41204119

41214120
// Get best block locator so that we can copy it to the watchonly and solvables
41224121
CBlockLocator best_block_locator;
4123-
if (!WalletBatch(GetDatabase()).ReadBestBlock(best_block_locator)) {
4122+
if (!local_wallet_batch.ReadBestBlock(best_block_locator)) {
41244123
return util::Error{_("Error: Unable to read wallet's best block locator record")};
41254124
}
41264125

@@ -4179,7 +4178,7 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
41794178
watchonly_batch.reset(); // Flush
41804179
// Do the removes
41814180
if (txids_to_delete.size() > 0) {
4182-
if (auto res = RemoveTxs(txids_to_delete); !res) {
4181+
if (auto res = RemoveTxs(local_wallet_batch, txids_to_delete); !res) {
41834182
return util::Error{_("Error: Could not delete watchonly transactions. ") + util::ErrorString(res)};
41844183
}
41854184
}
@@ -4253,18 +4252,13 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
42534252
}
42544253

42554254
// Remove the things to delete in this wallet
4256-
WalletBatch local_wallet_batch(GetDatabase());
4257-
local_wallet_batch.TxnBegin();
42584255
if (dests_to_delete.size() > 0) {
42594256
for (const auto& dest : dests_to_delete) {
42604257
if (!DelAddressBookWithDB(local_wallet_batch, dest)) {
42614258
return util::Error{_("Error: Unable to remove watchonly address book data")};
42624259
}
42634260
}
42644261
}
4265-
local_wallet_batch.TxnCommit();
4266-
4267-
WalletLogPrintf("Wallet migration complete.\n");
42684262

42694263
return {}; // all good
42704264
}
@@ -4377,11 +4371,14 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error,
43774371
}
43784372

43794373
// Add the descriptors to wallet, remove LegacyScriptPubKeyMan, and cleanup txs and address book data
4380-
if (auto res_migration = wallet.ApplyMigrationData(*data); !res_migration) {
4381-
error = util::ErrorString(res_migration);
4382-
return false;
4383-
}
4384-
return true;
4374+
return RunWithinTxn(wallet.GetDatabase(), /*process_desc=*/"apply migration process", [&](WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet){
4375+
if (auto res_migration = wallet.ApplyMigrationData(batch, *data); !res_migration) {
4376+
error = util::ErrorString(res_migration);
4377+
return false;
4378+
}
4379+
wallet.WalletLogPrintf("Wallet migration complete.\n");
4380+
return true;
4381+
});
43854382
}
43864383

43874384
util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& wallet_name, const SecureString& passphrase, WalletContext& context)

src/wallet/wallet.h

+1-1
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-
util::Result<void> ApplyMigrationData(MigrationData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1054+
util::Result<void> ApplyMigrationData(WalletBatch& local_wallet_batch, 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)