chore: cleanup engine api errors (#2674)

This commit is contained in:
Matthias Seitz
2023-05-15 14:27:22 +02:00
committed by GitHub
parent 89349782ba
commit a73e6c8ca8
7 changed files with 101 additions and 89 deletions

View File

@@ -1,6 +1,6 @@
use crate::{EngineApiError, EngineApiMessageVersion, EngineApiResult};
use async_trait::async_trait;
use jsonrpsee_core::RpcResult as Result;
use jsonrpsee_core::RpcResult;
use reth_beacon_consensus::BeaconConsensusEngineHandle;
use reth_interfaces::consensus::ForkchoiceState;
use reth_payload_builder::PayloadStore;
@@ -296,14 +296,14 @@ where
/// Handler for `engine_newPayloadV1`
/// See also <https://github.com/ethereum/execution-apis/blob/8db51dcd2f4bdfbd9ad6e4a7560aac97010ad063/src/engine/specification.md#engine_newpayloadv1>
/// Caution: This should not accept the `withdrawals` field
async fn new_payload_v1(&self, payload: ExecutionPayload) -> Result<PayloadStatus> {
async fn new_payload_v1(&self, payload: ExecutionPayload) -> RpcResult<PayloadStatus> {
trace!(target: "rpc::eth", "Serving engine_newPayloadV1");
Ok(EngineApi::new_payload_v1(self, payload).await?)
}
/// Handler for `engine_newPayloadV1`
/// See also <https://github.com/ethereum/execution-apis/blob/8db51dcd2f4bdfbd9ad6e4a7560aac97010ad063/src/engine/specification.md#engine_newpayloadv1>
async fn new_payload_v2(&self, payload: ExecutionPayload) -> Result<PayloadStatus> {
async fn new_payload_v2(&self, payload: ExecutionPayload) -> RpcResult<PayloadStatus> {
trace!(target: "rpc::eth", "Serving engine_newPayloadV1");
Ok(EngineApi::new_payload_v2(self, payload).await?)
}
@@ -316,7 +316,7 @@ where
&self,
fork_choice_state: ForkchoiceState,
payload_attributes: Option<PayloadAttributes>,
) -> Result<ForkchoiceUpdated> {
) -> RpcResult<ForkchoiceUpdated> {
trace!(target: "rpc::eth", "Serving engine_forkchoiceUpdatedV1");
Ok(EngineApi::fork_choice_updated_v1(self, fork_choice_state, payload_attributes).await?)
}
@@ -327,7 +327,7 @@ where
&self,
fork_choice_state: ForkchoiceState,
payload_attributes: Option<PayloadAttributes>,
) -> Result<ForkchoiceUpdated> {
) -> RpcResult<ForkchoiceUpdated> {
trace!(target: "rpc::eth", "Serving engine_forkchoiceUpdatedV2");
Ok(EngineApi::fork_choice_updated_v2(self, fork_choice_state, payload_attributes).await?)
}
@@ -343,7 +343,7 @@ where
///
/// Note:
/// > Client software MAY stop the corresponding build process after serving this call.
async fn get_payload_v1(&self, payload_id: PayloadId) -> Result<ExecutionPayload> {
async fn get_payload_v1(&self, payload_id: PayloadId) -> RpcResult<ExecutionPayload> {
trace!(target: "rpc::eth", "Serving engine_getPayloadV1");
Ok(EngineApi::get_payload_v1(self, payload_id).await?)
}
@@ -357,7 +357,7 @@ where
///
/// Note:
/// > Client software MAY stop the corresponding build process after serving this call.
async fn get_payload_v2(&self, payload_id: PayloadId) -> Result<ExecutionPayloadEnvelope> {
async fn get_payload_v2(&self, payload_id: PayloadId) -> RpcResult<ExecutionPayloadEnvelope> {
trace!(target: "rpc::eth", "Serving engine_getPayloadV2");
Ok(EngineApi::get_payload_v2(self, payload_id).await?)
}
@@ -367,7 +367,7 @@ where
async fn get_payload_bodies_by_hash_v1(
&self,
block_hashes: Vec<BlockHash>,
) -> Result<ExecutionPayloadBodies> {
) -> RpcResult<ExecutionPayloadBodies> {
trace!(target: "rpc::eth", "Serving engine_getPayloadBodiesByHashV1");
Ok(EngineApi::get_payload_bodies_by_hash(self, block_hashes)?)
}
@@ -378,7 +378,7 @@ where
&self,
start: U64,
count: U64,
) -> Result<ExecutionPayloadBodies> {
) -> RpcResult<ExecutionPayloadBodies> {
trace!(target: "rpc::eth", "Serving engine_getPayloadBodiesByHashV1");
Ok(EngineApi::get_payload_bodies_by_range(self, start.as_u64(), count.as_u64())?)
}
@@ -388,14 +388,14 @@ where
async fn exchange_transition_configuration(
&self,
config: TransitionConfiguration,
) -> Result<TransitionConfiguration> {
) -> RpcResult<TransitionConfiguration> {
trace!(target: "rpc::eth", "Serving engine_getPayloadBodiesByHashV1");
Ok(EngineApi::exchange_transition_configuration(self, config)?)
}
/// Handler for `engine_exchangeCapabilitiesV1`
/// See also <https://github.com/ethereum/execution-apis/blob/6452a6b194d7db269bf1dbd087a267251d3cc7f8/src/engine/common.md#capabilities>
async fn exchange_capabilities(&self, _capabilities: Vec<String>) -> Result<Vec<String>> {
async fn exchange_capabilities(&self, _capabilities: Vec<String>) -> RpcResult<Vec<String>> {
Ok(CAPABILITIES.into_iter().map(str::to_owned).collect())
}
}

View File

@@ -1,9 +1,8 @@
use jsonrpsee_types::error::{INTERNAL_ERROR_CODE, INVALID_PARAMS_CODE};
use reth_beacon_consensus::{BeaconEngineError, BeaconForkChoiceUpdateError};
use reth_beacon_consensus::{BeaconForkChoiceUpdateError, BeaconOnNewPayloadError};
use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::{H256, U256};
use thiserror::Error;
use tokio::sync::{mpsc, oneshot};
/// The Engine API result type
pub type EngineApiResult<Ok> = Result<Ok, EngineApiError>;
@@ -15,7 +14,7 @@ pub const REQUEST_TOO_LARGE_CODE: i32 = -38004;
/// Error returned by [`EngineApi`][crate::EngineApi]
///
/// Note: This is a high fidelity error type which can be converted to an RPC error that adheres to the spec: <https://github.com/ethereum/execution-apis/blob/main/src/engine/common.md#errors>
/// Note: This is a high-fidelity error type which can be converted to an RPC error that adheres to the spec: <https://github.com/ethereum/execution-apis/blob/main/src/engine/common.md#errors>
#[derive(Error, Debug)]
pub enum EngineApiError {
/// Unknown payload requested.
@@ -64,35 +63,20 @@ pub enum EngineApiError {
/// Consensus terminal block hash.
consensus: H256,
},
/// Beacon consensus engine error.
#[error(transparent)]
ConsensusEngine(#[from] BeaconEngineError),
/// An error occurred while processing the fork choice update.
/// An error occurred while processing the fork choice update in the beacon consensus engine.
#[error(transparent)]
ForkChoiceUpdate(#[from] BeaconForkChoiceUpdateError),
/// An error occurred while processing a new payload in the beacon consensus engine.
#[error(transparent)]
NewPayload(#[from] BeaconOnNewPayloadError),
/// Encountered an internal error.
#[error(transparent)]
Internal(Box<dyn std::error::Error + Send + Sync>),
/// Failed to send message due ot closed channel
#[error("Closed channel")]
ChannelClosed,
/// Fetching the payload failed
#[error(transparent)]
GetPayloadError(#[from] PayloadBuilderError),
}
impl<T> From<mpsc::error::SendError<T>> for EngineApiError {
fn from(_: mpsc::error::SendError<T>) -> Self {
EngineApiError::ChannelClosed
}
}
impl From<oneshot::error::RecvError> for EngineApiError {
fn from(_: oneshot::error::RecvError) -> Self {
EngineApiError::ChannelClosed
}
}
impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
fn from(error: EngineApiError) -> Self {
let code = match error {
@@ -102,15 +86,22 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
EngineApiError::HasWithdrawalsPreShanghai => INVALID_PARAMS_CODE,
EngineApiError::UnknownPayload => UNKNOWN_PAYLOAD_CODE,
EngineApiError::PayloadRequestTooLarge { .. } => REQUEST_TOO_LARGE_CODE,
// Error responses from the consensus engine
EngineApiError::ForkChoiceUpdate(err) => match err {
BeaconForkChoiceUpdateError::ForkchoiceUpdateError(err) => return err.into(),
BeaconForkChoiceUpdateError::EngineUnavailable => INTERNAL_ERROR_CODE,
},
EngineApiError::NewPayload(ref err) => match err {
BeaconOnNewPayloadError::EngineUnavailable => INTERNAL_ERROR_CODE,
},
// Any other server error
_ => INTERNAL_ERROR_CODE,
EngineApiError::TerminalTD { .. } |
EngineApiError::TerminalBlockHash { .. } |
EngineApiError::Internal(_) |
EngineApiError::GetPayloadError(_) => INTERNAL_ERROR_CODE,
};
jsonrpsee_types::error::ErrorObject::owned(
code,
// TODO properly convert to rpc error
error.to_string(),
None::<()>,
)
jsonrpsee_types::error::ErrorObject::owned(code, error.to_string(), None::<()>)
}
}