Skip to content

Commit

Permalink
refactor(api): Extract oneshot VM executor to executor crate (#2806)
Browse files Browse the repository at this point in the history
## What ❔

Extracts oneshot VM executor to the executor crate.

## Why ❔

To make executor logic more reusable and maintainable.

## 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 authored Sep 10, 2024
1 parent fe08677 commit 6009499
Show file tree
Hide file tree
Showing 41 changed files with 981 additions and 825 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

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

9 changes: 2 additions & 7 deletions core/lib/multivm/src/tracers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
pub use self::{
call_tracer::CallTracer,
multivm_dispatcher::TracerDispatcher,
prestate_tracer::PrestateTracer,
storage_invocation::StorageInvocations,
validator::{
ValidationError, ValidationTracer, ValidationTracerParams, ViolatedValidationRule,
},
call_tracer::CallTracer, multivm_dispatcher::TracerDispatcher, prestate_tracer::PrestateTracer,
storage_invocation::StorageInvocations, validator::ValidationTracer,
};

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

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

mod types;
Expand Down Expand Up @@ -50,7 +52,7 @@ type ValidationRoundResult = Result<NewTrustedValidationItems, ViolatedValidatio

impl<H> ValidationTracer<H> {
pub fn new(
params: ValidationTracerParams,
params: ValidationParams,
vm_version: VmVersion,
) -> (Self, Arc<OnceCell<ViolatedValidationRule>>) {
let result = Arc::new(OnceCell::new());
Expand Down Expand Up @@ -179,8 +181,8 @@ impl<H> ValidationTracer<H> {
}
}

pub fn params(&self) -> ValidationTracerParams {
ValidationTracerParams {
pub fn params(&self) -> ValidationParams {
ValidationParams {
user_address: self.user_address,
paymaster_address: self.paymaster_address,
trusted_slots: self.trusted_slots.clone(),
Expand Down
76 changes: 1 addition & 75 deletions core/lib/multivm/src/tracers/validator/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{collections::HashSet, fmt, fmt::Display};

use zksync_types::{Address, H256, U256};
use zksync_utils::u256_to_h256;

use crate::interface::Halt;
use zksync_types::{Address, H256};

#[derive(Debug, Clone, Eq, PartialEq, Copy)]
#[allow(clippy::enum_variant_names)]
Expand All @@ -21,72 +16,3 @@ pub(super) struct NewTrustedValidationItems {
pub(super) new_allowed_slots: Vec<H256>,
pub(super) new_trusted_addresses: Vec<Address>,
}

#[derive(Debug, Clone)]
pub struct ValidationTracerParams {
pub user_address: Address,
pub paymaster_address: Address,
/// Slots that are trusted (i.e. the user can access them).
pub trusted_slots: HashSet<(Address, U256)>,
/// Trusted addresses (the user can access any slots on these addresses).
pub trusted_addresses: HashSet<Address>,
/// Slots, that are trusted and the value of them is the new trusted address.
/// They are needed to work correctly with beacon proxy, where the address of the implementation is
/// stored in the beacon.
pub trusted_address_slots: HashSet<(Address, U256)>,
/// Number of computational gas that validation step is allowed to use.
pub computational_gas_limit: u32,
}

#[derive(Debug, Clone)]
pub enum ViolatedValidationRule {
TouchedUnallowedStorageSlots(Address, U256),
CalledContractWithNoCode(Address),
TouchedUnallowedContext,
TookTooManyComputationalGas(u32),
}

impl fmt::Display for ViolatedValidationRule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ViolatedValidationRule::TouchedUnallowedStorageSlots(contract, key) => write!(
f,
"Touched unallowed storage slots: address {}, key: {}",
hex::encode(contract),
hex::encode(u256_to_h256(*key))
),
ViolatedValidationRule::CalledContractWithNoCode(contract) => {
write!(f, "Called contract with no code: {}", hex::encode(contract))
}
ViolatedValidationRule::TouchedUnallowedContext => {
write!(f, "Touched unallowed context")
}
ViolatedValidationRule::TookTooManyComputationalGas(gas_limit) => {
write!(
f,
"Took too many computational gas, allowed limit: {}",
gas_limit
)
}
}
}
}

#[derive(Debug, Clone)]
pub enum ValidationError {
FailedTx(Halt),
ViolatedRule(ViolatedValidationRule),
}

impl Display for ValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FailedTx(revert_reason) => {
write!(f, "Validation revert: {}", revert_reason)
}
Self::ViolatedRule(rule) => {
write!(f, "Violated validation rules: {}", rule)
}
}
}
}
8 changes: 4 additions & 4 deletions core/lib/multivm/src/tracers/validator/vm_1_4_1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::{
interface::{
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
tracer::{TracerExecutionStatus, TracerExecutionStopReason, ViolatedValidationRule},
Halt,
},
tracers::{
dynamic::vm_1_4_1::DynTracer,
validator::{
types::{NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule},
types::{NewTrustedValidationItems, ValidationTracerMode},
ValidationRoundResult, ValidationTracer,
},
},
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
Opcode::Context(context) => {
match context {
ContextOpcode::Meta => {
return Err(ViolatedValidationRule::TouchedUnallowedContext);
return Err(ViolatedValidationRule::TouchedDisallowedContext);
}
ContextOpcode::ErgsLeft => {
// TODO (SMA-1168): implement the correct restrictions for the gas left opcode.
Expand All @@ -102,7 +102,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
let msg_sender = state.vm_local_state.callstack.current.msg_sender;

if !self.is_allowed_storage_read(storage.clone(), this_address, key, msg_sender) {
return Err(ViolatedValidationRule::TouchedUnallowedStorageSlots(
return Err(ViolatedValidationRule::TouchedDisallowedStorageSlots(
this_address,
key,
));
Expand Down
8 changes: 4 additions & 4 deletions core/lib/multivm/src/tracers/validator/vm_1_4_2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::{
interface::{
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
tracer::{TracerExecutionStatus, TracerExecutionStopReason, ViolatedValidationRule},
Halt,
},
tracers::{
dynamic::vm_1_4_1::DynTracer,
validator::{
types::{NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule},
types::{NewTrustedValidationItems, ValidationTracerMode},
ValidationRoundResult, ValidationTracer,
},
},
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
Opcode::Context(context) => {
match context {
ContextOpcode::Meta => {
return Err(ViolatedValidationRule::TouchedUnallowedContext);
return Err(ViolatedValidationRule::TouchedDisallowedContext);
}
ContextOpcode::ErgsLeft => {
// TODO (SMA-1168): implement the correct restrictions for the gas left opcode.
Expand All @@ -102,7 +102,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
let msg_sender = state.vm_local_state.callstack.current.msg_sender;

if !self.is_allowed_storage_read(storage.clone(), this_address, key, msg_sender) {
return Err(ViolatedValidationRule::TouchedUnallowedStorageSlots(
return Err(ViolatedValidationRule::TouchedDisallowedStorageSlots(
this_address,
key,
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::{
interface::{
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
tracer::{TracerExecutionStatus, TracerExecutionStopReason, ViolatedValidationRule},
Halt,
},
tracers::{
dynamic::vm_1_4_0::DynTracer,
validator::{
types::{NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule},
types::{NewTrustedValidationItems, ValidationTracerMode},
ValidationRoundResult, ValidationTracer,
},
},
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
Opcode::Context(context) => {
match context {
ContextOpcode::Meta => {
return Err(ViolatedValidationRule::TouchedUnallowedContext);
return Err(ViolatedValidationRule::TouchedDisallowedContext);
}
ContextOpcode::ErgsLeft => {
// TODO (SMA-1168): implement the correct restrictions for the gas left opcode.
Expand All @@ -102,7 +102,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
let msg_sender = state.vm_local_state.callstack.current.msg_sender;

if !self.is_allowed_storage_read(storage.clone(), this_address, key, msg_sender) {
return Err(ViolatedValidationRule::TouchedUnallowedStorageSlots(
return Err(ViolatedValidationRule::TouchedDisallowedStorageSlots(
this_address,
key,
));
Expand Down
8 changes: 4 additions & 4 deletions core/lib/multivm/src/tracers/validator/vm_latest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::{
interface::{
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
tracer::{TracerExecutionStatus, TracerExecutionStopReason, ViolatedValidationRule},
Halt,
},
tracers::{
dynamic::vm_1_5_0::DynTracer,
validator::{
types::{NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule},
types::{NewTrustedValidationItems, ValidationTracerMode},
ValidationRoundResult, ValidationTracer,
},
},
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
Opcode::Context(context) => {
match context {
ContextOpcode::Meta => {
return Err(ViolatedValidationRule::TouchedUnallowedContext);
return Err(ViolatedValidationRule::TouchedDisallowedContext);
}
ContextOpcode::ErgsLeft => {
// TODO (SMA-1168): implement the correct restrictions for the gas left opcode.
Expand All @@ -100,7 +100,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
let msg_sender = state.vm_local_state.callstack.current.msg_sender;

if !self.is_allowed_storage_read(storage.clone(), this_address, key, msg_sender) {
return Err(ViolatedValidationRule::TouchedUnallowedStorageSlots(
return Err(ViolatedValidationRule::TouchedDisallowedStorageSlots(
this_address,
key,
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::{
interface::{
storage::{StoragePtr, WriteStorage},
tracer::{TracerExecutionStatus, TracerExecutionStopReason},
tracer::{TracerExecutionStatus, TracerExecutionStopReason, ViolatedValidationRule},
Halt,
},
tracers::{
dynamic::vm_1_3_3::DynTracer,
validator::{
types::{NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule},
types::{NewTrustedValidationItems, ValidationTracerMode},
ValidationRoundResult, ValidationTracer,
},
},
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
Opcode::Context(context) => {
match context {
ContextOpcode::Meta => {
return Err(ViolatedValidationRule::TouchedUnallowedContext);
return Err(ViolatedValidationRule::TouchedDisallowedContext);
}
ContextOpcode::ErgsLeft => {
// TODO (SMA-1168): implement the correct restrictions for the gas left opcode.
Expand All @@ -102,7 +102,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
let msg_sender = state.vm_local_state.callstack.current.msg_sender;

if !self.is_allowed_storage_read(storage.clone(), this_address, key, msg_sender) {
return Err(ViolatedValidationRule::TouchedUnallowedStorageSlots(
return Err(ViolatedValidationRule::TouchedDisallowedStorageSlots(
this_address,
key,
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use zksync_utils::{h256_to_account_address, u256_to_account_address, u256_to_h25
use crate::{
interface::{
storage::{StoragePtr, WriteStorage},
tracer::ViolatedValidationRule,
VmExecutionResultAndLogs,
},
tracers::{
dynamic::vm_1_3_3::DynTracer,
validator::{
types::{NewTrustedValidationItems, ValidationTracerMode, ViolatedValidationRule},
types::{NewTrustedValidationItems, ValidationTracerMode},
ValidationRoundResult, ValidationTracer,
},
},
Expand Down Expand Up @@ -87,7 +88,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
Opcode::Context(context) => {
match context {
ContextOpcode::Meta => {
return Err(ViolatedValidationRule::TouchedUnallowedContext);
return Err(ViolatedValidationRule::TouchedDisallowedContext);
}
ContextOpcode::ErgsLeft => {
// TODO (SMA-1168): implement the correct restrictions for the gas left opcode.
Expand All @@ -101,7 +102,7 @@ impl<H: HistoryMode> ValidationTracer<H> {
let msg_sender = state.vm_local_state.callstack.current.msg_sender;

if !self.is_allowed_storage_read(storage.clone(), this_address, key, msg_sender) {
return Err(ViolatedValidationRule::TouchedUnallowedStorageSlots(
return Err(ViolatedValidationRule::TouchedDisallowedStorageSlots(
this_address,
key,
));
Expand Down
1 change: 1 addition & 0 deletions core/lib/vm_executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ zksync_contracts.workspace = true
zksync_dal.workspace = true
zksync_types.workspace = true
zksync_multivm.workspace = true
zksync_utils.workspace = true

async-trait.workspace = true
once_cell.workspace = true
Expand Down
Loading

0 comments on commit 6009499

Please sign in to comment.