diff options
author | MacroFake <falke.marco@gmail.com> | 2022-08-05 15:12:58 +0200 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-08-05 14:59:15 +0200 |
commit | eeee5ada23f2a71d245671556b6ecfdaabfeddf4 (patch) | |
tree | 3b2c3c56611816254a8a80eb2defca00d3421fde | |
parent | fa3be799fe951a7ea9b4de78d5a907c6db71eeb8 (diff) | |
download | bitcoin-eeee5ada23f2a71d245671556b6ecfdaabfeddf4.tar.xz |
Make adjusted time type safe
-rw-r--r-- | src/bitcoin-chainstate.cpp | 2 | ||||
-rw-r--r-- | src/kernel/chainstatemanager_opts.h | 4 | ||||
-rw-r--r-- | src/net_processing.cpp | 8 | ||||
-rw-r--r-- | src/node/miner.cpp | 4 | ||||
-rw-r--r-- | src/timedata.cpp | 4 | ||||
-rw-r--r-- | src/timedata.h | 2 | ||||
-rw-r--r-- | src/validation.cpp | 7 | ||||
-rw-r--r-- | src/validation.h | 4 |
8 files changed, 19 insertions, 16 deletions
diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index fc3f91d492..5afae2e0c6 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -82,7 +82,7 @@ int main(int argc, char* argv[]) // SETUP: Chainstate const ChainstateManager::Options chainman_opts{ .chainparams = chainparams, - .adjusted_time_callback = static_cast<int64_t (*)()>(GetTime), + .adjusted_time_callback = NodeClock::now, }; ChainstateManager chainman{chainman_opts}; diff --git a/src/kernel/chainstatemanager_opts.h b/src/kernel/chainstatemanager_opts.h index 510a1f9edc..520d0e8e75 100644 --- a/src/kernel/chainstatemanager_opts.h +++ b/src/kernel/chainstatemanager_opts.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H #define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H +#include <util/time.h> + #include <cstdint> #include <functional> @@ -19,7 +21,7 @@ namespace kernel { */ struct ChainstateManagerOpts { const CChainParams& chainparams; - const std::function<int64_t()> adjusted_time_callback{nullptr}; + const std::function<NodeClock::time_point()> adjusted_time_callback{nullptr}; }; } // namespace kernel diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 64c2a29245..3a8d6daa39 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1145,7 +1145,7 @@ bool PeerManagerImpl::TipMayBeStale() bool PeerManagerImpl::CanDirectFetch() { - return m_chainman.ActiveChain().Tip()->GetBlockTime() > GetAdjustedTime() - m_chainparams.GetConsensus().nPowTargetSpacing * 20; + return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20; } static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) @@ -4886,7 +4886,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) { // Only actively request headers from a single peer, unless we're close to today. - if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { + if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) { const CBlockIndex* pindexStart = m_chainman.m_best_header; /* If possible, start at the block preceding the currently best known header. This ensures that we always get a @@ -4906,7 +4906,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) // Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling // to maintain precision std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} * - (GetAdjustedTime() - m_chainman.m_best_header->GetBlockTime()) / consensusParams.nPowTargetSpacing + Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing ); nSyncStarted++; } @@ -5223,7 +5223,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) // Check for headers sync timeouts if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) { // Detect whether this is a stalling initial-headers-sync peer - if (m_chainman.m_best_header->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) { + if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) { if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) { // Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer, // and we have others we could be using instead. diff --git a/src/node/miner.cpp b/src/node/miner.cpp index 9db10feae4..f04742deeb 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -30,7 +30,7 @@ namespace node { int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) { int64_t nOldTime = pblock->nTime; - int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime()); + int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime()))}; if (nOldTime < nNewTime) { pblock->nTime = nNewTime; @@ -133,7 +133,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion); } - pblock->nTime = GetAdjustedTime(); + pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime()); m_lock_time_cutoff = pindexPrev->GetMedianTimePast(); int nPackagesSelected = 0; diff --git a/src/timedata.cpp b/src/timedata.cpp index ceee08e68c..fe9a5fbed7 100644 --- a/src/timedata.cpp +++ b/src/timedata.cpp @@ -32,9 +32,9 @@ int64_t GetTimeOffset() return nTimeOffset; } -int64_t GetAdjustedTime() +NodeClock::time_point GetAdjustedTime() { - return GetTime() + GetTimeOffset(); + return NodeClock::now() + std::chrono::seconds{GetTimeOffset()}; } #define BITCOIN_TIMEDATA_MAX_SAMPLES 200 diff --git a/src/timedata.h b/src/timedata.h index cfe5111644..669a571f47 100644 --- a/src/timedata.h +++ b/src/timedata.h @@ -75,7 +75,7 @@ public: /** Functions to keep track of adjusted P2P time */ int64_t GetTimeOffset(); -int64_t GetAdjustedTime(); +NodeClock::time_point GetAdjustedTime(); void AddTimeData(const CNetAddr& ip, int64_t nTime); /** diff --git a/src/validation.cpp b/src/validation.cpp index d30333f710..f9f6b6b0e4 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3442,7 +3442,7 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock& * in ConnectBlock(). * Note that -reindex-chainstate skips the validation that happens here! */ -static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) +static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, NodeClock::time_point now) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { AssertLockHeld(::cs_main); assert(pindexPrev != nullptr); @@ -3470,8 +3470,9 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early"); // Check timestamp - if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME) + if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) { return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future"); + } // Reject blocks with outdated version if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB)) || @@ -3832,7 +3833,7 @@ bool TestBlockValidity(BlockValidationState& state, CChainState& chainstate, const CBlock& block, CBlockIndex* pindexPrev, - const std::function<int64_t()>& adjusted_time_callback, + const std::function<NodeClock::time_point()>& adjusted_time_callback, bool fCheckPOW, bool fCheckMerkleRoot) { diff --git a/src/validation.h b/src/validation.h index ec9043ad7a..c0e6213247 100644 --- a/src/validation.h +++ b/src/validation.h @@ -336,7 +336,7 @@ bool TestBlockValidity(BlockValidationState& state, CChainState& chainstate, const CBlock& block, CBlockIndex* pindexPrev, - const std::function<int64_t()>& adjusted_time_callback, + const std::function<NodeClock::time_point()>& adjusted_time_callback, bool fCheckPOW = true, bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main); @@ -840,7 +840,7 @@ private: const CChainParams m_chainparams; - const std::function<int64_t()> m_adjusted_time_callback; + const std::function<NodeClock::time_point()> m_adjusted_time_callback; //! Internal helper for ActivateSnapshot(). [[nodiscard]] bool PopulateAndValidateSnapshot( |