Skip to content

Commit

Permalink
Merge bitcoin#15433: Use a single wallet batch for UpgradeKeyMetadata
Browse files Browse the repository at this point in the history
0bedcba Use a single wallet batch for UpgradeKeyMetadata (Jonas Schnelli)

Pull request description:

  Opening wallets (the first time) after bitcoin#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
  • Loading branch information
meshcollider authored and vijaydasmp committed Dec 31, 2021
1 parent 61612e7 commit ccc73a9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ void CWallet::UpgradeKeyMetadata()
return;
}

std::unique_ptr<WalletBatch> batch = MakeUnique<WalletBatch>(*database);
size_t cnt = 0;
for (auto& meta_pair : mapKeyMetadata) {
CKeyMetadata& meta = meta_pair.second;
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
Expand All @@ -540,10 +542,15 @@ void CWallet::UpgradeKeyMetadata()
// Write meta to wallet
CPubKey pubkey;
if (GetPubKey(meta_pair.first, pubkey)) {
WriteKeyMetadata(meta, pubkey, true);
batch->WriteKeyMetadata(meta, pubkey, true);
if (++cnt % 1000 == 0) {
// avoid creating overlarge in-memory batches in case the wallet contains large amounts of keys
batch.reset(new WalletBatch(*database));
}
}
}
}
batch.reset(); //write before setting the flag
SetWalletFlag(WALLET_FLAG_KEY_ORIGIN_METADATA);
}

Expand Down

0 comments on commit ccc73a9

Please sign in to comment.