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 decommit::DecommitOpcodeOutcome,
16 fat_pointer::FatPointer,
17 instruction::{ExecutionEnd, Instruction},
18 mode_requirements::ModeRequirements,
19 predication::Predicate,
20 program::Program,
21 vm::{Settings, VirtualMachine},
22 world_diff::{Snapshot, StorageChange, StorageWriteEntry, WorldDiff},
23};
24use crate::precompiles::{LegacyPrecompiles, Precompiles};
25
26pub mod addressing_modes;
27#[cfg(not(feature = "single_instruction_test"))]
28mod bitset;
29mod callframe;
30mod decode;
31mod decommit;
32mod fat_pointer;
33#[cfg(not(feature = "single_instruction_test"))]
34mod heap;
35mod instruction;
36mod instruction_handlers;
37mod mode_requirements;
38mod page_ids;
39pub mod precompiles;
40mod predication;
41#[cfg(not(feature = "single_instruction_test"))]
42mod program;
43mod rollback;
44#[cfg(feature = "single_instruction_test")]
45pub mod single_instruction_test;
46#[cfg(not(feature = "single_instruction_test"))]
47mod stack;
48mod state;
49pub mod testonly;
50#[cfg(all(test, not(feature = "single_instruction_test")))]
51mod tests;
52mod tracing;
53mod vm;
54mod world_diff;
55
56#[derive(Debug, Clone, Copy)]
58pub struct StorageSlot {
59 pub value: U256,
61 pub is_write_initial: bool,
63}
64
65impl StorageSlot {
66 pub const EMPTY: Self = Self {
68 value: U256([0; 4]),
69 is_write_initial: true,
70 };
71}
72
73pub trait StorageInterface {
75 fn read_storage(&mut self, contract: H160, key: U256) -> StorageSlot;
79
80 fn read_storage_value(&mut self, contract: H160, key: U256) -> U256 {
84 self.read_storage(contract, key).value
85 }
86
87 fn cost_of_writing_storage(&mut self, initial_slot: StorageSlot, new_value: U256) -> u32;
89
90 fn is_free_storage_slot(&self, contract: &H160, key: &U256) -> bool;
92}
93
94pub trait World<T: Tracer>: StorageInterface + Sized {
97 fn decommit(&mut self, hash: U256) -> Program<T, Self>;
102
103 fn decommit_code(&mut self, hash: U256) -> Vec<u8>;
105
106 fn precompiles(&self) -> &impl Precompiles {
108 &LegacyPrecompiles
109 }
110}
111
112#[cfg_attr(feature = "single_instruction_test", allow(dead_code))] pub(crate) fn hash_for_debugging(value: &impl Hash) -> u64 {
116 let mut hasher = DefaultHasher::new();
117 value.hash(&mut hasher);
118 hasher.finish()
119}