anvil_zksync_core/node/
state.rs

1use super::inner::{SerializableForkStorage, SerializableStorage};
2use super::TransactionResult;
3use serde::{Deserialize, Serialize};
4use zksync_types::api::{Block, TransactionVariant};
5use zksync_types::H256;
6
7#[derive(Clone, Debug, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum VersionedState {
10    V1 {
11        version: StateVersion<1>,
12        #[serde(flatten)]
13        state: StateV1,
14    },
15    Unknown {
16        version: u8,
17    },
18}
19
20impl VersionedState {
21    pub fn v1(state: StateV1) -> Self {
22        VersionedState::V1 {
23            version: StateVersion::<1>,
24            state,
25        }
26    }
27}
28
29/// Workaround while serde does not allow integer tags in enums (see https://github.com/serde-rs/serde/issues/745).
30#[derive(Copy, Clone, Debug)]
31pub struct StateVersion<const V: u8>;
32
33impl<const V: u8> Serialize for StateVersion<V> {
34    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
35    where
36        S: serde::Serializer,
37    {
38        serializer.serialize_u8(V)
39    }
40}
41
42impl<'de, const V: u8> Deserialize<'de> for StateVersion<V> {
43    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
44    where
45        D: serde::Deserializer<'de>,
46    {
47        let value = u8::deserialize(deserializer)?;
48        if value == V {
49            Ok(StateVersion::<V>)
50        } else {
51            Err(serde::de::Error::custom("unknown state version"))
52        }
53    }
54}
55
56#[derive(Clone, Debug, Serialize, Deserialize)]
57pub struct StateV1 {
58    /// All blocks sealed on this node up to the current moment.
59    pub blocks: Vec<Block<TransactionVariant>>,
60    /// All transactions executed on this node up to the current moment.
61    pub transactions: Vec<TransactionResult>,
62    /// Current node's storage state.
63    #[serde(flatten)]
64    pub fork_storage: SerializableForkStorage,
65    /// Historical states of storage at particular block hashes.
66    pub historical_states: Vec<(H256, SerializableStorage)>,
67}