Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vm): Use the one interface for all vms #277

Merged
merged 33 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cce0d1b
Just new interface
Deniallugo Oct 18, 2023
4563b0a
Remove redundant trait from DefaultTracer
Deniallugo Oct 18, 2023
3c579ca
Fix interface of tracer
Deniallugo Oct 18, 2023
7ac263e
Fix interface of tracer
Deniallugo Oct 18, 2023
547e8ea
HistoryMode in the latest vm works correcly
Deniallugo Oct 19, 2023
d956403
Use the only tracer
Deniallugo Oct 19, 2023
cb916ed
Start migrating vm virtual blocks
Deniallugo Oct 19, 2023
c191286
auto impl for traits
Deniallugo Oct 20, 2023
241e518
Move call tracer and remove old multi tracer
Deniallugo Oct 20, 2023
6bad2fa
Move storage invocation tracer
Deniallugo Oct 20, 2023
40aac00
Fully migrate vm virtual blocks to the new interface
Deniallugo Oct 20, 2023
27625a6
Use tracer dispatcher
Deniallugo Oct 25, 2023
0a42fdf
Use tracer dispatcher for vm intance
Deniallugo Oct 25, 2023
2025ab0
Use tracer pointer
Deniallugo Oct 25, 2023
ba30de8
Clunky implementation with clone
Deniallugo Oct 25, 2023
7f2b1cc
some more fixes
Deniallugo Oct 26, 2023
89fca24
Move validation tracer
Deniallugo Oct 26, 2023
de06cc9
Migrate the core server to the new interface
Deniallugo Oct 26, 2023
403b4ab
Fix lints
Deniallugo Oct 26, 2023
245af44
Restore tests for vm latest
Deniallugo Oct 26, 2023
b0cf6e2
Recover tests for vm virtual blocks
Deniallugo Oct 26, 2023
44a41f0
Add inlines
Deniallugo Oct 26, 2023
3278584
Change refcell to box
Deniallugo Oct 26, 2023
3594c0f
Use boxes instead of rc ref cell
Deniallugo Oct 27, 2023
cafa23c
Get rid of pub crate in tracer dispatcher
Deniallugo Oct 27, 2023
6db2e23
Remove useless multivm tracer trait
Deniallugo Oct 27, 2023
59313c4
Improve comments
Deniallugo Oct 30, 2023
a5279b4
Fix nits and fix call tracers
Deniallugo Nov 1, 2023
7569bdf
Rename vm -> vm_instance
Deniallugo Nov 1, 2023
20a09e7
Merge branch 'main' into deniallugo-vm-interface
Deniallugo Nov 2, 2023
fe6aaef
Merge branch 'main' into deniallugo-vm-interface
Deniallugo Nov 6, 2023
aaf24a9
Apply interface for latest and vm refunds enhancement
Deniallugo Nov 7, 2023
466742d
Fix tests
Deniallugo Nov 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 15 additions & 18 deletions core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use multivm::interface::{L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode};
use multivm::interface::dyn_tracers::vm_1_3_3::DynTracer;
use multivm::interface::tracer::VmExecutionStopReason;
use multivm::interface::{
L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode, VmInterface,
};
use multivm::vm_latest::{
constants::{BLOCK_GAS_LIMIT, BOOTLOADER_HEAP_PAGE},
BootloaderState, BoxedTracer, DynTracer, HistoryEnabled, HistoryMode, Vm,
VmExecutionStopReason, VmTracer, ZkSyncVmState,
BootloaderState, HistoryEnabled, HistoryMode, SimpleMemory, ToTracerPointer, Vm, VmTracer,
ZkSyncVmState,
};
use once_cell::sync::Lazy;
use std::cell::RefCell;
Expand Down Expand Up @@ -31,7 +35,7 @@ struct SpecialBootloaderTracer {
output: Rc<RefCell<u32>>,
}

impl<S: WriteStorage, H: HistoryMode> DynTracer<S, H> for SpecialBootloaderTracer {}
impl<S: WriteStorage, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for SpecialBootloaderTracer {}

impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for SpecialBootloaderTracer {
fn initialize_tracer(&mut self, state: &mut ZkSyncVmState<S, H>) {
Expand Down Expand Up @@ -247,14 +251,11 @@ pub(super) fn execute_internal_transfer_test() -> u32 {
let tracer = SpecialBootloaderTracer {
input,
output: tracer_result.clone(),
};
let mut vm = Vm::new(
l1_batch,
system_env,
Rc::new(RefCell::new(storage_view)),
HistoryEnabled,
);
let result = vm.inspect(vec![tracer.into_boxed()], VmExecutionMode::Bootloader);
}
.into_tracer_pointer();
let mut vm: Vm<_, HistoryEnabled> =
Vm::new(l1_batch, system_env, Rc::new(RefCell::new(storage_view)));
let result = vm.inspect(tracer.into(), VmExecutionMode::Bootloader);

assert!(!result.result.is_failed(), "The internal call has reverted");
tracer_result.take()
Expand Down Expand Up @@ -307,12 +308,8 @@ pub(super) fn execute_user_txs_in_test_gas_vm(
chain_id: L2ChainId::default(),
};

let mut vm = Vm::new(
l1_batch,
system_env,
Rc::new(RefCell::new(storage_view)),
HistoryEnabled,
);
let mut vm: Vm<_, HistoryEnabled> =
Vm::new(l1_batch, system_env, Rc::new(RefCell::new(storage_view)));

let mut total_gas_refunded = 0;
for tx in txs {
Expand Down
4 changes: 1 addition & 3 deletions core/lib/multivm/src/glue/init_vm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::GlueInto;
use crate::glue::history_mode::HistoryMode;
use crate::interface::{L1BatchEnv, SystemEnv};
use crate::interface::{L1BatchEnv, SystemEnv, VmInterface};
use crate::vm_instance::VmInstanceVersion;
use crate::VmInstance;
use zksync_state::{ReadStorage, StoragePtr, StorageView};
Expand Down Expand Up @@ -154,7 +154,6 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
l1_batch_env.glue_into(),
system_env.clone().glue_into(),
storage_view.clone(),
H::VmVirtualBlocksMode::default(),
);
let vm = VmInstanceVersion::VmVirtualBlocks(Box::new(vm));
Self {
Expand All @@ -168,7 +167,6 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
l1_batch_env.glue_into(),
system_env.clone(),
storage_view.clone(),
H::VmVirtualBlocksRefundsEnhancement::default(),
);
let vm = VmInstanceVersion::VmVirtualBlocksRefundsEnhancement(Box::new(vm));
Self {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/glue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) mod block_properties;
pub(crate) mod history_mode;
pub(crate) mod init_vm;
pub(crate) mod oracle_tools;
pub(crate) mod tracer;
pub mod tracers;
mod types;

/// This trait is a workaround on the Rust'c [orphan rule](orphan_rule).
Expand Down
56 changes: 0 additions & 56 deletions core/lib/multivm/src/glue/tracer/implementations.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
//! # Multivm Tracing
//!
//! The MultiVM tracing module enables Tracers support for different versions of virtual machines
//! The MultiVM tracing module enables support for Tracers in different versions of virtual machines.
//!
//! ## Overview
//!
//! Different VM versions may have distinct requirements and types for Tracers. To accommodate these differences,
//! this module defines one primary trait:
//!
//! - `MultivmTracer<S, H>`: This trait represents a tracer that can be converted into a tracer for
//! specific VM version.
//! a specific VM version.
//!
//! Specific traits for each VM version, which supports Custom Tracers
//! Specific traits for each VM version, which support Custom Tracers:
//! - `IntoLatestTracer<S, H>`: This trait is responsible for converting a tracer
//! into a form compatible with the latest VM version.
//! It defines a method `latest` for obtaining a boxed tracer.
//!
//! - `IntoVmVirtualBlocksTracer<S, H>`:This trait is responsible for converting a tracer
//! - `IntoVmVirtualBlocksTracer<S, H>`: This trait is responsible for converting a tracer
//! into a form compatible with the vm_virtual_blocks version.
//! It defines a method `vm_virtual_blocks` for obtaining a boxed tracer.
//!
//! For `MultivmTracer` to be implemented, Tracer must implement all N currently
//! For `MultivmTracer` to be implemented, the Tracer must implement all N currently
//! existing sub-traits.
//!
//! Any tracer compatible with the current VM automatically
//! supports conversion into the latest VM,
//! but the remaining traits required by the MultivmTracer should be implemented manually.
//! If a certain tracer is not intended to be used with VMs other than the latest one,
//! these traits should still be implemented, but one may just write a panicking implementation.
//!
//! ## Adding a new VM version
//!
//! To add support for one more VM version to MultivmTracer, one needs to:
//! - Create a new trait performing conversion to the specified VM tracer, e.g. `Into<VmVersion>Tracer`.
//! - Provide implementations of this trait for all the structures that currently implement `MultivmTracer`.
//! - Create a new trait performing conversion to the specified VM tracer, e.g., `Into<VmVersion>Tracer`.
//! - Add this trait as a trait bound to the `MultivmTracer`.
//! - Add this trait as a trait bound for `T` in `MultivmTracer` implementation.
//! - Integrate the newly added method to the MultiVM itself (e.g. add required tracer conversions where applicable).
mod implementations;

//! — Implement the trait for `T` with a bound to `VmTracer` for a specific version.
//!
use crate::HistoryMode;
use zksync_state::WriteStorage;

pub type MultiVmTracerPointer<S, H> = Box<dyn MultivmTracer<S, H>>;

pub trait MultivmTracer<S: WriteStorage, H: HistoryMode>:
IntoLatestTracer<S, H> + IntoVmVirtualBlocksTracer<S, H>
{
fn into_boxed(self) -> Box<dyn MultivmTracer<S, H>>
fn into_tracer_pointer(self) -> MultiVmTracerPointer<S, H>
where
Self: Sized + 'static,
{
Expand All @@ -53,15 +47,13 @@ pub trait MultivmTracer<S: WriteStorage, H: HistoryMode>:
}

pub trait IntoLatestTracer<S: WriteStorage, H: HistoryMode> {
fn latest(
&self,
) -> Box<dyn crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>>;
fn latest(&self) -> crate::vm_latest::TracerPointer<S, H::VmVirtualBlocksRefundsEnhancement>;
}

pub trait IntoVmVirtualBlocksTracer<S: WriteStorage, H: HistoryMode> {
fn vm_virtual_blocks(
&self,
) -> Box<dyn crate::vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>>;
) -> crate::vm_virtual_blocks::TracerPointer<S, H::VmVirtualBlocksMode>;
}

impl<S, T, H> IntoLatestTracer<S, H> for T
Expand All @@ -70,9 +62,20 @@ where
H: HistoryMode,
T: crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement> + Clone + 'static,
{
fn latest(
fn latest(&self) -> crate::vm_latest::TracerPointer<S, H::VmVirtualBlocksRefundsEnhancement> {
Box::new(self.clone())
}
}

impl<S, T, H> IntoVmVirtualBlocksTracer<S, H> for T
where
S: WriteStorage,
H: HistoryMode,
T: crate::vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode> + Clone + 'static,
{
fn vm_virtual_blocks(
&self,
) -> Box<dyn crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>> {
) -> crate::vm_virtual_blocks::TracerPointer<S, H::VmVirtualBlocksMode> {
Box::new(self.clone())
}
}
Expand All @@ -81,10 +84,6 @@ impl<S, H, T> MultivmTracer<S, H> for T
where
S: WriteStorage,
H: HistoryMode,
T: crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>
+ IntoLatestTracer<S, H>
+ IntoVmVirtualBlocksTracer<S, H>
+ Clone
+ 'static,
T: IntoLatestTracer<S, H> + IntoVmVirtualBlocksTracer<S, H>,
{
}
7 changes: 7 additions & 0 deletions core/lib/multivm/src/interface/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub(crate) mod traits;

pub use traits::{
tracers::dyn_tracers,
vm::{VmInterface, VmInterfaceHistoryEnabled},
};
pub mod types;

pub use types::{
Expand All @@ -9,4 +15,5 @@ pub use types::{
BootloaderMemory, CurrentExecutionState, ExecutionResult, FinishedL1Batch, L2Block,
Refunds, VmExecutionResultAndLogs, VmExecutionStatistics, VmMemoryMetrics,
},
tracer,
};
2 changes: 2 additions & 0 deletions core/lib/multivm/src/interface/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod tracers;
pub mod vm;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod vm_1_3_3;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use zk_evm_1_3_3::abstractions::Memory;
use zk_evm_1_3_3::tracing::{
AfterDecodingData, AfterExecutionData, BeforeExecutionData, VmLocalStateData,
};
use zksync_state::StoragePtr;

/// Version of zk_evm_1_3_3::Tracer suitable for dynamic dispatch.
pub trait DynTracer<S, M: Memory> {
fn before_decoding(&mut self, _state: VmLocalStateData<'_>, _memory: &M) {}
fn after_decoding(
&mut self,
_state: VmLocalStateData<'_>,
_data: AfterDecodingData,
_memory: &M,
) {
}
fn before_execution(
&mut self,
_state: VmLocalStateData<'_>,
_data: BeforeExecutionData,
_memory: &M,
_storage: StoragePtr<S>,
) {
}
fn after_execution(
&mut self,
_state: VmLocalStateData<'_>,
_data: AfterExecutionData,
_memory: &M,
_storage: StoragePtr<S>,
) {
}
}
1 change: 1 addition & 0 deletions core/lib/multivm/src/interface/traits/tracers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod dyn_tracers;
Loading
Loading