alloy_trie/proof/
error.rs

1use alloy_primitives::{Bytes, B256};
2use core::fmt;
3use nybbles::Nibbles;
4
5/// Error during proof verification.
6#[derive(PartialEq, Eq, Debug)]
7pub enum ProofVerificationError {
8    /// State root does not match the expected.
9    RootMismatch {
10        /// Computed state root.
11        got: B256,
12        /// State root provided to verify function.
13        expected: B256,
14    },
15    /// The node value does not match at specified path.
16    ValueMismatch {
17        /// Path at which error occurred.
18        path: Nibbles,
19        /// Value in the proof.
20        got: Option<Bytes>,
21        /// Expected value.
22        expected: Option<Bytes>,
23    },
24    /// Encountered unexpected empty root node.
25    UnexpectedEmptyRoot,
26    /// Error during RLP decoding of trie node.
27    Rlp(alloy_rlp::Error),
28}
29
30/// Enable Error trait implementation when core is stabilized.
31/// <https://github.com/rust-lang/rust/issues/103765>
32#[cfg(feature = "std")]
33impl std::error::Error for ProofVerificationError {
34    fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> {
35        #[allow(deprecated)]
36        match self {
37            Self::Rlp { 0: transparent } => {
38                std::error::Error::source(transparent as &dyn std::error::Error)
39            }
40            _ => None,
41        }
42    }
43}
44
45impl fmt::Display for ProofVerificationError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Self::RootMismatch { got, expected } => {
49                write!(f, "root mismatch. got: {got}. expected: {expected}")
50            }
51            Self::ValueMismatch { path, got, expected } => {
52                write!(f, "value mismatch at path {path:?}. got: {got:?}. expected: {expected:?}")
53            }
54            Self::UnexpectedEmptyRoot => {
55                write!(f, "unexpected empty root node")
56            }
57            Self::Rlp(error) => fmt::Display::fmt(error, f),
58        }
59    }
60}
61
62impl From<alloy_rlp::Error> for ProofVerificationError {
63    fn from(source: alloy_rlp::Error) -> Self {
64        Self::Rlp(source)
65    }
66}