aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRussell Yanofsky <russ@yanofsky.org>2016-11-08 16:28:20 -0500
committerRussell Yanofsky <russ@yanofsky.org>2017-02-10 15:49:00 -0500
commita58370e6a2d4dce50eefbcab5bde9f14facef8fc (patch)
treeb5c4fd59bba41e4383f1536d908f0fcedbe176f8 /src
parent266a8114cbe2a87a6c84d7690a7716a18d782c56 (diff)
downloadbitcoin-a58370e6a2d4dce50eefbcab5bde9f14facef8fc.tar.xz
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.
Diffstat (limited to 'src')
-rw-r--r--src/wallet/rpcdump.cpp13
-rw-r--r--src/wallet/wallet.cpp19
-rw-r--r--src/wallet/wallet.h6
-rw-r--r--src/wallet/walletdb.cpp9
4 files changed, 24 insertions, 23 deletions
diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp
index 9310a320c7..f358d720cd 100644
--- a/src/wallet/rpcdump.cpp
+++ b/src/wallet/rpcdump.cpp
@@ -143,7 +143,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
// whenever a key is imported, we need to scan the whole chain
- pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value'
+ pwalletMain->UpdateTimeFirstKey(1);
if (fRescan) {
pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true);
@@ -500,8 +500,7 @@ UniValue importwallet(const JSONRPCRequest& request)
while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200)
pindex = pindex->pprev;
- if (!pwalletMain->nTimeFirstKey || nTimeBegin < pwalletMain->nTimeFirstKey)
- pwalletMain->nTimeFirstKey = nTimeBegin;
+ pwalletMain->UpdateTimeFirstKey(nTimeBegin);
LogPrintf("Rescanning last %i blocks\n", chainActive.Height() - pindex->nHeight + 1);
pwalletMain->ScanForWalletTransactions(pindex);
@@ -782,9 +781,7 @@ UniValue ProcessImport(const UniValue& data, const int64_t timestamp)
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
}
- if (timestamp < pwalletMain->nTimeFirstKey) {
- pwalletMain->nTimeFirstKey = timestamp;
- }
+ pwalletMain->UpdateTimeFirstKey(timestamp);
}
}
@@ -912,9 +909,7 @@ UniValue ProcessImport(const UniValue& data, const int64_t timestamp)
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
}
- if (timestamp < pwalletMain->nTimeFirstKey) {
- pwalletMain->nTimeFirstKey = timestamp;
- }
+ pwalletMain->UpdateTimeFirstKey(timestamp);
success = true;
}
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index a5e8752fc0..192af22cb8 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -113,8 +113,7 @@ CPubKey CWallet::GenerateNewKey()
assert(secret.VerifyPubKey(pubkey));
mapKeyMetadata[pubkey.GetID()] = metadata;
- if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
- nTimeFirstKey = nCreationTime;
+ UpdateTimeFirstKey(nCreationTime);
if (!AddKeyPubKey(secret, pubkey))
throw std::runtime_error(std::string(__func__) + ": AddKey failed");
@@ -210,9 +209,7 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
{
AssertLockHeld(cs_wallet); // mapKeyMetadata
- if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
- nTimeFirstKey = meta.nCreateTime;
-
+ UpdateTimeFirstKey(meta.nCreateTime);
mapKeyMetadata[pubkey.GetID()] = meta;
return true;
}
@@ -222,6 +219,18 @@ bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigne
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
}
+void CWallet::UpdateTimeFirstKey(int64_t nCreateTime)
+{
+ AssertLockHeld(cs_wallet);
+ if (nCreateTime <= 1) {
+ // Cannot determine birthday information, so set the wallet birthday to
+ // the beginning of time.
+ nTimeFirstKey = 1;
+ } else if (!nTimeFirstKey || nCreateTime < nTimeFirstKey) {
+ nTimeFirstKey = nCreateTime;
+ }
+}
+
bool CWallet::AddCScript(const CScript& redeemScript)
{
if (!CCryptoKeyStore::AddCScript(redeemScript))
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index ea4787c363..8d3ee9dddc 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -611,6 +611,9 @@ private:
bool fFileBacked;
std::set<int64_t> setKeyPool;
+
+ int64_t nTimeFirstKey;
+
public:
/*
* Main wallet lock.
@@ -688,8 +691,6 @@ public:
std::set<COutPoint> setLockedCoins;
- int64_t nTimeFirstKey;
-
const CWalletTx* GetWalletTx(const uint256& hash) const;
//! check whether we are allowed to upgrade (or already support) to the named feature
@@ -730,6 +731,7 @@ public:
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
+ void UpdateTimeFirstKey(int64_t nCreateTime);
//! Adds an encrypted key to the store, and saves it to disk.
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index b00ce36b70..15e1e4c45f 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -357,7 +357,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
// Watch-only addresses have no birthday information for now,
// so set the wallet birthday to the beginning of time.
- pwallet->nTimeFirstKey = 1;
+ pwallet->UpdateTimeFirstKey(1);
}
else if (strType == "key" || strType == "wkey")
{
@@ -467,11 +467,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
wss.nKeyMeta++;
pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
-
- // find earliest key creation time, as wallet birthday
- if (!pwallet->nTimeFirstKey ||
- (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
- pwallet->nTimeFirstKey = keyMeta.nCreateTime;
}
else if (strType == "defaultkey")
{
@@ -626,7 +621,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
// nTimeFirstKey is only reliable if all keys have metadata
if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
- pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
+ pwallet->UpdateTimeFirstKey(1);
BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
WriteTx(pwallet->mapWallet[hash]);