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

Commit

Permalink
refactor: add BouncerConfig to BlockContext (#1956)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored Jun 5, 2024
1 parent faa3e44 commit 63cb2a8
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 112 deletions.
4 changes: 1 addition & 3 deletions crates/blockifier/bench/blockifier_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use blockifier::abi::abi_utils::selector_from_name;
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::TransactionExecutor;
use blockifier::bouncer::BouncerConfig;
use blockifier::context::{BlockContext, ChainInfo};
use blockifier::invoke_tx_args;
use blockifier::test_utils::contracts::FeatureContract;
Expand Down Expand Up @@ -61,8 +60,7 @@ impl TransfersGenerator {
let state = test_state(&chain_info, BALANCE * 1000, &[(account_contract, N_ACCOUNTS)]);
// TODO(Avi, 20/05/2024): Enable concurrency.
let executor_config = TransactionExecutorConfig::default();
let executor =
TransactionExecutor::new(state, block_context, BouncerConfig::max(), executor_config);
let executor = TransactionExecutor::new(state, block_context, executor_config);
let account_addresses = (0..N_ACCOUNTS)
.map(|instance_id| account_contract.get_instance_address(instance_id))
.collect::<Vec<_>>();
Expand Down
10 changes: 9 additions & 1 deletion crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::bouncer::BouncerConfig;
use crate::context::{BlockContext, ChainInfo};
use crate::state::errors::StateError;
use crate::state::state_api::{State, StateResult};
Expand Down Expand Up @@ -62,6 +63,7 @@ pub fn pre_process_block(
block_info: BlockInfo,
chain_info: ChainInfo,
versioned_constants: VersionedConstants,
bouncer_config: BouncerConfig,
concurrency_mode: bool,
) -> StateResult<BlockContext> {
let should_block_hash_be_provided =
Expand All @@ -81,7 +83,13 @@ pub fn pre_process_block(
return Err(StateError::OldBlockHashNotProvided);
}

Ok(BlockContext { block_info, chain_info, versioned_constants, concurrency_mode })
Ok(BlockContext {
block_info,
chain_info,
versioned_constants,
bouncer_config,
concurrency_mode,
})
}

pub struct BlockNumberHashPair {
Expand Down
4 changes: 4 additions & 0 deletions crates/blockifier/src/blockifier/block_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use starknet_api::state::StorageKey;

use crate::abi::constants;
use crate::blockifier::block::{pre_process_block, BlockInfo, BlockNumberHashPair};
use crate::bouncer::BouncerConfig;
use crate::context::ChainInfo;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
Expand All @@ -29,6 +30,7 @@ fn test_pre_process_block() {
block_info,
ChainInfo::default(),
VersionedConstants::default(),
BouncerConfig::max(),
false,
)
.unwrap();
Expand All @@ -50,6 +52,7 @@ fn test_pre_process_block() {
block_info,
ChainInfo::default(),
VersionedConstants::default(),
BouncerConfig::max(),
false,
)
.is_ok()
Expand All @@ -63,6 +66,7 @@ fn test_pre_process_block() {
block_info,
ChainInfo::default(),
VersionedConstants::default(),
BouncerConfig::max(),
false,
);
assert_eq!(
Expand Down
10 changes: 2 additions & 8 deletions crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use thiserror::Error;

use crate::blockifier::config::TransactionExecutorConfig;
use crate::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError};
use crate::bouncer::BouncerConfig;
use crate::context::{BlockContext, TransactionContext};
use crate::execution::call_info::CallInfo;
use crate::fee::actual_cost::TransactionReceipt;
Expand Down Expand Up @@ -47,14 +46,9 @@ impl<S: StateReader> StatefulValidator<S> {
state: CachedState<S>,
block_context: BlockContext,
max_nonce_for_validation_skip: Nonce,
bouncer_config: BouncerConfig,
) -> Self {
let tx_executor = TransactionExecutor::new(
state,
block_context,
bouncer_config,
TransactionExecutorConfig::default(),
);
let tx_executor =
TransactionExecutor::new(state, block_context, TransactionExecutorConfig::default());
Self { tx_executor, max_nonce_for_validation_skip }
}

Expand Down
8 changes: 1 addition & 7 deletions crates/blockifier/src/blockifier/stateful_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rstest::rstest;
use starknet_api::transaction::{Fee, TransactionVersion};

use crate::blockifier::stateful_validator::StatefulValidator;
use crate::bouncer::BouncerConfig;
use crate::context::BlockContext;
use crate::nonce;
use crate::test_utils::contracts::FeatureContract;
Expand Down Expand Up @@ -65,12 +64,7 @@ fn test_transaction_validator(
}

// Test the stateful validator.
let mut stateful_validator = StatefulValidator::create(
state,
block_context,
nonce!(0_u32),
BouncerConfig::create_for_testing(),
);
let mut stateful_validator = StatefulValidator::create(state, block_context, nonce!(0_u32));

let reuslt = stateful_validator.perform_validations(tx, None);
assert!(reuslt.is_ok(), "Validation failed: {:?}", reuslt.unwrap_err());
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::core::ClassHash;
use thiserror::Error;

use crate::blockifier::config::TransactionExecutorConfig;
use crate::bouncer::{Bouncer, BouncerConfig, BouncerWeights};
use crate::bouncer::{Bouncer, BouncerWeights};
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::fee::actual_cost::TransactionReceipt;
Expand Down Expand Up @@ -52,10 +52,10 @@ impl<S: StateReader> TransactionExecutor<S> {
pub fn new(
state: CachedState<S>,
block_context: BlockContext,
bouncer_config: BouncerConfig,
config: TransactionExecutorConfig,
) -> Self {
log::debug!("Initializing Transaction Executor...");
let bouncer_config = block_context.bouncer_config.clone();
// Note: the state might not be empty even at this point; it is the creator's
// responsibility to tune the bouncer according to pre and post block process.
let tx_executor =
Expand Down
56 changes: 16 additions & 40 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::transaction::{Fee, TransactionVersion};

use crate::blockifier::config::TransactionExecutorConfig;
use crate::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError};
use crate::bouncer::{Bouncer, BouncerConfig, BouncerWeights};
use crate::bouncer::{Bouncer, BouncerWeights};
use crate::context::BlockContext;
use crate::state::cached_state::CachedState;
use crate::state::state_api::StateReader;
Expand Down Expand Up @@ -35,12 +35,8 @@ fn tx_executor_test_body<S: StateReader>(
charge_fee: bool,
expected_bouncer_weights: BouncerWeights,
) {
let mut tx_executor = TransactionExecutor::new(
state,
block_context,
BouncerConfig::create_for_testing(),
TransactionExecutorConfig::default(),
);
let mut tx_executor =
TransactionExecutor::new(state, block_context, TransactionExecutorConfig::default());
// TODO(Arni, 30/03/2024): Consider adding a test for the transaction execution info. If A test
// should not be added, rename the test to `test_bouncer_info`.
// TODO(Arni, 30/03/2024): Test all bouncer weights.
Expand Down Expand Up @@ -245,27 +241,16 @@ fn test_l1_handler(block_context: BlockContext, #[values(true, false)] charge_fe
exceeds the maximum block capacity.")]
#[case::transaction_too_large(BouncerWeights::default(), 11)]

fn test_bouncing(
block_context: BlockContext,
#[case] initial_bouncer_weights: BouncerWeights,
#[case] n_events: usize,
) {
fn test_bouncing(#[case] initial_bouncer_weights: BouncerWeights, #[case] n_events: usize) {
let max_n_events_in_block = 10;
let block_context = BlockContext::create_for_bouncer_testing(max_n_events_in_block);

let TestInitData { state, account_address, contract_address, mut nonce_manager } =
create_test_init_data(&block_context.chain_info, CairoVersion::Cairo1);

let mut tx_executor = TransactionExecutor::new(
state,
block_context,
BouncerConfig {
block_max_capacity: BouncerWeights {
n_events: max_n_events_in_block,
..BouncerWeights::max(false)
},
..BouncerConfig::default()
},
TransactionExecutorConfig::default(),
);
let mut tx_executor =
TransactionExecutor::new(state, block_context, TransactionExecutorConfig::default());

tx_executor.bouncer.set_accumulated_weights(initial_bouncer_weights);

tx_executor
Expand All @@ -283,24 +268,15 @@ fn test_bouncing(
}

#[rstest]
fn test_execute_txs_bouncing(block_context: BlockContext) {
fn test_execute_txs_bouncing() {
let max_n_events_in_block = 10;
let block_context = BlockContext::create_for_bouncer_testing(max_n_events_in_block);

let TestInitData { state, account_address, contract_address, .. } =
create_test_init_data(&block_context.chain_info, CairoVersion::Cairo1);

let max_n_events_in_block = 10;
let bouncer_config = BouncerConfig {
block_max_capacity: BouncerWeights {
n_events: max_n_events_in_block,
..BouncerWeights::max(false)
},
..BouncerConfig::default()
};
let mut tx_executor = TransactionExecutor::new(
state,
block_context,
bouncer_config.clone(),
TransactionExecutorConfig::default(),
);
let mut tx_executor =
TransactionExecutor::new(state, block_context, TransactionExecutorConfig::default());

let txs: Vec<Transaction> = [
emit_n_events_tx(1, account_address, contract_address, nonce!(0_u32)),
Expand Down Expand Up @@ -346,7 +322,7 @@ fn test_execute_txs_bouncing(block_context: BlockContext) {
assert_eq!(remaining_tx_results.len(), 0);

// Reset the bouncer and add the remaining transactions.
tx_executor.bouncer = Bouncer::new(bouncer_config);
tx_executor.bouncer = Bouncer::new(tx_executor.block_context.bouncer_config.clone());
let remaining_tx_results = tx_executor.execute_txs(remaining_txs, true);

assert_eq!(remaining_tx_results.len(), 2);
Expand Down
3 changes: 3 additions & 0 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use starknet_api::core::{ChainId, ContractAddress};

use crate::blockifier::block::BlockInfo;
use crate::bouncer::BouncerConfig;
use crate::transaction::objects::{
FeeType, HasRelatedFeeType, TransactionInfo, TransactionInfoCreator,
};
Expand All @@ -24,6 +25,7 @@ pub struct BlockContext {
pub(crate) block_info: BlockInfo,
pub(crate) chain_info: ChainInfo,
pub(crate) versioned_constants: VersionedConstants,
pub(crate) bouncer_config: BouncerConfig,
pub(crate) concurrency_mode: bool,
}

Expand All @@ -40,6 +42,7 @@ impl BlockContext {
block_info: block_info.clone(),
chain_info: chain_info.clone(),
versioned_constants: versioned_constants.clone(),
bouncer_config: BouncerConfig::max(),
concurrency_mode: false,
}
}
Expand Down
54 changes: 16 additions & 38 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet_api::{contract_address, patricia_key};

use super::update_json_value;
use crate::blockifier::block::{BlockInfo, GasPrices};
use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount};
use crate::bouncer::{BouncerConfig, BouncerWeights};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{ContractClassV0, ContractClassV1};
Expand Down Expand Up @@ -142,6 +142,7 @@ impl BlockContext {
block_info: BlockInfo::create_for_testing(),
chain_info: ChainInfo::create_for_testing(),
versioned_constants: VersionedConstants::create_for_testing(),
bouncer_config: BouncerConfig::max(),
concurrency_mode: false,
}
}
Expand All @@ -151,10 +152,24 @@ impl BlockContext {
block_info: BlockInfo::create_for_testing(),
chain_info: ChainInfo::create_for_testing(),
versioned_constants: VersionedConstants::create_for_account_testing(),
bouncer_config: BouncerConfig::max(),
concurrency_mode: false,
}
}

pub fn create_for_bouncer_testing(max_n_events_in_block: usize) -> Self {
Self {
bouncer_config: BouncerConfig {
block_max_capacity: BouncerWeights {
n_events: max_n_events_in_block,
..BouncerWeights::max(false)
},
..BouncerConfig::default()
},
..Self::create_for_testing()
}
}

pub fn create_for_account_testing_with_kzg(use_kzg_da: bool) -> Self {
Self {
block_info: BlockInfo::create_for_testing_with_kzg(use_kzg_da),
Expand Down Expand Up @@ -188,40 +203,3 @@ impl ContractClassV1 {
Self::try_from_json_string(&raw_contract_class).unwrap()
}
}

impl BouncerConfig {
pub fn create_for_testing() -> Self {
Self {
block_max_capacity_with_keccak: BouncerWeights::create_for_testing(true),
block_max_capacity: BouncerWeights::create_for_testing(false),
}
}
}

impl BouncerWeights {
pub fn create_for_testing(with_keccak: bool) -> Self {
Self {
gas: 2500000,
n_steps: 2500000,
message_segment_length: 3750,
state_diff_size: 20000,
n_events: 10000,
builtin_count: BuiltinCount::create_for_testing(with_keccak),
}
}
}

impl BuiltinCount {
pub fn create_for_testing(with_keccak: bool) -> Self {
let keccak = if with_keccak { 1220 } else { 0 };
Self {
bitwise: 39062,
ecdsa: 1220,
ec_op: 2441,
keccak,
pedersen: 78125,
poseidon: 78125,
range_check: 156250,
}
}
}
11 changes: 5 additions & 6 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,12 @@ impl PyBlockExecutor {
&self.general_config,
&next_block_info,
&self.versioned_constants,
self.bouncer_config.clone(),
self.tx_executor_config.concurrency_config.enabled,
)?;

let tx_executor = TransactionExecutor::new(
state,
block_context,
self.bouncer_config.clone(),
self.tx_executor_config.clone(),
);
let tx_executor =
TransactionExecutor::new(state, block_context, self.tx_executor_config.clone());
self.tx_executor = Some(tx_executor);

Ok(())
Expand Down Expand Up @@ -540,6 +537,7 @@ fn pre_process_block(
general_config: &PyGeneralConfig,
block_info: &PyBlockInfo,
versioned_constants: &VersionedConstants,
bouncer_config: BouncerConfig,
concurrency_mode: bool,
) -> NativeBlockifierResult<BlockContext> {
let old_block_number_and_hash = old_block_number_and_hash
Expand All @@ -563,6 +561,7 @@ fn pre_process_block(
block_info,
chain_info,
versioned_constants.clone(),
bouncer_config,
concurrency_mode,
)?;

Expand Down
Loading

0 comments on commit 63cb2a8

Please sign in to comment.