From a81e7e41de28f1254700c5a68829e6c332e9d0fc Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 30 Mar 2024 15:03:46 +0100 Subject: [PATCH] chore: add EthApiError::Other (#7398) --- crates/rpc/rpc/src/eth/api/transactions.rs | 10 ++--- crates/rpc/rpc/src/eth/error.rs | 46 +++++++--------------- crates/rpc/rpc/src/eth/mod.rs | 3 ++ crates/rpc/rpc/src/eth/optimism.rs | 46 ++++++++++++++++++++++ 4 files changed, 69 insertions(+), 36 deletions(-) create mode 100644 crates/rpc/rpc/src/eth/optimism.rs diff --git a/crates/rpc/rpc/src/eth/api/transactions.rs b/crates/rpc/rpc/src/eth/api/transactions.rs index 12580a321..f1016e475 100644 --- a/crates/rpc/rpc/src/eth/api/transactions.rs +++ b/crates/rpc/rpc/src/eth/api/transactions.rs @@ -52,7 +52,7 @@ use std::future::Future; #[cfg(feature = "optimism")] use crate::eth::api::optimism::OptimismTxMeta; #[cfg(feature = "optimism")] -use crate::eth::error::OptimismEthApiError; +use crate::eth::optimism::OptimismEthApiError; #[cfg(feature = "optimism")] use reth_revm::optimism::RethL1BlockInfo; #[cfg(feature = "optimism")] @@ -1321,10 +1321,10 @@ where &envelope_buf, tx.is_deposit(), ) - .map_err(|_| EthApiError::Optimism(OptimismEthApiError::L1BlockFeeError))?; + .map_err(|_| OptimismEthApiError::L1BlockFeeError)?; let inner_l1_data_gas = l1_block_info .l1_data_gas(&self.inner.provider.chain_spec(), block_timestamp, &envelope_buf) - .map_err(|_| EthApiError::Optimism(OptimismEthApiError::L1BlockGasError))?; + .map_err(|_| OptimismEthApiError::L1BlockGasError)?; (Some(inner_l1_fee), Some(inner_l1_data_gas)) } else { (None, None) @@ -1351,7 +1351,7 @@ where target = "rpc::eth", "Failed to serialize transaction for forwarding to sequencer" ); - EthApiError::Optimism(OptimismEthApiError::InvalidSequencerTransaction) + OptimismEthApiError::InvalidSequencerTransaction })?; self.inner @@ -1361,7 +1361,7 @@ where .body(body) .send() .await - .map_err(|err| EthApiError::Optimism(OptimismEthApiError::HttpError(err)))?; + .map_err(OptimismEthApiError::HttpError)?; } Ok(()) } diff --git a/crates/rpc/rpc/src/eth/error.rs b/crates/rpc/rpc/src/eth/error.rs index a4e8d4d6e..3e54c2093 100644 --- a/crates/rpc/rpc/src/eth/error.rs +++ b/crates/rpc/rpc/src/eth/error.rs @@ -20,6 +20,12 @@ use std::time::Duration; /// Result alias pub type EthResult = Result; +/// A tait for custom rpc errors used by [EthApiError::Other]. +pub trait ToRpcError: std::error::Error + Send + Sync + 'static { + /// Converts the error to a JSON-RPC error object. + fn to_rpc_error(&self) -> ErrorObject<'static>; +} + /// Errors that can occur when interacting with the `eth_` namespace #[derive(Debug, thiserror::Error)] pub enum EthApiError { @@ -106,10 +112,6 @@ pub enum EthApiError { #[error(transparent)] /// Call Input error when both `data` and `input` fields are set and not equal. TransactionInputError(#[from] TransactionInputError), - /// Optimism related error - #[error(transparent)] - #[cfg(feature = "optimism")] - Optimism(#[from] OptimismEthApiError), /// Evm generic purpose error. #[error("Revm error: {0}")] EvmCustom(String), @@ -119,27 +121,16 @@ pub enum EthApiError { /// Error thrown when tracing with a muxTracer fails #[error(transparent)] MuxTracerError(#[from] MuxError), + /// Any other error + #[error("0")] + Other(Box), } -/// Eth Optimism Api Error -#[cfg(feature = "optimism")] -#[derive(Debug, thiserror::Error)] -pub enum OptimismEthApiError { - /// Wrapper around a [hyper::Error]. - #[error(transparent)] - HyperError(#[from] hyper::Error), - /// Wrapper around an [reqwest::Error]. - #[error(transparent)] - HttpError(#[from] reqwest::Error), - /// Thrown when serializing transaction to forward to sequencer - #[error("invalid sequencer transaction")] - InvalidSequencerTransaction, - /// Thrown when calculating L1 gas fee - #[error("failed to calculate l1 gas fee")] - L1BlockFeeError, - /// Thrown when calculating L1 gas used - #[error("failed to calculate l1 gas used")] - L1BlockGasError, +impl EthApiError { + /// crates a new [EthApiError::Other] variant. + pub fn other(err: E) -> Self { + EthApiError::Other(Box::new(err)) + } } impl From for ErrorObject<'static> { @@ -178,14 +169,7 @@ impl From for ErrorObject<'static> { err @ EthApiError::InternalBlockingTaskError => internal_rpc_err(err.to_string()), err @ EthApiError::InternalEthError => internal_rpc_err(err.to_string()), err @ EthApiError::TransactionInputError(_) => invalid_params_rpc_err(err.to_string()), - #[cfg(feature = "optimism")] - EthApiError::Optimism(err) => match err { - OptimismEthApiError::HyperError(err) => internal_rpc_err(err.to_string()), - OptimismEthApiError::HttpError(err) => internal_rpc_err(err.to_string()), - OptimismEthApiError::InvalidSequencerTransaction | - OptimismEthApiError::L1BlockFeeError | - OptimismEthApiError::L1BlockGasError => internal_rpc_err(err.to_string()), - }, + EthApiError::Other(err) => err.to_rpc_error(), EthApiError::MuxTracerError(msg) => internal_rpc_err(msg.to_string()), } } diff --git a/crates/rpc/rpc/src/eth/mod.rs b/crates/rpc/rpc/src/eth/mod.rs index 97f57af68..318305093 100644 --- a/crates/rpc/rpc/src/eth/mod.rs +++ b/crates/rpc/rpc/src/eth/mod.rs @@ -13,6 +13,9 @@ pub mod revm_utils; mod signer; pub(crate) mod utils; +#[cfg(feature = "optimism")] +pub mod optimism; + pub use api::{ fee_history::{fee_history_cache_new_blocks_task, FeeHistoryCache, FeeHistoryCacheConfig}, EthApi, EthApiSpec, EthTransactions, TransactionSource, RPC_DEFAULT_GAS_CAP, diff --git a/crates/rpc/rpc/src/eth/optimism.rs b/crates/rpc/rpc/src/eth/optimism.rs new file mode 100644 index 000000000..2dedf2625 --- /dev/null +++ b/crates/rpc/rpc/src/eth/optimism.rs @@ -0,0 +1,46 @@ +//! Optimism specific types. + +use crate::{ + eth::error::{EthApiError, ToRpcError}, + result::internal_rpc_err, +}; +use jsonrpsee::types::ErrorObject; + +/// Eth Optimism Api Error +#[cfg(feature = "optimism")] +#[derive(Debug, thiserror::Error)] +pub enum OptimismEthApiError { + /// Wrapper around a [hyper::Error]. + #[error(transparent)] + HyperError(#[from] hyper::Error), + /// Wrapper around an [reqwest::Error]. + #[error(transparent)] + HttpError(#[from] reqwest::Error), + /// Thrown when serializing transaction to forward to sequencer + #[error("invalid sequencer transaction")] + InvalidSequencerTransaction, + /// Thrown when calculating L1 gas fee + #[error("failed to calculate l1 gas fee")] + L1BlockFeeError, + /// Thrown when calculating L1 gas used + #[error("failed to calculate l1 gas used")] + L1BlockGasError, +} + +impl ToRpcError for OptimismEthApiError { + fn to_rpc_error(&self) -> ErrorObject<'static> { + match self { + OptimismEthApiError::HyperError(err) => internal_rpc_err(err.to_string()), + OptimismEthApiError::HttpError(err) => internal_rpc_err(err.to_string()), + OptimismEthApiError::InvalidSequencerTransaction | + OptimismEthApiError::L1BlockFeeError | + OptimismEthApiError::L1BlockGasError => internal_rpc_err(self.to_string()), + } + } +} + +impl From for EthApiError { + fn from(err: OptimismEthApiError) -> Self { + EthApiError::other(err) + } +}