Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add consensus helper methods to BlockHeader #1781

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading