Skip to main content

eth_state_diff/
error.rs

1use std::fmt;
2
3/// Errors that can occur while creating or applying a state delta.
4///
5/// Delta application can fail when the delta is incompatible with the target
6/// state or when the delta payload cannot be interpreted or applied safely.
7/// This error type distinguishes fork-specific incompatibilities from
8/// malformed delta data.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// The delta was created for a different Ethereum consensus fork than
13    /// the state it is being applied to.
14    ///
15    /// A delta must only be applied to a state from the same fork because
16    /// state fields and their SSZ layouts may differ between forks.
17    ForkMismatch {
18        /// Fork of the state receiving the delta.
19        state_fork: crate::ForkName,
20
21        /// Fork for which the delta was created.
22        delta_fork: crate::ForkName,
23    },
24
25    /// The delta contains a field that is not part of the target fork's
26    /// state representation.
27    ///
28    /// This generally indicates an incorrectly constructed delta or an
29    /// attempt to apply a delta using the wrong fork-specific schema.
30    InvalidFieldForFork {
31        /// Name of the field included in the delta.
32        field: &'static str,
33
34        /// Fork against which the field was validated.
35        fork: crate::ForkName,
36    },
37
38    /// The delta is structurally valid enough to decode, but its contents
39    /// are inconsistent or incompatible with the state to which it is
40    /// being applied.
41    InvalidDelta(String),
42
43    /// The delta payload cannot be decoded or does not contain the data
44    /// required to apply it safely.
45    ///
46    /// This includes malformed or corrupted serialized data, truncated or
47    /// otherwise invalid encoded payloads, and failures encountered while
48    /// decoding compressed delta data.
49    MalformedDelta(String),
50}
51
52impl fmt::Display for Error {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            Self::ForkMismatch {
56                state_fork,
57                delta_fork,
58            } => {
59                write!(
60                    f,
61                    "Fork mismatch: cannot apply {delta_fork:?} delta to {state_fork:?} state",
62                )
63            }
64            Self::InvalidFieldForFork { field, fork } => {
65                write!(f, "Field '{field}' is invalid for fork {fork:?}")
66            }
67            Self::InvalidDelta(message) => {
68                write!(f, "Invalid delta: {message}")
69            }
70            Self::MalformedDelta(message) => {
71                write!(f, "Malformed delta payload: {message}")
72            }
73        }
74    }
75}
76
77impl std::error::Error for Error {}