Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
feat(execution): moved global max step limit to input validation
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <dori@starkware.co>
  • Loading branch information
dorimedini-starkware committed Feb 1, 2024
1 parent 7a5c141 commit ab20f67
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/blockifier/src/abi/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub const CONSUMED_MSG_TO_L2_ENCODED_DATA_SIZE: usize =
(L1_TO_L2_MSG_HEADER_SIZE + 1) - CONSUMED_MSG_TO_L2_N_TOPICS;

// Transaction resource names.
pub const MAX_VALIDATE_STEPS_PER_TX: usize = 1_000_000;
pub const MAX_STEPS_PER_TX: usize = 4_000_000;
pub const MAX_VALIDATE_STEPS_PER_TX: u32 = 1_000_000;
pub const MAX_STEPS_PER_TX: u32 = 4_000_000;
pub const L1_GAS_USAGE: &str = "l1_gas_usage";
pub const BLOB_GAS_USAGE: &str = "l1_blob_gas_usage";
pub const N_STEPS_RESOURCE: &str = "n_steps";
Expand Down
22 changes: 8 additions & 14 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,14 @@ impl EntryPointExecutionContext {
let block_upper_bound = match mode {
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion
// works.
ExecutionMode::Validate => min(
versioned_constants
.validate_max_n_steps
.try_into()
.expect("Failed to convert u32 to usize."),
constants::MAX_VALIDATE_STEPS_PER_TX,
),
ExecutionMode::Execute => min(
versioned_constants
.invoke_tx_max_n_steps
.try_into()
.expect("Failed to convert u32 to usize."),
constants::MAX_STEPS_PER_TX,
),
ExecutionMode::Validate => versioned_constants
.validate_max_n_steps
.try_into()
.expect("Failed to convert validate_max_n_steps (u32) to usize."),
ExecutionMode::Execute => versioned_constants
.invoke_tx_max_n_steps
.try_into()
.expect("Failed to convert invoke_tx_max_n_steps (u32) to usize."),
};

if !limit_steps_by_resources || !account_tx_context.enforce_fee()? {
Expand Down
4 changes: 4 additions & 0 deletions crates/native_blockifier/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ native_blockifier_errors!(

#[derive(Debug, Error)]
pub enum NativeBlockifierInputError {
#[error("Max steps per tx out of range: {0}.")]
MaxStepsPerTxOutOfRange(u32),
#[error("Max validate steps per tx out of range: {0}.")]
MaxValidateStepsPerTxOutOfRange(u32),
#[error(transparent)]
ParseError(#[from] ParseError),
#[error(transparent)]
Expand Down
16 changes: 14 additions & 2 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use blockifier::abi::constants::{MAX_STEPS_PER_TX, MAX_VALIDATE_STEPS_PER_TX};
use blockifier::block::{BlockInfo, GasPrices};
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::state::cached_state::{GlobalContractCache, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST};
Expand All @@ -10,7 +11,7 @@ use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress};
use starknet_api::hash::StarkFelt;

use crate::errors::{NativeBlockifierError, NativeBlockifierResult};
use crate::errors::{NativeBlockifierError, NativeBlockifierInputError, NativeBlockifierResult};
use crate::py_state_diff::{PyBlockInfo, PyStateDiff};
use crate::py_transaction_execution_info::PyBouncerInfo;
use crate::py_utils::{int_to_chain_id, py_attr, versioned_constants_with_overrides, PyFelt};
Expand Down Expand Up @@ -315,5 +316,16 @@ pub fn into_block_context(
use_kzg_da: block_info.use_kzg_da,
};

Ok(BlockContext::new_unchecked(&block_info, &chain_info, versioned_constants))
// Input validation.
if versioned_constants.invoke_tx_max_n_steps > MAX_STEPS_PER_TX {
Err(NativeBlockifierInputError::MaxStepsPerTxOutOfRange(
versioned_constants.invoke_tx_max_n_steps,
))?
} else if versioned_constants.validate_max_n_steps > MAX_VALIDATE_STEPS_PER_TX {
Err(NativeBlockifierInputError::MaxValidateStepsPerTxOutOfRange(
versioned_constants.validate_max_n_steps,
))?
} else {
Ok(BlockContext::new_unchecked(&block_info, &chain_info, versioned_constants))
}
}

0 comments on commit ab20f67

Please sign in to comment.