From a3bb1966eb80acaa213c9ef5ec75665a0b2f6c62 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Tue, 4 Feb 2020 19:46:48 +0000 Subject: [PATCH] Add constant for how far a block timestamp can be ahead of adjusted time. Loosely based on https://github.com/bitcoin/bitcoin/commit/e57a1fd8999800b3fc744d45bb96354cae294032 Signed-off-by: Daira Hopwood --- src/chain.h | 16 ++++++++++++++++ src/main.cpp | 2 +- src/wallet/rpcdump.cpp | 3 ++- src/wallet/wallet.cpp | 8 +++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/chain.h b/src/chain.h index 7512b06b4..fa5e3e29d 100644 --- a/src/chain.h +++ b/src/chain.h @@ -17,6 +17,22 @@ static const int SPROUT_VALUE_VERSION = 1001400; static const int SAPLING_VALUE_VERSION = 1010100; +/** + * Maximum amount of time that a block timestamp is allowed to be ahead of the + * current network-adjusted time. + */ +static const int64_t MAX_FUTURE_BLOCK_TIME_ADJUSTED = 2 * 60 * 60; + +/** + * Timestamp window used as a grace period by code that compares external + * timestamps (such as timestamps passed to RPCs, or wallet key creation times) + * to block timestamps. + */ +static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME_ADJUSTED + 60; + +static_assert(TIMESTAMP_WINDOW > MAX_FUTURE_BLOCK_TIME_ADJUSTED); + + class CBlockFileInfo { public: diff --git a/src/main.cpp b/src/main.cpp index ac47a9daf..a44a4fa0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3785,7 +3785,7 @@ bool ContextualCheckBlockHeader( } // Check timestamp - auto nTimeLimit = GetAdjustedTime() + 2 * 60 * 60; + auto nTimeLimit = GetAdjustedTime() + MAX_FUTURE_BLOCK_TIME_ADJUSTED; if (block.GetBlockTime() > nTimeLimit) { return state.Invalid(error("%s: block at height %d. timestamp %d is too far ahead of adjusted time, limit is %d", __func__, nHeight, block.GetBlockTime(), nTimeLimit), diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index cad0cb0f5..d59e8aff0 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -448,8 +448,9 @@ UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys pwalletMain->ShowProgress("", 100); // hide progress dialog in GUI CBlockIndex *pindex = chainActive.Tip(); - while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - 7200) + while (pindex && pindex->pprev && pindex->GetBlockTime() > nTimeBegin - TIMESTAMP_WINDOW) { pindex = pindex->pprev; + } if (!pwalletMain->nTimeFirstKey || nTimeBegin < pwalletMain->nTimeFirstKey) pwalletMain->nTimeFirstKey = nTimeBegin; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index fc74f0677..1608c8066 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2523,8 +2523,9 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate) // no need to read and scan block, if block was created before // our wallet birthday (as adjusted for block time variability) - while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200))) + while (pindex && nTimeFirstKey && pindex->GetBlockTime() < nTimeFirstKey - TIMESTAMP_WINDOW) { pindex = chainActive.Next(pindex); + } ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup double dProgressStart = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false); @@ -4347,8 +4348,9 @@ void CWallet::GetKeyBirthTimes(std::map &mapKeyBirth) const { } // Extract block timestamps for those keys - for (std::map::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) - mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off + for (std::map::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) { + mapKeyBirth[it->first] = it->second->GetBlockTime() - TIMESTAMP_WINDOW; // block times can be off + } } bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)