Skip to main content

reth_engine_tree/tree/
error.rs

1//! Internal errors for the tree module.
2
3use crate::tree::payload_processor::bal::BalExecutionError;
4use alloy_consensus::BlockHeader;
5use reth_consensus::ConsensusError;
6use reth_errors::{BlockExecutionError, BlockValidationError, ProviderError};
7use reth_evm::execute::InternalBlockExecutionError;
8use reth_payload_primitives::NewPayloadError;
9use reth_primitives_traits::{Block, BlockBody, SealedBlock};
10
11/// This is an error that can come from advancing persistence.
12#[derive(Debug, thiserror::Error)]
13pub enum AdvancePersistenceError {
14    /// The persistence channel was closed unexpectedly
15    #[error("persistence channel closed")]
16    ChannelClosed,
17    /// A provider error
18    #[error(transparent)]
19    Provider(#[from] ProviderError),
20}
21
22#[derive(thiserror::Error)]
23#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {}",
24    .block.hash(),
25    .block.number(),
26    .block.parent_hash(),
27    .kind)]
28struct InsertBlockErrorData<B: Block> {
29    block: SealedBlock<B>,
30    #[source]
31    kind: InsertBlockErrorKind,
32}
33
34impl<B: Block> std::fmt::Debug for InsertBlockErrorData<B> {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        f.debug_struct("InsertBlockError")
37            .field("error", &self.kind)
38            .field("hash", &self.block.hash())
39            .field("number", &self.block.number())
40            .field("parent_hash", &self.block.parent_hash())
41            .field("num_txs", &self.block.body().transactions().len())
42            .finish_non_exhaustive()
43    }
44}
45
46impl<B: Block> InsertBlockErrorData<B> {
47    const fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
48        Self { block, kind }
49    }
50
51    fn boxed(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Box<Self> {
52        Box::new(Self::new(block, kind))
53    }
54}
55
56/// Error thrown when inserting a block failed because the block is considered invalid.
57#[derive(thiserror::Error)]
58#[error(transparent)]
59pub struct InsertBlockError<B: Block> {
60    inner: Box<InsertBlockErrorData<B>>,
61}
62
63// === impl InsertBlockErrorTwo ===
64
65impl<B: Block> InsertBlockError<B> {
66    /// Create a new `InsertInvalidBlockErrorTwo`
67    pub fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
68        Self { inner: InsertBlockErrorData::boxed(block, kind) }
69    }
70
71    /// Create a new `InsertInvalidBlockError` from a consensus error
72    pub fn consensus_error(error: ConsensusError, block: SealedBlock<B>) -> Self {
73        Self::new(block, InsertBlockErrorKind::Consensus(error))
74    }
75
76    /// Consumes the error and returns the block that resulted in the error
77    #[inline]
78    pub fn into_block(self) -> SealedBlock<B> {
79        self.inner.block
80    }
81
82    /// Returns the error kind
83    #[inline]
84    pub const fn kind(&self) -> &InsertBlockErrorKind {
85        &self.inner.kind
86    }
87
88    /// Returns the block that resulted in the error
89    #[inline]
90    pub const fn block(&self) -> &SealedBlock<B> {
91        &self.inner.block
92    }
93
94    /// Consumes the type and returns the block and error kind.
95    #[inline]
96    pub fn split(self) -> (SealedBlock<B>, InsertBlockErrorKind) {
97        let inner = *self.inner;
98        (inner.block, inner.kind)
99    }
100}
101
102impl<B: Block> std::fmt::Debug for InsertBlockError<B> {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        std::fmt::Debug::fmt(&self.inner, f)
105    }
106}
107
108/// All error variants possible when inserting a block
109#[derive(Debug, thiserror::Error)]
110pub enum InsertBlockErrorKind {
111    /// Block violated consensus rules.
112    #[error(transparent)]
113    Consensus(#[from] ConsensusError),
114    /// Block execution failed.
115    #[error(transparent)]
116    Execution(#[from] BlockExecutionError),
117    /// Provider error.
118    #[error(transparent)]
119    Provider(#[from] ProviderError),
120    /// Other errors.
121    #[error(transparent)]
122    Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
123}
124
125impl From<BalExecutionError> for InsertBlockErrorKind {
126    fn from(e: BalExecutionError) -> Self {
127        match e {
128            BalExecutionError::Consensus(inner) => Self::Consensus(inner),
129            BalExecutionError::Execution(inner) => Self::Execution(inner),
130            BalExecutionError::Provider(inner) => Self::Provider(inner),
131            BalExecutionError::Other(inner) => Self::Other(inner),
132        }
133    }
134}
135
136impl InsertBlockErrorKind {
137    /// Returns an [`InsertBlockValidationError`] if the error is caused by an invalid block.
138    ///
139    /// Returns an [`InsertBlockFatalError`] if the error is caused by an error that is not
140    /// validation related or is otherwise fatal.
141    ///
142    /// This is intended to be used to determine if we should respond `INVALID` as a response when
143    /// processing a new block.
144    pub fn ensure_validation_error(
145        self,
146    ) -> Result<InsertBlockValidationError, InsertBlockFatalError> {
147        match self {
148            Self::Consensus(err) => Ok(InsertBlockValidationError::Consensus(err)),
149            // other execution errors that are considered internal errors
150            Self::Execution(err) => {
151                match err {
152                    BlockExecutionError::Validation(err) => {
153                        Ok(InsertBlockValidationError::Validation(err))
154                    }
155                    // these are internal errors, not caused by an invalid block
156                    BlockExecutionError::Internal(error) => {
157                        Err(InsertBlockFatalError::BlockExecutionError(error))
158                    }
159                }
160            }
161            Self::Provider(err) => Err(InsertBlockFatalError::Provider(err)),
162            Self::Other(err) => Err(InternalBlockExecutionError::Other(err).into()),
163        }
164    }
165}
166
167/// Error variants that are not caused by invalid blocks
168#[derive(Debug, thiserror::Error)]
169pub enum InsertBlockFatalError {
170    /// A provider error
171    #[error(transparent)]
172    Provider(#[from] ProviderError),
173    /// An internal / fatal block execution error
174    #[error(transparent)]
175    BlockExecutionError(#[from] InternalBlockExecutionError),
176}
177
178/// Error variants that are caused by invalid blocks
179#[derive(Debug, thiserror::Error)]
180pub enum InsertBlockValidationError {
181    /// Block violated consensus rules.
182    #[error(transparent)]
183    Consensus(#[from] ConsensusError),
184    /// Validation error, transparently wrapping [`BlockValidationError`]
185    #[error(transparent)]
186    Validation(#[from] BlockValidationError),
187}
188
189/// Errors that may occur when inserting a payload.
190#[derive(Debug, thiserror::Error)]
191pub enum InsertPayloadError<B: Block> {
192    /// Block validation error
193    #[error(transparent)]
194    Block(#[from] InsertBlockError<B>),
195    /// Payload validation error
196    #[error(transparent)]
197    Payload(#[from] NewPayloadError),
198}