Skip to content

Commit a58370e

Browse files
committed
Dedup nTimeFirstKey update logic
Also make nTimeFirstKey member variable private. This is just a cleanup change, it doesn't change behavior in any significant way.
1 parent 266a811 commit a58370e

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

src/wallet/rpcdump.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
143143
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
144144

145145
// whenever a key is imported, we need to scan the whole chain
146-
pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
146+
pwalletMain->UpdateTimeFirstKey(1);
147147

148148
if (fRescan) {
149149
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
@@ -500,8 +500,7 @@ UniValue importwallet(const JSONRPCRequest& request)
500500
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200)
501501
pindex = pindex->pprev;
502502

503-
if (!pwalletMain->nTimeFirstKey || nTimeBegin < pwalletMain->nTimeFirstKey)
504-
pwalletMain->nTimeFirstKey = nTimeBegin;
503+
pwalletMain->UpdateTimeFirstKey(nTimeBegin);
505504

506505
LogPrintf("Rescanning last %i blocks\n", chainActive.Height() - pindex->nHeight + 1);
507506
pwalletMain->ScanForWalletTransactions(pindex);
@@ -782,9 +781,7 @@ UniValue ProcessImport(const UniValue& data, const int64_t timestamp)
782781
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
783782
}
784783

785-
if (timestamp < pwalletMain->nTimeFirstKey) {
786-
pwalletMain->nTimeFirstKey = timestamp;
787-
}
784+
pwalletMain->UpdateTimeFirstKey(timestamp);
788785
}
789786
}
790787

@@ -912,9 +909,7 @@ UniValue ProcessImport(const UniValue& data, const int64_t timestamp)
912909
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
913910
}
914911

915-
if (timestamp < pwalletMain->nTimeFirstKey) {
916-
pwalletMain->nTimeFirstKey = timestamp;
917-
}
912+
pwalletMain->UpdateTimeFirstKey(timestamp);
918913

919914
success = true;
920915
}

src/wallet/wallet.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ CPubKey CWallet::GenerateNewKey()
113113
assert(secret.VerifyPubKey(pubkey));
114114

115115
mapKeyMetadata[pubkey.GetID()] = metadata;
116-
if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
117-
nTimeFirstKey = nCreationTime;
116+
UpdateTimeFirstKey(nCreationTime);
118117

119118
if (!AddKeyPubKey(secret, pubkey))
120119
throw std::runtime_error(std::string(__func__) + ": AddKey failed");
@@ -210,9 +209,7 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
210209
bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
211210
{
212211
AssertLockHeld(cs_wallet); // mapKeyMetadata
213-
if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
214-
nTimeFirstKey = meta.nCreateTime;
215-
212+
UpdateTimeFirstKey(meta.nCreateTime);
216213
mapKeyMetadata[pubkey.GetID()] = meta;
217214
return true;
218215
}
@@ -222,6 +219,18 @@ bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigne
222219
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
223220
}
224221

222+
void CWallet::UpdateTimeFirstKey(int64_t nCreateTime)
223+
{
224+
AssertLockHeld(cs_wallet);
225+
if (nCreateTime <= 1) {
226+
// Cannot determine birthday information, so set the wallet birthday to
227+
// the beginning of time.
228+
nTimeFirstKey = 1;
229+
} else if (!nTimeFirstKey || nCreateTime < nTimeFirstKey) {
230+
nTimeFirstKey = nCreateTime;
231+
}
232+
}
233+
225234
bool CWallet::AddCScript(const CScript& redeemScript)
226235
{
227236
if (!CCryptoKeyStore::AddCScript(redeemScript))

src/wallet/wallet.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
611611
bool fFileBacked;
612612

613613
std::set<int64_t> setKeyPool;
614+
615+
int64_t nTimeFirstKey;
616+
614617
public:
615618
/*
616619
* Main wallet lock.
@@ -688,8 +691,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
688691

689692
std::set<COutPoint> setLockedCoins;
690693

691-
int64_t nTimeFirstKey;
692-
693694
const CWalletTx* GetWalletTx(const uint256& hash) const;
694695

695696
//! check whether we are allowed to upgrade (or already support) to the named feature
@@ -730,6 +731,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
730731
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
731732

732733
bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
734+
void UpdateTimeFirstKey(int64_t nCreateTime);
733735

734736
//! Adds an encrypted key to the store, and saves it to disk.
735737
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);

src/wallet/walletdb.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
357357

358358
// Watch-only addresses have no birthday information for now,
359359
// so set the wallet birthday to the beginning of time.
360-
pwallet->nTimeFirstKey = 1;
360+
pwallet->UpdateTimeFirstKey(1);
361361
}
362362
else if (strType == "key" || strType == "wkey")
363363
{
@@ -467,11 +467,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
467467
wss.nKeyMeta++;
468468

469469
pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
470-
471-
// find earliest key creation time, as wallet birthday
472-
if (!pwallet->nTimeFirstKey ||
473-
(keyMeta.nCreateTime < pwallet->nTimeFirstKey))
474-
pwallet->nTimeFirstKey = keyMeta.nCreateTime;
475470
}
476471
else if (strType == "defaultkey")
477472
{
@@ -626,7 +621,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
626621

627622
// nTimeFirstKey is only reliable if all keys have metadata
628623
if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
629-
pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
624+
pwallet->UpdateTimeFirstKey(1);
630625

631626
BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
632627
WriteTx(pwallet->mapWallet[hash]);

0 commit comments

Comments
 (0)