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(katana): rename simulation flags for consistency #2581

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/katana/executor/benches/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::Duration;
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkGroup, Criterion};
use katana_executor::implementation::blockifier::BlockifierFactory;
use katana_executor::{ExecutorFactory, SimulationFlag};
use katana_executor::{ExecutionFlags, ExecutorFactory};
use katana_primitives::env::{BlockEnv, CfgEnv};
use katana_primitives::transaction::ExecutableTxWithHash;
use katana_provider::test_utils;
Expand All @@ -28,7 +28,7 @@ fn concurrent(c: &mut Criterion) {
group.warm_up_time(Duration::from_millis(200));

let provider = test_utils::test_provider();
let flags = SimulationFlag::new().skip_validate();
let flags = ExecutionFlags::new().with_account_validation(false);

let tx = tx();
let envs = envs();
Expand All @@ -40,7 +40,7 @@ fn blockifier(
group: &mut BenchmarkGroup<'_, WallTime>,
concurrency_size: usize,
provider: impl StateFactoryProvider,
flags: SimulationFlag,
flags: ExecutionFlags,
(block_env, cfg_env): (BlockEnv, CfgEnv),
tx: ExecutableTxWithHash,
) {
Expand Down
6 changes: 3 additions & 3 deletions crates/katana/executor/benches/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use blockifier::state::cached_state::CachedState;
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkGroup, Criterion};
use katana_executor::{SimulationFlag, StateProviderDb};
use katana_executor::{ExecutionFlags, StateProviderDb};
use katana_primitives::env::{BlockEnv, CfgEnv};
use katana_primitives::transaction::ExecutableTxWithHash;
use katana_provider::test_utils;
Expand All @@ -19,7 +19,7 @@ fn executor_transact(c: &mut Criterion) {
group.warm_up_time(Duration::from_millis(200));

let provider = test_utils::test_provider();
let flags = SimulationFlag::new();
let flags = ExecutionFlags::new();

let tx = tx();
let envs = envs();
Expand All @@ -30,7 +30,7 @@ fn executor_transact(c: &mut Criterion) {
fn blockifier(
group: &mut BenchmarkGroup<'_, WallTime>,
provider: impl StateFactoryProvider,
execution_flags: &SimulationFlag,
execution_flags: &ExecutionFlags,
block_envs: &(BlockEnv, CfgEnv),
tx: ExecutableTxWithHash,
) {
Expand Down
10 changes: 5 additions & 5 deletions crates/katana/executor/src/abstraction/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use katana_primitives::Felt;
use katana_provider::traits::state::StateProvider;

use crate::{
EntryPointCall, ExecutionError, ExecutionOutput, ExecutionResult, ExecutorResult,
ResultAndStates, SimulationFlag,
EntryPointCall, ExecutionError, ExecutionFlags, ExecutionOutput, ExecutionResult,
ExecutorResult, ResultAndStates,
};

/// A type that can create [BlockExecutor] instance.
Expand All @@ -30,7 +30,7 @@ pub trait ExecutorFactory: Send + Sync + 'static + core::fmt::Debug {
fn cfg(&self) -> &CfgEnv;

/// Returns the execution flags set by the factory.
fn execution_flags(&self) -> &SimulationFlag;
fn execution_flags(&self) -> &ExecutionFlags;
}

/// An executor that can execute a block of transactions.
Expand Down Expand Up @@ -61,14 +61,14 @@ pub trait ExecutorExt {
fn simulate(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: SimulationFlag,
flags: ExecutionFlags,
) -> Vec<ResultAndStates>;

/// Get the fee estimation for the given transactions.
fn estimate_fee(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: SimulationFlag,
flags: ExecutionFlags,
) -> Vec<Result<TxFeeInfo, ExecutionError>>;

/// Perform a contract entry point call and return the output.
Expand Down
74 changes: 42 additions & 32 deletions crates/katana/executor/src/abstraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,59 @@ pub type ExecutorResult<T> = Result<T, error::ExecutorError>;
/// Transaction execution simulation flags.
///
/// These flags can be used to control the behavior of the transaction execution, such as skipping
/// the transaction execution or validation, or ignoring the maximum fee when validating the
/// transaction.
#[derive(Debug, Clone, Default)]
pub struct SimulationFlag {
/// Skip the transaction execution.
pub skip_execute: bool,
/// Skip the transaction validation.
pub skip_validate: bool,
/// Skip checking nonce when validating the transaction.
pub skip_nonce_check: bool,
/// Skip the fee transfer after the transaction execution.
pub skip_fee_transfer: bool,
/// Ignore the maximum fee when validating the transaction.
pub ignore_max_fee: bool,
/// the transaction validation, or ignoring any fee related checks.
#[derive(Debug, Clone)]
pub struct ExecutionFlags {
/// Determine whether to perform the transaction sender's account validation logic.
account_validation: bool,
/// Determine whether to perform fee related checks and operations ie., fee transfer.
fee: bool,
/// Determine whether to perform transaction's sender nonce check.
nonce_check: bool,
}

impl Default for ExecutionFlags {
fn default() -> Self {
Self { account_validation: true, fee: true, nonce_check: true }
}
}

impl SimulationFlag {
impl ExecutionFlags {
pub fn new() -> Self {
Self::default()
}

/// Enables the skip execution flag.
pub fn skip_execute(mut self) -> Self {
self.skip_execute = true;
/// Set whether to enable or disable the account validation.
pub fn with_account_validation(mut self, enable: bool) -> Self {
self.account_validation = enable;
self
}

/// Enables the skip validation flag.
pub fn skip_validate(mut self) -> Self {
self.skip_validate = true;
/// Set whether to enable or disable the fee related operations.
pub fn with_fee(mut self, enable: bool) -> Self {
self.fee = enable;
self
}

/// Enables the skip nonce check flag.
pub fn skip_nonce_check(mut self) -> Self {
self.skip_nonce_check = true;
/// Set whether to enable or disable the nonce check.
pub fn with_nonce_check(mut self, enable: bool) -> Self {
self.nonce_check = enable;
self
}

/// Enables the skip fee transfer flag.
pub fn skip_fee_transfer(mut self) -> Self {
self.skip_fee_transfer = true;
self
/// Returns whether the account validation is enabled.
pub fn account_validation(&self) -> bool {
self.account_validation
}

/// Enables the ignore max fee flag.
pub fn ignore_max_fee(mut self) -> Self {
self.ignore_max_fee = true;
self
/// Returns whether the fee related operations are enabled.
pub fn fee(&self) -> bool {
self.fee
}

/// Returns whether the nonce check is enabled.
pub fn nonce_check(&self) -> bool {
self.nonce_check
}
}

Expand Down Expand Up @@ -109,29 +113,35 @@ pub enum ExecutionResult {
}

impl ExecutionResult {
/// Creates a new successful execution result.
pub fn new_success(receipt: Receipt, trace: TxExecInfo) -> Self {
ExecutionResult::Success { receipt, trace }
}

/// Creates a new failed execution result with the given error.
pub fn new_failed(error: impl Into<ExecutionError>) -> Self {
ExecutionResult::Failed { error: error.into() }
}

/// Returns `true` if the execution was successful.
pub fn is_success(&self) -> bool {
matches!(self, ExecutionResult::Success { .. })
}

/// Returns `true` if the execution failed.
pub fn is_failed(&self) -> bool {
!self.is_success()
}

/// Returns the receipt of the execution if it was successful. Otherwise, returns `None`.
pub fn receipt(&self) -> Option<&Receipt> {
match self {
ExecutionResult::Success { receipt, .. } => Some(receipt),
_ => None,
}
}

/// Returns the execution info if it was successful. Otherwise, returns `None`.
pub fn trace(&self) -> Option<&TxExecInfo> {
match self {
ExecutionResult::Success { trace, .. } => Some(trace),
Expand Down
20 changes: 10 additions & 10 deletions crates/katana/executor/src/implementation/blockifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use tracing::info;

use self::state::CachedState;
use crate::{
BlockExecutor, EntryPointCall, ExecutionError, ExecutionOutput, ExecutionResult,
ExecutionStats, ExecutorExt, ExecutorFactory, ExecutorResult, ResultAndStates, SimulationFlag,
BlockExecutor, EntryPointCall, ExecutionError, ExecutionFlags, ExecutionOutput,
ExecutionResult, ExecutionStats, ExecutorExt, ExecutorFactory, ExecutorResult, ResultAndStates,
StateProviderDb,
};

Expand All @@ -32,12 +32,12 @@ pub(crate) const LOG_TARGET: &str = "katana::executor::blockifier";
#[derive(Debug)]
pub struct BlockifierFactory {
cfg: CfgEnv,
flags: SimulationFlag,
flags: ExecutionFlags,
}

impl BlockifierFactory {
/// Create a new factory with the given configuration and simulation flags.
pub fn new(cfg: CfgEnv, flags: SimulationFlag) -> Self {
pub fn new(cfg: CfgEnv, flags: ExecutionFlags) -> Self {
Self { cfg, flags }
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl ExecutorFactory for BlockifierFactory {
}

/// Returns the execution flags set by the factory.
fn execution_flags(&self) -> &SimulationFlag {
fn execution_flags(&self) -> &ExecutionFlags {
&self.flags
}
}
Expand All @@ -78,7 +78,7 @@ pub struct StarknetVMProcessor<'a> {
block_context: BlockContext,
state: CachedState<StateProviderDb<'a>>,
transactions: Vec<(TxWithHash, ExecutionResult)>,
simulation_flags: SimulationFlag,
simulation_flags: ExecutionFlags,
stats: ExecutionStats,
}

Expand All @@ -87,7 +87,7 @@ impl<'a> StarknetVMProcessor<'a> {
state: Box<dyn StateProvider + 'a>,
block_env: BlockEnv,
cfg_env: CfgEnv,
simulation_flags: SimulationFlag,
simulation_flags: ExecutionFlags,
) -> Self {
let transactions = Vec::new();
let block_context = utils::block_context_from_envs(&block_env, &cfg_env);
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<'a> StarknetVMProcessor<'a> {
fn simulate_with<F, T>(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: &SimulationFlag,
flags: &ExecutionFlags,
kariy marked this conversation as resolved.
Show resolved Hide resolved
mut op: F,
) -> Vec<T>
where
Expand Down Expand Up @@ -250,7 +250,7 @@ impl ExecutorExt for StarknetVMProcessor<'_> {
fn simulate(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: SimulationFlag,
flags: ExecutionFlags,
) -> Vec<ResultAndStates> {
self.simulate_with(transactions, &flags, |_, (_, result)| ResultAndStates {
result,
Expand All @@ -261,7 +261,7 @@ impl ExecutorExt for StarknetVMProcessor<'_> {
fn estimate_fee(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: SimulationFlag,
flags: ExecutionFlags,
) -> Vec<Result<TxFeeInfo, ExecutionError>> {
self.simulate_with(transactions, &flags, |_, (_, res)| match res {
ExecutionResult::Success { receipt, .. } => {
Expand Down
12 changes: 6 additions & 6 deletions crates/katana/executor/src/implementation/blockifier/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,30 @@ use katana_provider::traits::contract::ContractClassProvider;
use starknet::core::utils::parse_cairo_short_string;

use super::state::{CachedState, StateDb};
use crate::abstraction::{EntryPointCall, SimulationFlag};
use crate::abstraction::{EntryPointCall, ExecutionFlags};
use crate::utils::build_receipt;
use crate::{ExecutionError, ExecutionResult};

pub fn transact<S: StateReader>(
state: &mut cached_state::CachedState<S>,
block_context: &BlockContext,
simulation_flags: &SimulationFlag,
simulation_flags: &ExecutionFlags,
tx: ExecutableTxWithHash,
) -> ExecutionResult {
fn transact_inner<S: StateReader>(
state: &mut cached_state::CachedState<S>,
block_context: &BlockContext,
simulation_flags: &SimulationFlag,
simulation_flags: &ExecutionFlags,
tx: Transaction,
) -> Result<(TransactionExecutionInfo, TxFeeInfo), ExecutionError> {
let validate = !simulation_flags.skip_validate;
let charge_fee = !simulation_flags.skip_fee_transfer;
let validate = simulation_flags.account_validation();
let charge_fee = simulation_flags.fee();
// Blockifier doesn't provide a way to fully skip nonce check during the tx validation
// stage. The `nonce_check` flag in `tx.execute()` only 'relaxes' the check for
// nonce that is equal or higher than the current (expected) account nonce.
//
// Related commit on Blockifier: https://github.com/dojoengine/blockifier/commit/2410b6055453f247d48759f223c34b3fb5fa777
let nonce_check = !simulation_flags.skip_nonce_check;
let nonce_check = simulation_flags.nonce_check();

let fee_type = get_fee_type_from_tx(&tx);
let info = match tx {
Expand Down
12 changes: 6 additions & 6 deletions crates/katana/executor/src/implementation/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
use katana_provider::ProviderResult;

use crate::abstraction::{
BlockExecutor, EntryPointCall, ExecutionOutput, ExecutionResult, ExecutorExt, ExecutorFactory,
ExecutorResult, ResultAndStates, SimulationFlag,
BlockExecutor, EntryPointCall, ExecutionFlags, ExecutionOutput, ExecutionResult, ExecutorExt,
ExecutorFactory, ExecutorResult, ResultAndStates,
};
use crate::ExecutionError;

/// A no-op executor factory. Creates an executor that does nothing.
#[derive(Debug, Default)]
pub struct NoopExecutorFactory {
cfg: CfgEnv,
execution_flags: SimulationFlag,
execution_flags: ExecutionFlags,
}

impl NoopExecutorFactory {
Expand Down Expand Up @@ -55,7 +55,7 @@
&self.cfg
}

fn execution_flags(&self) -> &SimulationFlag {
fn execution_flags(&self) -> &ExecutionFlags {

Check warning on line 58 in crates/katana/executor/src/implementation/noop.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/executor/src/implementation/noop.rs#L58

Added line #L58 was not covered by tests
&self.execution_flags
}
}
Expand All @@ -69,7 +69,7 @@
fn simulate(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: SimulationFlag,
flags: ExecutionFlags,

Check warning on line 72 in crates/katana/executor/src/implementation/noop.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/executor/src/implementation/noop.rs#L72

Added line #L72 was not covered by tests
) -> Vec<ResultAndStates> {
let _ = transactions;
let _ = flags;
Expand All @@ -79,7 +79,7 @@
fn estimate_fee(
&self,
transactions: Vec<ExecutableTxWithHash>,
flags: SimulationFlag,
flags: ExecutionFlags,

Check warning on line 82 in crates/katana/executor/src/implementation/noop.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/executor/src/implementation/noop.rs#L82

Added line #L82 was not covered by tests
) -> Vec<Result<TxFeeInfo, ExecutionError>> {
let _ = transactions;
let _ = flags;
Expand Down
Loading
Loading