Skip to content

Commit

Permalink
feat: expose storage to tracers
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Oct 16, 2024
1 parent a233d44 commit 2fead2d
Show file tree
Hide file tree
Showing 23 changed files with 161 additions and 117 deletions.
7 changes: 7 additions & 0 deletions crates/vm2-interface/src/state_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub trait StateInterface {

/// Iterates over storage slots read or written during VM execution.
fn get_storage_state(&self) -> impl Iterator<Item = ((H160, U256), U256)>;
/// Gets value of the specified storage slot.
fn get_storage(&mut self, address: H160, slot: U256) -> U256;

/// Iterates over all transient storage slots set during VM execution.
fn get_transient_storage_state(&self) -> impl Iterator<Item = ((H160, U256), U256)>;
/// Gets value of the specified transient storage slot.
Expand Down Expand Up @@ -274,6 +277,10 @@ impl StateInterface for DummyState {
std::iter::empty()
}

fn get_storage(&mut self, _: H160, _: U256) -> U256 {
unimplemented!()
}

fn get_transient_storage_state(&self) -> impl Iterator<Item = ((H160, U256), U256)> {
std::iter::empty()
}
Expand Down
8 changes: 6 additions & 2 deletions crates/vm2-interface/src/tracer_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,15 @@ pub trait Tracer {
/// Executes logic before an instruction handler.
///
/// The default implementation does nothing.
fn before_instruction<OP: OpcodeType, S: StateInterface>(&mut self, _state: &mut S) {}
fn before_instruction<OP: OpcodeType, S: StateInterface>(&mut self, state: &mut S) {
let _ = state;
}
/// Executes logic after an instruction handler.
///
/// The default implementation does nothing.
fn after_instruction<OP: OpcodeType, S: StateInterface>(&mut self, _state: &mut S) {}
fn after_instruction<OP: OpcodeType, S: StateInterface>(&mut self, state: &mut S) {
let _ = state;
}

/// Provides cycle statistics for "complex" instructions from the prover perspective (mostly precompile calls).
///
Expand Down
4 changes: 2 additions & 2 deletions crates/vm2/src/callframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
program::Program,
stack::{Stack, StackSnapshot},
world_diff::Snapshot,
Instruction,
Instruction, World,
};

#[derive(Debug)]
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<T, W> Callframe<T, W> {
}
}

impl<T: Tracer, W> Callframe<T, W> {
impl<T: Tracer, W: World<T>> Callframe<T, W> {
pub(crate) fn push_near_call(
&mut self,
gas_to_call: u32,
Expand Down
1 change: 1 addition & 0 deletions crates/vm2/src/instruction_handlers/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn binop<T, W, Op, In1, Out, const SWAP: bool, const SET_FLAGS: bool>(
) -> ExecutionStatus
where
T: Tracer,
W: World<T>,
Op: Binop,
In1: Source,
Out: Destination,
Expand Down
21 changes: 12 additions & 9 deletions crates/vm2/src/instruction_handlers/common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use zksync_vm2_interface::{opcodes, OpcodeType, Tracer};

use super::ret::free_panic;
use crate::{addressing_modes::Arguments, instruction::ExecutionStatus, VirtualMachine};
use crate::{
addressing_modes::Arguments, instruction::ExecutionStatus, tracing::VmAndWorld, VirtualMachine,
World,
};

#[inline(always)]
pub(crate) fn boilerplate<Opcode: OpcodeType, T: Tracer, W>(
pub(crate) fn boilerplate<Opcode: OpcodeType, T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -17,7 +20,7 @@ pub(crate) fn boilerplate<Opcode: OpcodeType, T: Tracer, W>(
}

#[inline(always)]
pub(crate) fn boilerplate_ext<Opcode: OpcodeType, T: Tracer, W>(
pub(crate) fn boilerplate_ext<Opcode: OpcodeType, T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -30,7 +33,7 @@ pub(crate) fn boilerplate_ext<Opcode: OpcodeType, T: Tracer, W>(
}

#[inline(always)]
pub(crate) fn full_boilerplate<Opcode: OpcodeType, T: Tracer, W>(
pub(crate) fn full_boilerplate<Opcode: OpcodeType, T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -49,19 +52,19 @@ pub(crate) fn full_boilerplate<Opcode: OpcodeType, T: Tracer, W>(
vm.state.current_frame.is_static,
)
{
return free_panic(vm, tracer);
return free_panic(vm, world, tracer);
}

if args.predicate().satisfied(&vm.state.flags) {
tracer.before_instruction::<Opcode, _>(vm);
tracer.before_instruction::<Opcode, _>(&mut VmAndWorld { vm, world });
vm.state.current_frame.pc = unsafe { vm.state.current_frame.pc.add(1) };
let result = business_logic(vm, args, world, tracer);
tracer.after_instruction::<Opcode, _>(vm);
tracer.after_instruction::<Opcode, _>(&mut VmAndWorld { vm, world });
result
} else {
tracer.before_instruction::<opcodes::Nop, _>(vm);
tracer.before_instruction::<opcodes::Nop, _>(&mut VmAndWorld { vm, world });
vm.state.current_frame.pc = unsafe { vm.state.current_frame.pc.add(1) };
tracer.after_instruction::<opcodes::Nop, _>(vm);
tracer.after_instruction::<opcodes::Nop, _>(&mut VmAndWorld { vm, world });
ExecutionStatus::Running
}
}
14 changes: 7 additions & 7 deletions crates/vm2/src/instruction_handlers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
addressing_modes::{Arguments, Destination, Register1, Source},
instruction::ExecutionStatus,
state::State,
Instruction, VirtualMachine,
Instruction, VirtualMachine, World,
};

pub(crate) fn address_into_u256(address: H160) -> U256 {
Expand All @@ -19,7 +19,7 @@ pub(crate) fn address_into_u256(address: H160) -> U256 {
U256::from_big_endian(&buffer)
}

fn context<T, W, Op>(
fn context<T, W: World<T>, Op>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -74,7 +74,7 @@ impl ContextOp for SP {
}
}

fn context_meta<T: Tracer, W>(
fn context_meta<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -102,7 +102,7 @@ fn context_meta<T: Tracer, W>(
})
}

fn set_context_u128<T: Tracer, W>(
fn set_context_u128<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -113,7 +113,7 @@ fn set_context_u128<T: Tracer, W>(
})
}

fn increment_tx_number<T: Tracer, W>(
fn increment_tx_number<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -123,7 +123,7 @@ fn increment_tx_number<T: Tracer, W>(
})
}

fn aux_mutating<T: Tracer, W>(
fn aux_mutating<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -134,7 +134,7 @@ fn aux_mutating<T: Tracer, W>(
}

/// Context-related instructions.
impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
fn from_context<Op: ContextOp>(out: Register1, arguments: Arguments) -> Self {
Self {
handler: context::<T, W, Op>,
Expand Down
8 changes: 4 additions & 4 deletions crates/vm2/src/instruction_handlers/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use super::common::boilerplate_ext;
use crate::{
addressing_modes::{Arguments, Immediate1, Register1, Register2, Source},
instruction::ExecutionStatus,
Instruction, VirtualMachine,
Instruction, VirtualMachine, World,
};

fn event<T: Tracer, W>(
fn event<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -31,7 +31,7 @@ fn event<T: Tracer, W>(
})
}

fn l2_to_l1<T: Tracer, W>(
fn l2_to_l1<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -51,7 +51,7 @@ fn l2_to_l1<T: Tracer, W>(
})
}

impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
/// Creates an [`Event`](opcodes::Event) instruction with the provided params.
pub fn from_event(
key: Register1,
Expand Down
4 changes: 2 additions & 2 deletions crates/vm2/src/instruction_handlers/far_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
///
/// Even though all errors happen before the new stack frame, they cause a panic in the new frame,
/// not in the caller!
fn far_call<T, W, M, const IS_STATIC: bool, const IS_SHARD: bool>(
fn far_call<T, W: World<T>, M, const IS_STATIC: bool, const IS_SHARD: bool>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -109,7 +109,7 @@ where

let Some((calldata, program, is_evm_interpreter)) = failing_part else {
vm.state.current_frame.gas += new_frame_gas.saturating_sub(RETURN_COST);
panic_from_failed_far_call(vm, tracer, exception_handler);
panic_from_failed_far_call(vm, world, tracer, exception_handler);
return;
};

Expand Down
10 changes: 5 additions & 5 deletions crates/vm2/src/instruction_handlers/heap_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
fat_pointer::FatPointer,
instruction::ExecutionStatus,
state::State,
ExecutionEnd, Instruction, VirtualMachine,
ExecutionEnd, Instruction, VirtualMachine, World,
};

pub(crate) trait HeapFromState {
Expand Down Expand Up @@ -64,7 +64,7 @@ fn bigger_than_last_address(x: U256) -> bool {
x.0[0] > LAST_ADDRESS.into() || x.0[1] != 0 || x.0[2] != 0 || x.0[3] != 0
}

fn load<T: Tracer, W, H: HeapFromState, In: Source, const INCREMENT: bool>(
fn load<T: Tracer, W: World<T>, H: HeapFromState, In: Source, const INCREMENT: bool>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -100,7 +100,7 @@ fn load<T: Tracer, W, H: HeapFromState, In: Source, const INCREMENT: bool>(
})
}

fn store<T, W, H, In, const INCREMENT: bool, const HOOKING_ENABLED: bool>(
fn store<T, W: World<T>, H, In, const INCREMENT: bool, const HOOKING_ENABLED: bool>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -163,7 +163,7 @@ pub(crate) fn grow_heap<T, W, H: HeapFromState>(
Ok(())
}

fn load_pointer<T: Tracer, W, const INCREMENT: bool>(
fn load_pointer<T: Tracer, W: World<T>, const INCREMENT: bool>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -197,7 +197,7 @@ fn load_pointer<T: Tracer, W, const INCREMENT: bool>(
})
}

impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
/// Creates a [`HeapRead`](opcodes::HeapRead) instruction with the provided params.
pub fn from_heap_read(
src: RegisterOrImmediate,
Expand Down
6 changes: 3 additions & 3 deletions crates/vm2/src/instruction_handlers/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use crate::{
Immediate1, Register1, RelativeStack, Source,
},
instruction::{ExecutionStatus, Instruction},
VirtualMachine,
VirtualMachine, World,
};

fn jump<T: Tracer, W, In: Source>(
fn jump<T: Tracer, W: World<T>, In: Source>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -29,7 +29,7 @@ fn jump<T: Tracer, W, In: Source>(
})
}

impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
/// Creates a [`Jump`](opcodes::Jump) instruction with the provided params.
pub fn from_jump(source: AnySource, destination: Register1, arguments: Arguments) -> Self {
Self {
Expand Down
6 changes: 3 additions & 3 deletions crates/vm2/src/instruction_handlers/near_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::{
addressing_modes::{Arguments, Immediate1, Immediate2, Register1, Source},
instruction::ExecutionStatus,
predication::Flags,
Instruction, VirtualMachine,
Instruction, VirtualMachine, World,
};

fn near_call<T: Tracer, W>(
fn near_call<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -35,7 +35,7 @@ fn near_call<T: Tracer, W>(
})
}

impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
/// Creates a [`NearCall`](opcodes::NearCall) instruction with the provided params.
pub fn from_near_call(
gas: Register1,
Expand Down
6 changes: 3 additions & 3 deletions crates/vm2/src/instruction_handlers/nop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use super::common::boilerplate;
use crate::{
addressing_modes::{destination_stack_address, AdvanceStackPointer, Arguments, Source},
instruction::ExecutionStatus,
Instruction, VirtualMachine,
Instruction, VirtualMachine, World,
};

fn nop<T: Tracer, W>(
fn nop<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand All @@ -23,7 +23,7 @@ fn nop<T: Tracer, W>(
})
}

impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
/// Creates a [`Nop`](opcodes::Nop) instruction with the provided params.
pub fn from_nop(
pop: AdvanceStackPointer,
Expand Down
6 changes: 3 additions & 3 deletions crates/vm2/src/instruction_handlers/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use crate::{
},
fat_pointer::FatPointer,
instruction::ExecutionStatus,
Instruction, VirtualMachine,
Instruction, VirtualMachine, World,
};

fn ptr<T: Tracer, W, Op: PtrOp, In1: Source, Out: Destination, const SWAP: bool>(
fn ptr<T: Tracer, W: World<T>, Op: PtrOp, In1: Source, Out: Destination, const SWAP: bool>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -124,7 +124,7 @@ macro_rules! from_ptr_op {
}

/// Pointer-related instructions.
impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
from_ptr_op!(from_pointer_add<PointerAdd>);
from_ptr_op!(from_pointer_sub<PointerSub>);
from_ptr_op!(from_pointer_pack<PointerPack>);
Expand Down
6 changes: 3 additions & 3 deletions crates/vm2/src/instruction_handlers/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use crate::{
addressing_modes::{Arguments, Destination, Register1, Register2, Source},
heap::Heaps,
instruction::ExecutionStatus,
Instruction, VirtualMachine,
Instruction, VirtualMachine, World,
};

fn precompile_call<T: Tracer, W>(
fn precompile_call<T: Tracer, W: World<T>>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Memory for Heaps {
}
}

impl<T: Tracer, W> Instruction<T, W> {
impl<T: Tracer, W: World<T>> Instruction<T, W> {
/// Creates a [`PrecompileCall`](opcodes::PrecompileCall) instruction with the provided params.
pub fn from_precompile_call(
abi: Register1,
Expand Down
Loading

0 comments on commit 2fead2d

Please sign in to comment.