Skip to main content

ethrex_trie/
error.rs

1#[cfg(not(feature = "std"))]
2use alloc::{boxed::Box, string::String};
3use ethereum_types::H256;
4use ethrex_rlp::error::RLPDecodeError;
5use thiserror::Error;
6
7use crate::Nibbles;
8
9#[derive(Debug, Error)]
10pub enum TrieError {
11    #[error(transparent)]
12    RLPDecode(#[from] RLPDecodeError),
13    #[error("Verification Error: {0}")]
14    Verify(String),
15    #[error("Inconsistent internal tree structure: {0}")]
16    InconsistentTree(Box<InconsistentTreeError>),
17    // Box was added to make the error smaller since the InconsistentTreeError variants size vary up to more than 168 bytes.
18    #[error("Lock Error: Panicked when trying to acquire a lock")]
19    LockError,
20    #[error("Database error: {0}")]
21    DbError(anyhow::Error),
22    #[error("Invalid trie input")]
23    InvalidInput,
24}
25
26#[derive(Debug, Error)]
27pub enum InconsistentTreeError {
28    #[error("Child node of {0}, differs from expected")]
29    ExtensionNodeChildDiffers(ExtensionNodeErrorData),
30    #[error("No Child Node found of {0}")]
31    ExtensionNodeChildNotFound(ExtensionNodeErrorData),
32    #[error("Node with hash {0:#x} not found in Branch Node with hash {1:#x} using path {2:?}")]
33    NodeNotFoundOnBranchNode(H256, H256, Nibbles),
34    #[error("Root node with hash {0:#x} not found")]
35    RootNotFound(H256),
36    #[error("Root node not found")]
37    RootNotFoundNoHash,
38}
39
40#[derive(Debug)]
41pub struct ExtensionNodeErrorData {
42    pub node_hash: H256,
43    pub extension_node_hash: H256,
44    pub extension_node_prefix: Nibbles,
45    pub node_path: Nibbles,
46}
47
48impl core::fmt::Display for ExtensionNodeErrorData {
49    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50        write!(
51            f,
52            "Node with hash {:#x}, child of the Extension Node (hash {:#x}, prefix {:?}) on path {:?}",
53            self.node_hash, self.extension_node_hash, self.extension_node_hash, self.node_path
54        )
55    }
56}