From 9dae839935d82a1e73be220d17567f3382131039 Mon Sep 17 00:00:00 2001 From: Joonatan Saarhelo Date: Fri, 1 Nov 2024 14:37:35 +0000 Subject: [PATCH] feat: allow vm2 tracers to stop execution (#3183) Makes validation tracer nicer to implement. --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- .../multivm/src/versions/vm_fast/circuits_tracer.rs | 11 +++++++++-- .../multivm/src/versions/vm_fast/evm_deploy_tracer.rs | 8 ++++++-- core/lib/multivm/src/versions/vm_fast/vm.rs | 10 ++++++++++ prover/Cargo.lock | 4 ++-- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb2a72eb8c4..8af4e90323c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12226,7 +12226,7 @@ dependencies = [ [[package]] name = "zksync_vm2" version = "0.2.1" -source = "git+https://github.com/matter-labs/vm2.git?rev=df5bec3d04d64d434f9b0ccb285ba4681008f7b3#df5bec3d04d64d434f9b0ccb285ba4681008f7b3" +source = "git+https://github.com/matter-labs/vm2.git?rev=457d8a7eea9093af9440662e33e598c13ba41633#457d8a7eea9093af9440662e33e598c13ba41633" dependencies = [ "enum_dispatch", "primitive-types", @@ -12238,7 +12238,7 @@ dependencies = [ [[package]] name = "zksync_vm2_interface" version = "0.2.1" -source = "git+https://github.com/matter-labs/vm2.git?rev=df5bec3d04d64d434f9b0ccb285ba4681008f7b3#df5bec3d04d64d434f9b0ccb285ba4681008f7b3" +source = "git+https://github.com/matter-labs/vm2.git?rev=457d8a7eea9093af9440662e33e598c13ba41633#457d8a7eea9093af9440662e33e598c13ba41633" dependencies = [ "primitive-types", ] diff --git a/Cargo.toml b/Cargo.toml index e7cce4c4c42..cdbc0d107f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -240,7 +240,7 @@ zk_evm_1_4_1 = { package = "zk_evm", version = "0.141" } zk_evm_1_5_0 = { package = "zk_evm", version = "=0.150.7" } # New VM; pinned to a specific commit because of instability -zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "df5bec3d04d64d434f9b0ccb285ba4681008f7b3" } +zksync_vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "457d8a7eea9093af9440662e33e598c13ba41633" } # Consensus dependencies. zksync_concurrency = "=0.5.0" diff --git a/core/lib/multivm/src/versions/vm_fast/circuits_tracer.rs b/core/lib/multivm/src/versions/vm_fast/circuits_tracer.rs index 1999db47fb9..9c1c0b7dfb7 100644 --- a/core/lib/multivm/src/versions/vm_fast/circuits_tracer.rs +++ b/core/lib/multivm/src/versions/vm_fast/circuits_tracer.rs @@ -1,5 +1,7 @@ use circuit_sequencer_api_1_5_0::{geometry_config::get_geometry_config, toolset::GeometryConfig}; -use zksync_vm2::interface::{CycleStats, GlobalStateInterface, Opcode, OpcodeType, Tracer}; +use zksync_vm2::interface::{ + CycleStats, GlobalStateInterface, Opcode, OpcodeType, ShouldStop, Tracer, +}; use zksync_vm_interface::CircuitStatistic; use crate::vm_latest::tracers::circuits_capacity::*; @@ -24,7 +26,10 @@ pub(super) struct CircuitsTracer { } impl Tracer for CircuitsTracer { - fn after_instruction(&mut self, _: &mut S) { + fn after_instruction( + &mut self, + _: &mut S, + ) -> ShouldStop { self.main_vm_cycles += 1; match OP::VALUE { @@ -110,6 +115,8 @@ impl Tracer for CircuitsTracer { self.ram_permutation_cycles += UMA_READ_RAM_CYCLES; } } + + ShouldStop::Continue } fn on_extra_prover_cycles(&mut self, stats: CycleStats) { diff --git a/core/lib/multivm/src/versions/vm_fast/evm_deploy_tracer.rs b/core/lib/multivm/src/versions/vm_fast/evm_deploy_tracer.rs index d869796cd2c..1bc0ff5134f 100644 --- a/core/lib/multivm/src/versions/vm_fast/evm_deploy_tracer.rs +++ b/core/lib/multivm/src/versions/vm_fast/evm_deploy_tracer.rs @@ -6,7 +6,7 @@ use zksync_system_constants::{CONTRACT_DEPLOYER_ADDRESS, KNOWN_CODES_STORAGE_ADD use zksync_types::U256; use zksync_utils::{bytecode::hash_evm_bytecode, h256_to_u256}; use zksync_vm2::interface::{ - CallframeInterface, CallingMode, GlobalStateInterface, Opcode, OpcodeType, Tracer, + CallframeInterface, CallingMode, GlobalStateInterface, Opcode, OpcodeType, ShouldStop, Tracer, }; use super::utils::read_fat_pointer; @@ -76,9 +76,13 @@ impl EvmDeployTracer { impl Tracer for EvmDeployTracer { #[inline(always)] - fn after_instruction(&mut self, state: &mut S) { + fn after_instruction( + &mut self, + state: &mut S, + ) -> ShouldStop { if matches!(OP::VALUE, Opcode::FarCall(CallingMode::Normal)) { self.handle_far_call(state); } + ShouldStop::Continue } } diff --git a/core/lib/multivm/src/versions/vm_fast/vm.rs b/core/lib/multivm/src/versions/vm_fast/vm.rs index b7113bf8f19..f90bac149d0 100644 --- a/core/lib/multivm/src/versions/vm_fast/vm.rs +++ b/core/lib/multivm/src/versions/vm_fast/vm.rs @@ -212,6 +212,16 @@ impl Vm { }; break (ExecutionResult::Halt { reason }, true); } + ExecutionEnd::StoppedByTracer => { + break ( + ExecutionResult::Halt { + reason: Halt::TracerCustom( + "Unexpectedly stopped by tracer".to_string(), + ), + }, + false, + ); + } }; match Hook::from_u32(hook) { diff --git a/prover/Cargo.lock b/prover/Cargo.lock index d68ef368a4a..809ed9b3a66 100644 --- a/prover/Cargo.lock +++ b/prover/Cargo.lock @@ -8685,7 +8685,7 @@ dependencies = [ [[package]] name = "zksync_vm2" version = "0.2.1" -source = "git+https://github.com/matter-labs/vm2.git?rev=df5bec3d04d64d434f9b0ccb285ba4681008f7b3#df5bec3d04d64d434f9b0ccb285ba4681008f7b3" +source = "git+https://github.com/matter-labs/vm2.git?rev=457d8a7eea9093af9440662e33e598c13ba41633#457d8a7eea9093af9440662e33e598c13ba41633" dependencies = [ "enum_dispatch", "primitive-types", @@ -8697,7 +8697,7 @@ dependencies = [ [[package]] name = "zksync_vm2_interface" version = "0.2.1" -source = "git+https://github.com/matter-labs/vm2.git?rev=df5bec3d04d64d434f9b0ccb285ba4681008f7b3#df5bec3d04d64d434f9b0ccb285ba4681008f7b3" +source = "git+https://github.com/matter-labs/vm2.git?rev=457d8a7eea9093af9440662e33e598c13ba41633#457d8a7eea9093af9440662e33e598c13ba41633" dependencies = [ "primitive-types", ]