From 6e51b3bddf782f53527cf968445b298ebdec9bbc Mon Sep 17 00:00:00 2001 From: patrick s Date: Wed, 28 Aug 2013 23:53:26 -0700 Subject: improve wallet load time by removing duplicated calls to EC_KEY_check_key and adding a hash for vchPubKey/vchPrivKey entries in wallet.dat backwards compatible with previous wallet.dat format --- src/walletdb.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/walletdb.cpp') diff --git a/src/walletdb.cpp b/src/walletdb.cpp index 554d14002..f7edd0845 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -306,6 +306,8 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, } CKey key; CPrivKey pkey; + uint256 hash = 0; + if (strType == "key") { wss.nKeys++; @@ -315,14 +317,34 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> wkey; pkey = wkey.vchPrivKey; } - if (!key.SetPrivKey(pkey, vchPubKey.IsCompressed())) + try { - strErr = "Error reading wallet database: CPrivKey corrupt"; - return false; + ssValue >> hash; + } + catch(...){} + + bool fSkipCheck = false; + + if (hash != 0) + { + // hash pubkey/privkey to accelerate wallet load + std::vector vchKey; + vchKey.reserve(vchPubKey.size() + pkey.size()); + vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); + vchKey.insert(vchKey.end(), pkey.begin(), pkey.end()); + + if (Hash(vchKey.begin(), vchKey.end()) != hash) + { + strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt"; + return false; + } + + fSkipCheck = true; } - if (key.GetPubKey() != vchPubKey) + + if (!key.Load(pkey, vchPubKey, fSkipCheck)) { - strErr = "Error reading wallet database: CPrivKey pubkey inconsistency"; + strErr = "Error reading wallet database: CPrivKey corrupt"; return false; } if (!pwallet->LoadKey(key, vchPubKey)) -- cgit v1.2.3 From bc68788317a4ece16f0cfb0cb7eb1e0e220cbc6f Mon Sep 17 00:00:00 2001 From: phantomcircuit Date: Sat, 12 Oct 2013 23:44:28 -0700 Subject: comment explaining new wallet format for key/wkey entries --- src/walletdb.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/walletdb.cpp') diff --git a/src/walletdb.cpp b/src/walletdb.cpp index f7edd0845..d31db5e42 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -317,6 +317,12 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> wkey; pkey = wkey.vchPrivKey; } + + // Old wallets store keys as "key" [pubkey] => [privkey] + // ... which was slow for wallets with lots of keys, because the public key is re-derived from the private key + // using EC operations as a checksum. + // Newer wallets store keys as "key"[pubkey] => [privkey][hash(pubkey,privkey)], which is much faster while + // remaining backwards-compatible. try { ssValue >> hash; -- cgit v1.2.3