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 21 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
17 changes: 15 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.clone().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
7 changes: 3 additions & 4 deletions core/lib/multivm/src/glue/init_vm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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::vm_latest::Vm;
use crate::VmInstance;
use zksync_state::{ReadStorage, StoragePtr, StorageView};
use zksync_types::VmVersion;
Expand Down Expand Up @@ -154,7 +155,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 @@ -164,11 +164,10 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
}
}
VmVersion::VmVirtualBlocksRefundsEnhancement => {
let vm = crate::vm_latest::Vm::new(
let vm: Vm<_, H> = crate::vm_latest::Vm::new(
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.

montekki marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
//! - 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;

use crate::HistoryMode;
use std::cell::RefCell;
use std::rc::Rc;
use zksync_state::WriteStorage;

pub trait MultivmTracer<S: WriteStorage, H: HistoryMode>:
Expand All @@ -55,13 +55,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>>;
) -> Rc<RefCell<dyn crate::vm_latest::VmTracer<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>>;
) -> Rc<RefCell<dyn crate::vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>>>;
}

impl<S, T, H> IntoLatestTracer<S, H> for T
Expand All @@ -72,19 +72,28 @@ where
{
fn latest(
&self,
) -> Box<dyn crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>> {
Box::new(self.clone())
) -> Rc<RefCell<dyn crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>>> {
Rc::new(RefCell::new(self.clone()))
}
}
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved

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,
) -> Rc<RefCell<dyn crate::vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>>> {
Rc::new(RefCell::new(self.clone()))
}
}

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>,
{
}
5 changes: 5 additions & 0 deletions core/lib/multivm/src/interface/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub(crate) mod traits;

pub use traits::tracers::{dyn_tracers, multivm_tracer::MultivmTracer};
pub use traits::vm::{VmInterface, VmInterfaceHistoryEnabled};
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
pub mod types;

pub use types::{
Expand All @@ -9,4 +13,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,32 @@
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>,
) {
}
}
2 changes: 2 additions & 0 deletions core/lib/multivm/src/interface/traits/tracers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod dyn_tracers;
pub mod multivm_tracer;
23 changes: 23 additions & 0 deletions core/lib/multivm/src/interface/traits/tracers/multivm_tracer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::HistoryMode;
use zksync_state::WriteStorage;

pub trait MultivmTracer<S: WriteStorage, H: HistoryMode>:
crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>
+ crate::vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>
{
fn into_boxed(self) -> Box<dyn MultivmTracer<S, H>>
where
Self: Sized + 'static,
{
Box::new(self)
}
}

impl<S, H, T> MultivmTracer<S, H> for T
where
S: WriteStorage,
H: HistoryMode,
T: crate::vm_latest::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>
+ crate::vm_virtual_blocks::VmTracer<S, H::VmVirtualBlocksMode>,
{
}
Loading