Skip to content

Commit

Permalink
chore(blockifier): add pre_process_and_create c-tor for TransactionEx…
Browse files Browse the repository at this point in the history
…ecutor
  • Loading branch information
Yoni-Starkware committed Sep 26, 2024
1 parent f430bc8 commit 824e2ff
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 27 deletions.
30 changes: 22 additions & 8 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ use itertools::Itertools;
use starknet_api::core::ClassHash;
use thiserror::Error;

use crate::blockifier::block::{pre_process_block, BlockNumberHashPair};
use crate::blockifier::config::TransactionExecutorConfig;
use crate::bouncer::{Bouncer, BouncerWeights};
#[cfg(feature = "concurrency")]
use crate::concurrency::worker_logic::WorkerExecutor;
use crate::context::BlockContext;
use crate::state::cached_state::{CachedState, CommitmentStateDiff, TransactionalState};
use crate::state::errors::StateError;
use crate::state::state_api::StateReader;
use crate::state::state_api::{StateReader, StateResult};
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::TransactionExecutionInfo;
use crate::transaction::transaction_execution::Transaction;
Expand All @@ -44,7 +45,7 @@ pub enum TransactionExecutorError {
pub type TransactionExecutorResult<T> = Result<T, TransactionExecutorError>;
pub type VisitedSegmentsMapping = Vec<(ClassHash, Vec<usize>)>;

// TODO(Gilad): make this hold TransactionContext instead of BlockContext.
/// A transaction executor, used for building a single block.
pub struct TransactionExecutor<S: StateReader> {
pub block_context: BlockContext,
pub bouncer: Bouncer,
Expand All @@ -60,24 +61,37 @@ pub struct TransactionExecutor<S: StateReader> {
}

impl<S: StateReader> TransactionExecutor<S> {
/// Performs pre-processing required for block building before creating the executor.
pub fn pre_process_and_create(
initial_state_reader: S,
block_context: BlockContext,
old_block_number_and_hash: Option<BlockNumberHashPair>,
config: TransactionExecutorConfig,
) -> StateResult<Self> {
let mut block_state = CachedState::new(initial_state_reader);
pre_process_block(
&mut block_state,
old_block_number_and_hash,
block_context.block_info().block_number,
)?;
Ok(Self::new(block_state, block_context, config))
}

// TODO(Yoni): consider making this c-tor private.
pub fn new(
block_state: CachedState<S>,
block_context: BlockContext,
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 = Self {
Self {
block_context,
bouncer: Bouncer::new(bouncer_config),
config,
block_state: Some(block_state),
};
log::debug!("Initialized Transaction Executor.");

tx_executor
}
}

/// Executes the given transaction on the state maintained by the executor.
Expand Down
13 changes: 10 additions & 3 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet_api::transaction::{Fee, TransactionVersion};
use starknet_api::{declare_tx_args, deploy_account_tx_args, felt, invoke_tx_args, nonce};
use starknet_types_core::felt::Felt;

use crate::blockifier::block::BlockNumberHashPair;
use crate::blockifier::config::TransactionExecutorConfig;
use crate::blockifier::transaction_executor::{
TransactionExecutor,
Expand Down Expand Up @@ -34,15 +35,21 @@ use crate::transaction::test_utils::{
};
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::L1HandlerTransaction;

fn tx_executor_test_body<S: StateReader>(
state: CachedState<S>,
block_context: BlockContext,
tx: Transaction,
expected_bouncer_weights: BouncerWeights,
) {
let mut tx_executor =
TransactionExecutor::new(state, block_context, TransactionExecutorConfig::default());
let block_number_hash_pair =
BlockNumberHashPair::create_dummy_given_current(block_context.block_info().block_number);
let mut tx_executor = TransactionExecutor::pre_process_and_create(
state,
block_context,
block_number_hash_pair,
TransactionExecutorConfig::default(),
)
.unwrap();
// 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
1 change: 0 additions & 1 deletion crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::transaction::objects::{
};
use crate::versioned_constants::VersionedConstants;

/// Create via [`crate::blockifier::block::pre_process_block`] to ensure correctness.
#[derive(Clone, Debug)]
pub struct TransactionContext {
pub block_context: BlockContext,
Expand Down
18 changes: 16 additions & 2 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use std::sync::Arc;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde_json::Value;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::transaction::{Calldata, Fee, TransactionHash, TransactionVersion};
use starknet_api::{calldata, contract_address, felt, patricia_key};
use starknet_types_core::felt::Felt;

use super::update_json_value;
use crate::abi::abi_utils::selector_from_name;
use crate::blockifier::block::{BlockInfo, GasPrices};
use crate::abi::constants;
use crate::blockifier::block::{BlockInfo, BlockNumberHashPair, GasPrices};
use crate::bouncer::{BouncerConfig, BouncerWeights};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
Expand Down Expand Up @@ -139,6 +141,18 @@ impl GasCosts {
}
}

impl BlockNumberHashPair {
pub fn create_dummy_given_current(block_number: BlockNumber) -> Option<Self> {
if block_number.0 < constants::STORED_BLOCK_HASH_BUFFER {
return None;
}
Some(Self {
number: BlockNumber(block_number.0 - constants::STORED_BLOCK_HASH_BUFFER),
hash: BlockHash(StarkHash::ONE),
})
}
}

impl ChainInfo {
pub fn create_for_testing() -> Self {
Self {
Expand Down
19 changes: 6 additions & 13 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::collections::HashMap;

use blockifier::blockifier::block::pre_process_block;
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::{TransactionExecutor, TransactionExecutorError};
use blockifier::bouncer::BouncerConfig;
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::execution::call_info::CallInfo;
use blockifier::state::cached_state::CachedState;
use blockifier::state::global_cache::GlobalContractCache;
use blockifier::transaction::objects::{GasVector, ResourcesMapping, TransactionExecutionInfo};
use blockifier::transaction::transaction_execution::Transaction;
Expand Down Expand Up @@ -133,18 +131,13 @@ impl PyBlockExecutor {

// Create state reader.
let papyrus_reader = self.get_aligned_reader(next_block_number);
let mut state = CachedState::new(papyrus_reader);

pre_process_block(
&mut state,
// Create and set executor.
self.tx_executor = Some(TransactionExecutor::pre_process_and_create(
papyrus_reader,
block_context,
into_block_number_hash_pair(old_block_number_and_hash),
next_block_number,
)?;

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

self.tx_executor_config.clone(),
)?);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ fn execute_transactions(
_ => None,
};
let blockifier_tx = to_blockifier_tx(tx, tx_hash, transaction_index)?;
// TODO(Yoni): use the TransactionExecutor instead.
let tx_execution_info_result =
blockifier_tx.execute(&mut transactional_state, &block_context, charge_fee, validate);
let state_diff =
Expand Down

0 comments on commit 824e2ff

Please sign in to comment.