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;
6pub use reth_engine_primitives::{
7    InsertBlockErrorKind, InsertBlockFatalError, InsertBlockValidationError,
8};
9use reth_errors::ProviderError;
10use reth_payload_primitives::NewPayloadError;
11use reth_primitives_traits::{Block, BlockBody, SealedBlock};
12
13/// This is an error that can come from advancing persistence.
14#[derive(Debug, thiserror::Error)]
15pub enum AdvancePersistenceError {
16    /// The persistence channel was closed unexpectedly
17    #[error("persistence channel closed")]
18    ChannelClosed,
19    /// A provider error
20    #[error(transparent)]
21    Provider(#[from] ProviderError),
22}
23
24#[derive(thiserror::Error)]
25#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {}",
26    .block.hash(),
27    .block.number(),
28    .block.parent_hash(),
29    .kind)]
30struct InsertBlockErrorData<B: Block> {
31    block: SealedBlock<B>,
32    #[source]
33    kind: InsertBlockErrorKind,
34}
35
36impl<B: Block> std::fmt::Debug for InsertBlockErrorData<B> {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        f.debug_struct("InsertBlockError")
39            .field("error", &self.kind)
40            .field("hash", &self.block.hash())
41            .field("number", &self.block.number())
42            .field("parent_hash", &self.block.parent_hash())
43            .field("num_txs", &self.block.body().transactions().len())
44            .finish_non_exhaustive()
45    }
46}
47
48impl<B: Block> InsertBlockErrorData<B> {
49    const fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
50        Self { block, kind }
51    }
52
53    fn boxed(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Box<Self> {
54        Box::new(Self::new(block, kind))
55    }
56}
57
58/// Error thrown when inserting a block failed because the block is considered invalid.
59#[derive(thiserror::Error)]
60#[error(transparent)]
61pub struct InsertBlockError<B: Block> {
62    inner: Box<InsertBlockErrorData<B>>,
63}
64
65// === impl InsertBlockErrorTwo ===
66
67impl<B: Block> InsertBlockError<B> {
68    /// Create a new `InsertInvalidBlockErrorTwo`
69    pub fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
70        Self { inner: InsertBlockErrorData::boxed(block, kind) }
71    }
72
73    /// Create a new `InsertInvalidBlockError` from a consensus error
74    pub fn consensus_error(error: ConsensusError, block: SealedBlock<B>) -> Self {
75        Self::new(block, InsertBlockErrorKind::Consensus(error))
76    }
77
78    /// Consumes the error and returns the block that resulted in the error
79    #[inline]
80    pub fn into_block(self) -> SealedBlock<B> {
81        self.inner.block
82    }
83
84    /// Returns the error kind
85    #[inline]
86    pub const fn kind(&self) -> &InsertBlockErrorKind {
87        &self.inner.kind
88    }
89
90    /// Returns the block that resulted in the error
91    #[inline]
92    pub const fn block(&self) -> &SealedBlock<B> {
93        &self.inner.block
94    }
95
96    /// Consumes the type and returns the block and error kind.
97    #[inline]
98    pub fn split(self) -> (SealedBlock<B>, InsertBlockErrorKind) {
99        let inner = *self.inner;
100        (inner.block, inner.kind)
101    }
102}
103
104impl<B: Block> std::fmt::Debug for InsertBlockError<B> {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        std::fmt::Debug::fmt(&self.inner, f)
107    }
108}
109
110impl From<BalExecutionError> for InsertBlockErrorKind {
111    fn from(e: BalExecutionError) -> Self {
112        match e {
113            BalExecutionError::Consensus(inner) => Self::Consensus(inner),
114            BalExecutionError::Execution(inner) => Self::Execution(inner),
115            BalExecutionError::Provider(inner) => Self::Provider(inner),
116            BalExecutionError::Other(inner) => Self::Other(inner),
117        }
118    }
119}
120
121/// Errors that may occur when inserting a payload.
122#[derive(Debug, thiserror::Error)]
123pub enum InsertPayloadError<B: Block> {
124    /// Block validation error
125    #[error(transparent)]
126    Block(#[from] InsertBlockError<B>),
127    /// Payload validation error
128    #[error(transparent)]
129    Payload(#[from] NewPayloadError),
130}