From 18bf17e9b5eecfd77f033532d988dcff28de778b Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:44:07 +0100 Subject: [PATCH] add `is_timestamp_in_past` and `exceeds_allowed_future_timestamp` for `Header` (#6160) --- crates/consensus/beacon-core/src/lib.rs | 6 +++--- crates/consensus/common/src/validation.rs | 2 +- crates/primitives/src/header.rs | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/consensus/beacon-core/src/lib.rs b/crates/consensus/beacon-core/src/lib.rs index d2aad7b1c..ad04672f1 100644 --- a/crates/consensus/beacon-core/src/lib.rs +++ b/crates/consensus/beacon-core/src/lib.rs @@ -12,8 +12,8 @@ use reth_consensus_common::validation; use reth_interfaces::consensus::{Consensus, ConsensusError}; use reth_primitives::{ - constants::{ALLOWED_FUTURE_BLOCK_TIME_SECONDS, MAXIMUM_EXTRA_DATA_SIZE}, - Chain, ChainSpec, Hardfork, Header, SealedBlock, SealedHeader, EMPTY_OMMER_ROOT_HASH, U256, + constants::MAXIMUM_EXTRA_DATA_SIZE, Chain, ChainSpec, Hardfork, Header, SealedBlock, + SealedHeader, EMPTY_OMMER_ROOT_HASH, U256, }; use std::{sync::Arc, time::SystemTime}; /// Ethereum beacon consensus @@ -88,7 +88,7 @@ impl Consensus for BeaconConsensus { let present_timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); - if header.timestamp > present_timestamp + ALLOWED_FUTURE_BLOCK_TIME_SECONDS { + if header.exceeds_allowed_future_timestamp(present_timestamp) { return Err(ConsensusError::TimestampIsInFuture { timestamp: header.timestamp, present_timestamp, diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 560d1fb71..f0e9333de 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -323,7 +323,7 @@ pub fn validate_header_regarding_parent( } // timestamp in past check - if child.timestamp <= parent.timestamp { + if child.header.is_timestamp_in_past(parent.timestamp) { return Err(ConsensusError::TimestampIsInPast { parent_timestamp: parent.timestamp, timestamp: child.timestamp, diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index 2f6a270a5..10acec149 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -1,6 +1,6 @@ use crate::{ basefee::calculate_next_block_base_fee, - constants::{EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH}, + constants::{ALLOWED_FUTURE_BLOCK_TIME_SECONDS, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH}, eip4844::{calc_blob_gasprice, calculate_excess_blob_gas}, keccak256, Address, BaseFeeParams, BlockHash, BlockNumHash, BlockNumber, Bloom, Bytes, B256, B64, U256, @@ -177,6 +177,22 @@ impl Header { Ok(()) } + /// Checks if the block's timestamp is in the past compared to the parent block's timestamp. + /// + /// Note: This check is relevant only pre-merge. + pub fn is_timestamp_in_past(&self, parent_timestamp: u64) -> bool { + self.timestamp <= parent_timestamp + } + + /// Checks if the block's timestamp is in the future based on the present timestamp. + /// + /// Clock can drift but this can be consensus issue. + /// + /// Note: This check is relevant only pre-merge. + pub fn exceeds_allowed_future_timestamp(&self, present_timestamp: u64) -> bool { + self.timestamp > present_timestamp + ALLOWED_FUTURE_BLOCK_TIME_SECONDS + } + /// Returns the parent block's number and hash pub fn parent_num_hash(&self) -> BlockNumHash { BlockNumHash { number: self.number.saturating_sub(1), hash: self.parent_hash }