Skip to content

Commit

Permalink
added helper error enums for L1BlockInfoError (#14326)
Browse files Browse the repository at this point in the history
  • Loading branch information
PoulavBhowmick03 authored Feb 9, 2025
1 parent 104bd6e commit 21370c3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 96 deletions.
2 changes: 1 addition & 1 deletion crates/net/network/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ impl PeersManager {

/// Connects a peer and its address with the given kind.
///
/// Note: This is invoked ond demand via an external command received by the manager
/// Note: This is invoked on demand via an external command received by the manager
pub(crate) fn add_and_connect_kind(
&mut self,
peer_id: PeerId,
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/test_utils/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ impl NetworkEventStream {
Some(peer_id)
}

/// Awaits the next event for a peer remvoed.
/// Awaits the next event for a peer removed.
pub async fn peer_removed(&mut self) -> Option<PeerId> {
let peer_id = match self.inner.next().await {
Some(NetworkEvent::Peer(PeerEvent::PeerRemoved(peer_id))) => peer_id,
Expand Down
49 changes: 43 additions & 6 deletions crates/optimism/evm/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
//! Error types for the Optimism EVM module.
use alloc::string::String;
use reth_evm::execute::BlockExecutionError;

/// L1 Block Info specific errors
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum L1BlockInfoError {
/// Could not find L1 block info transaction in the L2 block
#[error("could not find l1 block info tx in the L2 block")]
MissingTransaction,
/// Invalid L1 block info transaction calldata
#[error("invalid l1 block info transaction calldata in the L2 block")]
InvalidCalldata,
/// Unexpected L1 block info transaction calldata length
#[error("unexpected l1 block info tx calldata length found")]
UnexpectedCalldataLength,
/// Base fee conversion error
#[error("could not convert l1 base fee")]
BaseFeeConversion,
/// Fee overhead conversion error
#[error("could not convert l1 fee overhead")]
FeeOverheadConversion,
/// Fee scalar conversion error
#[error("could not convert l1 fee scalar")]
FeeScalarConversion,
/// Base Fee Scalar conversion error
#[error("could not convert base fee scalar")]
BaseFeeScalarConversion,
/// Blob base fee conversion error
#[error("could not convert l1 blob base fee")]
BlobBaseFeeConversion,
/// Blob base fee scalar conversion error
#[error("could not convert l1 blob base fee scalar")]
BlobBaseFeeScalarConversion,
/// Operator fee scalar conversion error
#[error("could not convert operator fee scalar")]
OperatorFeeScalarConversion,
/// Operator fee constant conversion error
#[error("could not convert operator fee constant")]
OperatorFeeConstantConversion,
/// Optimism hardforks not active
#[error("Optimism hardforks are not active")]
HardforksNotActive,
}

/// Optimism Block Executor Errors
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum OpBlockExecutionError {
/// Error when trying to parse L1 block info
#[error("could not get L1 block info from L2 block: {message}")]
L1BlockInfoError {
/// The inner error message
message: String,
},
#[error(transparent)]
L1BlockInfo(#[from] L1BlockInfoError),
/// Thrown when force deploy of create2deployer code fails.
#[error("failed to force create2deployer account code")]
ForceCreate2DeployerFail,
Expand Down
130 changes: 42 additions & 88 deletions crates/optimism/evm/src/l1.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Optimism-specific implementation and utilities for the executor
use crate::OpBlockExecutionError;
use alloc::string::ToString;
use crate::{error::L1BlockInfoError, OpBlockExecutionError};
use alloy_consensus::Transaction;
use alloy_primitives::{address, b256, hex, Address, Bytes, B256, U256};
use reth_execution_errors::BlockExecutionError;
Expand Down Expand Up @@ -34,10 +33,10 @@ const L1_BLOCK_ISTHMUS_SELECTOR: [u8; 4] = hex!("098999be");
///
/// Returns an error if the L1 info transaction is not found, if the block is empty.
pub fn extract_l1_info<B: BlockBody>(body: &B) -> Result<L1BlockInfo, OpBlockExecutionError> {
let l1_info_tx =
body.transactions().first().ok_or_else(|| OpBlockExecutionError::L1BlockInfoError {
message: "could not find l1 block info tx in the L2 block".to_string(),
})?;
let l1_info_tx = body
.transactions()
.first()
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::MissingTransaction))?;
extract_l1_info_from_tx(l1_info_tx)
}

Expand All @@ -50,9 +49,7 @@ pub fn extract_l1_info_from_tx<T: Transaction>(
) -> Result<L1BlockInfo, OpBlockExecutionError> {
let l1_info_tx_data = tx.input();
if l1_info_tx_data.len() < 4 {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "invalid l1 block info transaction calldata in the L2 block".to_string(),
})
return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::InvalidCalldata));
}

parse_l1_info(l1_info_tx_data)
Expand Down Expand Up @@ -93,26 +90,15 @@ pub fn parse_l1_info_tx_bedrock(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
// + 32 bytes for the fee overhead
// + 32 bytes for the fee scalar
if data.len() != 256 {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "unexpected l1 block info tx calldata length found".to_string(),
})
return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::UnexpectedCalldataLength));
}

let l1_base_fee = U256::try_from_be_slice(&data[64..96]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 base fee".to_string(),
}
})?;
let l1_fee_overhead = U256::try_from_be_slice(&data[192..224]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 fee overhead".to_string(),
}
})?;
let l1_fee_scalar = U256::try_from_be_slice(&data[224..256]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 fee scalar".to_string(),
}
})?;
let l1_base_fee = U256::try_from_be_slice(&data[64..96])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeConversion))?;
let l1_fee_overhead = U256::try_from_be_slice(&data[192..224])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::FeeOverheadConversion))?;
let l1_fee_scalar = U256::try_from_be_slice(&data[224..256])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::FeeScalarConversion))?;

let mut l1block = L1BlockInfo::default();
l1block.l1_base_fee = l1_base_fee;
Expand All @@ -138,9 +124,7 @@ pub fn parse_l1_info_tx_bedrock(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
/// <https://github.com/ethereum-optimism/optimism/blob/957e13dd504fb336a4be40fb5dd0d8ba0276be34/packages/contracts-bedrock/src/L2/L1Block.sol#L136>
pub fn parse_l1_info_tx_ecotone(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecutionError> {
if data.len() != 160 {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "unexpected l1 block info tx calldata length found".to_string(),
})
return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::UnexpectedCalldataLength));
}

// https://github.com/ethereum-optimism/op-geth/blob/60038121c7571a59875ff9ed7679c48c9f73405d/core/types/rollup_cost.go#L317-L328
Expand All @@ -158,26 +142,15 @@ pub fn parse_l1_info_tx_ecotone(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
// 100 bytes32 _hash,
// 132 bytes32 _batcherHash,

let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 base fee scalar".to_string(),
}
})?;
let l1_blob_base_fee_scalar = U256::try_from_be_slice(&data[4..8]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 blob base fee scalar".to_string(),
}
})?;
let l1_base_fee = U256::try_from_be_slice(&data[32..64]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 blob base fee".to_string(),
}
})?;
let l1_blob_base_fee = U256::try_from_be_slice(&data[64..96]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 blob base fee".to_string(),
}
let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeScalarConversion))?;
let l1_blob_base_fee_scalar = U256::try_from_be_slice(&data[4..8]).ok_or({
OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BlobBaseFeeScalarConversion)
})?;
let l1_base_fee = U256::try_from_be_slice(&data[32..64])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeConversion))?;
let l1_blob_base_fee = U256::try_from_be_slice(&data[64..96])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BlobBaseFeeConversion))?;

let mut l1block = L1BlockInfo::default();
l1block.l1_base_fee = l1_base_fee;
Expand All @@ -204,9 +177,7 @@ pub fn parse_l1_info_tx_ecotone(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
/// 11. _operatorFeeConstant Operator fee constant
pub fn parse_l1_info_tx_isthmus(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecutionError> {
if data.len() != 172 {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "unexpected l1 block info tx calldata length found".to_string(),
})
return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::UnexpectedCalldataLength));
}

// https://github.com/ethereum-optimism/op-geth/blob/60038121c7571a59875ff9ed7679c48c9f73405d/core/types/rollup_cost.go#L317-L328
Expand All @@ -226,35 +197,20 @@ pub fn parse_l1_info_tx_isthmus(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
// 164 uint32 _operatorFeeScalar
// 168 uint64 _operatorFeeConstant

let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 base fee scalar".to_string(),
}
let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeScalarConversion))?;
let l1_blob_base_fee_scalar = U256::try_from_be_slice(&data[4..8]).ok_or({
OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BlobBaseFeeScalarConversion)
})?;
let l1_blob_base_fee_scalar = U256::try_from_be_slice(&data[4..8]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 blob base fee scalar".to_string(),
}
})?;
let l1_base_fee = U256::try_from_be_slice(&data[32..64]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 blob base fee".to_string(),
}
let l1_base_fee = U256::try_from_be_slice(&data[32..64])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeConversion))?;
let l1_blob_base_fee = U256::try_from_be_slice(&data[64..96])
.ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BlobBaseFeeConversion))?;
let operator_fee_scalar = U256::try_from_be_slice(&data[160..164]).ok_or({
OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::OperatorFeeScalarConversion)
})?;
let l1_blob_base_fee = U256::try_from_be_slice(&data[64..96]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert l1 blob base fee".to_string(),
}
})?;
let operator_fee_scalar = U256::try_from_be_slice(&data[160..164]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert operator fee scalar".to_string(),
}
})?;
let operator_fee_constant = U256::try_from_be_slice(&data[164..172]).ok_or_else(|| {
OpBlockExecutionError::L1BlockInfoError {
message: "could not convert operator fee constant".to_string(),
}
let operator_fee_constant = U256::try_from_be_slice(&data[164..172]).ok_or({
OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::OperatorFeeConstantConversion)
})?;

let mut l1block = L1BlockInfo::default();
Expand Down Expand Up @@ -312,7 +268,7 @@ impl RethL1BlockInfo for L1BlockInfo {
is_deposit: bool,
) -> Result<U256, BlockExecutionError> {
if is_deposit {
return Ok(U256::ZERO)
return Ok(U256::ZERO);
}

let spec_id = if chain_spec.is_fjord_active_at_timestamp(timestamp) {
Expand All @@ -324,10 +280,9 @@ impl RethL1BlockInfo for L1BlockInfo {
} else if chain_spec.is_bedrock_active_at_block(block_number) {
SpecId::BEDROCK
} else {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "Optimism hardforks are not active".to_string(),
}
.into())
return Err(
OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::HardforksNotActive).into()
);
};
Ok(self.calculate_tx_l1_cost(input, spec_id))
}
Expand All @@ -346,10 +301,9 @@ impl RethL1BlockInfo for L1BlockInfo {
} else if chain_spec.is_bedrock_active_at_block(block_number) {
SpecId::BEDROCK
} else {
return Err(OpBlockExecutionError::L1BlockInfoError {
message: "Optimism hardforks are not active".to_string(),
}
.into())
return Err(
OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::HardforksNotActive).into()
);
};
Ok(self.data_gas(input, spec_id))
}
Expand Down

0 comments on commit 21370c3

Please sign in to comment.