zksync_state/
lib.rs

1//! Execution of transaction in ZKsync Era
2
3// Linter settings.
4#![warn(missing_debug_implementations, missing_docs, bare_trait_objects)]
5#![warn(clippy::all, clippy::pedantic)]
6#![allow(
7    clippy::must_use_candidate,
8    clippy::module_name_repetitions,
9    clippy::doc_markdown // frequent false positive: RocksDB
10)]
11
12use std::{cell::RefCell, collections::HashMap, fmt, rc::Rc};
13
14use zksync_types::{
15    get_known_code_key,
16    storage::{StorageKey, StorageValue},
17    H256,
18};
19
20pub use self::{
21    cache::sequential_cache::SequentialCache,
22    catchup::{AsyncCatchupTask, RocksdbCell},
23    // Note, that `test_infra` of the bootloader tests relies on this value to be exposed
24    in_memory::InMemoryStorage,
25    in_memory::IN_MEMORY_STORAGE_DEFAULT_NETWORK_ID,
26    postgres::{PostgresStorage, PostgresStorageCaches, PostgresStorageCachesTask},
27    rocksdb::{
28        RocksdbStorage, RocksdbStorageBuilder, RocksdbStorageOptions, StateKeeperColumnFamily,
29    },
30    shadow_storage::ShadowStorage,
31    storage_factory::{BatchDiff, PgOrRocksdbStorage, ReadStorageFactory, RocksdbWithMemory},
32    storage_view::{StorageView, StorageViewCache, StorageViewMetrics},
33    witness::WitnessStorage,
34};
35
36mod cache;
37mod catchup;
38mod in_memory;
39mod postgres;
40mod rocksdb;
41mod shadow_storage;
42mod storage_factory;
43mod storage_view;
44#[cfg(test)]
45mod test_utils;
46mod witness;
47
48/// Functionality to read from the VM storage.
49pub trait ReadStorage: fmt::Debug {
50    /// Read value of the key.
51    fn read_value(&mut self, key: &StorageKey) -> StorageValue;
52
53    /// Checks whether a write to this storage at the specified `key` would be an initial write.
54    /// Roughly speaking, this is the case when the storage doesn't contain `key`, although
55    /// in case of mutable storage, the caveats apply (a write to a key that is present
56    /// in the storage but was not committed is still an initial write).
57    fn is_write_initial(&mut self, key: &StorageKey) -> bool;
58
59    /// Load the factory dependency code by its hash.
60    fn load_factory_dep(&mut self, hash: H256) -> Option<Vec<u8>>;
61
62    /// Returns whether a bytecode hash is "known" to the system.
63    fn is_bytecode_known(&mut self, bytecode_hash: &H256) -> bool {
64        let code_key = get_known_code_key(bytecode_hash);
65        self.read_value(&code_key) != H256::zero()
66    }
67
68    /// Retrieves the enumeration index for a given `key`.
69    fn get_enumeration_index(&mut self, key: &StorageKey) -> Option<u64>;
70}
71
72/// Functionality to write to the VM storage in a batch.
73///
74/// So far, this trait is implemented only for [`StorageView`].
75pub trait WriteStorage: ReadStorage {
76    /// Returns the map with the key–value pairs read by this batch.
77    fn read_storage_keys(&self) -> &HashMap<StorageKey, StorageValue>;
78
79    /// Sets the new value under a given key and returns the previous value.
80    fn set_value(&mut self, key: StorageKey, value: StorageValue) -> StorageValue;
81
82    /// Returns a map with the key–value pairs updated by this batch.
83    fn modified_storage_keys(&self) -> &HashMap<StorageKey, StorageValue>;
84
85    /// Returns the number of read / write ops for which the value was read from the underlying
86    /// storage.
87    fn missed_storage_invocations(&self) -> usize;
88}
89
90/// Smart pointer to [`WriteStorage`].
91pub type StoragePtr<S> = Rc<RefCell<S>>;