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

consensus: implement validate_against_parent_4844 for Header #1572

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ derive_more = { workspace = true, features = [
"from",
"deref",
"deref_mut",
"into_iterator"
"into_iterator",
"error"
], default-features = false }
auto_impl.workspace = true

Expand Down
73 changes: 73 additions & 0 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ use alloy_primitives::{
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};
use core::mem;

/// Consensus Errors
#[derive(Debug, PartialEq, Eq, Clone, derive_more::Display, derive_more::Error)]
pub enum ConsensusError {
/// Error when blob gas used is missing.
#[display("missing blob gas used")]
BlobGasUsedMissing,

/// Error when excess blob gas is missing.
#[display("missing excess blob gas")]
ExcessBlobGasMissing,

/// Error when there is an invalid excess blob gas.
#[display(
"invalid excess blob gas, got: {got}, expected: {expected}; \
parent excess blob gas: {parent_excess_blob_gas}, \
parent blob gas used: {parent_blob_gas_used}"
)]
ExcessBlobGasDiff {
/// Expected excess blob gas.
expected: u64,
/// Actual excess blob gas.
got: u64,
Comment on lines +33 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen several places in alloy where the got/expected error pattern is present, maybe it would be good to move the reth primitive

https://github.com/paradigmxyz/reth/blob/ab07fcfb113cb0b579e1f9f55a5dd9511b576687/crates/primitives-traits/src/error.rs#L9

into alloy-primitives to be able to use it everywhere

/// The parent excess blob gas.
parent_excess_blob_gas: u64,
/// The parent blob gas used.
parent_blob_gas_used: u64,
},
}

/// Ethereum Block header
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -346,6 +375,50 @@ impl Header {
pub const fn seal(self, hash: B256) -> Sealed<Self> {
Sealed::new_unchecked(self, hash)
}

/// Validates that the EIP-4844 fields in the current block header are correct compared to the
/// parent block header.
///
/// Specifically, this function checks two things:
/// 1. The current block header contains the `blob_gas_used` and `excess_blob_gas` fields.
/// 2. The `excess_blob_gas` field matches the expected value calculated from the parent header
/// fields.
pub fn validate_against_parent_4844(&self, parent: &Self) -> Result<(), ConsensusError> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to create a follow up PR with unit tests to validate all the scenarios, but first I wanted to know if this proposal of migration would be accepted

// From [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension):
//
// > For the first post-fork block, both parent.blob_gas_used and parent.excess_blob_gas
// > are evaluated as 0.
//
// This means in the first post-fork block, calc_excess_blob_gas will return 0.
let parent_blob_gas_used = parent.blob_gas_used.unwrap_or(0);
let parent_excess_blob_gas = parent.excess_blob_gas.unwrap_or(0);

// Ensure `blob_gas_used` exists in the current block header.
// Return an error if it is missing.
if self.blob_gas_used.is_none() {
return Err(ConsensusError::BlobGasUsedMissing);
}

// Retrieve the `excess_blob_gas` field from the current block header.
let excess_blob_gas = self.excess_blob_gas.ok_or(ConsensusError::ExcessBlobGasMissing)?;

// Calculate the expected `excess_blob_gas` for the current block.
let expected_excess_blob_gas =
calc_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used);

// Check if the calculated `excess_blob_gas` matches the actual value in the header.
// If they are different, return an error with both values for comparison.
if expected_excess_blob_gas != excess_blob_gas {
return Err(ConsensusError::ExcessBlobGasDiff {
got: excess_blob_gas,
expected: expected_excess_blob_gas,
parent_excess_blob_gas,
parent_blob_gas_used,
});
}

Ok(())
}
}

impl Encodable for Header {
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod encodable_signature;
pub use encodable_signature::EncodableSignature;

mod header;
pub use header::{BlockHeader, Header};
pub use header::{BlockHeader, ConsensusError, Header};

mod receipt;
pub use receipt::{
Expand Down
Loading