Skip to content

Commit

Permalink
feat(vm): Extract oneshot VM executor interface (#2671)
Browse files Browse the repository at this point in the history
## What ❔

Extracts oneshot VM executor from the API server crate.

## Why ❔

Simplifies reasoning about oneshot VM execution and its maintenance.
Allows for alternative implementations.

## 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`.
  • Loading branch information
slowli committed Aug 27, 2024
1 parent beaf155 commit 951d5f2
Show file tree
Hide file tree
Showing 19 changed files with 859 additions and 811 deletions.
4 changes: 3 additions & 1 deletion core/lib/multivm/src/tracers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ pub use self::{
multivm_dispatcher::TracerDispatcher,
prestate_tracer::PrestateTracer,
storage_invocation::StorageInvocations,
validator::{ValidationError, ValidationTracer, ValidationTracerParams},
validator::{
ValidationError, ValidationTracer, ValidationTracerParams, ViolatedValidationRule,
},
};

mod call_tracer;
Expand Down
6 changes: 2 additions & 4 deletions core/lib/multivm/src/tracers/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ use zksync_types::{
};
use zksync_utils::{be_bytes_to_safe_address, u256_to_account_address, u256_to_h256};

pub use crate::tracers::validator::types::{ValidationError, ValidationTracerParams};
use self::types::{NewTrustedValidationItems, ValidationTracerMode};
pub use self::types::{ValidationError, ValidationTracerParams, ViolatedValidationRule};
use crate::{
glue::tracers::IntoOldVmTracer,
interface::storage::{StoragePtr, WriteStorage},
tracers::validator::types::{
NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule,
},
};

mod types;
Expand Down
5 changes: 4 additions & 1 deletion core/lib/vm_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ pub use crate::{
BytecodeCompressionError, Halt, TxRevertReason, VmRevertReason,
VmRevertReasonParsingError,
},
inputs::{L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode},
inputs::{
L1BatchEnv, L2BlockEnv, OneshotEnv, StoredL2BlockEnv, SystemEnv, TxExecutionMode,
VmExecutionMode,
},
outputs::{
BootloaderMemory, Call, CallType, CircuitStatistic, CompressedBytecodeInfo,
CurrentExecutionState, DeduplicatedWritesMetrics, ExecutionResult, FinishedL1Batch,
Expand Down
19 changes: 14 additions & 5 deletions core/lib/vm_interface/src/types/inputs/l2_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ pub struct L2BlockEnv {
}

impl L2BlockEnv {
pub fn from_l2_block_data(miniblock_execution_data: &L2BlockExecutionData) -> Self {
pub fn from_l2_block_data(execution_data: &L2BlockExecutionData) -> Self {
Self {
number: miniblock_execution_data.number.0,
timestamp: miniblock_execution_data.timestamp,
prev_block_hash: miniblock_execution_data.prev_block_hash,
max_virtual_blocks_to_create: miniblock_execution_data.virtual_blocks,
number: execution_data.number.0,
timestamp: execution_data.timestamp,
prev_block_hash: execution_data.prev_block_hash,
max_virtual_blocks_to_create: execution_data.virtual_blocks,
}
}
}

/// Current block information stored in the system context contract. Can be used to set up
/// oneshot transaction / call execution.
#[derive(Debug, Clone, Copy)]
pub struct StoredL2BlockEnv {
pub number: u32,
pub timestamp: u64,
pub txs_rolling_hash: H256,
}
14 changes: 13 additions & 1 deletion core/lib/vm_interface/src/types/inputs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
pub use self::{
execution_mode::VmExecutionMode,
l1_batch_env::L1BatchEnv,
l2_block::L2BlockEnv,
l2_block::{L2BlockEnv, StoredL2BlockEnv},
system_env::{SystemEnv, TxExecutionMode},
};

mod execution_mode;
mod l1_batch_env;
mod l2_block;
mod system_env;

/// Full environment for oneshot transaction / call execution.
#[derive(Debug)]
pub struct OneshotEnv {
/// System environment.
pub system: SystemEnv,
/// Part of the environment specific to an L1 batch.
pub l1_batch: L1BatchEnv,
/// Part of the environment representing the current L2 block. Can be used to override storage slots
/// in the system context contract, which are set from `L1BatchEnv.first_l2_block` by default.
pub current_block: Option<StoredL2BlockEnv>,
}
Loading

0 comments on commit 951d5f2

Please sign in to comment.