add is_timestamp_in_past and exceeds_allowed_future_timestamp for Header (#6160)

This commit is contained in:
Thomas Coratger
2024-01-22 17:44:07 +01:00
committed by GitHub
parent fe63b1089d
commit 18bf17e9b5
3 changed files with 21 additions and 5 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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 }