diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index ee8b0a44c5..c1a71ed749 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -117,7 +117,6 @@ int main(int argc, char* argv[]) const ChainstateManager::Options chainman_opts{ .chainparams = *chainparams, .datadir = abs_datadir, - .adjusted_time_callback = NodeClock::now, .notifications = *notifications, }; const node::BlockManager::Options blockman_opts{ diff --git a/src/headerssync.cpp b/src/headerssync.cpp index 234fc8da60..e1dfe10483 100644 --- a/src/headerssync.cpp +++ b/src/headerssync.cpp @@ -41,7 +41,7 @@ HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus // exceeds this bound, because it's not possible for a consensus-valid // chain to be longer than this (at the current time -- in the future we // could try again, if necessary, to sync a longer chain). - m_max_commitments = 6*(Ticks(GetAdjustedTime() - NodeSeconds{std::chrono::seconds{chain_start->GetMedianTimePast()}}) + MAX_FUTURE_BLOCK_TIME) / HEADER_COMMITMENT_PERIOD; + m_max_commitments = 6*(Ticks(NodeClock::now() - NodeSeconds{std::chrono::seconds{chain_start->GetMedianTimePast()}}) + MAX_FUTURE_BLOCK_TIME) / HEADER_COMMITMENT_PERIOD; LogPrint(BCLog::NET, "Initial headers sync started with peer=%d: height=%i, max_commitments=%i, min_work=%s\n", m_id, m_current_height, m_max_commitments, m_minimum_required_work.ToString()); } diff --git a/src/init.cpp b/src/init.cpp index 4245b7bb47..c777da00a6 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1589,7 +1589,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) ChainstateManager::Options chainman_opts{ .chainparams = chainparams, .datadir = args.GetDataDirNet(), - .adjusted_time_callback = GetAdjustedTime, .minimum_chain_work = UintToArith256(chainparams.GetConsensus().nMinimumChainWork), .assumed_valid_block = chainparams.GetConsensus().defaultAssumeValid, .notifications = *node.notifications, diff --git a/src/kernel/chainstatemanager_opts.h b/src/kernel/chainstatemanager_opts.h index ee20eabd79..864aac336e 100644 --- a/src/kernel/chainstatemanager_opts.h +++ b/src/kernel/chainstatemanager_opts.h @@ -32,7 +32,6 @@ namespace kernel { struct ChainstateManagerOpts { const CChainParams& chainparams; fs::path datadir; - const std::function adjusted_time_callback{nullptr}; std::optional check_block_index{}; bool checkpoints_enabled{DEFAULT_CHECKPOINTS_ENABLED}; //! If set, it will override the minimum work we will assume exists on some valid chain. diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 46d38a479e..53c75656bf 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1341,7 +1341,7 @@ bool PeerManagerImpl::CanDirectFetch() LogPrint(BCLog::NET, "Tried to call CanDirectFetch with no currently-active chain.\n"); return false; } - return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20; + return m_chainman.ActiveChain().Tip()->Time() > NodeClock::now() - m_chainparams.GetConsensus().PowTargetSpacing() * 20; } static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) @@ -5671,7 +5671,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) bool got_enough_headers = node::fTrimHeaders && (headers_ahead >= node::nHeaderDownloadBuffer); if (!state.fSyncStarted && CanServeBlocks(*peer) && !m_chainman.m_blockman.LoadingBlocks() && !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->Time() > GetAdjustedTime() - 24h) { + if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > NodeClock::now() - 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 @@ -5691,7 +5691,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} * - Ticks(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing + Ticks(NodeClock::now() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing ); nSyncStarted++; } @@ -5997,7 +5997,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) // Check for headers sync timeouts if (state.fSyncStarted && peer->m_headers_sync_timeout < std::chrono::microseconds::max()) { // Detect whether this is a stalling initial-headers-sync peer - if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) { + if (m_chainman.m_best_header->Time() <= NodeClock::now() - 24h) { if (current_time > peer->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/interfaces.cpp b/src/node/interfaces.cpp index 7a458bdae3..46fbf7d217 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -834,7 +834,7 @@ public: bool testBlockValidity(BlockValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot) override { LOCK(::cs_main); Assert(m_node.chainman); - return TestBlockValidity(state, chainparams, m_node.chainman->ActiveChainstate(), block, pindexPrev, GetAdjustedTime, fCheckPOW, fCheckMerkleRoot); + return TestBlockValidity(state, chainparams, m_node.chainman->ActiveChainstate(), block, pindexPrev, fCheckPOW, fCheckMerkleRoot); } // end ELEMENTS diff --git a/src/node/miner.cpp b/src/node/miner.cpp index 76c134dafe..5c7e7a2946 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -43,7 +43,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, TicksSinceEpoch(GetAdjustedTime()))}; + int64_t nNewTime{std::max(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch(NodeClock::now()))}; 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 = TicksSinceEpoch(GetAdjustedTime()); + pblock->nTime = TicksSinceEpoch(NodeClock::now()); m_lock_time_cutoff = pindexPrev->GetMedianTimePast(); // ELEMENTS: @@ -224,7 +224,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc BlockValidationState state; if (m_options.test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, - GetAdjustedTime, /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) { + /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) { throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString())); } const auto time_2{SteadyClock::now()}; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 53c75ae110..dc38ce77bb 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -403,7 +403,7 @@ static RPCHelpMan generateblock() LOCK(cs_main); BlockValidationState state; - if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), GetAdjustedTime, false, false)) { + if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) { throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString())); } } @@ -719,7 +719,7 @@ static RPCHelpMan getblocktemplate() if (block.hashPrevBlock != pindexPrev->GetBlockHash()) return "inconclusive-not-best-prevblk"; BlockValidationState state; - TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, GetAdjustedTime, false, true); + TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, false, true); return BIP22ValidationResult(state); } @@ -1600,7 +1600,7 @@ static RPCHelpMan testproposedblock() throw JSONRPCError(RPC_VERIFY_ERROR, "proposal was not based on our best chain"); BlockValidationState state; - if (!TestBlockValidity(state, Params(), chainman.ActiveChainstate(), block, pindexPrev, GetAdjustedTime, false, true) || !state.IsValid()) { + if (!TestBlockValidity(state, Params(), chainman.ActiveChainstate(), block, pindexPrev, false, true) || !state.IsValid()) { std::string strRejectReason = state.GetRejectReason(); if (strRejectReason.empty()) throw JSONRPCError(RPC_VERIFY_ERROR, state.IsInvalid() ? "Block proposal was invalid" : "Error checking block proposal"); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index da1e194cc9..08bbe227dd 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -203,7 +203,6 @@ ChainTestingSetup::ChainTestingSetup(const ChainType chainType, const std::vecto const ChainstateManager::Options chainman_opts{ .chainparams = chainparams, .datadir = m_args.GetDataDirNet(), - .adjusted_time_callback = GetAdjustedTime, .check_block_index = true, .minimum_chain_work = std::nullopt, .assumed_valid_block = std::nullopt, diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp index 81c797cc25..8f2a7fecd4 100644 --- a/src/test/validation_chainstatemanager_tests.cpp +++ b/src/test/validation_chainstatemanager_tests.cpp @@ -384,7 +384,6 @@ struct SnapshotTestSetup : TestChain100Setup { const ChainstateManager::Options chainman_opts{ .chainparams = ::Params(), .datadir = chainman.m_options.datadir, - .adjusted_time_callback = GetAdjustedTime, .minimum_chain_work = std::nullopt, .assumed_valid_block = std::nullopt, .notifications = *m_node.notifications, diff --git a/src/timedata.cpp b/src/timedata.cpp index 15ca90ee6a..d948de976f 100644 --- a/src/timedata.cpp +++ b/src/timedata.cpp @@ -33,11 +33,6 @@ int64_t GetTimeOffset() return nTimeOffset; } -NodeClock::time_point GetAdjustedTime() -{ - return NodeClock::now() + std::chrono::seconds{GetTimeOffset()}; -} - #define BITCOIN_TIMEDATA_MAX_SAMPLES 200 static std::set g_sources; diff --git a/src/timedata.h b/src/timedata.h index c6c36d9a39..90428d071c 100644 --- a/src/timedata.h +++ b/src/timedata.h @@ -75,11 +75,10 @@ public: /** Functions to keep track of adjusted P2P time */ int64_t GetTimeOffset(); -NodeClock::time_point GetAdjustedTime(); void AddTimeData(const CNetAddr& ip, int64_t nTime); /** - * Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData(). + * Reset the internal state of GetTimeOffset() and AddTimeData(). */ void TestOnlyResetTimeData(); diff --git a/src/validation.cpp b/src/validation.cpp index 6f6b8664b3..64afcb2a50 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4215,7 +4215,7 @@ arith_uint256 CalculateHeadersWork(const std::vector& headers) * 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, CBlockIndex* pindexPrev, NodeClock::time_point now) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) +static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, CBlockIndex* pindexPrev) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { AssertLockHeld(::cs_main); assert(pindexPrev != nullptr); @@ -4250,7 +4250,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio } // Check timestamp - if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) { + if (block.Time() > NodeClock::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"); } @@ -4413,7 +4413,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida LogPrint(BCLog::VALIDATION, "header %s has prev block invalid: %s\n", hash.ToString(), block.hashPrevBlock.ToString()); return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk"); } - if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev, m_options.adjusted_time_callback())) { + if (!ContextualCheckBlockHeader(block, state, m_blockman, *this, pindexPrev)) { LogPrint(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString()); return false; } @@ -4706,7 +4706,6 @@ bool TestBlockValidity(BlockValidationState& state, Chainstate& chainstate, const CBlock& block, CBlockIndex* pindexPrev, - const std::function& adjusted_time_callback, bool fCheckPOW, bool fCheckMerkleRoot) { @@ -4720,7 +4719,7 @@ bool TestBlockValidity(BlockValidationState& state, indexDummy.phashBlock = &block_hash; // NOTE: CheckBlockHeader is called by CheckBlock - if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev, adjusted_time_callback())) + if (!ContextualCheckBlockHeader(block, state, chainstate.m_blockman, chainstate.m_chainman, pindexPrev)) return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, state.ToString()); if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot)) return error("%s: Consensus::CheckBlock: %s", __func__, state.ToString()); @@ -6256,7 +6255,6 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts) if (!opts.check_block_index.has_value()) opts.check_block_index = opts.chainparams.DefaultConsistencyChecks(); if (!opts.minimum_chain_work.has_value()) opts.minimum_chain_work = UintToArith256(opts.chainparams.GetConsensus().nMinimumChainWork); if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid; - Assert(opts.adjusted_time_callback); return std::move(opts); } diff --git a/src/validation.h b/src/validation.h index f932aea41e..c1dcea2e36 100644 --- a/src/validation.h +++ b/src/validation.h @@ -386,7 +386,6 @@ bool TestBlockValidity(BlockValidationState& state, Chainstate& chainstate, const CBlock& block, CBlockIndex* pindexPrev, - const std::function& adjusted_time_callback, bool fCheckPOW = true, bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main); diff --git a/src/wallet/rpc/elements.cpp b/src/wallet/rpc/elements.cpp index 0de85c9572..a5defe105b 100644 --- a/src/wallet/rpc/elements.cpp +++ b/src/wallet/rpc/elements.cpp @@ -207,7 +207,6 @@ RPCHelpMan getpeginaddress() ChainstateManager::Options chain_opts{ .chainparams = chainparams, .datadir = gArgs.GetDataDirNet(), - .adjusted_time_callback = GetAdjustedTime, .minimum_chain_work = UintToArith256(consensus.nMinimumChainWork), .assumed_valid_block = consensus.defaultAssumeValid, .notifications = notifications,