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