Skip to content

Commit

Permalink
chore: add consensus helper methods to BlockHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Dec 10, 2024
1 parent 447b2bc commit 6393354
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions crates/consensus/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6393354

Please sign in to comment.