anvil_zksync_core/node/
state.rs

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
use super::inner::{SerializableForkStorage, SerializableStorage};
use super::TransactionResult;
use serde::{Deserialize, Serialize};
use zksync_types::api::{Block, TransactionVariant};
use zksync_types::H256;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum VersionedState {
    V1 {
        version: StateVersion<1>,
        #[serde(flatten)]
        state: StateV1,
    },
    Unknown {
        version: u8,
    },
}

impl VersionedState {
    pub fn v1(state: StateV1) -> Self {
        VersionedState::V1 {
            version: StateVersion::<1>,
            state,
        }
    }
}

/// Workaround while serde does not allow integer tags in enums (see https://github.com/serde-rs/serde/issues/745).
#[derive(Copy, Clone, Debug)]
pub struct StateVersion<const V: u8>;

impl<const V: u8> Serialize for StateVersion<V> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u8(V)
    }
}

impl<'de, const V: u8> Deserialize<'de> for StateVersion<V> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = u8::deserialize(deserializer)?;
        if value == V {
            Ok(StateVersion::<V>)
        } else {
            Err(serde::de::Error::custom("unknown state version"))
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StateV1 {
    /// All blocks sealed on this node up to the current moment.
    pub blocks: Vec<Block<TransactionVariant>>,
    /// All transactions executed on this node up to the current moment.
    pub transactions: Vec<TransactionResult>,
    /// Current node's storage state.
    #[serde(flatten)]
    pub fork_storage: SerializableForkStorage,
    /// Historical states of storage at particular block hashes.
    pub historical_states: Vec<(H256, SerializableStorage)>,
}