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;
5
6#[derive(Debug, thiserror::Error)]
11pub enum BeaconOnNewPayloadError {
12 #[error("beacon consensus engine task stopped")]
14 EngineUnavailable,
15 #[error(transparent)]
20 InvalidParams(Box<dyn core::error::Error + Send + Sync>),
21 #[error(transparent)]
23 Internal(Box<dyn core::error::Error + Send + Sync>),
24}
25
26impl BeaconOnNewPayloadError {
27 pub fn internal<E: core::error::Error + Send + Sync + 'static>(e: E) -> Self {
29 Self::Internal(Box::new(e))
30 }
31}
32
33impl From<InsertBlockFatalError> for BeaconOnNewPayloadError {
34 fn from(error: InsertBlockFatalError) -> Self {
35 Self::internal(error)
36 }
37}
38
39impl From<InsertBlockProcessingError> for BeaconOnNewPayloadError {
40 fn from(error: InsertBlockProcessingError) -> Self {
41 match error {
42 InsertBlockProcessingError::MalformedInput(error) => Self::InvalidParams(error),
43 InsertBlockProcessingError::Fatal(error) => Self::internal(error),
44 }
45 }
46}
47
48#[derive(Debug, thiserror::Error)]
53pub enum BeaconForkChoiceUpdateError {
54 #[error("forkchoice update error: {0}")]
56 ForkchoiceUpdateError(#[from] ForkchoiceUpdateError),
57 #[error("beacon consensus engine task stopped")]
59 EngineUnavailable,
60 #[error(transparent)]
62 Internal(Box<dyn core::error::Error + Send + Sync>),
63}
64
65impl BeaconForkChoiceUpdateError {
66 pub fn internal<E: core::error::Error + Send + Sync + 'static>(e: E) -> Self {
68 Self::Internal(Box::new(e))
69 }
70}
71
72#[derive(Debug, thiserror::Error)]
74pub enum InsertBlockErrorKind {
75 #[error(transparent)]
77 Consensus(#[from] ConsensusError),
78 #[error(transparent)]
80 BlockAccessListDecode(#[from] BlockAccessListDecodeError),
81 #[error(transparent)]
83 Execution(#[from] BlockExecutionError),
84 #[error(transparent)]
86 Provider(#[from] ProviderError),
87 #[error(transparent)]
89 Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
90}
91
92impl InsertBlockErrorKind {
93 pub const fn is_validation_error(&self) -> bool {
95 matches!(
96 self,
97 Self::Consensus(_) |
98 Self::BlockAccessListDecode(_) |
99 Self::Execution(BlockExecutionError::Validation(_))
100 )
101 }
102
103 pub fn ensure_validation_error(
110 self,
111 ) -> Result<InsertBlockValidationError, InsertBlockProcessingError> {
112 match self {
113 Self::BlockAccessListDecode(error) => {
114 Ok(InsertBlockValidationError::BlockAccessListDecode(error))
115 }
116 Self::Consensus(err) => Ok(InsertBlockValidationError::Consensus(err)),
117 Self::Execution(err) => match err {
118 BlockExecutionError::Validation(err) => {
119 Ok(InsertBlockValidationError::Validation(err))
120 }
121 BlockExecutionError::Internal(error) => Err(InsertBlockProcessingError::Fatal(
122 InsertBlockFatalError::BlockExecutionError(error),
123 )),
124 },
125 Self::Provider(err) => {
126 Err(InsertBlockProcessingError::Fatal(InsertBlockFatalError::Provider(err)))
127 }
128 Self::Other(err) => Err(InsertBlockProcessingError::Fatal(
129 InternalBlockExecutionError::Other(err).into(),
130 )),
131 }
132 }
133}
134
135#[derive(Debug, thiserror::Error)]
137#[error("failed to decode block access list: {0}")]
138pub struct BlockAccessListDecodeError(#[source] Box<dyn core::error::Error + Send + Sync>);
139
140impl BlockAccessListDecodeError {
141 pub fn new<E>(error: E) -> Self
143 where
144 E: core::error::Error + Send + Sync + 'static,
145 {
146 Self(Box::new(error))
147 }
148}
149
150#[derive(Debug, thiserror::Error)]
158pub enum InsertBlockProcessingError {
159 #[error(transparent)]
161 MalformedInput(Box<dyn core::error::Error + Send + Sync>),
162 #[error(transparent)]
164 Fatal(#[from] InsertBlockFatalError),
165}
166
167#[derive(Debug, thiserror::Error)]
169pub enum InsertBlockFatalError {
170 #[error(transparent)]
172 Provider(#[from] ProviderError),
173 #[error(transparent)]
175 BlockExecutionError(#[from] InternalBlockExecutionError),
176}
177
178#[derive(Debug, thiserror::Error)]
180pub enum InsertBlockValidationError {
181 #[error(transparent)]
183 Consensus(#[from] ConsensusError),
184 #[error(transparent)]
186 BlockAccessListDecode(#[from] BlockAccessListDecodeError),
187 #[error(transparent)]
189 Validation(#[from] BlockValidationError),
190}
191
192#[cfg(test)]
193mod tests {
194 use super::*;
195
196 #[test]
197 fn ensure_insert_block_validation_error() {
198 let err = InsertBlockErrorKind::BlockAccessListDecode(BlockAccessListDecodeError::new(
199 alloy_rlp::Error::UnexpectedString,
200 ));
201 assert!(err.is_validation_error());
202 assert!(matches!(
203 err.ensure_validation_error(),
204 Ok(InsertBlockValidationError::BlockAccessListDecode(_))
205 ));
206
207 assert!(matches!(
208 InsertBlockErrorKind::Consensus(ConsensusError::BlockAccessListHashMissing)
209 .ensure_validation_error(),
210 Ok(InsertBlockValidationError::Consensus(_))
211 ));
212
213 assert!(matches!(
214 InsertBlockErrorKind::Provider(ProviderError::BestBlockNotFound)
215 .ensure_validation_error(),
216 Err(InsertBlockProcessingError::Fatal(InsertBlockFatalError::Provider(_)))
217 ));
218 }
219}