Merge #15433: Use a single wallet batch for UpgradeKeyMetadata

0bedcbafd 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
This commit is contained in:
MeshCollider 2019-02-19 09:38:53 +13:00 committed by UdjinM6
parent ed0e2dd075
commit 7d02a25c70
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9

View File

@ -526,6 +526,8 @@ void CWallet::UpgradeKeyMetadata()
masterKey.SetSeed(vchSeed.data(), vchSeed.size()); masterKey.SetSeed(vchSeed.data(), vchSeed.size());
CKeyID master_id = masterKey.key.GetPubKey().GetID(); CKeyID master_id = masterKey.key.GetPubKey().GetID();
std::unique_ptr<WalletBatch> batch = MakeUnique<WalletBatch>(*database);
size_t cnt = 0;
for (auto& meta_pair : mapKeyMetadata) { for (auto& meta_pair : mapKeyMetadata) {
const CKeyID& keyid = meta_pair.first; const CKeyID& keyid = meta_pair.first;
CKeyMetadata& meta = meta_pair.second; CKeyMetadata& meta = meta_pair.second;
@ -546,9 +548,14 @@ void CWallet::UpgradeKeyMetadata()
} }
// Write meta to wallet // Write meta to wallet
WriteKeyMetadata(meta, mi->second.extPubKey.pubkey, true); batch->WriteKeyMetadata(meta, mi->second.extPubKey.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);
} }