reth_execution_errors/
trie.rs

1//! Errors when computing the state root.
2
3use alloc::{boxed::Box, string::ToString};
4use alloy_primitives::{Bytes, B256};
5use nybbles::Nibbles;
6use reth_storage_errors::{db::DatabaseError, provider::ProviderError};
7use thiserror::Error;
8
9/// State root errors.
10#[derive(Error, Clone, Debug)]
11pub enum StateRootError {
12    /// Internal database error.
13    #[error(transparent)]
14    Database(#[from] DatabaseError),
15    /// Storage root error.
16    #[error(transparent)]
17    StorageRootError(#[from] StorageRootError),
18    /// Provider error when loading prefix sets
19    #[error(transparent)]
20    PrefixSetLoadError(#[from] ProviderError),
21}
22
23impl From<StateRootError> for ProviderError {
24    fn from(value: StateRootError) -> Self {
25        match value {
26            StateRootError::Database(err) |
27            StateRootError::StorageRootError(StorageRootError::Database(err)) => {
28                Self::Database(err)
29            }
30            StateRootError::PrefixSetLoadError(err) => err,
31        }
32    }
33}
34
35/// Storage root error.
36#[derive(Error, Clone, Debug)]
37pub enum StorageRootError {
38    /// Internal database error.
39    #[error(transparent)]
40    Database(#[from] DatabaseError),
41}
42
43impl From<StorageRootError> for DatabaseError {
44    fn from(err: StorageRootError) -> Self {
45        match err {
46            StorageRootError::Database(err) => err,
47        }
48    }
49}
50
51/// State proof errors.
52#[derive(Error, Clone, Debug)]
53pub enum StateProofError {
54    /// Internal database error.
55    #[error(transparent)]
56    Database(#[from] DatabaseError),
57    /// RLP decoding error.
58    #[error(transparent)]
59    Rlp(#[from] alloy_rlp::Error),
60}
61
62impl From<StateProofError> for ProviderError {
63    fn from(value: StateProofError) -> Self {
64        match value {
65            StateProofError::Database(error) => Self::Database(error),
66            StateProofError::Rlp(error) => Self::Rlp(error),
67        }
68    }
69}
70
71/// Result type with [`SparseStateTrieError`] as error.
72pub type SparseStateTrieResult<Ok> = Result<Ok, SparseStateTrieError>;
73
74/// Error encountered in `SparseStateTrie`.
75#[derive(Error, Debug)]
76#[error(transparent)]
77pub struct SparseStateTrieError(#[from] Box<SparseStateTrieErrorKind>);
78
79impl<T: Into<SparseStateTrieErrorKind>> From<T> for SparseStateTrieError {
80    #[cold]
81    fn from(value: T) -> Self {
82        Self(Box::new(value.into()))
83    }
84}
85
86impl From<SparseTrieError> for SparseStateTrieErrorKind {
87    #[cold]
88    fn from(value: SparseTrieError) -> Self {
89        Self::Sparse(*value.0)
90    }
91}
92
93impl SparseStateTrieError {
94    /// Returns the error kind.
95    pub const fn kind(&self) -> &SparseStateTrieErrorKind {
96        &self.0
97    }
98
99    /// Consumes the error and returns the error kind.
100    pub fn into_kind(self) -> SparseStateTrieErrorKind {
101        *self.0
102    }
103}
104
105/// Error encountered in `SparseStateTrie`.
106#[derive(Error, Debug)]
107pub enum SparseStateTrieErrorKind {
108    /// Encountered invalid root node.
109    #[error("invalid root node at {path:?}: {node:?}")]
110    InvalidRootNode {
111        /// Path to first proof node.
112        path: Nibbles,
113        /// Encoded first proof node.
114        node: Bytes,
115    },
116    /// Storage sparse trie error.
117    #[error("error in storage trie for address {0:?}: {1:?}")]
118    SparseStorageTrie(B256, SparseTrieErrorKind),
119    /// Sparse trie error.
120    #[error(transparent)]
121    Sparse(#[from] SparseTrieErrorKind),
122    /// RLP error.
123    #[error(transparent)]
124    Rlp(#[from] alloy_rlp::Error),
125}
126
127/// Result type with [`SparseTrieError`] as error.
128pub type SparseTrieResult<Ok> = Result<Ok, SparseTrieError>;
129
130/// Error encountered in `SparseTrie`.
131#[derive(Error, Debug)]
132#[error(transparent)]
133pub struct SparseTrieError(#[from] Box<SparseTrieErrorKind>);
134
135impl<T: Into<SparseTrieErrorKind>> From<T> for SparseTrieError {
136    #[cold]
137    fn from(value: T) -> Self {
138        Self(Box::new(value.into()))
139    }
140}
141
142impl SparseTrieError {
143    /// Returns the error kind.
144    pub const fn kind(&self) -> &SparseTrieErrorKind {
145        &self.0
146    }
147
148    /// Consumes the error and returns the error kind.
149    pub fn into_kind(self) -> SparseTrieErrorKind {
150        *self.0
151    }
152}
153
154/// [`SparseTrieError`] kind.
155#[derive(Error, Debug)]
156pub enum SparseTrieErrorKind {
157    /// Sparse trie is still blind. Thrown on attempt to update it.
158    #[error("sparse trie is blind")]
159    Blind,
160    /// Encountered blinded node on update.
161    #[error("attempted to update blind node at {path:?}: {hash}")]
162    BlindedNode {
163        /// Blind node path.
164        path: Nibbles,
165        /// Node hash
166        hash: B256,
167    },
168    /// Encountered unexpected node at path when revealing.
169    #[error("encountered an invalid node at path {path:?} when revealing: {node:?}")]
170    Reveal {
171        /// Path to the node.
172        path: Nibbles,
173        /// Node that was at the path when revealing.
174        node: Box<dyn core::fmt::Debug + Send>,
175    },
176    /// RLP error.
177    #[error(transparent)]
178    Rlp(#[from] alloy_rlp::Error),
179    /// Node not found in provider during revealing.
180    #[error("node {path:?} not found in provider during revealing")]
181    NodeNotFoundInProvider {
182        /// Path to the missing node.
183        path: Nibbles,
184    },
185    /// Other.
186    #[error(transparent)]
187    Other(#[from] Box<dyn core::error::Error + Send>),
188}
189
190/// Trie witness errors.
191#[derive(Error, Debug)]
192pub enum TrieWitnessError {
193    /// Error gather proofs.
194    #[error(transparent)]
195    Proof(#[from] StateProofError),
196    /// RLP decoding error.
197    #[error(transparent)]
198    Rlp(#[from] alloy_rlp::Error),
199    /// Sparse state trie error.
200    #[error(transparent)]
201    Sparse(#[from] SparseStateTrieError),
202    /// Missing account.
203    #[error("missing account {_0}")]
204    MissingAccount(B256),
205}
206
207impl From<SparseStateTrieErrorKind> for TrieWitnessError {
208    fn from(error: SparseStateTrieErrorKind) -> Self {
209        Self::Sparse(error.into())
210    }
211}
212
213impl From<TrieWitnessError> for ProviderError {
214    fn from(error: TrieWitnessError) -> Self {
215        Self::TrieWitnessError(error.to_string())
216    }
217}