Skip to content

Commit 904308d

Browse files
committed
Merge #15433: Use a single wallet batch for UpgradeKeyMetadata
0bedcba Use a single wallet batch for UpgradeKeyMetadata (Jonas Schnelli) Pull request description: Opening wallets (the first time) after #14021 took on my end around 30 seconds due to the keymetadata migration (tested on regtest). Using a single wallet batch reduces the required time for the migration down to <1s on my system for a default 2k keypool wallet. Tree-SHA512: f68739e452d382f5294186f47511b94884a1a0868688dd3179034a7e091a67f93bc9dd45cdfc9fa6b1fe90362772b719278012f2f56b752b803c87db8597a7b0
2 parents f78cd3d + 0bedcba commit 904308d

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)