Skip to content

Commit

Permalink
chore(blockifier): ues account_transaction::ExecutionFlags instead of…
Browse files Browse the repository at this point in the history
… transaction::ExecutionFlags
  • Loading branch information
avivg-starkware committed Dec 3, 2024
1 parent 669e72b commit 5bf81e9
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 109 deletions.
6 changes: 2 additions & 4 deletions crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::state::cached_state::{CachedState, CommitmentStateDiff, Transactional
use crate::state::errors::StateError;
use crate::state::state_api::{StateReader, StateResult};
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::{TransactionExecutionInfo, TransactionInfoCreator};
use crate::transaction::objects::TransactionExecutionInfo;
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::{ExecutableTransaction, ExecutionFlags};

Expand Down Expand Up @@ -100,11 +100,9 @@ impl<S: StateReader> TransactionExecutor<S> {
let mut transactional_state = TransactionalState::create_transactional(
self.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR),
);
let tx_charge_fee = tx.create_tx_info().enforce_fee();

// Executing a single transaction cannot be done in a concurrent mode.
let execution_flags =
ExecutionFlags { charge_fee: tx_charge_fee, validate: true, concurrency_mode: false };
let execution_flags = ExecutionFlags { concurrency_mode: false };
let tx_execution_result =
tx.execute_raw(&mut transactional_state, &self.block_context, execution_flags);
match tx_execution_result {
Expand Down
24 changes: 17 additions & 7 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::declare::declare_tx;
use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::invoke::invoke_tx;
use crate::test_utils::l1_handler::l1handler_tx;
use crate::test_utils::{
create_calldata,
Expand All @@ -32,7 +33,6 @@ use crate::test_utils::{
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::test_utils::{
account_invoke_tx,
block_context,
calculate_class_info_for_testing,
create_test_init_data,
Expand All @@ -41,6 +41,7 @@ use crate::transaction::test_utils::{
TestInitData,
};
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::enforce_fee;
fn tx_executor_test_body<S: StateReader>(
state: CachedState<S>,
block_context: BlockContext,
Expand Down Expand Up @@ -131,7 +132,9 @@ fn test_declare(
},
calculate_class_info_for_testing(declared_contract.get_class()),
);
let execution_flags = ExecutionFlags::default();
let only_query = false;
let charge_fee = enforce_fee(&declare_tx, only_query);
let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() };
let tx = AccountTransaction { tx: declare_tx, execution_flags }.into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
}
Expand All @@ -153,13 +156,17 @@ fn test_deploy_account(
},
&mut NonceManager::default(),
);
let tx = AccountTransaction { tx: deploy_account_tx, execution_flags: ExecutionFlags::default() }.into();
let only_query = false;
let charge_fee = enforce_fee(&deploy_account_tx, only_query);
let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() };
let tx = AccountTransaction { tx: deploy_account_tx, execution_flags }.into();
let expected_bouncer_weights = BouncerWeights {
state_diff_size: 3,
message_segment_length: 0,
n_events: 0,
..BouncerWeights::empty()
};
}
.into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
}

Expand Down Expand Up @@ -219,12 +226,15 @@ fn test_invoke(

let calldata =
create_calldata(test_contract.get_instance_address(0), entry_point_name, &entry_point_args);
let tx = account_invoke_tx(invoke_tx_args! {
let invoke_tx = invoke_tx(invoke_tx_args! {
sender_address: account_contract.get_instance_address(0),
calldata,
version,
})
.into();
});
let only_query = false;
let charge_fee = enforce_fee(&invoke_tx, only_query);
let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() };
let tx = AccountTransaction { tx: invoke_tx, execution_flags }.into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/concurrency/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn create_fee_transfer_call_info<S: StateReader>(
) -> CallInfo {
let block_context = BlockContext::create_for_account_testing();
let mut transactional_state = TransactionalState::create_transactional(state);
let execution_flags = ExecutionFlags { charge_fee: true, validate: true, concurrency_mode };
let execution_flags = ExecutionFlags { concurrency_mode };
let execution_info =
account_tx.execute_raw(&mut transactional_state, &block_context, execution_flags).unwrap();

Expand Down
10 changes: 2 additions & 8 deletions crates/blockifier/src/concurrency/worker_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ use crate::concurrency::TxIndex;
use crate::context::BlockContext;
use crate::state::cached_state::{ContractClassMapping, StateMaps, TransactionalState};
use crate::state::state_api::{StateReader, UpdatableState};
use crate::transaction::objects::{
TransactionExecutionInfo,
TransactionExecutionResult,
TransactionInfoCreator,
};
use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult};
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::{ExecutableTransaction, ExecutionFlags};

Expand Down Expand Up @@ -127,11 +123,9 @@ impl<'a, S: StateReader> WorkerExecutor<'a, S> {
fn execute_tx(&self, tx_index: TxIndex) {
let mut tx_versioned_state = self.state.pin_version(tx_index);
let tx = &self.chunk[tx_index];
let tx_charge_fee = tx.create_tx_info().enforce_fee();
let mut transactional_state =
TransactionalState::create_transactional(&mut tx_versioned_state);
let execution_flags =
ExecutionFlags { charge_fee: tx_charge_fee, validate: true, concurrency_mode: true };
let execution_flags = ExecutionFlags { concurrency_mode: true };
let execution_result =
tx.execute_raw(&mut transactional_state, self.block_context, execution_flags);

Expand Down
6 changes: 6 additions & 0 deletions crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::execution::syscalls::hint_processor::ENTRYPOINT_FAILED_ERROR;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::{create_calldata, CairoVersion, BALANCE};
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::test_utils::{
account_invoke_tx,
block_context,
Expand Down Expand Up @@ -608,6 +609,11 @@ fn test_validate_trace(
}
}

// TODO(AvivG): Change this fixup to not create account_tx twice w wrong charge_fee.
let execution_flags =
ExecutionFlags { charge_fee: account_tx.enforce_fee(), ..ExecutionFlags::default() };
let account_tx = AccountTransaction { tx: account_tx.tx, execution_flags };

let contract_address = *sender_address.0.key();

let expected_error = match cairo_version {
Expand Down
12 changes: 6 additions & 6 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
&self,
state: &mut TransactionalState<'_, U>,
block_context: &BlockContext,
execution_flags: TransactionExecutionFlags,
execution_flags_: TransactionExecutionFlags,
) -> TransactionExecutionResult<TransactionExecutionInfo> {
let tx_context = Arc::new(block_context.to_tx_context(self));
self.verify_tx_version(tx_context.tx_info.version())?;
Expand All @@ -731,7 +731,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
self.perform_pre_validation_stage(
state,
&tx_context,
execution_flags.charge_fee,
self.execution_flags.charge_fee,
strict_nonce_check,
)?;

Expand All @@ -752,15 +752,15 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
state,
&mut remaining_gas,
tx_context.clone(),
execution_flags.validate,
execution_flags.charge_fee,
self.execution_flags.validate,
self.execution_flags.charge_fee,
)?;
let fee_transfer_call_info = Self::handle_fee(
state,
tx_context,
final_fee,
execution_flags.charge_fee,
execution_flags.concurrency_mode,
self.execution_flags.charge_fee,
execution_flags_.concurrency_mode,
)?;

let tx_execution_info = TransactionExecutionInfo {
Expand Down
19 changes: 9 additions & 10 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::declare::declare_tx;
use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::invoke::invoke_tx;
use crate::test_utils::syscall::build_recurse_calldata;
use crate::test_utils::{
create_calldata,
Expand Down Expand Up @@ -1371,8 +1372,7 @@ fn test_count_actual_storage_changes(
nonce: nonce_manager.next(account_address),
};
let account_tx = account_invoke_tx(invoke_args.clone());
let execution_flags =
ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: false };
let execution_flags = ExecutionFlags { concurrency_mode: false };
let execution_info =
account_tx.execute_raw(&mut state, &block_context, execution_flags).unwrap();

Expand Down Expand Up @@ -1542,8 +1542,7 @@ fn test_concurrency_execute_fee_transfer(
// Case 1: The transaction did not read form/ write to the sequenser balance before executing
// fee transfer.
let mut transactional_state = TransactionalState::create_transactional(state);
let execution_flags =
ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: true };
let execution_flags = ExecutionFlags { concurrency_mode: true };
let result =
account_tx.execute_raw(&mut transactional_state, &block_context, execution_flags).unwrap();
assert!(!result.is_reverted());
Expand Down Expand Up @@ -1638,8 +1637,7 @@ fn test_concurrent_fee_transfer_when_sender_is_sequencer(
let fee_token_address = block_context.chain_info.fee_token_address(fee_type);

let mut transactional_state = TransactionalState::create_transactional(state);
let execution_flags =
ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: true };
let execution_flags = ExecutionFlags { concurrency_mode: true };
let result =
account_tx.execute_raw(&mut transactional_state, &block_context, execution_flags).unwrap();
assert!(!result.is_reverted());
Expand Down Expand Up @@ -1766,12 +1764,13 @@ fn test_revert_in_execute(

// Skip validate phase, as we want to test the revert in the execute phase.
let validate = false;
let tx_execution_info = account_invoke_tx(invoke_tx_args! {
let tx = invoke_tx(invoke_tx_args! {
resource_bounds: default_all_resource_bounds,
..tx_args
})
.execute(state, &block_context, true, validate)
.unwrap();
});
let execution_flags = AccountExecutionFlags { validate, ..AccountExecutionFlags::default() };
let account_tx = AccountTransaction { tx, execution_flags };
let tx_execution_info = account_tx.execute(state, &block_context, true, validate).unwrap();

assert!(tx_execution_info.is_reverted());
assert!(
Expand Down
Loading

0 comments on commit 5bf81e9

Please sign in to comment.