reth_engine_tree/tree/
error.rs1use alloy_consensus::BlockHeader;
4use alloy_primitives::B256;
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};
10use tokio::sync::oneshot::error::TryRecvError;
11
12#[derive(Debug, thiserror::Error)]
15pub enum AdvancePersistenceError {
16 #[error(transparent)]
18 RecvError(#[from] TryRecvError),
19 #[error(transparent)]
21 Provider(#[from] ProviderError),
22 #[error("Missing ancestor with hash {0}")]
35 MissingAncestor(B256),
36}
37
38#[derive(thiserror::Error)]
39#[error("Failed to insert block (hash={}, number={}, parent_hash={}): {}",
40 .block.hash(),
41 .block.number(),
42 .block.parent_hash(),
43 .kind)]
44struct InsertBlockErrorData<B: Block> {
45 block: SealedBlock<B>,
46 #[source]
47 kind: InsertBlockErrorKind,
48}
49
50impl<B: Block> std::fmt::Debug for InsertBlockErrorData<B> {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("InsertBlockError")
53 .field("error", &self.kind)
54 .field("hash", &self.block.hash())
55 .field("number", &self.block.number())
56 .field("parent_hash", &self.block.parent_hash())
57 .field("num_txs", &self.block.body().transactions().len())
58 .finish_non_exhaustive()
59 }
60}
61
62impl<B: Block> InsertBlockErrorData<B> {
63 const fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
64 Self { block, kind }
65 }
66
67 fn boxed(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Box<Self> {
68 Box::new(Self::new(block, kind))
69 }
70}
71
72#[derive(thiserror::Error)]
74#[error(transparent)]
75pub struct InsertBlockError<B: Block> {
76 inner: Box<InsertBlockErrorData<B>>,
77}
78
79impl<B: Block> InsertBlockError<B> {
82 pub fn new(block: SealedBlock<B>, kind: InsertBlockErrorKind) -> Self {
84 Self { inner: InsertBlockErrorData::boxed(block, kind) }
85 }
86
87 pub fn consensus_error(error: ConsensusError, block: SealedBlock<B>) -> Self {
89 Self::new(block, InsertBlockErrorKind::Consensus(error))
90 }
91
92 #[inline]
94 pub fn into_block(self) -> SealedBlock<B> {
95 self.inner.block
96 }
97
98 #[inline]
100 pub const fn kind(&self) -> &InsertBlockErrorKind {
101 &self.inner.kind
102 }
103
104 #[inline]
106 pub const fn block(&self) -> &SealedBlock<B> {
107 &self.inner.block
108 }
109
110 #[inline]
112 pub fn split(self) -> (SealedBlock<B>, InsertBlockErrorKind) {
113 let inner = *self.inner;
114 (inner.block, inner.kind)
115 }
116}
117
118impl<B: Block> std::fmt::Debug for InsertBlockError<B> {
119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120 std::fmt::Debug::fmt(&self.inner, f)
121 }
122}
123
124#[derive(Debug, thiserror::Error)]
126pub enum InsertBlockErrorKind {
127 #[error(transparent)]
129 Consensus(#[from] ConsensusError),
130 #[error(transparent)]
132 Execution(#[from] BlockExecutionError),
133 #[error(transparent)]
135 Provider(#[from] ProviderError),
136 #[error(transparent)]
138 Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
139}
140
141impl InsertBlockErrorKind {
142 pub fn ensure_validation_error(
150 self,
151 ) -> Result<InsertBlockValidationError, InsertBlockFatalError> {
152 match self {
153 Self::Consensus(err) => Ok(InsertBlockValidationError::Consensus(err)),
154 Self::Execution(err) => {
156 match err {
157 BlockExecutionError::Validation(err) => {
158 Ok(InsertBlockValidationError::Validation(err))
159 }
160 BlockExecutionError::Internal(error) => {
162 Err(InsertBlockFatalError::BlockExecutionError(error))
163 }
164 }
165 }
166 Self::Provider(err) => Err(InsertBlockFatalError::Provider(err)),
167 Self::Other(err) => Err(InternalBlockExecutionError::Other(err).into()),
168 }
169 }
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 Validation(#[from] BlockValidationError),
192}
193
194#[derive(Debug, thiserror::Error)]
196pub enum InsertPayloadError<B: Block> {
197 #[error(transparent)]
199 Block(#[from] InsertBlockError<B>),
200 #[error(transparent)]
202 Payload(#[from] NewPayloadError),
203}