1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Errors when computing the state root.

use alloc::string::ToString;
use alloy_primitives::B256;
use derive_more::{Display, From};
use nybbles::Nibbles;
use reth_storage_errors::{db::DatabaseError, provider::ProviderError};

/// State root errors.
#[derive(Display, Debug, From, PartialEq, Eq, Clone)]
pub enum StateRootError {
    /// Internal database error.
    Database(DatabaseError),
    /// Storage root error.
    StorageRootError(StorageRootError),
}

#[cfg(feature = "std")]
impl std::error::Error for StateRootError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Database(source) => std::error::Error::source(source),
            Self::StorageRootError(source) => std::error::Error::source(source),
        }
    }
}

impl From<StateRootError> for DatabaseError {
    fn from(err: StateRootError) -> Self {
        match err {
            StateRootError::Database(err) |
            StateRootError::StorageRootError(StorageRootError::Database(err)) => err,
        }
    }
}

/// Storage root error.
#[derive(Display, From, PartialEq, Eq, Clone, Debug)]
pub enum StorageRootError {
    /// Internal database error.
    Database(DatabaseError),
}

impl From<StorageRootError> for DatabaseError {
    fn from(err: StorageRootError) -> Self {
        match err {
            StorageRootError::Database(err) => err,
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for StorageRootError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Database(source) => std::error::Error::source(source),
        }
    }
}

/// State proof errors.
#[derive(Display, From, Debug, PartialEq, Eq, Clone)]
pub enum StateProofError {
    /// Internal database error.
    Database(DatabaseError),
    /// RLP decoding error.
    Rlp(alloy_rlp::Error),
}

impl From<StateProofError> for ProviderError {
    fn from(value: StateProofError) -> Self {
        match value {
            StateProofError::Database(error) => Self::Database(error),
            StateProofError::Rlp(error) => Self::Rlp(error),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for StateProofError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Database(source) => std::error::Error::source(source),
            Self::Rlp(source) => std::error::Error::source(source),
        }
    }
}

/// Trie witness errors.
#[derive(Display, From, Debug, PartialEq, Eq, Clone)]
pub enum TrieWitnessError {
    /// Error gather proofs.
    #[from]
    Proof(StateProofError),
    /// RLP decoding error.
    #[from]
    Rlp(alloy_rlp::Error),
    /// Missing account.
    #[display("missing account {_0}")]
    MissingAccount(B256),
    /// Missing target node.
    #[display("target node missing from proof {_0:?}")]
    MissingTargetNode(Nibbles),
}

impl From<TrieWitnessError> for ProviderError {
    fn from(error: TrieWitnessError) -> Self {
        Self::TrieWitnessError(error.to_string())
    }
}

#[cfg(feature = "std")]
impl std::error::Error for TrieWitnessError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Proof(source) => std::error::Error::source(source),
            Self::Rlp(source) => std::error::Error::source(source),
            _ => Option::None,
        }
    }
}