Skip to main content

ethrex_trie/
error.rs

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