From 6393354b30d2ba14a4dae6db0437c6754ef9a73b Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:21:02 -0500 Subject: [PATCH] chore: add consensus helper methods to BlockHeader --- crates/consensus/src/block/header.rs | 51 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/crates/consensus/src/block/header.rs b/crates/consensus/src/block/header.rs index d5a3ec493cc..6406094badf 100644 --- a/crates/consensus/src/block/header.rs +++ b/crates/consensus/src/block/header.rs @@ -353,28 +353,6 @@ impl Header { BlockWithParent::new(self.parent_hash, self.num_hash_slow()) } - /// Checks if the block's difficulty is set to zero, indicating a Proof-of-Stake header. - /// - /// This function is linked to EIP-3675, proposing the consensus upgrade to Proof-of-Stake: - /// [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#replacing-difficulty-with-0) - /// - /// Verifies whether, as per the EIP, the block's difficulty is updated to zero, - /// signifying the transition to a Proof-of-Stake mechanism. - /// - /// Returns `true` if the block's difficulty matches the constant zero set by the EIP. - pub fn is_zero_difficulty(&self) -> bool { - self.difficulty.is_zero() - } - - /// 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 const fn exceeds_allowed_future_timestamp(&self, present_timestamp: u64) -> bool { - self.timestamp > present_timestamp + ALLOWED_FUTURE_BLOCK_TIME_SECONDS - } - /// Seal the header with a known hash. /// /// WARNING: This method does not perform validation whether the hash is correct. @@ -731,6 +709,35 @@ pub trait BlockHeader { txs_and_ommers_empty && withdrawals_root == EMPTY_ROOT_HASH }) } + + /// Checks if the block's difficulty is set to zero, indicating a Proof-of-Stake header. + /// + /// This function is linked to EIP-3675, proposing the consensus upgrade to Proof-of-Stake: + /// [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#replacing-difficulty-with-0) + /// + /// Verifies whether, as per the EIP, the block's difficulty is updated to zero, + /// signifying the transition to a Proof-of-Stake mechanism. + /// + /// Returns `true` if the block's difficulty matches the constant zero set by the EIP. + fn is_zero_difficulty(&self) -> bool { + self.difficulty().is_zero() + } + + /// 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. + fn exceeds_allowed_future_timestamp(&self, present_timestamp: u64) -> bool { + self.timestamp() > present_timestamp + ALLOWED_FUTURE_BLOCK_TIME_SECONDS + } + + /// Checks if the nonce exists, and if it exists, if it's zero. + /// + /// If the nonce is `None`, then this returns `false`. + fn is_nonce_zero(&self) -> bool { + self.nonce().is_some_and(|nonce| nonce.is_zero()) + } } impl BlockHeader for Header {