reth_engine_primitives/
error.rs1use alloc::boxed::Box;
2use alloy_rpc_types_engine::ForkchoiceUpdateError;
3use reth_errors::{BlockExecutionError, BlockValidationError, ConsensusError, ProviderError};
4use reth_execution_errors::InternalBlockExecutionError;
5use reth_payload_primitives::PayloadBuilderError;
6
7#[derive(Debug, thiserror::Error)]
12pub enum BeaconOnNewPayloadError {
13 #[error("beacon consensus engine task stopped")]
15 EngineUnavailable,
16 #[error(transparent)]
21 InvalidParams(Box<dyn core::error::Error + Send + Sync>),
22 #[error(transparent)]
24 Internal(Box<dyn core::error::Error + Send + Sync>),
25}
26
27impl BeaconOnNewPayloadError {
28 pub fn internal<E: core::error::Error + Send + Sync + 'static>(e: E) -> Self {
30 Self::Internal(Box::new(e))
31 }
32}
33
34impl From<InsertBlockFatalError> for BeaconOnNewPayloadError {
35 fn from(error: InsertBlockFatalError) -> Self {
36 Self::internal(error)
37 }
38}
39
40impl From<InsertBlockProcessingError> for BeaconOnNewPayloadError {
41 fn from(error: InsertBlockProcessingError) -> Self {
42 match error {
43 InsertBlockProcessingError::MalformedInput(error) => Self::InvalidParams(error),
44 InsertBlockProcessingError::Fatal(error) => Self::internal(error),
45 }
46 }
47}
48
49#[derive(Debug, thiserror::Error)]
54pub enum BeaconForkChoiceUpdateError {
55 #[error("forkchoice update error: {0}")]
57 ForkchoiceUpdateError(#[from] ForkchoiceUpdateError),
58 #[error("beacon consensus engine task stopped")]
60 EngineUnavailable,
61 #[error("failed to start payload job: {0}")]
64 PayloadJob(#[source] PayloadBuilderError),
65 #[error(transparent)]
67 Internal(Box<dyn core::error::Error + Send + Sync>),
68}
69
70impl BeaconForkChoiceUpdateError {
71 pub fn internal<E: core::error::Error + Send + Sync + 'static>(e: E) -> Self {
73 Self::Internal(Box::new(e))
74 }
75}
76
77#[derive(Debug, thiserror::Error)]
79pub enum InsertBlockErrorKind {
80 #[error(transparent)]
82 Consensus(#[from] ConsensusError),
83 #[error(transparent)]
85 BlockAccessListDecode(#[from] BlockAccessListDecodeError),
86 #[error(transparent)]
88 Execution(#[from] BlockExecutionError),
89 #[error(transparent)]
91 Provider(#[from] ProviderError),
92 #[error(transparent)]
94 Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
95}
96
97impl InsertBlockErrorKind {
98 pub const fn is_validation_error(&self) -> bool {
100 matches!(
101 self,
102 Self::Consensus(_) |
103 Self::BlockAccessListDecode(_) |
104 Self::Execution(BlockExecutionError::Validation(_))
105 )
106 }
107
108 pub fn ensure_validation_error(
115 self,
116 ) -> Result<InsertBlockValidationError, InsertBlockProcessingError> {
117 match self {
118 Self::BlockAccessListDecode(error) => {
119 Ok(InsertBlockValidationError::BlockAccessListDecode(error))
120 }
121 Self::Consensus(err) => Ok(InsertBlockValidationError::Consensus(err)),
122 Self::Execution(err) => match err {
123 BlockExecutionError::Validation(err) => {
124 Ok(InsertBlockValidationError::Validation(err))
125 }
126 BlockExecutionError::Internal(error) => Err(InsertBlockProcessingError::Fatal(
127 InsertBlockFatalError::BlockExecutionError(error),
128 )),
129 },
130 Self::Provider(err) => {
131 Err(InsertBlockProcessingError::Fatal(InsertBlockFatalError::Provider(err)))
132 }
133 Self::Other(err) => Err(InsertBlockProcessingError::Fatal(
134 InternalBlockExecutionError::Other(err).into(),
135 )),
136 }
137 }
138}
139
140#[derive(Debug, thiserror::Error)]
142#[error("failed to decode block access list: {0}")]
143pub struct BlockAccessListDecodeError(#[source] Box<dyn core::error::Error + Send + Sync>);
144
145impl BlockAccessListDecodeError {
146 pub fn new<E>(error: E) -> Self
148 where
149 E: core::error::Error + Send + Sync + 'static,
150 {
151 Self(Box::new(error))
152 }
153}
154
155#[derive(Debug, thiserror::Error)]
163pub enum InsertBlockProcessingError {
164 #[error(transparent)]
166 MalformedInput(Box<dyn core::error::Error + Send + Sync>),
167 #[error(transparent)]
169 Fatal(#[from] InsertBlockFatalError),
170}
171
172#[derive(Debug, thiserror::Error)]
174pub enum InsertBlockFatalError {
175 #[error(transparent)]
177 Provider(#[from] ProviderError),
178 #[error(transparent)]
180 BlockExecutionError(#[from] InternalBlockExecutionError),
181}
182
183#[derive(Debug, thiserror::Error)]
185pub enum InsertBlockValidationError {
186 #[error(transparent)]
188 Consensus(#[from] ConsensusError),
189 #[error(transparent)]
191 BlockAccessListDecode(#[from] BlockAccessListDecodeError),
192 #[error(transparent)]
194 Validation(#[from] BlockValidationError),
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 #[test]
202 fn ensure_insert_block_validation_error() {
203 let err = InsertBlockErrorKind::BlockAccessListDecode(BlockAccessListDecodeError::new(
204 alloy_rlp::Error::UnexpectedString,
205 ));
206 assert!(err.is_validation_error());
207 assert!(matches!(
208 err.ensure_validation_error(),
209 Ok(InsertBlockValidationError::BlockAccessListDecode(_))
210 ));
211
212 assert!(matches!(
213 InsertBlockErrorKind::Consensus(ConsensusError::BlockAccessListHashMissing)
214 .ensure_validation_error(),
215 Ok(InsertBlockValidationError::Consensus(_))
216 ));
217
218 assert!(matches!(
219 InsertBlockErrorKind::Provider(ProviderError::BestBlockNotFound)
220 .ensure_validation_error(),
221 Err(InsertBlockProcessingError::Fatal(InsertBlockFatalError::Provider(_)))
222 ));
223 }
224}