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 safely. This error
7/// type distinguishes structural incompatibilities from malformed data.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Error {
10    /// The delta was created for a different Ethereum consensus fork than
11    /// the state it is being applied to.
12    ///
13    /// A delta must only be applied to a state from the same fork because
14    /// state fields and their SSZ layouts may differ between forks.
15    ForkMismatch {
16        /// Fork of the state receiving the delta.
17        state_fork: crate::ForkName,
18
19        /// Fork for which the delta was created.
20        delta_fork: crate::ForkName,
21    },
22
23    /// The delta contains a field that is not part of the target fork's
24    /// state representation.
25    ///
26    /// This generally indicates an incorrectly constructed delta or an
27    /// attempt to apply a delta using the wrong fork-specific schema.
28    InvalidFieldForFork {
29        /// Name of the field included in the delta.
30        field: &'static str,
31
32        /// Fork against which the field was validated.
33        fork: crate::ForkName,
34    },
35
36    /// The delta payload could not be decoded or does not contain the data
37    /// required to apply it.
38    ///
39    /// This can represent malformed or corrupted serialized data, including
40    /// invalid `rkyv` data or a failed `zstd` decompression.
41    MalformedDelta(String),
42}
43
44impl fmt::Display for Error {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::ForkMismatch {
48                state_fork,
49                delta_fork,
50            } => {
51                write!(
52                    f,
53                    "Fork mismatch: cannot apply {delta_fork:?} delta to {state_fork:?} state",
54                )
55            }
56            Self::InvalidFieldForFork { field, fork } => {
57                write!(f, "Field '{field}' is invalid for fork {fork:?}")
58            }
59            Self::MalformedDelta(message) => {
60                write!(f, "Malformed delta payload: {message}")
61            }
62        }
63    }
64}
65
66impl std::error::Error for Error {}