Skip to content

Commit 22d7f33

Browse files
authored
fix: re-add validate_against_parent_4844 to optimism header validation (#8835)
1 parent 925d406 commit 22d7f33

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

crates/consensus/common/src/validation.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use reth_primitives::{
66
eip4844::{DATA_GAS_PER_BLOB, MAX_DATA_GAS_PER_BLOCK},
77
MAXIMUM_EXTRA_DATA_SIZE,
88
},
9+
eip4844::calculate_excess_blob_gas,
910
ChainSpec, GotExpected, Hardfork, Header, SealedBlock, SealedHeader,
1011
};
1112

@@ -229,6 +230,41 @@ pub fn validate_against_parent_timestamp(
229230
Ok(())
230231
}
231232

233+
/// Validates that the EIP-4844 header fields are correct with respect to the parent block. This
234+
/// ensures that the `blob_gas_used` and `excess_blob_gas` fields exist in the child header, and
235+
/// that the `excess_blob_gas` field matches the expected `excess_blob_gas` calculated from the
236+
/// parent header fields.
237+
pub fn validate_against_parent_4844(
238+
header: &SealedHeader,
239+
parent: &SealedHeader,
240+
) -> Result<(), ConsensusError> {
241+
// From [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension):
242+
//
243+
// > For the first post-fork block, both parent.blob_gas_used and parent.excess_blob_gas
244+
// > are evaluated as 0.
245+
//
246+
// This means in the first post-fork block, calculate_excess_blob_gas will return 0.
247+
let parent_blob_gas_used = parent.blob_gas_used.unwrap_or(0);
248+
let parent_excess_blob_gas = parent.excess_blob_gas.unwrap_or(0);
249+
250+
if header.blob_gas_used.is_none() {
251+
return Err(ConsensusError::BlobGasUsedMissing)
252+
}
253+
let excess_blob_gas = header.excess_blob_gas.ok_or(ConsensusError::ExcessBlobGasMissing)?;
254+
255+
let expected_excess_blob_gas =
256+
calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used);
257+
if expected_excess_blob_gas != excess_blob_gas {
258+
return Err(ConsensusError::ExcessBlobGasDiff {
259+
diff: GotExpected { got: excess_blob_gas, expected: expected_excess_blob_gas },
260+
parent_excess_blob_gas,
261+
parent_blob_gas_used,
262+
})
263+
}
264+
265+
Ok(())
266+
}
267+
232268
#[cfg(test)]
233269
mod tests {
234270
use super::*;

crates/ethereum/consensus/src/lib.rs

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010

1111
use reth_consensus::{Consensus, ConsensusError, PostExecutionInput};
1212
use reth_consensus_common::validation::{
13-
validate_4844_header_standalone, validate_against_parent_eip1559_base_fee,
14-
validate_against_parent_hash_number, validate_against_parent_timestamp,
15-
validate_block_pre_execution, validate_header_base_fee, validate_header_extradata,
16-
validate_header_gas,
13+
validate_4844_header_standalone, validate_against_parent_4844,
14+
validate_against_parent_eip1559_base_fee, validate_against_parent_hash_number,
15+
validate_against_parent_timestamp, validate_block_pre_execution, validate_header_base_fee,
16+
validate_header_extradata, validate_header_gas,
1717
};
1818
use reth_primitives::{
19-
constants::MINIMUM_GAS_LIMIT, eip4844::calculate_excess_blob_gas, BlockWithSenders, Chain,
20-
ChainSpec, GotExpected, Hardfork, Header, SealedBlock, SealedHeader, EMPTY_OMMER_ROOT_HASH,
21-
U256,
19+
constants::MINIMUM_GAS_LIMIT, BlockWithSenders, Chain, ChainSpec, Hardfork, Header,
20+
SealedBlock, SealedHeader, EMPTY_OMMER_ROOT_HASH, U256,
2221
};
2322
use std::{sync::Arc, time::SystemTime};
2423

@@ -40,41 +39,6 @@ impl EthBeaconConsensus {
4039
Self { chain_spec }
4140
}
4241

43-
/// Validates that the EIP-4844 header fields are correct with respect to the parent block. This
44-
/// ensures that the `blob_gas_used` and `excess_blob_gas` fields exist in the child header, and
45-
/// that the `excess_blob_gas` field matches the expected `excess_blob_gas` calculated from the
46-
/// parent header fields.
47-
pub fn validate_against_parent_4844(
48-
header: &SealedHeader,
49-
parent: &SealedHeader,
50-
) -> Result<(), ConsensusError> {
51-
// From [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension):
52-
//
53-
// > For the first post-fork block, both parent.blob_gas_used and parent.excess_blob_gas
54-
// > are evaluated as 0.
55-
//
56-
// This means in the first post-fork block, calculate_excess_blob_gas will return 0.
57-
let parent_blob_gas_used = parent.blob_gas_used.unwrap_or(0);
58-
let parent_excess_blob_gas = parent.excess_blob_gas.unwrap_or(0);
59-
60-
if header.blob_gas_used.is_none() {
61-
return Err(ConsensusError::BlobGasUsedMissing)
62-
}
63-
let excess_blob_gas = header.excess_blob_gas.ok_or(ConsensusError::ExcessBlobGasMissing)?;
64-
65-
let expected_excess_blob_gas =
66-
calculate_excess_blob_gas(parent_excess_blob_gas, parent_blob_gas_used);
67-
if expected_excess_blob_gas != excess_blob_gas {
68-
return Err(ConsensusError::ExcessBlobGasDiff {
69-
diff: GotExpected { got: excess_blob_gas, expected: expected_excess_blob_gas },
70-
parent_excess_blob_gas,
71-
parent_blob_gas_used,
72-
})
73-
}
74-
75-
Ok(())
76-
}
77-
7842
/// Checks the gas limit for consistency between parent and self headers.
7943
///
8044
/// The maximum allowable difference between self and parent gas limits is determined by the
@@ -175,7 +139,7 @@ impl Consensus for EthBeaconConsensus {
175139

176140
// ensure that the blob gas fields for this block
177141
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp) {
178-
Self::validate_against_parent_4844(header, parent)?;
142+
validate_against_parent_4844(header, parent)?;
179143
}
180144

181145
Ok(())

crates/optimism/consensus/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
use reth_consensus::{Consensus, ConsensusError, PostExecutionInput};
1313
use reth_consensus_common::validation::{
14-
validate_against_parent_eip1559_base_fee, validate_against_parent_hash_number,
15-
validate_against_parent_timestamp, validate_block_pre_execution, validate_header_base_fee,
16-
validate_header_extradata, validate_header_gas,
14+
validate_against_parent_4844, validate_against_parent_eip1559_base_fee,
15+
validate_against_parent_hash_number, validate_against_parent_timestamp,
16+
validate_block_pre_execution, validate_header_base_fee, validate_header_extradata,
17+
validate_header_gas,
1718
};
1819
use reth_primitives::{
1920
BlockWithSenders, ChainSpec, Header, SealedBlock, SealedHeader, EMPTY_OMMER_ROOT_HASH, U256,
@@ -63,6 +64,11 @@ impl Consensus for OptimismBeaconConsensus {
6364

6465
validate_against_parent_eip1559_base_fee(header, parent, &self.chain_spec)?;
6566

67+
// ensure that the blob gas fields for this block
68+
if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp) {
69+
validate_against_parent_4844(header, parent)?;
70+
}
71+
6672
Ok(())
6773
}
6874

0 commit comments

Comments
 (0)