Skip to content

Commit 86960cd

Browse files
committed
wallet: migration, batch addressbook records removal
Instead of doing one db transaction per removed record, we now batch all removals in a single db transaction. Speeding up the process and preventing the wallet from entering an inconsistent state when any of the intermediate writes fail.
1 parent 342c45f commit 86960cd

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/wallet/wallet.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,8 +2395,13 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& s
23952395

23962396
bool CWallet::DelAddressBook(const CTxDestination& address)
23972397
{
2398-
const std::string& dest = EncodeDestination(address);
23992398
WalletBatch batch(GetDatabase());
2399+
return DelAddressBookWithDB(batch, address);
2400+
}
2401+
2402+
bool CWallet::DelAddressBookWithDB(WalletBatch& batch, const CTxDestination& address)
2403+
{
2404+
const std::string& dest = EncodeDestination(address);
24002405
{
24012406
LOCK(cs_wallet);
24022407
// If we want to delete receiving addresses, we should avoid calling EraseAddressData because it will delete the previously_spent value. Could instead just erase the label so it becomes a change address, and keep the data.
@@ -4079,14 +4084,17 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
40794084
}
40804085

40814086
// Remove the things to delete in this wallet
4087+
WalletBatch local_wallet_batch(GetDatabase());
4088+
local_wallet_batch.TxnBegin();
40824089
if (dests_to_delete.size() > 0) {
40834090
for (const auto& dest : dests_to_delete) {
4084-
if (!DelAddressBook(dest)) {
4091+
if (!DelAddressBookWithDB(local_wallet_batch, dest)) {
40854092
error = _("Error: Unable to remove watchonly address book data");
40864093
return false;
40874094
}
40884095
}
40894096
}
4097+
local_wallet_batch.TxnCommit();
40904098

40914099
// Connect the SPKM signals
40924100
ConnectScriptPubKeyManNotifiers();

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
795795
bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::optional<AddressPurpose>& purpose);
796796

797797
bool DelAddressBook(const CTxDestination& address);
798+
bool DelAddressBookWithDB(WalletBatch& batch, const CTxDestination& address);
798799

799800
bool IsAddressPreviouslySpent(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
800801
bool SetAddressPreviouslySpent(WalletBatch& batch, const CTxDestination& dest, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

0 commit comments

Comments
 (0)