1use std::hash::{DefaultHasher, Hash, Hasher};
6
7use primitive_types::{H160, U256};
8pub use zksync_vm2_interface as interface;
9use zksync_vm2_interface::Tracer;
10
11#[cfg(feature = "single_instruction_test")]
13pub(crate) use self::single_instruction_test::{heap, program, stack};
14pub use self::{
15 fat_pointer::FatPointer,
16 instruction::{ExecutionEnd, Instruction},
17 mode_requirements::ModeRequirements,
18 predication::Predicate,
19 program::Program,
20 vm::{Settings, VirtualMachine},
21 world_diff::{Snapshot, StorageChange, WorldDiff},
22};
23use crate::precompiles::{LegacyPrecompiles, Precompiles};
24
25pub mod addressing_modes;
26#[cfg(not(feature = "single_instruction_test"))]
27mod bitset;
28mod callframe;
29mod decode;
30mod decommit;
31mod fat_pointer;
32#[cfg(not(feature = "single_instruction_test"))]
33mod heap;
34mod instruction;
35mod instruction_handlers;
36mod mode_requirements;
37mod page_ids;
38pub mod precompiles;
39mod predication;
40#[cfg(not(feature = "single_instruction_test"))]
41mod program;
42mod rollback;
43#[cfg(feature = "single_instruction_test")]
44pub mod single_instruction_test;
45#[cfg(not(feature = "single_instruction_test"))]
46mod stack;
47mod state;
48pub mod testonly;
49#[cfg(all(test, not(feature = "single_instruction_test")))]
50mod tests;
51mod tracing;
52mod vm;
53mod world_diff;
54
55#[derive(Debug, Clone, Copy)]
57pub struct StorageSlot {
58 pub value: U256,
60 pub is_write_initial: bool,
62}
63
64impl StorageSlot {
65 pub const EMPTY: Self = Self {
67 value: U256([0; 4]),
68 is_write_initial: true,
69 };
70}
71
72pub trait StorageInterface {
74 fn read_storage(&mut self, contract: H160, key: U256) -> StorageSlot;
78
79 fn read_storage_value(&mut self, contract: H160, key: U256) -> U256 {
83 self.read_storage(contract, key).value
84 }
85
86 fn cost_of_writing_storage(&mut self, initial_slot: StorageSlot, new_value: U256) -> u32;
88
89 fn is_free_storage_slot(&self, contract: &H160, key: &U256) -> bool;
91}
92
93pub trait World<T: Tracer>: StorageInterface + Sized {
96 fn decommit(&mut self, hash: U256) -> Program<T, Self>;
101
102 fn decommit_code(&mut self, hash: U256) -> Vec<u8>;
104
105 fn precompiles(&self) -> &impl Precompiles {
107 &LegacyPrecompiles
108 }
109}
110
111#[cfg_attr(feature = "single_instruction_test", allow(dead_code))] pub(crate) fn hash_for_debugging(value: &impl Hash) -> u64 {
115 let mut hasher = DefaultHasher::new();
116 value.hash(&mut hasher);
117 hasher.finish()
118}