reth_storage_errors/
provider.rs1use crate::{any::AnyError, db::DatabaseError};
2use alloc::{boxed::Box, string::String};
3use alloy_eips::{BlockHashOrNumber, HashOrNumber};
4use alloy_primitives::{Address, BlockHash, BlockNumber, TxNumber, B256};
5use derive_more::Display;
6use reth_primitives_traits::{transaction::signed::RecoveryError, GotExpected};
7use reth_prune_types::PruneSegmentError;
8use reth_static_file_types::StaticFileSegment;
9use revm_database_interface::DBErrorMarker;
10
11pub type ProviderResult<Ok> = Result<Ok, ProviderError>;
13
14#[derive(Clone, Debug, thiserror::Error)]
16pub enum ProviderError {
17 #[error(transparent)]
19 Database(#[from] DatabaseError),
20 #[error(transparent)]
22 Pruning(#[from] PruneSegmentError),
23 #[error("{_0}")]
25 Rlp(alloy_rlp::Error),
26 #[error("trie witness error: {_0}")]
28 TrieWitnessError(String),
29 #[error("failed to recover sender for transaction")]
31 SenderRecoveryError,
32 #[error("block hash {_0} does not exist in Headers table")]
34 BlockHashNotFound(BlockHash),
35 #[error("block meta not found for block #{_0}")]
37 BlockBodyIndicesNotFound(BlockNumber),
38 #[error(
41 "storage change set for address {address} and key {storage_key} at block #{block_number} does not exist"
42 )]
43 StorageChangesetNotFound {
44 block_number: BlockNumber,
46 address: Address,
48 storage_key: Box<B256>,
52 },
53 #[error("account change set for address {address} at block #{block_number} does not exist")]
55 AccountChangesetNotFound {
56 block_number: BlockNumber,
58 address: Address,
60 },
61 #[error("no header found for {_0:?}")]
63 HeaderNotFound(BlockHashOrNumber),
64 #[error("no transaction found for {_0:?}")]
66 TransactionNotFound(HashOrNumber),
67 #[error("no receipt found for {_0:?}")]
69 ReceiptNotFound(HashOrNumber),
70 #[error("best block does not exist")]
72 BestBlockNotFound,
73 #[error("finalized block does not exist")]
75 FinalizedBlockNotFound,
76 #[error("safe block does not exist")]
78 SafeBlockNotFound,
79 #[error("unknown block {_0}")]
81 UnknownBlockHash(B256),
82 #[error("no state found for block {_0}")]
84 StateForHashNotFound(B256),
85 #[error("no state found for block number {_0}")]
87 StateForNumberNotFound(u64),
88 #[error("unable to find the block number for a given transaction index")]
90 BlockNumberForTransactionIndexNotFound,
91 #[error("merkle trie {_0}")]
93 StateRootMismatch(Box<RootMismatch>),
94 #[error("unwind merkle trie {_0}")]
96 UnwindStateRootMismatch(Box<RootMismatch>),
97 #[error("state at block #{_0} is pruned")]
99 StateAtBlockPruned(BlockNumber),
100 #[error("this provider does not support this request")]
102 UnsupportedProvider,
103 #[cfg(feature = "std")]
105 #[error("not able to find {_0} static file at {_1:?}")]
106 MissingStaticFilePath(StaticFileSegment, std::path::PathBuf),
107 #[error("not able to find {_0} static file for block number {_1}")]
109 MissingStaticFileBlock(StaticFileSegment, BlockNumber),
110 #[error("unable to find {_0} static file for transaction id {_1}")]
112 MissingStaticFileTx(StaticFileSegment, TxNumber),
113 #[error("unable to write block #{_1} to finalized static file {_0}")]
115 FinalizedStaticFile(StaticFileSegment, BlockNumber),
116 #[error("trying to append data to {_0} as block #{_1} but expected block #{_2}")]
118 UnexpectedStaticFileBlockNumber(StaticFileSegment, BlockNumber, BlockNumber),
119 #[error("trying to append row to {_0} at index #{_1} but expected index #{_2}")]
121 UnexpectedStaticFileTxNumber(StaticFileSegment, TxNumber, TxNumber),
122 #[error("cannot get a writer on a read-only environment.")]
124 ReadOnlyStaticFileAccess,
125 #[error("failed to initialize consistent view: {_0}")]
127 ConsistentView(Box<ConsistentViewError>),
128 #[error("received invalid output from storage")]
130 InvalidStorageOutput,
131 #[error("missing trie updates for block {0}")]
133 MissingTrieUpdates(B256),
134 #[error("insufficient changesets to revert to block #{requested}. Available changeset range: {available:?}")]
136 InsufficientChangesets {
137 requested: BlockNumber,
139 available: core::ops::RangeInclusive<BlockNumber>,
141 },
142 #[error(transparent)]
144 Other(#[from] AnyError),
145}
146
147impl ProviderError {
148 pub fn other<E>(error: E) -> Self
151 where
152 E: core::error::Error + Send + Sync + 'static,
153 {
154 Self::Other(AnyError::new(error))
155 }
156
157 pub fn as_other(&self) -> Option<&(dyn core::error::Error + Send + Sync + 'static)> {
159 match self {
160 Self::Other(err) => Some(err.as_error()),
161 _ => None,
162 }
163 }
164
165 pub fn downcast_other_ref<T: core::error::Error + 'static>(&self) -> Option<&T> {
169 let other = self.as_other()?;
170 other.downcast_ref()
171 }
172
173 pub fn is_other<T: core::error::Error + 'static>(&self) -> bool {
176 self.as_other().map(|err| err.is::<T>()).unwrap_or(false)
177 }
178}
179
180impl DBErrorMarker for ProviderError {}
181
182impl From<alloy_rlp::Error> for ProviderError {
183 fn from(error: alloy_rlp::Error) -> Self {
184 Self::Rlp(error)
185 }
186}
187
188impl From<RecoveryError> for ProviderError {
189 fn from(_: RecoveryError) -> Self {
190 Self::SenderRecoveryError
191 }
192}
193
194#[derive(Clone, Debug, PartialEq, Eq, Display)]
196#[display("root mismatch at #{block_number} ({block_hash}): {root}")]
197pub struct RootMismatch {
198 pub root: GotExpected<B256>,
200 pub block_number: BlockNumber,
202 pub block_hash: BlockHash,
204}
205
206#[derive(Debug, thiserror::Error)]
208#[error("{message}")]
209pub struct StaticFileWriterError {
210 pub message: String,
212}
213
214impl StaticFileWriterError {
215 pub fn new(message: impl Into<String>) -> Self {
217 Self { message: message.into() }
218 }
219}
220#[derive(Clone, Debug, PartialEq, Eq, Display)]
222pub enum ConsistentViewError {
223 #[display("node is syncing. best block: {best_block:?}")]
225 Syncing {
226 best_block: GotExpected<BlockNumber>,
228 },
229 #[display("inconsistent database state: {tip:?}")]
231 Inconsistent {
232 tip: GotExpected<Option<B256>>,
234 },
235 #[display("database view no longer contains block: {block:?}")]
237 Reorged {
238 block: B256,
240 },
241}
242
243impl From<ConsistentViewError> for ProviderError {
244 fn from(error: ConsistentViewError) -> Self {
245 Self::ConsistentView(Box::new(error))
246 }
247}
248
249#[cfg(test)]
250mod tests {
251 use super::*;
252
253 #[derive(thiserror::Error, Debug)]
254 #[error("E")]
255 struct E;
256
257 #[test]
258 fn other_err() {
259 let err = ProviderError::other(E);
260 assert!(err.is_other::<E>());
261 assert!(err.downcast_other_ref::<E>().is_some());
262 }
263}