anvil_zksync_core/node/inner/
mod.rs

1//! This module encapsulates mutable parts of the system and provides read-only views on various
2//! components of the system's state (e.g. time, storage, blocks). It is still possible to mutate
3//! the state outside of this module but only through [`InMemoryNodeInner`]'s public high-level
4//! methods.
5//!
6//! The idea behind this is being able to read current time to answer API requests while a lock on
7//! [`InMemoryNodeInner`] is being held for block production. At the same time it is impossible to
8//! advance the time without holding a lock to [`InMemoryNodeInner`].
9//!
10//! FIXME: The above is not 100% true yet (there are some internal parts of InMemoryNodeInner that
11//!        are available outside of this module)
12pub mod blockchain;
13pub mod fork;
14mod fork_storage;
15mod in_memory_inner;
16pub mod node_executor;
17pub mod storage;
18pub mod time;
19mod vm_runner;
20
21pub use fork_storage::{SerializableForkStorage, SerializableStorage};
22pub use in_memory_inner::InMemoryNodeInner;
23
24use crate::filters::EthFilters;
25use crate::node::blockchain::Blockchain;
26use crate::node::inner::storage::ReadStorageDyn;
27use crate::node::inner::vm_runner::VmRunner;
28use crate::node::keys::StorageKeyLayout;
29use crate::node::{ImpersonationManager, TestNodeFeeInputProvider};
30use crate::system_contracts::SystemContracts;
31use anvil_zksync_config::constants::NON_FORK_FIRST_BLOCK_TIMESTAMP;
32use anvil_zksync_config::TestNodeConfig;
33use blockchain::ReadBlockchain;
34use fork::{Fork, ForkClient, ForkSource};
35use fork_storage::ForkStorage;
36use std::sync::Arc;
37use time::{ReadTime, Time};
38use tokio::sync::RwLock;
39
40impl InMemoryNodeInner {
41    // TODO: Bake in Arc<RwLock<_>> into the struct itself
42    #[allow(clippy::type_complexity)]
43    #[allow(clippy::too_many_arguments)]
44    pub fn init(
45        fork_client_opt: Option<ForkClient>,
46        fee_input_provider: TestNodeFeeInputProvider,
47        filters: Arc<RwLock<EthFilters>>,
48        config: TestNodeConfig,
49        impersonation: ImpersonationManager,
50        system_contracts: SystemContracts,
51        storage_key_layout: StorageKeyLayout,
52        generate_system_logs: bool,
53    ) -> (
54        Arc<RwLock<Self>>,
55        Box<dyn ReadStorageDyn>,
56        Box<dyn ReadBlockchain>,
57        Box<dyn ReadTime>,
58        Box<dyn ForkSource>,
59        VmRunner,
60    ) {
61        // TODO: We wouldn't have to clone cache config here if there was a proper per-component
62        //       config separation.
63        let fork = Fork::new(fork_client_opt, config.cache_config.clone());
64        let fork_details = fork.details();
65        let time = Time::new(
66            fork_details
67                .as_ref()
68                .map(|fd| fd.block_timestamp)
69                .unwrap_or(NON_FORK_FIRST_BLOCK_TIMESTAMP),
70        );
71        let blockchain = Blockchain::new(
72            system_contracts.protocol_version,
73            fork_details.as_ref(),
74            config.genesis.as_ref(),
75            config.genesis_timestamp,
76        );
77        // TODO: Create read-only/mutable versions of `ForkStorage` like `blockchain` and `time` above
78        let fork_storage = ForkStorage::new(
79            fork.clone(),
80            config.system_contracts_options,
81            system_contracts.protocol_version,
82            config.chain_id,
83            config.system_contracts_path.as_deref(),
84        );
85        let vm_runner = VmRunner::new(
86            time.clone(),
87            fork_storage.clone(),
88            system_contracts.clone(),
89            generate_system_logs,
90            config.is_bytecode_compression_enforced(),
91            storage_key_layout,
92        );
93
94        let node_inner = InMemoryNodeInner::new(
95            blockchain.clone(),
96            time.clone(),
97            fork_storage.clone(),
98            fork.clone(),
99            fee_input_provider.clone(),
100            filters,
101            config.clone(),
102            impersonation.clone(),
103            system_contracts.clone(),
104            storage_key_layout,
105        );
106
107        (
108            Arc::new(RwLock::new(node_inner)),
109            Box::new(fork_storage),
110            Box::new(blockchain),
111            Box::new(time),
112            Box::new(fork),
113            vm_runner,
114        )
115    }
116}