From 784d8dc597fa8eebf1ef45b7dcfa9374ea94e669 Mon Sep 17 00:00:00 2001 From: Darshan Kathiriya <8559992+lakshya-sky@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:19:33 -0400 Subject: [PATCH] refactor: replace OP error variant with general purpose error (#7844) Co-authored-by: Oliver Nordbjerg --- crates/optimism/node/src/rpc.rs | 3 ++- crates/rpc/rpc-engine-api/src/error.rs | 26 ++++++++++++-------------- crates/rpc/rpc-types/src/eth/error.rs | 9 +++++++++ crates/rpc/rpc-types/src/eth/mod.rs | 1 + crates/rpc/rpc-types/src/lib.rs | 1 + crates/rpc/rpc-types/src/mev.rs | 2 +- crates/rpc/rpc/src/eth/error.rs | 10 +++------- crates/rpc/rpc/src/eth/optimism.rs | 6 ++---- 8 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 crates/rpc/rpc-types/src/eth/error.rs diff --git a/crates/optimism/node/src/rpc.rs b/crates/optimism/node/src/rpc.rs index 66eb824505..25a399e185 100644 --- a/crates/optimism/node/src/rpc.rs +++ b/crates/optimism/node/src/rpc.rs @@ -3,9 +3,10 @@ use jsonrpsee::types::ErrorObject; use reqwest::Client; use reth_rpc::eth::{ - error::{EthApiError, EthResult, ToRpcError}, + error::{EthApiError, EthResult}, traits::RawTransactionForwarder, }; +use reth_rpc_types::ToRpcError; use std::sync::{atomic::AtomicUsize, Arc}; /// Error type when interacting with the Sequencer diff --git a/crates/rpc/rpc-engine-api/src/error.rs b/crates/rpc/rpc-engine-api/src/error.rs index 01b99a54f6..57318d0d66 100644 --- a/crates/rpc/rpc-engine-api/src/error.rs +++ b/crates/rpc/rpc-engine-api/src/error.rs @@ -5,6 +5,7 @@ use reth_beacon_consensus::{BeaconForkChoiceUpdateError, BeaconOnNewPayloadError use reth_engine_primitives::EngineObjectValidationError; use reth_payload_builder::error::PayloadBuilderError; use reth_primitives::{B256, U256}; +use reth_rpc_types::ToRpcError; use thiserror::Error; /// The Engine API result type @@ -86,11 +87,16 @@ pub enum EngineApiError { /// The payload or attributes are known to be malformed before processing. #[error(transparent)] EngineObjectValidationError(#[from] EngineObjectValidationError), - /// If the optimism feature flag is enabled, the payload attributes must have a present - /// gas limit for the forkchoice updated method. - #[cfg(feature = "optimism")] - #[error("Missing gas limit in payload attributes")] - MissingGasLimitInPayloadAttributes, + /// Any other error + #[error("{0}")] + Other(Box), +} + +impl EngineApiError { + /// Crates a new [EngineApiError::Other] variant. + pub fn other(err: E) -> Self { + Self::Other(Box::new(err)) + } } /// Helper type to represent the `error` field in the error response: @@ -188,15 +194,6 @@ impl From for jsonrpsee_types::error::ErrorObject<'static> { ) } }, - // Optimism errors - #[cfg(feature = "optimism")] - EngineApiError::MissingGasLimitInPayloadAttributes => { - jsonrpsee_types::error::ErrorObject::owned( - INVALID_PARAMS_CODE, - INVALID_PARAMS_MSG, - Some(ErrorData::new(error)), - ) - } // Any other server error EngineApiError::TerminalTD { .. } | EngineApiError::TerminalBlockHash { .. } | @@ -206,6 +203,7 @@ impl From for jsonrpsee_types::error::ErrorObject<'static> { SERVER_ERROR_MSG, Some(ErrorData::new(error)), ), + EngineApiError::Other(err) => err.to_rpc_error(), } } } diff --git a/crates/rpc/rpc-types/src/eth/error.rs b/crates/rpc/rpc-types/src/eth/error.rs new file mode 100644 index 0000000000..e8d55b0874 --- /dev/null +++ b/crates/rpc/rpc-types/src/eth/error.rs @@ -0,0 +1,9 @@ +//! Implementation specific Errors for the `eth_` namespace. + +use jsonrpsee_types::ErrorObject; + +/// A tait to convert an error to an RPC error. +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>; +} diff --git a/crates/rpc/rpc-types/src/eth/mod.rs b/crates/rpc/rpc-types/src/eth/mod.rs index dd36e7fd5c..6313dbeedb 100644 --- a/crates/rpc/rpc-types/src/eth/mod.rs +++ b/crates/rpc/rpc-types/src/eth/mod.rs @@ -1,5 +1,6 @@ //! Ethereum related types +pub(crate) mod error; pub mod transaction; // re-export diff --git a/crates/rpc/rpc-types/src/lib.rs b/crates/rpc/rpc-types/src/lib.rs index 964144ed65..68ad11c6eb 100644 --- a/crates/rpc/rpc-types/src/lib.rs +++ b/crates/rpc/rpc-types/src/lib.rs @@ -37,6 +37,7 @@ pub use eth::{ engine::{ ExecutionPayload, ExecutionPayloadV1, ExecutionPayloadV2, ExecutionPayloadV3, PayloadError, }, + error::ToRpcError, transaction::{self, TransactionKind, TransactionRequest, TypedTransactionRequest}, }; diff --git a/crates/rpc/rpc-types/src/mev.rs b/crates/rpc/rpc-types/src/mev.rs index 2137e1ecf2..ae94375dbc 100644 --- a/crates/rpc/rpc-types/src/mev.rs +++ b/crates/rpc/rpc-types/src/mev.rs @@ -706,7 +706,7 @@ mod u256_numeric_string { match val { serde_json::Value::String(s) => { if let Ok(val) = s.parse::() { - return Ok(U256::from(val)) + return Ok(U256::from(val)); } U256::from_str(&s).map_err(de::Error::custom) } diff --git a/crates/rpc/rpc/src/eth/error.rs b/crates/rpc/rpc/src/eth/error.rs index d8add63972..75fbcc220b 100644 --- a/crates/rpc/rpc/src/eth/error.rs +++ b/crates/rpc/rpc/src/eth/error.rs @@ -6,7 +6,9 @@ use jsonrpsee::types::{error::CALL_EXECUTION_FAILED_CODE, ErrorObject}; use reth_interfaces::RethError; use reth_primitives::{revm_primitives::InvalidHeader, Address, Bytes, U256}; use reth_revm::tracing::{js::JsInspectorError, MuxError}; -use reth_rpc_types::{error::EthRpcErrorCode, request::TransactionInputError, BlockError}; +use reth_rpc_types::{ + error::EthRpcErrorCode, request::TransactionInputError, BlockError, ToRpcError, +}; use reth_transaction_pool::error::{ Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind, PoolTransactionError, @@ -17,12 +19,6 @@ 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 { diff --git a/crates/rpc/rpc/src/eth/optimism.rs b/crates/rpc/rpc/src/eth/optimism.rs index 2871058f80..24f6f36ff4 100644 --- a/crates/rpc/rpc/src/eth/optimism.rs +++ b/crates/rpc/rpc/src/eth/optimism.rs @@ -1,11 +1,9 @@ //! Optimism specific types. use jsonrpsee::types::ErrorObject; +use reth_rpc_types::ToRpcError; -use crate::{ - eth::error::{EthApiError, ToRpcError}, - result::internal_rpc_err, -}; +use crate::{eth::error::EthApiError, result::internal_rpc_err}; /// Eth Optimism Api Error #[cfg(feature = "optimism")]