Use a single wallet batch for UpgradeKeyMetadata

This commit is contained in:
Jonas Schnelli 2019-02-17 17:28:04 -10:00
parent b72c787dc8
commit 0bedcbafdf
No known key found for this signature in database
GPG key ID: 1EB776BB03C7922D

View file

@ -371,6 +371,8 @@ void CWallet::UpgradeKeyMetadata()
return; return;
} }
std::unique_ptr<WalletBatch> batch = MakeUnique<WalletBatch>(*database);
size_t cnt = 0;
for (auto& meta_pair : mapKeyMetadata) { for (auto& meta_pair : mapKeyMetadata) {
CKeyMetadata& meta = meta_pair.second; 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 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()
// Write meta to wallet // Write meta to wallet
CPubKey pubkey; CPubKey pubkey;
if (GetPubKey(meta_pair.first, 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); SetWalletFlag(WALLET_FLAG_KEY_ORIGIN_METADATA);
} }