Skip to content

Commit 93bc66f

Browse files
authored
feat(vm): Split old and new VM implementations (#2915)
## What ❔ Splits old and new VM implementations in the `multivm` crate: - Old VMs are encapsulated in the `LegacyVmInstance` enum, while new ones in the `FastVmInstance` enum (which includes plain and shadowed VM variants). - Fast VM and `FastVmInstance` now expose a tracer type. - Usage of the Fast VM in the batch executor are updated correspondingly. ## Why ❔ It seems infeasible to unify the tracer model for old and new VMs, so keeping them all in a single enum makes little sense. ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [x] Tests for the changes have been added / updated. - [x] Documentation comments have been added / updated. - [x] Code has been formatted via `zk fmt` and `zk lint`.
1 parent 2224f8f commit 93bc66f

File tree

56 files changed

+662
-340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+662
-340
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/bin/system-constants-generator/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
262262
}
263263
.into_tracer_pointer();
264264
let mut vm: Vm<_, HistoryEnabled> = Vm::new(l1_batch, system_env, storage_view.to_rc_ptr());
265-
let result = vm.inspect(tracer.into(), VmExecutionMode::Bootloader);
265+
let result = vm.inspect(&mut tracer.into(), VmExecutionMode::Bootloader);
266266

267267
assert!(!result.result.is_failed(), "The internal call has reverted");
268268
tracer_result.take()

core/lib/multivm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use crate::{
1616
vm_1_3_2, vm_1_4_1, vm_1_4_2, vm_boojum_integration, vm_fast, vm_latest, vm_m5, vm_m6,
1717
vm_refunds_enhancement, vm_virtual_blocks,
1818
},
19-
vm_instance::VmInstance,
19+
vm_instance::{FastVmInstance, LegacyVmInstance},
2020
};
2121

2222
mod glue;

core/lib/multivm/src/versions/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
};
3030

3131
type ReferenceVm<S = InMemoryStorage> = vm_latest::Vm<StorageView<S>, HistoryEnabled>;
32-
type ShadowedVmFast<S = InMemoryStorage> = crate::vm_instance::ShadowedVmFast<S, HistoryEnabled>;
32+
type ShadowedFastVm<S = InMemoryStorage> = crate::vm_instance::ShadowedFastVm<S>;
3333

3434
fn hash_block(block_env: L2BlockEnv, tx_hashes: &[H256]) -> H256 {
3535
let mut hasher = L2BlockHasher::new(
@@ -248,12 +248,12 @@ fn sanity_check_shadow_vm() {
248248

249249
#[test]
250250
fn shadow_vm_basics() {
251-
let (vm, harness) = sanity_check_vm::<ShadowedVmFast>();
251+
let (vm, harness) = sanity_check_vm::<ShadowedFastVm>();
252252
let mut dump = vm.dump_state();
253253
Harness::assert_dump(&mut dump);
254254

255255
// Test standard playback functionality.
256-
let replayed_dump = dump.clone().play_back::<ShadowedVmFast<_>>().dump_state();
256+
let replayed_dump = dump.clone().play_back::<ShadowedFastVm<_>>().dump_state();
257257
pretty_assertions::assert_eq!(replayed_dump, dump);
258258

259259
// Check that the VM executes identically when reading from the original storage and one restored from the dump.

core/lib/multivm/src/versions/vm_1_3_2/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
3636

3737
fn inspect(
3838
&mut self,
39-
tracer: Self::TracerDispatcher,
39+
tracer: &mut Self::TracerDispatcher,
4040
execution_mode: VmExecutionMode,
4141
) -> VmExecutionResultAndLogs {
4242
if let Some(storage_invocations) = tracer.storage_invocations {
@@ -80,7 +80,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
8080

8181
fn inspect_transaction_with_bytecode_compression(
8282
&mut self,
83-
tracer: Self::TracerDispatcher,
83+
tracer: &mut Self::TracerDispatcher,
8484
tx: Transaction,
8585
with_compression: bool,
8686
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {

core/lib/multivm/src/versions/vm_1_4_1/implementation/execution.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem;
2+
13
use zk_evm_1_4_1::aux_structures::Timestamp;
24

35
use crate::{
@@ -20,7 +22,7 @@ use crate::{
2022
impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
2123
pub(crate) fn inspect_inner(
2224
&mut self,
23-
dispatcher: TracerDispatcher<S, H::Vm1_4_1>,
25+
dispatcher: &mut TracerDispatcher<S, H::Vm1_4_1>,
2426
execution_mode: VmExecutionMode,
2527
custom_pubdata_tracer: Option<PubdataTracer<S>>,
2628
) -> VmExecutionResultAndLogs {
@@ -44,7 +46,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
4446
/// Collect the result from the default tracers.
4547
fn inspect_and_collect_results(
4648
&mut self,
47-
dispatcher: TracerDispatcher<S, H::Vm1_4_1>,
49+
dispatcher: &mut TracerDispatcher<S, H::Vm1_4_1>,
4850
execution_mode: VmExecutionMode,
4951
with_refund_tracer: bool,
5052
custom_pubdata_tracer: Option<PubdataTracer<S>>,
@@ -54,7 +56,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
5456
let mut tx_tracer: DefaultExecutionTracer<S, H::Vm1_4_1> = DefaultExecutionTracer::new(
5557
self.system_env.default_validation_computational_gas_limit,
5658
execution_mode,
57-
dispatcher,
59+
mem::take(dispatcher),
5860
self.storage.clone(),
5961
refund_tracers,
6062
custom_pubdata_tracer
@@ -90,6 +92,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
9092
circuit_statistic_from_cycles(tx_tracer.circuits_tracer.statistics),
9193
);
9294
let result = tx_tracer.result_tracer.into_result();
95+
*dispatcher = tx_tracer.dispatcher;
9396

9497
let result = VmExecutionResultAndLogs {
9598
result,

core/lib/multivm/src/versions/vm_1_4_1/vm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
9090
/// Execute VM with custom tracers.
9191
fn inspect(
9292
&mut self,
93-
tracer: Self::TracerDispatcher,
93+
tracer: &mut Self::TracerDispatcher,
9494
execution_mode: VmExecutionMode,
9595
) -> VmExecutionResultAndLogs {
9696
self.inspect_inner(tracer, execution_mode, None)
@@ -102,7 +102,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
102102

103103
fn inspect_transaction_with_bytecode_compression(
104104
&mut self,
105-
tracer: Self::TracerDispatcher,
105+
tracer: &mut Self::TracerDispatcher,
106106
tx: Transaction,
107107
with_compression: bool,
108108
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
@@ -129,7 +129,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
129129
}
130130

131131
fn finish_batch(&mut self) -> FinishedL1Batch {
132-
let result = self.inspect(TracerDispatcher::default(), VmExecutionMode::Batch);
132+
let result = self.inspect(&mut TracerDispatcher::default(), VmExecutionMode::Batch);
133133
let execution_state = self.get_current_execution_state();
134134
let bootloader_memory = self.bootloader_state.bootloader_memory();
135135
FinishedL1Batch {

core/lib/multivm/src/versions/vm_1_4_2/vm.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem;
2+
13
use circuit_sequencer_api_1_4_2::sort_storage_access::sort_storage_access_queries;
24
use zksync_types::{
35
l2_to_l1_log::{SystemL2ToL1Log, UserL2ToL1Log},
@@ -90,10 +92,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
9092
/// Execute VM with custom tracers.
9193
fn inspect(
9294
&mut self,
93-
tracer: Self::TracerDispatcher,
95+
tracer: &mut Self::TracerDispatcher,
9496
execution_mode: VmExecutionMode,
9597
) -> VmExecutionResultAndLogs {
96-
self.inspect_inner(tracer, execution_mode, None)
98+
self.inspect_inner(mem::take(tracer), execution_mode, None)
9799
}
98100

99101
fn start_new_l2_block(&mut self, l2_block_env: L2BlockEnv) {
@@ -102,12 +104,12 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
102104

103105
fn inspect_transaction_with_bytecode_compression(
104106
&mut self,
105-
tracer: Self::TracerDispatcher,
107+
tracer: &mut Self::TracerDispatcher,
106108
tx: Transaction,
107109
with_compression: bool,
108110
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
109111
self.push_transaction_with_compression(tx, with_compression);
110-
let result = self.inspect_inner(tracer, VmExecutionMode::OneTx, None);
112+
let result = self.inspect_inner(mem::take(tracer), VmExecutionMode::OneTx, None);
111113
if self.has_unpublished_bytecodes() {
112114
(
113115
Err(BytecodeCompressionError::BytecodeCompressionFailed),
@@ -129,7 +131,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
129131
}
130132

131133
fn finish_batch(&mut self) -> FinishedL1Batch {
132-
let result = self.inspect(TracerDispatcher::default(), VmExecutionMode::Batch);
134+
let result = self.inspect(&mut TracerDispatcher::default(), VmExecutionMode::Batch);
133135
let execution_state = self.get_current_execution_state();
134136
let bootloader_memory = self.bootloader_state.bootloader_memory();
135137
FinishedL1Batch {

core/lib/multivm/src/versions/vm_boojum_integration/implementation/execution.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem;
2+
13
use zk_evm_1_4_0::aux_structures::Timestamp;
24

35
use crate::{
@@ -20,7 +22,7 @@ use crate::{
2022
impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
2123
pub(crate) fn inspect_inner(
2224
&mut self,
23-
dispatcher: TracerDispatcher<S, H::VmBoojumIntegration>,
25+
dispatcher: &mut TracerDispatcher<S, H::VmBoojumIntegration>,
2426
execution_mode: VmExecutionMode,
2527
) -> VmExecutionResultAndLogs {
2628
let mut enable_refund_tracer = false;
@@ -39,7 +41,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
3941
/// Collect the result from the default tracers.
4042
fn inspect_and_collect_results(
4143
&mut self,
42-
dispatcher: TracerDispatcher<S, H::VmBoojumIntegration>,
44+
dispatcher: &mut TracerDispatcher<S, H::VmBoojumIntegration>,
4345
execution_mode: VmExecutionMode,
4446
with_refund_tracer: bool,
4547
) -> (VmExecutionStopReason, VmExecutionResultAndLogs) {
@@ -49,7 +51,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
4951
DefaultExecutionTracer::new(
5052
self.system_env.default_validation_computational_gas_limit,
5153
execution_mode,
52-
dispatcher,
54+
mem::take(dispatcher),
5355
self.storage.clone(),
5456
refund_tracers,
5557
Some(PubdataTracer::new(self.batch_env.clone(), execution_mode)),
@@ -84,6 +86,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
8486
circuit_statistic_from_cycles(tx_tracer.circuits_tracer.statistics),
8587
);
8688
let result = tx_tracer.result_tracer.into_result();
89+
*dispatcher = tx_tracer.dispatcher; // return the dispatcher back
8790

8891
let result = VmExecutionResultAndLogs {
8992
result,

core/lib/multivm/src/versions/vm_boojum_integration/vm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
9090
/// Execute VM with custom tracers.
9191
fn inspect(
9292
&mut self,
93-
tracer: Self::TracerDispatcher,
93+
tracer: &mut Self::TracerDispatcher,
9494
execution_mode: VmExecutionMode,
9595
) -> VmExecutionResultAndLogs {
9696
self.inspect_inner(tracer, execution_mode)
@@ -103,7 +103,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
103103
/// Inspect transaction with optional bytecode compression.
104104
fn inspect_transaction_with_bytecode_compression(
105105
&mut self,
106-
tracer: Self::TracerDispatcher,
106+
tracer: &mut Self::TracerDispatcher,
107107
tx: Transaction,
108108
with_compression: bool,
109109
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
@@ -130,7 +130,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
130130
}
131131

132132
fn finish_batch(&mut self) -> FinishedL1Batch {
133-
let result = self.inspect(TracerDispatcher::default(), VmExecutionMode::Batch);
133+
let result = self.inspect(&mut TracerDispatcher::default(), VmExecutionMode::Batch);
134134
let execution_state = self.get_current_execution_state();
135135
let bootloader_memory = self.bootloader_state.bootloader_memory();
136136
FinishedL1Batch {

0 commit comments

Comments
 (0)