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

refactor(api): Extract oneshot VM executor to executor crate #2806

Merged
merged 13 commits into from
Sep 10, 2024
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