reth_beacon_consensus/engine/
error.rs

1use crate::engine::hooks::EngineHookError;
2use alloy_rpc_types_engine::ForkchoiceUpdateError;
3use reth_errors::{DatabaseError, RethError};
4use reth_stages_api::PipelineError;
5
6/// Beacon engine result.
7pub type BeaconEngineResult<Ok> = Result<Ok, BeaconConsensusEngineError>;
8
9/// The error type for the beacon consensus engine service
10/// [`BeaconConsensusEngine`](crate::BeaconConsensusEngine)
11///
12/// Represents all possible error cases for the beacon consensus engine.
13#[derive(Debug, thiserror::Error)]
14pub enum BeaconConsensusEngineError {
15    /// Pipeline channel closed.
16    #[error("pipeline channel closed")]
17    PipelineChannelClosed,
18    /// Pipeline error.
19    #[error(transparent)]
20    Pipeline(#[from] Box<PipelineError>),
21    /// Pruner channel closed.
22    #[error("pruner channel closed")]
23    PrunerChannelClosed,
24    /// Hook error.
25    #[error(transparent)]
26    Hook(#[from] EngineHookError),
27    /// Common error. Wrapper around [`RethError`].
28    #[error(transparent)]
29    Common(#[from] RethError),
30}
31
32// box the pipeline error as it is a large enum.
33impl From<PipelineError> for BeaconConsensusEngineError {
34    fn from(e: PipelineError) -> Self {
35        Self::Pipeline(Box::new(e))
36    }
37}
38
39// for convenience in the beacon engine
40impl From<DatabaseError> for BeaconConsensusEngineError {
41    fn from(e: DatabaseError) -> Self {
42        Self::Common(e.into())
43    }
44}
45
46/// Represents error cases for an applied forkchoice update.
47///
48/// This represents all possible error cases, that must be returned as JSON RCP errors back to the
49/// beacon node.
50#[derive(Debug, thiserror::Error)]
51pub enum BeaconForkChoiceUpdateError {
52    /// Thrown when a forkchoice update resulted in an error.
53    #[error("forkchoice update error: {0}")]
54    ForkchoiceUpdateError(#[from] ForkchoiceUpdateError),
55    /// Thrown when the engine task is unavailable/stopped.
56    #[error("beacon consensus engine task stopped")]
57    EngineUnavailable,
58    /// An internal error occurred, not necessarily related to the update.
59    #[error(transparent)]
60    Internal(Box<dyn core::error::Error + Send + Sync>),
61}
62
63impl BeaconForkChoiceUpdateError {
64    /// Create a new internal error.
65    pub fn internal<E: core::error::Error + Send + Sync + 'static>(e: E) -> Self {
66        Self::Internal(Box::new(e))
67    }
68}
69
70impl From<RethError> for BeaconForkChoiceUpdateError {
71    fn from(e: RethError) -> Self {
72        Self::internal(e)
73    }
74}
75impl From<DatabaseError> for BeaconForkChoiceUpdateError {
76    fn from(e: DatabaseError) -> Self {
77        Self::internal(e)
78    }
79}