Skip to content

Commit

Permalink
Apply interface for latest and vm refunds enhancement
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <deniallugo@gmail.com>
  • Loading branch information
Deniallugo committed Nov 7, 2023
1 parent fe6aaef commit 1a98417
Show file tree
Hide file tree
Showing 49 changed files with 619 additions and 854 deletions.
2 changes: 1 addition & 1 deletion core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use multivm::interface::dyn_tracers::vm_1_3_3::DynTracer;
use multivm::interface::dyn_tracers::vm_1_4_0::DynTracer;
use multivm::interface::tracer::VmExecutionStopReason;
use multivm::interface::{
L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode, VmInterface,
Expand Down
1 change: 0 additions & 1 deletion core/lib/multivm/src/glue/init_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ impl<S: ReadStorage, H: HistoryMode> VmInstance<S, H> {
l1_batch_env.glue_into(),
system_env.clone(),
storage_view.clone(),
H::VmBoojumIntegration::default(),
);
let vm = VmInstanceVersion::VmBoojumIntegration(Box::new(vm));
Self {
Expand Down
24 changes: 21 additions & 3 deletions core/lib/multivm/src/glue/tracers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait MultivmTracer<S: WriteStorage, H: HistoryMode>:
}

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

pub trait IntoVmVirtualBlocksTracer<S: WriteStorage, H: HistoryMode> {
Expand All @@ -68,7 +68,7 @@ where
H: HistoryMode,
T: crate::vm_latest::VmTracer<S, H::VmBoojumIntegration> + Clone + 'static,
{
fn latest(&self) -> crate::vm_latest::TracerPointer<S, H::VmVirtualBlocksRefundsEnhancement> {
fn latest(&self) -> crate::vm_latest::TracerPointer<S, H::VmBoojumIntegration> {
Box::new(self.clone())
}
}
Expand All @@ -86,10 +86,28 @@ where
}
}

impl<S, T, H> IntoVmRefundsEnhancementTracer<S, H> for T
where
S: WriteStorage,
H: HistoryMode,
T: crate::vm_refunds_enhancement::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>
+ Clone
+ 'static,
{
fn vm_refunds_enhancement(
&self,
) -> Box<dyn crate::vm_refunds_enhancement::VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>>
{
Box::new(self.clone())
}
}

impl<S, H, T> MultivmTracer<S, H> for T
where
S: WriteStorage,
H: HistoryMode,
T: IntoLatestTracer<S, H> + IntoVmVirtualBlocksTracer<S, H>,
T: IntoLatestTracer<S, H>
+ IntoVmVirtualBlocksTracer<S, H>
+ IntoVmRefundsEnhancementTracer<S, H>,
{
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod vm_1_3_3;
pub mod vm_1_4_0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use zk_evm_1_4_0::abstractions::Memory;
use zk_evm_1_4_0::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/tracers/call_tracer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use zksync_types::vm_trace::Call;

pub mod vm_latest;
pub mod vm_refunds_enhancement;
pub mod vm_virtual_blocks;

#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/tracers/call_tracer/vm_latest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::interface::tracer::VmExecutionStopReason;
use crate::interface::{traits::tracers::dyn_tracers::vm_1_3_3::DynTracer, VmRevertReason};
use crate::interface::{traits::tracers::dyn_tracers::vm_1_4_0::DynTracer, VmRevertReason};
use crate::tracers::call_tracer::{CallTracer, FarcallAndNearCallCount};
use crate::vm_latest::VmTracer;
use crate::vm_latest::{BootloaderState, HistoryMode, SimpleMemory, ZkSyncVmState};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,20 @@
use once_cell::sync::OnceCell;
use std::marker::PhantomData;
use std::sync::Arc;

use crate::interface::tracer::VmExecutionStopReason;
use crate::interface::{traits::tracers::dyn_tracers::vm_1_3_3::DynTracer, VmRevertReason};
use crate::tracers::call_tracer::{CallTracer, FarcallAndNearCallCount};
use crate::vm_refunds_enhancement::VmTracer;
use crate::vm_refunds_enhancement::{BootloaderState, HistoryMode, SimpleMemory, ZkSyncVmState};
use zk_evm_1_3_3::tracing::{AfterExecutionData, VmLocalStateData};
use zk_evm_1_3_3::zkevm_opcode_defs::{
FarCallABI, FarCallOpcode, FatPointer, Opcode, RetOpcode,
CALL_IMPLICIT_CALLDATA_FAT_PTR_REGISTER, RET_IMPLICIT_RETURNDATA_PARAMS_REGISTER,
FarCallABI, FatPointer, Opcode, RetOpcode, CALL_IMPLICIT_CALLDATA_FAT_PTR_REGISTER,
RET_IMPLICIT_RETURNDATA_PARAMS_REGISTER,
};

use crate::interface::VmRevertReason;
use zksync_state::{StoragePtr, WriteStorage};
use zksync_system_constants::CONTRACT_DEPLOYER_ADDRESS;
use zksync_types::vm_trace::{Call, CallType};
use zksync_types::FarCallOpcode;
use zksync_types::U256;

use crate::vm_refunds_enhancement::old_vm::history_recorder::HistoryMode;
use crate::vm_refunds_enhancement::old_vm::memory::SimpleMemory;
use crate::vm_refunds_enhancement::tracers::traits::{DynTracer, VmTracer};
use crate::vm_refunds_enhancement::types::internals::ZkSyncVmState;
use crate::vm_refunds_enhancement::{BootloaderState, VmExecutionStopReason};

#[derive(Debug, Clone)]
pub struct CallTracer<H: HistoryMode> {
stack: Vec<FarcallAndNearCallCount>,
pub result: Arc<OnceCell<Vec<Call>>>,
_phantom: PhantomData<fn() -> H>,
}

#[derive(Debug, Clone)]
struct FarcallAndNearCallCount {
farcall: Call,
near_calls_after: usize,
}

impl<H: HistoryMode> CallTracer<H> {
pub fn new(resulted_stack: Arc<OnceCell<Vec<Call>>>, _history: H) -> Self {
Self {
stack: vec![],
result: resulted_stack,
_phantom: PhantomData,
}
}
}

impl<S, H: HistoryMode> DynTracer<S, H> for CallTracer<H> {
impl<S, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for CallTracer {
fn after_execution(
&mut self,
state: VmLocalStateData<'_>,
Expand Down Expand Up @@ -75,43 +46,35 @@ impl<S, H: HistoryMode> DynTracer<S, H> for CallTracer<H> {
..Default::default()
};

self.handle_far_call_op_code(state, data, memory, &mut current_call);
self.handle_far_call_op_code_refunds_enhancement(state, memory, &mut current_call);
self.stack.push(FarcallAndNearCallCount {
farcall: current_call,
near_calls_after: 0,
});
}
Opcode::Ret(ret_code) => {
self.handle_ret_op_code(state, data, memory, ret_code);
self.handle_ret_op_code_refunds_enhancement(state, memory, ret_code);
}
_ => {}
};
}
}

impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for CallTracer<H> {
impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for CallTracer {
fn after_vm_execution(
&mut self,
_state: &mut ZkSyncVmState<S, H>,
_bootloader_state: &BootloaderState,
_stop_reason: VmExecutionStopReason,
) {
self.result
.set(
std::mem::take(&mut self.stack)
.into_iter()
.map(|x| x.farcall)
.collect(),
)
.expect("Result is already set");
self.store_result()
}
}

impl<H: HistoryMode> CallTracer<H> {
fn handle_far_call_op_code(
impl CallTracer {
fn handle_far_call_op_code_refunds_enhancement<H: HistoryMode>(
&mut self,
state: VmLocalStateData<'_>,
_data: AfterExecutionData,
memory: &SimpleMemory<H>,
current_call: &mut Call,
) {
Expand Down Expand Up @@ -164,7 +127,7 @@ impl<H: HistoryMode> CallTracer<H> {
current_call.gas = current.ergs_remaining;
}

fn save_output(
fn save_output_refunds_enhancement<H: HistoryMode>(
&mut self,
state: VmLocalStateData<'_>,
memory: &SimpleMemory<H>,
Expand Down Expand Up @@ -208,10 +171,9 @@ impl<H: HistoryMode> CallTracer<H> {
}
}

fn handle_ret_op_code(
fn handle_ret_op_code_refunds_enhancement<H: HistoryMode>(
&mut self,
state: VmLocalStateData<'_>,
_data: AfterExecutionData,
memory: &SimpleMemory<H>,
ret_opcode: RetOpcode,
) {
Expand All @@ -230,7 +192,7 @@ impl<H: HistoryMode> CallTracer<H> {
.parent_gas
.saturating_sub(state.vm_local_state.callstack.current.ergs_remaining);

self.save_output(state, memory, ret_opcode, &mut current_call.farcall);
self.save_output_refunds_enhancement(state, memory, ret_opcode, &mut current_call.farcall);

// If there is a parent call, push the current call to it
// Otherwise, push the current call to the stack, because it's the top level call
Expand Down
1 change: 1 addition & 0 deletions core/lib/multivm/src/tracers/storage_invocation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod vm_latest;
pub mod vm_refunds_enhancement;
pub mod vm_virtual_blocks;

/// Tracer responsible for calculating the number of storage invocations and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::interface::tracer::{TracerExecutionStatus, TracerExecutionStopReason};
use crate::interface::{traits::tracers::dyn_tracers::vm_1_3_3::DynTracer, Halt};
use crate::interface::{traits::tracers::dyn_tracers::vm_1_4_0::DynTracer, Halt};
use crate::tracers::storage_invocation::StorageInvocations;
use crate::vm_latest::VmTracer;
use crate::vm_latest::{BootloaderState, HistoryMode, SimpleMemory, ZkSyncVmState};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::interface::tracer::{TracerExecutionStatus, TracerExecutionStopReason};
use crate::interface::{traits::tracers::dyn_tracers::vm_1_3_3::DynTracer, Halt};
use crate::tracers::storage_invocation::StorageInvocations;
use crate::vm_refunds_enhancement::VmTracer;
use crate::vm_refunds_enhancement::{BootloaderState, HistoryMode, SimpleMemory, ZkSyncVmState};
use zksync_state::WriteStorage;

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

impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H> for StorageInvocations {
fn finish_cycle(
&mut self,
state: &mut ZkSyncVmState<S, H>,
_bootloader_state: &mut BootloaderState,
) -> TracerExecutionStatus {
let current = state
.storage
.storage
.get_ptr()
.borrow()
.missed_storage_invocations();

if current >= self.limit {
return TracerExecutionStatus::Stop(TracerExecutionStopReason::Abort(
Halt::TracerCustom("Storage invocations limit reached".to_string()),
));
}
TracerExecutionStatus::Continue
}
}
1 change: 1 addition & 0 deletions core/lib/multivm/src/tracers/validator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod types;
mod vm_latest;
mod vm_refunds_enhancement;
mod vm_virtual_blocks;

use std::sync::Arc;
Expand Down
34 changes: 17 additions & 17 deletions core/lib/multivm/src/tracers/validator/vm_latest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use zk_evm_1_3_3::{
use zk_evm_1_4_0::{
tracing::{BeforeExecutionData, VmLocalStateData},
zkevm_opcode_defs::{ContextOpcode, FarCallABI, LogOpcode, Opcode},
};
Expand All @@ -15,23 +15,25 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::vm_latest::tracers::utils::{
computational_gas_price, get_calldata_page_via_abi, print_debug_if_needed, VmHook,
};
use crate::vm_latest::SimpleMemory;
use crate::vm_latest::VmTracer;

use crate::interface::traits::tracers::dyn_tracers::vm_1_3_3::DynTracer;
use crate::interface::types::tracer::{TracerExecutionStatus, TracerExecutionStopReason};
use crate::interface::Halt;
use crate::tracers::validator::types::{NewTrustedValidationItems, ValidationTracerMode};
use crate::tracers::validator::{ValidationRoundResult, ValidationTracer};
use crate::interface::{
traits::tracers::dyn_tracers::vm_1_4_0::DynTracer,
types::tracer::{TracerExecutionStatus, TracerExecutionStopReason},
Halt,
};
use crate::tracers::validator::{
types::{NewTrustedValidationItems, ValidationTracerMode},
{ValidationRoundResult, ValidationTracer},
};

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

impl<H: HistoryMode> ValidationTracer<H> {
fn check_user_restrictions_vm_latest<S: WriteStorage>(
&mut self,
state: VmLocalStateData<'_>,
data: BeforeExecutionData,
memory: &SimpleMemory<H::VmVirtualBlocksRefundsEnhancement>,
memory: &SimpleMemory<H::VmBoojumIntegration>,
storage: StoragePtr<S>,
) -> ValidationRoundResult {
if self.computational_gas_used > self.computational_gas_limit {
Expand Down Expand Up @@ -125,14 +127,14 @@ impl<H: HistoryMode> ValidationTracer<H> {
}
}

impl<S: WriteStorage, H: HistoryMode>
DynTracer<S, SimpleMemory<H::VmVirtualBlocksRefundsEnhancement>> for ValidationTracer<H>
impl<S: WriteStorage, H: HistoryMode> DynTracer<S, SimpleMemory<H::VmBoojumIntegration>>
for ValidationTracer<H>
{
fn before_execution(
&mut self,
state: VmLocalStateData<'_>,
data: BeforeExecutionData,
memory: &SimpleMemory<H::VmVirtualBlocksRefundsEnhancement>,
memory: &SimpleMemory<H::VmBoojumIntegration>,
storage: StoragePtr<S>,
) {
// For now, we support only validations for users.
Expand Down Expand Up @@ -180,12 +182,10 @@ impl<S: WriteStorage, H: HistoryMode>
}
}

impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H::VmVirtualBlocksRefundsEnhancement>
for ValidationTracer<H>
{
impl<S: WriteStorage, H: HistoryMode> VmTracer<S, H::VmBoojumIntegration> for ValidationTracer<H> {
fn finish_cycle(
&mut self,
_state: &mut ZkSyncVmState<S, H::VmVirtualBlocksRefundsEnhancement>,
_state: &mut ZkSyncVmState<S, H::VmBoojumIntegration>,
_bootloader_state: &mut BootloaderState,
) -> TracerExecutionStatus {
if self.should_stop_execution {
Expand Down
Loading

0 comments on commit 1a98417

Please sign in to comment.