Skip to content

Commit 0bedcba

Browse files
committed
Use a single wallet batch for UpgradeKeyMetadata
1 parent b72c787 commit 0bedcba

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/wallet/wallet.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ void CWallet::UpgradeKeyMetadata()
371371
return;
372372
}
373373

374+
std::unique_ptr<WalletBatch> batch = MakeUnique<WalletBatch>(*database);
375+
size_t cnt = 0;
374376
for (auto& meta_pair : mapKeyMetadata) {
375377
CKeyMetadata& meta = meta_pair.second;
376378
if (!meta.hd_seed_id.IsNull() && !meta.has_key_origin && meta.hdKeypath != "s") { // If the hdKeypath is "s", that's the seed and it doesn't have a key origin
@@ -392,10 +394,15 @@ void CWallet::UpgradeKeyMetadata()
392394
// Write meta to wallet
393395
CPubKey pubkey;
394396
if (GetPubKey(meta_pair.first, pubkey)) {
395-
WriteKeyMetadata(meta, pubkey, true);
397+
batch->WriteKeyMetadata(meta, pubkey, true);
398+
if (++cnt % 1000 == 0) {
399+
// avoid creating overlarge in-memory batches in case the wallet contains large amounts of keys
400+
batch.reset(new WalletBatch(*database));
401+
}
396402
}
397403
}
398404
}
405+
batch.reset(); //write before setting the flag
399406
SetWalletFlag(WALLET_FLAG_KEY_ORIGIN_METADATA);
400407
}
401408

0 commit comments

Comments
 (0)