-
Notifications
You must be signed in to change notification settings - Fork 249
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
tcoratger
wants to merge
4
commits into
alloy-rs:main
Choose a base branch
from
tcoratger:validate_against_parent_4844
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
/// 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))] | ||
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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