Skip to content

Commit

Permalink
Fix nits and fix call tracers
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Nov 1, 2023
1 parent 59313c4 commit a5279b4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 60 deletions.
97 changes: 49 additions & 48 deletions core/lib/multivm/src/interface/traits/vm.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
/// This module contains traits for the VM interface.
/// All VMs should implement these traits to be used in our system.
/// The trait is generic over the storage type, allowing it to be used with any storage implementation.
/// Additionally, this trait is generic over HistoryMode, allowing it to be used with or without history.
///
/// `TracerDispatcher` is an associated type used to dispatch tracers in VMs.
/// It manages tracers across different VM versions.
/// Even though we use the same interface for all VM versions,
/// we can now specify only the necessary trait bounds for each VM version.
///
/// Generally speaking, in most cases, the tracer dispatcher is a wrapper around `Vec<Box<dyn VmTracer>>`,
/// where `VmTracer` is a trait implemented for a specific VM version.
///
/// Example usage:
/// ```
/// use std::{
/// cell::RefCell,
/// rc::Rc,
/// sync::Arc
/// };
/// use once_cell::sync::OnceCell;
/// use multivm::{
/// interface::{L1BatchEnv, SystemEnv, VmInterface},
/// tracers::CallTracer ,
/// vm_latest::ToTracerPointer
/// };
/// use zksync_state::{InMemoryStorage, StorageView};
/// use zksync_types::Transaction;
///
/// // Prepare the environment for the VM.
/// let l1_batch_env = L1BatchEnv::new();
/// let system_env = SystemEnv::default();
/// // Create storage
/// let storage = Rc::new(RefCell::new(StorageView::new(InMemoryStorage::default())));
/// // Instantiate VM with the desired version.
/// let mut vm = multivm::vm_latest::Vm::new(l1_batch_env, system_env, storage);
/// // Push a transaction to the VM.
/// let tx = Transaction::default();
/// vm.push_transaction(tx);
/// // Instantiate a tracer.
/// let result = Arc::new(OnceCell::new());
/// let call_tracer = CallTracer::new(result.clone()).into_tracer_pointer();
/// // Inspect the transaction with a tracer. You can use either one tracer or a vector of tracers.
/// let result = vm.inspect(call_tracer.into(), multivm::interface::VmExecutionMode::OneTx);
///
/// // To obtain the result of the entire batch, you can use the following code:
/// let result = vm.execute(multivm::interface::VmExecutionMode::Batch);
/// ```
//! This module contains traits for the VM interface.
//! All VMs should implement these traits to be used in our system.
//! The trait is generic over the storage type, allowing it to be used with any storage implementation.
//! Additionally, this trait is generic over HistoryMode, allowing it to be used with or without history.
//!
//! `TracerDispatcher` is an associated type used to dispatch tracers in VMs.
//! It manages tracers across different VM versions.
//! Even though we use the same interface for all VM versions,
//! we can now specify only the necessary trait bounds for each VM version.
//!
//! Generally speaking, in most cases, the tracer dispatcher is a wrapper around `Vec<Box<dyn VmTracer>>`,
//! where `VmTracer` is a trait implemented for a specific VM version.
//!
//! Example usage:
//! ```
//! use std::{
//! cell::RefCell,
//! rc::Rc,
//! sync::Arc
//! };
//! use once_cell::sync::OnceCell;
//! use multivm::{
//! interface::{L1BatchEnv, SystemEnv, VmInterface},
//! tracers::CallTracer ,
//! vm_latest::ToTracerPointer
//! };
//! use zksync_state::{InMemoryStorage, StorageView};
//! use zksync_types::Transaction;
//!
//! // Prepare the environment for the VM.
//! let l1_batch_env = L1BatchEnv::new();
//! let system_env = SystemEnv::default();
//! // Create storage
//! let storage = Rc::new(RefCell::new(StorageView::new(InMemoryStorage::default())));
//! // Instantiate VM with the desired version.
//! let mut vm = multivm::vm_latest::Vm::new(l1_batch_env, system_env, storage);
//! // Push a transaction to the VM.
//! let tx = Transaction::default();
//! vm.push_transaction(tx);
//! // Instantiate a tracer.
//! let result = Arc::new(OnceCell::new());
//! let call_tracer = CallTracer::new(result.clone()).into_tracer_pointer();
//! // Inspect the transaction with a tracer. You can use either one tracer or a vector of tracers.
//! let result = vm.inspect(call_tracer.into(), multivm::interface::VmExecutionMode::OneTx);
//!
//! // To obtain the result of the entire batch, you can use the following code:
//! let result = vm.execute(multivm::interface::VmExecutionMode::Batch);
//! ```

use crate::interface::types::errors::BytecodeCompressionError;
use crate::interface::types::inputs::{L1BatchEnv, L2BlockEnv, SystemEnv, VmExecutionMode};
use crate::interface::types::outputs::{
Expand Down
12 changes: 5 additions & 7 deletions core/lib/multivm/src/tracers/call_tracer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use zksync_types::vm_trace::Call;
pub mod vm_latest;
pub mod vm_virtual_blocks;

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct CallTracer {
stack: Vec<FarcallAndNearCallCount>,
result: Option<Arc<OnceCell<Vec<Call>>>>,
result: Arc<OnceCell<Vec<Call>>>,
}

#[derive(Debug, Clone)]
struct FarcallAndNearCallCount {
farcall: Call,
Expand All @@ -20,7 +21,7 @@ impl CallTracer {
pub fn new(result: Arc<OnceCell<Vec<Call>>>) -> Self {
Self {
stack: vec![],
result: Some(result),
result,
}
}

Expand All @@ -32,11 +33,8 @@ impl CallTracer {
}

fn store_result(&mut self) {
if self.result.is_none() {
return;
}
let result = self.extract_result();
let cell = self.result.as_ref().unwrap();
let cell = self.result.as_ref();
cell.set(result).unwrap();
}
}
3 changes: 0 additions & 3 deletions core/lib/multivm/src/tracers/validator/vm_latest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ use crate::tracers::validator::{ValidationRoundResult, ValidationTracer};

use crate::vm_latest::{BootloaderState, ZkSyncVmState};

/// Tracer that is used to ensure that the validation adheres to all the rules
/// to prevent DDoS attacks on the server.

impl<H: HistoryMode> ValidationTracer<H> {
fn check_user_restrictions_vm_latest<S: WriteStorage>(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use crate::interface::VmExecutionResultAndLogs;
use crate::tracers::validator::types::{NewTrustedValidationItems, ValidationTracerMode};
use crate::tracers::validator::{ValidationRoundResult, ValidationTracer};

/// Tracer that is used to ensure that the validation adheres to all the rules
/// to prevent DDoS attacks on the server.
impl<H: HistoryMode> ValidationTracer<H> {
fn check_user_restrictions_vm_virtual_blocks<S: WriteStorage>(
&mut self,
Expand Down
2 changes: 2 additions & 0 deletions core/lib/multivm/src/versions/vm_latest/tracers/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for TracerDispatcher<S, H>
tracer.initialize_tracer(_state);
}
}

/// Run after each vm execution cycle
#[inline(always)]
fn finish_cycle(
Expand All @@ -99,6 +100,7 @@ impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for TracerDispatcher<S, H>
}
result
}

/// Run after the vm execution
fn after_vm_execution(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl<S: WriteStorage, H: HistoryMode> ExecutionProcessing<S, H> for TracerDispat
tracer.initialize_tracer(_state);
}
}

/// Run after each vm execution cycle
fn after_cycle(
&mut self,
Expand Down

0 comments on commit a5279b4

Please sign in to comment.