anvil_zksync_core/deps/
mod.rs

1use anvil_zksync_config::types::SystemContractsOptions;
2use std::{collections::HashMap, path::Path};
3use zksync_multivm::interface::storage::ReadStorage;
4use zksync_types::{
5    get_code_key, get_known_code_key, get_system_context_init_logs, L2ChainId, ProtocolVersionId,
6    StorageKey, StorageLog, StorageValue, H256,
7};
8
9pub mod system_contracts;
10
11/// In-memory storage.
12#[derive(Debug, Default, Clone, PartialEq)]
13pub struct InMemoryStorage {
14    pub(crate) state: HashMap<StorageKey, StorageValue>,
15    pub(crate) factory_deps: HashMap<H256, Vec<u8>>,
16}
17
18impl InMemoryStorage {
19    /// Constructs a storage that contains system smart contracts (with a given chain id).
20    pub fn with_system_contracts_and_chain_id(
21        chain_id: L2ChainId,
22        bytecode_hasher: impl Fn(&[u8]) -> H256,
23        system_contracts_options: SystemContractsOptions,
24        protocol_version: ProtocolVersionId,
25        system_contracts_path: Option<&Path>,
26    ) -> Self {
27        let contracts = system_contracts::get_deployed_contracts(
28            system_contracts_options,
29            protocol_version,
30            system_contracts_path,
31        );
32
33        let system_context_init_log = get_system_context_init_logs(chain_id);
34        let state = contracts
35            .iter()
36            .flat_map(|contract| {
37                let bytecode_hash = bytecode_hasher(&contract.bytecode);
38
39                let deployer_code_key = get_code_key(contract.account_id.address());
40                let is_known_code_key = get_known_code_key(&bytecode_hash);
41
42                [
43                    StorageLog::new_write_log(deployer_code_key, bytecode_hash),
44                    StorageLog::new_write_log(is_known_code_key, H256::from_low_u64_be(1)),
45                ]
46            })
47            .chain(system_context_init_log)
48            .filter_map(|log| (log.is_write()).then_some((log.key, log.value)))
49            .collect();
50
51        let factory_deps = contracts
52            .into_iter()
53            .map(|contract| (bytecode_hasher(&contract.bytecode), contract.bytecode))
54            .collect();
55        Self {
56            state,
57            factory_deps,
58        }
59    }
60
61    pub fn read_value_opt(&self, key: &StorageKey) -> Option<StorageValue> {
62        self.state.get(key).copied()
63    }
64
65    /// Sets the storage `value` at the specified `key`.
66    pub fn set_value(&mut self, key: StorageKey, value: StorageValue) {
67        self.state.insert(key, value);
68    }
69
70    /// Stores a factory dependency with the specified `hash` and `bytecode`.
71    pub fn store_factory_dep(&mut self, hash: H256, bytecode: Vec<u8>) {
72        self.factory_deps.insert(hash, bytecode);
73    }
74}
75
76impl ReadStorage for &InMemoryStorage {
77    fn read_value(&mut self, key: &StorageKey) -> StorageValue {
78        self.state.get(key).copied().unwrap_or_default()
79    }
80
81    fn is_write_initial(&mut self, key: &StorageKey) -> bool {
82        !self.state.contains_key(key)
83    }
84
85    fn load_factory_dep(&mut self, hash: H256) -> Option<Vec<u8>> {
86        self.factory_deps.get(&hash).cloned()
87    }
88
89    fn get_enumeration_index(&mut self, _key: &StorageKey) -> Option<u64> {
90        // TODO: Update this file to use proper enumeration index value once it's exposed for forks via API
91        //       This should happen as the migration of Boojum completes
92        Some(0_u64)
93    }
94}
95
96impl ReadStorage for InMemoryStorage {
97    fn read_value(&mut self, key: &StorageKey) -> StorageValue {
98        (&*self).read_value(key)
99    }
100
101    fn is_write_initial(&mut self, key: &StorageKey) -> bool {
102        (&*self).is_write_initial(key)
103    }
104
105    fn load_factory_dep(&mut self, hash: H256) -> Option<Vec<u8>> {
106        (&*self).load_factory_dep(hash)
107    }
108
109    fn get_enumeration_index(&mut self, key: &StorageKey) -> Option<u64> {
110        (&*self).get_enumeration_index(key)
111    }
112}