reth_engine_tree/tree/
error.rs1use alloy_consensus::BlockHeader;
4use reth_consensus::ConsensusError;
5use reth_errors::{BlockExecutionError, BlockValidationError, ProviderError};
6use reth_evm::execute::InternalBlockExecutionError;
7use reth_payload_primitives::NewPayloadError;
8use reth_primitives_traits::{Block, BlockBody, SealedBlock};
9use tokio::sync::oneshot::error::TryRecvError;
10
11#[derive(Debug, thiserror::Error)]
14pub enum AdvancePersistenceError {
15    #[error(transparent)]
17    RecvError(#[from] TryRecvError),
18    #[error(transparent)]
20    Provider(#[from] ProviderError),
21}
22
23#[derive(thiserror::Error)]
24#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {}",
25    .block.hash(),
26    .block.number(),
27    .block.parent_hash(),
28    .kind)]
29struct InsertBlockErrorData<B: Block> {
30    block: SealedBlock<B>,
31    #[source]
32    kind: InsertBlockErrorKind,
33}
34
35impl<B: Block> std::fmt::Debug for InsertBlockErrorData<B> {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        f.debug_struct("InsertBlockError")
38            .field("error", &self.kind)
39            .field("hash", &self.block.hash())
40            .field("number", &self.block.number())
41            .field("parent_hash", &self.block.parent_hash())
42            .field("num_txs", &self.block.body().transactions().len())
43            .finish_non_exhaustive()
44    }
45}
46
47impl<B: Block> InsertBlockErrorData<B> {
48    const fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
49        Self { block, kind }
50    }
51
52    fn boxed(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Box<Self> {
53        Box::new(Self::new(block, kind))
54    }
55}
56
57#[derive(thiserror::Error)]
59#[error(transparent)]
60pub struct InsertBlockError<B: Block> {
61    inner: Box<InsertBlockErrorData<B>>,
62}
63
64impl<B: Block> InsertBlockError<B> {
67    pub fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
69        Self { inner: InsertBlockErrorData::boxed(block, kind) }
70    }
71
72    pub fn consensus_error(error: ConsensusError, block: SealedBlock<B>) -> Self {
74        Self::new(block, InsertBlockErrorKind::Consensus(error))
75    }
76
77    #[inline]
79    pub fn into_block(self) -> SealedBlock<B> {
80        self.inner.block
81    }
82
83    #[inline]
85    pub const fn kind(&self) -> &InsertBlockErrorKind {
86        &self.inner.kind
87    }
88
89    #[inline]
91    pub const fn block(&self) -> &SealedBlock<B> {
92        &self.inner.block
93    }
94
95    #[inline]
97    pub fn split(self) -> (SealedBlock<B>, InsertBlockErrorKind) {
98        let inner = *self.inner;
99        (inner.block, inner.kind)
100    }
101}
102
103impl<B: Block> std::fmt::Debug for InsertBlockError<B> {
104    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105        std::fmt::Debug::fmt(&self.inner, f)
106    }
107}
108
109#[derive(Debug, thiserror::Error)]
111pub enum InsertBlockErrorKind {
112    #[error(transparent)]
114    Consensus(#[from] ConsensusError),
115    #[error(transparent)]
117    Execution(#[from] BlockExecutionError),
118    #[error(transparent)]
120    Provider(#[from] ProviderError),
121    #[error(transparent)]
123    Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
124}
125
126impl InsertBlockErrorKind {
127    pub fn ensure_validation_error(
135        self,
136    ) -> Result<InsertBlockValidationError, InsertBlockFatalError> {
137        match self {
138            Self::Consensus(err) => Ok(InsertBlockValidationError::Consensus(err)),
139            Self::Execution(err) => {
141                match err {
142                    BlockExecutionError::Validation(err) => {
143                        Ok(InsertBlockValidationError::Validation(err))
144                    }
145                    BlockExecutionError::Internal(error) => {
147                        Err(InsertBlockFatalError::BlockExecutionError(error))
148                    }
149                }
150            }
151            Self::Provider(err) => Err(InsertBlockFatalError::Provider(err)),
152            Self::Other(err) => Err(InternalBlockExecutionError::Other(err).into()),
153        }
154    }
155}
156
157#[derive(Debug, thiserror::Error)]
159pub enum InsertBlockFatalError {
160    #[error(transparent)]
162    Provider(#[from] ProviderError),
163    #[error(transparent)]
165    BlockExecutionError(#[from] InternalBlockExecutionError),
166}
167
168#[derive(Debug, thiserror::Error)]
170pub enum InsertBlockValidationError {
171    #[error(transparent)]
173    Consensus(#[from] ConsensusError),
174    #[error(transparent)]
176    Validation(#[from] BlockValidationError),
177}
178
179#[derive(Debug, thiserror::Error)]
181pub enum InsertPayloadError<B: Block> {
182    #[error(transparent)]
184    Block(#[from] InsertBlockError<B>),
185    #[error(transparent)]
187    Payload(#[from] NewPayloadError),
188}