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

Commit

Permalink
feat(execution,concurrency): add ConcurrencyConfig (#1861)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored May 5, 2024
1 parent 454b18e commit 179b851
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/src/blockifier.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod block;
pub mod config;
pub mod stateful_validator;
pub mod transaction_executor;
3 changes: 2 additions & 1 deletion crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub fn pre_process_block(
block_info: BlockInfo,
chain_info: ChainInfo,
versioned_constants: VersionedConstants,
concurrency_mode: bool,
) -> StateResult<BlockContext> {
let should_block_hash_be_provided =
block_info.block_number >= BlockNumber(constants::STORED_BLOCK_HASH_BUFFER);
Expand All @@ -80,7 +81,7 @@ pub fn pre_process_block(
return Err(StateError::OldBlockHashNotProvided);
}

Ok(BlockContext { block_info, chain_info, versioned_constants, concurrency_mode: false })
Ok(BlockContext { block_info, chain_info, versioned_constants, concurrency_mode })
}

pub struct BlockNumberHashPair {
Expand Down
5 changes: 4 additions & 1 deletion crates/blockifier/src/blockifier/block_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn test_pre_process_block() {
block_info,
ChainInfo::default(),
VersionedConstants::default(),
false,
)
.unwrap();

Expand All @@ -48,7 +49,8 @@ fn test_pre_process_block() {
None,
block_info,
ChainInfo::default(),
VersionedConstants::default()
VersionedConstants::default(),
false,
)
.is_ok()
);
Expand All @@ -61,6 +63,7 @@ fn test_pre_process_block() {
block_info,
ChainInfo::default(),
VersionedConstants::default(),
false,
);
assert_eq!(
format!(
Expand Down
11 changes: 11 additions & 0 deletions crates/blockifier/src/blockifier/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(Debug, Default, Clone)]
pub struct TransactionExecutorConfig {
pub concurrency_config: ConcurrencyConfig,
}

#[derive(Debug, Default, Clone)]
pub struct ConcurrencyConfig {
pub enabled: bool,
pub n_workers: usize,
pub chunk_size: usize,
}
8 changes: 7 additions & 1 deletion crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use starknet_api::hash::StarkFelt;
use starknet_api::transaction::TransactionHash;
use thiserror::Error;

use crate::blockifier::config::TransactionExecutorConfig;
use crate::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError};
use crate::bouncer::BouncerConfig;
use crate::context::{BlockContext, TransactionContext};
Expand Down Expand Up @@ -48,7 +49,12 @@ impl<S: StateReader> StatefulValidator<S> {
max_nonce_for_validation_skip: Nonce,
bouncer_config: BouncerConfig,
) -> Self {
let tx_executor = TransactionExecutor::new(state, block_context, bouncer_config);
let tx_executor = TransactionExecutor::new(
state,
block_context,
bouncer_config,
TransactionExecutorConfig::default(),
);
Self { tx_executor, max_nonce_for_validation_skip }
}

Expand Down
19 changes: 18 additions & 1 deletion crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::ClassHash;
use thiserror::Error;

use crate::blockifier::config::TransactionExecutorConfig;
use crate::bouncer::{Bouncer, BouncerConfig};
use crate::context::BlockContext;
use crate::execution::call_info::CallInfo;
Expand Down Expand Up @@ -38,6 +39,8 @@ pub type VisitedSegmentsMapping = Vec<(ClassHash, Vec<usize>)>;
pub struct TransactionExecutor<S: StateReader> {
pub block_context: BlockContext,
pub bouncer: Bouncer,
// Note: this config must not affect the execution result (e.g. state diff and traces).
pub config: TransactionExecutorConfig,

// State-related fields.
pub state: CachedState<S>,
Expand All @@ -48,11 +51,13 @@ impl<S: StateReader> TransactionExecutor<S> {
state: CachedState<S>,
block_context: BlockContext,
bouncer_config: BouncerConfig,
config: TransactionExecutorConfig,
) -> Self {
log::debug!("Initializing Transaction Executor...");
// 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 = Self { block_context, bouncer: Bouncer::new(bouncer_config), state };
let tx_executor =
Self { block_context, bouncer: Bouncer::new(bouncer_config), config, state };
log::debug!("Initialized Transaction Executor.");

tx_executor
Expand Down Expand Up @@ -95,6 +100,18 @@ impl<S: StateReader> TransactionExecutor<S> {
&mut self,
txs: &[Transaction],
charge_fee: bool,
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
if !self.config.concurrency_config.enabled {
self.execute_chunk_sequentially(txs, charge_fee)
} else {
todo!()
}
}

pub fn execute_chunk_sequentially(
&mut self,
txs: &[Transaction],
charge_fee: bool,
) -> Vec<TransactionExecutorResult<TransactionExecutionInfo>> {
let mut results = Vec::new();
for tx in txs {
Expand Down
17 changes: 14 additions & 3 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use starknet_api::hash::StarkFelt;
use starknet_api::stark_felt;
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::context::BlockContext;
Expand Down Expand Up @@ -34,8 +35,12 @@ 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());
let mut tx_executor = TransactionExecutor::new(
state,
block_context,
BouncerConfig::create_for_testing(),
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 @@ -258,6 +263,7 @@ fn test_bouncing(
},
..BouncerConfig::default()
},
TransactionExecutorConfig::default(),
);
tx_executor.bouncer.set_accumulated_weights(initial_bouncer_weights);

Expand Down Expand Up @@ -288,7 +294,12 @@ fn test_execute_chunk_bouncing(block_context: BlockContext) {
},
..BouncerConfig::default()
};
let mut tx_executor = TransactionExecutor::new(state, block_context, bouncer_config.clone());
let mut tx_executor = TransactionExecutor::new(
state,
block_context,
bouncer_config.clone(),
TransactionExecutorConfig::default(),
);

let txs: Vec<Transaction> = [
emit_n_events_tx(1, account_address, contract_address, nonce!(0_u32)),
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
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use blockifier::blockifier::block::{
pre_process_block as pre_process_block_blockifier, BlockInfo, BlockNumberHashPair, GasPrices,
};
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError};
use blockifier::bouncer::BouncerConfig;
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
Expand Down Expand Up @@ -109,6 +110,7 @@ impl TypedTransactionExecutionInfo {
#[pyclass]
pub struct PyBlockExecutor {
pub bouncer_config: BouncerConfig,
pub tx_executor_config: TransactionExecutorConfig,
pub general_config: PyGeneralConfig,
pub versioned_constants: VersionedConstants,
pub tx_executor: Option<TransactionExecutor<PapyrusReader>>,
Expand Down Expand Up @@ -140,6 +142,7 @@ impl PyBlockExecutor {

Self {
bouncer_config: bouncer_config.into(),
tx_executor_config: TransactionExecutorConfig::default(),
general_config,
versioned_constants,
tx_executor: None,
Expand All @@ -165,10 +168,15 @@ impl PyBlockExecutor {
&self.general_config,
&next_block_info,
&self.versioned_constants,
self.tx_executor_config.concurrency_config.enabled,
)?;

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

Ok(())
Expand Down Expand Up @@ -377,6 +385,7 @@ impl PyBlockExecutor {
..BouncerWeights::max(true)
},
},
tx_executor_config: TransactionExecutorConfig::default(),
storage: Box::new(PapyrusStorage::new_for_testing(
path,
&general_config.starknet_os_config.chain_id,
Expand Down Expand Up @@ -409,6 +418,7 @@ impl PyBlockExecutor {
use blockifier::state::global_cache::GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST;
Self {
bouncer_config: BouncerConfig::max(),
tx_executor_config: TransactionExecutorConfig::default(),
storage: Box::new(storage),
general_config: PyGeneralConfig::default(),
versioned_constants: VersionedConstants::latest_constants().clone(),
Expand Down Expand Up @@ -518,6 +528,7 @@ fn pre_process_block(
general_config: &PyGeneralConfig,
block_info: &PyBlockInfo,
versioned_constants: &VersionedConstants,
concurrency_mode: bool,
) -> NativeBlockifierResult<BlockContext> {
let old_block_number_and_hash = old_block_number_and_hash
.map(|(block_number, block_hash)| BlockNumberHashPair::new(block_number, block_hash.0));
Expand All @@ -540,6 +551,7 @@ fn pre_process_block(
block_info,
chain_info,
versioned_constants.clone(),
concurrency_mode,
)?;

Ok(block_context)
Expand Down

0 comments on commit 179b851

Please sign in to comment.