@@ -4068,7 +4068,7 @@ std::optional<MigrationData> CWallet::GetDescriptorsForLegacy(bilingual_str& err
4068
4068
return res;
4069
4069
}
4070
4070
4071
- util::Result<void > CWallet::ApplyMigrationData (MigrationData& data)
4071
+ util::Result<void > CWallet::ApplyMigrationData (WalletBatch& local_wallet_batch, MigrationData& data)
4072
4072
{
4073
4073
AssertLockHeld (cs_wallet);
4074
4074
@@ -4096,7 +4096,7 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
4096
4096
}
4097
4097
4098
4098
// Remove the LegacyScriptPubKeyMan from disk
4099
- if (!legacy_spkm->DeleteRecords ( )) {
4099
+ if (!legacy_spkm->DeleteRecordsWithDB (local_wallet_batch )) {
4100
4100
return util::Error{_ (" Error: cannot remove legacy wallet records" )};
4101
4101
}
4102
4102
@@ -4106,9 +4106,8 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
4106
4106
m_internal_spk_managers.clear ();
4107
4107
4108
4108
// Setup new descriptors
4109
- SetWalletFlag ( WALLET_FLAG_DESCRIPTORS);
4109
+ SetWalletFlagWithDB (local_wallet_batch, WALLET_FLAG_DESCRIPTORS);
4110
4110
if (!IsWalletFlagSet (WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
4111
- WalletBatch local_wallet_batch (GetDatabase ());
4112
4111
// Use the existing master key if we have it
4113
4112
if (data.master_key .key .IsValid ()) {
4114
4113
SetupDescriptorScriptPubKeyMans (local_wallet_batch, data.master_key );
@@ -4120,7 +4119,7 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
4120
4119
4121
4120
// Get best block locator so that we can copy it to the watchonly and solvables
4122
4121
CBlockLocator best_block_locator;
4123
- if (!WalletBatch ( GetDatabase ()) .ReadBestBlock (best_block_locator)) {
4122
+ if (!local_wallet_batch .ReadBestBlock (best_block_locator)) {
4124
4123
return util::Error{_ (" Error: Unable to read wallet's best block locator record" )};
4125
4124
}
4126
4125
@@ -4179,7 +4178,7 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
4179
4178
watchonly_batch.reset (); // Flush
4180
4179
// Do the removes
4181
4180
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) {
4183
4182
return util::Error{_ (" Error: Could not delete watchonly transactions. " ) + util::ErrorString (res)};
4184
4183
}
4185
4184
}
@@ -4253,18 +4252,13 @@ util::Result<void> CWallet::ApplyMigrationData(MigrationData& data)
4253
4252
}
4254
4253
4255
4254
// Remove the things to delete in this wallet
4256
- WalletBatch local_wallet_batch (GetDatabase ());
4257
- local_wallet_batch.TxnBegin ();
4258
4255
if (dests_to_delete.size () > 0 ) {
4259
4256
for (const auto & dest : dests_to_delete) {
4260
4257
if (!DelAddressBookWithDB (local_wallet_batch, dest)) {
4261
4258
return util::Error{_ (" Error: Unable to remove watchonly address book data" )};
4262
4259
}
4263
4260
}
4264
4261
}
4265
- local_wallet_batch.TxnCommit ();
4266
-
4267
- WalletLogPrintf (" Wallet migration complete.\n " );
4268
4262
4269
4263
return {}; // all good
4270
4264
}
@@ -4377,11 +4371,14 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error,
4377
4371
}
4378
4372
4379
4373
// 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
+ });
4385
4382
}
4386
4383
4387
4384
util::Result<MigrationResult> MigrateLegacyToDescriptor (const std::string& wallet_name, const SecureString& passphrase, WalletContext& context)
0 commit comments