diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index 87f18371b1..7312ae45d4 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(GetTime), + .adjusted_time_callback = NodeClock::now, }; ChainstateManager chainman{chainman_opts}; diff --git a/src/chain.h b/src/chain.h index 69a58f597d..e3d03e516b 100644 --- a/src/chain.h +++ b/src/chain.h @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -357,6 +358,11 @@ public: */ bool HaveTxsDownloaded() const { return nChainTx != 0; } + NodeSeconds Time() const + { + return NodeSeconds{std::chrono::seconds{nTime}}; + } + int64_t GetBlockTime() const { return (int64_t)nTime; diff --git a/src/consensus/params.h b/src/consensus/params.h index 3f27839fc6..bfd0258aa3 100755 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -122,6 +123,10 @@ struct Params { bool fPowNoRetargeting; int64_t nPowTargetSpacing; int64_t nPowTargetTimespan; + std::chrono::seconds PowTargetSpacing() const + { + return std::chrono::seconds{nPowTargetSpacing}; + } int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; } /** The best chain should have at least this much work */ uint256 nMinimumChainWork; 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 + #include #include @@ -19,7 +21,7 @@ namespace kernel { */ struct ChainstateManagerOpts { const CChainParams& chainparams; - const std::function adjusted_time_callback{nullptr}; + const std::function adjusted_time_callback{nullptr}; }; } // namespace kernel diff --git a/src/net_processing.cpp b/src/net_processing.cpp index bc27b88cff..542487efba 100755 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1155,7 +1155,7 @@ bool PeerManagerImpl::CanDirectFetch() LogPrint(BCLog::NET, "Tried to call CanDirectFetch with no currently-active chain.\n"); return false; } - 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) @@ -4983,7 +4983,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) bool got_enough_headers = node::fTrimHeaders && (headers_ahead >= node::nHeaderDownloadBuffer); if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex && !got_enough_headers) { // 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 @@ -5003,7 +5003,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(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing ); nSyncStarted++; } @@ -5322,7 +5322,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 4b4409d0e1..481dcdf9a0 100755 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -42,7 +42,7 @@ void ResetProof(CBlockHeader& block) 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(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch(GetAdjustedTime()))}; if (nOldTime < nNewTime) { pblock->nTime = nNewTime; @@ -148,7 +148,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion); } - pblock->nTime = GetAdjustedTime(); + pblock->nTime = TicksSinceEpoch(GetAdjustedTime()); m_lock_time_cutoff = pindexPrev->GetMedianTimePast(); // ELEMENTS: diff --git a/src/primitives/block.h b/src/primitives/block.h index 1cb82c626e..bd8d139750 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -10,6 +10,7 @@ #include