added helper error enums for L1BlockInfoError (#14326)

This commit is contained in:
Poulav Bhowmick
2025-02-09 20:03:09 +05:30
committed by GitHub
parent 104bd6e039
commit 21370c3911
4 changed files with 87 additions and 96 deletions

View File

@ -790,7 +790,7 @@ impl PeersManager {
/// Connects a peer and its address with the given kind. /// 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( pub(crate) fn add_and_connect_kind(
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,

View File

@ -774,7 +774,7 @@ impl NetworkEventStream {
Some(peer_id) 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> { pub async fn peer_removed(&mut self) -> Option<PeerId> {
let peer_id = match self.inner.next().await { let peer_id = match self.inner.next().await {
Some(NetworkEvent::Peer(PeerEvent::PeerRemoved(peer_id))) => peer_id, Some(NetworkEvent::Peer(PeerEvent::PeerRemoved(peer_id))) => peer_id,

View File

@ -1,17 +1,54 @@
//! Error types for the Optimism EVM module. //! Error types for the Optimism EVM module.
use alloc::string::String;
use reth_evm::execute::BlockExecutionError; 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 /// Optimism Block Executor Errors
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum OpBlockExecutionError { pub enum OpBlockExecutionError {
/// Error when trying to parse L1 block info /// Error when trying to parse L1 block info
#[error("could not get L1 block info from L2 block: {message}")] #[error(transparent)]
L1BlockInfoError { L1BlockInfo(#[from] L1BlockInfoError),
/// The inner error message
message: String,
},
/// Thrown when force deploy of create2deployer code fails. /// Thrown when force deploy of create2deployer code fails.
#[error("failed to force create2deployer account code")] #[error("failed to force create2deployer account code")]
ForceCreate2DeployerFail, ForceCreate2DeployerFail,

View File

@ -1,7 +1,6 @@
//! Optimism-specific implementation and utilities for the executor //! Optimism-specific implementation and utilities for the executor
use crate::OpBlockExecutionError; use crate::{error::L1BlockInfoError, OpBlockExecutionError};
use alloc::string::ToString;
use alloy_consensus::Transaction; use alloy_consensus::Transaction;
use alloy_primitives::{address, b256, hex, Address, Bytes, B256, U256}; use alloy_primitives::{address, b256, hex, Address, Bytes, B256, U256};
use reth_execution_errors::BlockExecutionError; use reth_execution_errors::BlockExecutionError;
@ -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. /// 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> { pub fn extract_l1_info<B: BlockBody>(body: &B) -> Result<L1BlockInfo, OpBlockExecutionError> {
let l1_info_tx = let l1_info_tx = body
body.transactions().first().ok_or_else(|| OpBlockExecutionError::L1BlockInfoError { .transactions()
message: "could not find l1 block info tx in the L2 block".to_string(), .first()
})?; .ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::MissingTransaction))?;
extract_l1_info_from_tx(l1_info_tx) extract_l1_info_from_tx(l1_info_tx)
} }
@ -50,9 +49,7 @@ pub fn extract_l1_info_from_tx<T: Transaction>(
) -> Result<L1BlockInfo, OpBlockExecutionError> { ) -> Result<L1BlockInfo, OpBlockExecutionError> {
let l1_info_tx_data = tx.input(); let l1_info_tx_data = tx.input();
if l1_info_tx_data.len() < 4 { if l1_info_tx_data.len() < 4 {
return Err(OpBlockExecutionError::L1BlockInfoError { return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::InvalidCalldata));
message: "invalid l1 block info transaction calldata in the L2 block".to_string(),
})
} }
parse_l1_info(l1_info_tx_data) parse_l1_info(l1_info_tx_data)
@ -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 overhead
// + 32 bytes for the fee scalar // + 32 bytes for the fee scalar
if data.len() != 256 { if data.len() != 256 {
return Err(OpBlockExecutionError::L1BlockInfoError { return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::UnexpectedCalldataLength));
message: "unexpected l1 block info tx calldata length found".to_string(),
})
} }
let l1_base_fee = U256::try_from_be_slice(&data[64..96]).ok_or_else(|| { let l1_base_fee = U256::try_from_be_slice(&data[64..96])
OpBlockExecutionError::L1BlockInfoError { .ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeConversion))?;
message: "could not convert l1 base fee".to_string(), 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])
let l1_fee_overhead = U256::try_from_be_slice(&data[192..224]).ok_or_else(|| { .ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::FeeScalarConversion))?;
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 mut l1block = L1BlockInfo::default(); let mut l1block = L1BlockInfo::default();
l1block.l1_base_fee = l1_base_fee; l1block.l1_base_fee = l1_base_fee;
@ -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> /// <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> { pub fn parse_l1_info_tx_ecotone(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecutionError> {
if data.len() != 160 { if data.len() != 160 {
return Err(OpBlockExecutionError::L1BlockInfoError { return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::UnexpectedCalldataLength));
message: "unexpected l1 block info tx calldata length found".to_string(),
})
} }
// https://github.com/ethereum-optimism/op-geth/blob/60038121c7571a59875ff9ed7679c48c9f73405d/core/types/rollup_cost.go#L317-L328 // https://github.com/ethereum-optimism/op-geth/blob/60038121c7571a59875ff9ed7679c48c9f73405d/core/types/rollup_cost.go#L317-L328
@ -158,26 +142,15 @@ pub fn parse_l1_info_tx_ecotone(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
// 100 bytes32 _hash, // 100 bytes32 _hash,
// 132 bytes32 _batcherHash, // 132 bytes32 _batcherHash,
let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4]).ok_or_else(|| { let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4])
OpBlockExecutionError::L1BlockInfoError { .ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeScalarConversion))?;
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({
} 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_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 = 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(); let mut l1block = L1BlockInfo::default();
l1block.l1_base_fee = l1_base_fee; l1block.l1_base_fee = l1_base_fee;
@ -204,9 +177,7 @@ pub fn parse_l1_info_tx_ecotone(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
/// 11. _operatorFeeConstant Operator fee constant /// 11. _operatorFeeConstant Operator fee constant
pub fn parse_l1_info_tx_isthmus(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecutionError> { pub fn parse_l1_info_tx_isthmus(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecutionError> {
if data.len() != 172 { if data.len() != 172 {
return Err(OpBlockExecutionError::L1BlockInfoError { return Err(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::UnexpectedCalldataLength));
message: "unexpected l1 block info tx calldata length found".to_string(),
})
} }
// https://github.com/ethereum-optimism/op-geth/blob/60038121c7571a59875ff9ed7679c48c9f73405d/core/types/rollup_cost.go#L317-L328 // https://github.com/ethereum-optimism/op-geth/blob/60038121c7571a59875ff9ed7679c48c9f73405d/core/types/rollup_cost.go#L317-L328
@ -226,35 +197,20 @@ pub fn parse_l1_info_tx_isthmus(data: &[u8]) -> Result<L1BlockInfo, OpBlockExecu
// 164 uint32 _operatorFeeScalar // 164 uint32 _operatorFeeScalar
// 168 uint64 _operatorFeeConstant // 168 uint64 _operatorFeeConstant
let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4]).ok_or_else(|| { let l1_base_fee_scalar = U256::try_from_be_slice(&data[..4])
OpBlockExecutionError::L1BlockInfoError { .ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeScalarConversion))?;
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({
} OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BlobBaseFeeScalarConversion)
})?; })?;
let l1_blob_base_fee_scalar = U256::try_from_be_slice(&data[4..8]).ok_or_else(|| { let l1_base_fee = U256::try_from_be_slice(&data[32..64])
OpBlockExecutionError::L1BlockInfoError { .ok_or(OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::BaseFeeConversion))?;
message: "could not convert l1 blob base fee scalar".to_string(), 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_base_fee = U256::try_from_be_slice(&data[32..64]).ok_or_else(|| { let operator_fee_constant = U256::try_from_be_slice(&data[164..172]).ok_or({
OpBlockExecutionError::L1BlockInfoError { OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::OperatorFeeConstantConversion)
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 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 mut l1block = L1BlockInfo::default(); let mut l1block = L1BlockInfo::default();
@ -312,7 +268,7 @@ impl RethL1BlockInfo for L1BlockInfo {
is_deposit: bool, is_deposit: bool,
) -> Result<U256, BlockExecutionError> { ) -> Result<U256, BlockExecutionError> {
if is_deposit { if is_deposit {
return Ok(U256::ZERO) return Ok(U256::ZERO);
} }
let spec_id = if chain_spec.is_fjord_active_at_timestamp(timestamp) { let spec_id = if chain_spec.is_fjord_active_at_timestamp(timestamp) {
@ -324,10 +280,9 @@ impl RethL1BlockInfo for L1BlockInfo {
} else if chain_spec.is_bedrock_active_at_block(block_number) { } else if chain_spec.is_bedrock_active_at_block(block_number) {
SpecId::BEDROCK SpecId::BEDROCK
} else { } else {
return Err(OpBlockExecutionError::L1BlockInfoError { return Err(
message: "Optimism hardforks are not active".to_string(), OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::HardforksNotActive).into()
} );
.into())
}; };
Ok(self.calculate_tx_l1_cost(input, spec_id)) Ok(self.calculate_tx_l1_cost(input, spec_id))
} }
@ -346,10 +301,9 @@ impl RethL1BlockInfo for L1BlockInfo {
} else if chain_spec.is_bedrock_active_at_block(block_number) { } else if chain_spec.is_bedrock_active_at_block(block_number) {
SpecId::BEDROCK SpecId::BEDROCK
} else { } else {
return Err(OpBlockExecutionError::L1BlockInfoError { return Err(
message: "Optimism hardforks are not active".to_string(), OpBlockExecutionError::L1BlockInfo(L1BlockInfoError::HardforksNotActive).into()
} );
.into())
}; };
Ok(self.data_gas(input, spec_id)) Ok(self.data_gas(input, spec_id))
} }