Skip to content

Commit

Permalink
chore(blockifier): add struct ExecutionFlags to AccountTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware committed Dec 4, 2024
1 parent d6bb8c5 commit 0f38604
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 50 deletions.
7 changes: 4 additions & 3 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::test_utils::{
BALANCE,
DEFAULT_STRK_L1_GAS_PRICE,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::test_utils::{
block_context,
Expand Down Expand Up @@ -131,7 +131,8 @@ fn test_declare(
},
calculate_class_info_for_testing(declared_contract.get_class()),
);
let tx = AccountTransaction { tx: declare_tx, only_query: false }.into();
let execution_flags = ExecutionFlags::default();
let tx = AccountTransaction { tx: declare_tx, execution_flags }.into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
}

Expand All @@ -152,7 +153,7 @@ fn test_deploy_account(
},
&mut NonceManager::default(),
);
let tx = AccountTransaction { tx: deploy_account_tx, only_query: false }.into();
let tx = AccountTransaction { tx: deploy_account_tx, execution_flags: ExecutionFlags::default() }.into();
let expected_bouncer_weights = BouncerWeights {
state_diff_size: 3,
message_segment_length: 0,
Expand Down
12 changes: 7 additions & 5 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{CairoVersion, BALANCE, DEFAULT_STRK_L1_GAS_PRICE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{default_all_resource_bounds, l1_resource_bounds};
use crate::transaction::transactions::ExecutableTransaction;
use crate::transaction::transactions::{enforce_fee, ExecutableTransaction};

#[fixture]
pub fn safe_versioned_state(
Expand Down Expand Up @@ -236,8 +236,9 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
},
&mut NonceManager::default(),
);
let deploy_account_tx_1 = AccountTransaction { tx, only_query: false };
let enforce_fee = deploy_account_tx_1.enforce_fee();
let enforce_fee = enforce_fee(&tx, false);
let execution_flags = ExecutionFlags { charge_fee: enforce_fee, ..ExecutionFlags::default() };
let deploy_account_tx_1 = AccountTransaction { tx, execution_flags: execution_flags.clone() };

let class_hash = grindy_account.get_class_hash();
let ctor_storage_arg = felt!(1_u8);
Expand All @@ -251,8 +252,9 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
let nonce_manager = &mut NonceManager::default();
let delpoy_account_tx_2 = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
only_query: false,
execution_flags,
};

let account_address = delpoy_account_tx_2.sender_address();
let tx_context = block_context.to_tx_context(&delpoy_account_tx_2);
let fee_type = tx_context.tx_info.fee_type();
Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::test_utils::{
BALANCE,
TEST_ERC20_CONTRACT_ADDRESS2,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -561,7 +561,8 @@ fn test_deploy_before_declare(
},
test_class_info.clone(),
);
let declare_tx = AccountTransaction { tx, only_query: false };
let execution_flags = ExecutionFlags::default();
let declare_tx = AccountTransaction { tx, execution_flags };

// Deploy test contract.
let invoke_tx = account_invoke_tx(invoke_tx_args! {
Expand Down
12 changes: 9 additions & 3 deletions crates/blockifier/src/test_utils/transfers_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::invoke::invoke_tx;
use crate::test_utils::{CairoVersion, BALANCE, MAX_FEE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::transaction_execution::Transaction;

use crate::transaction::transactions::enforce_fee;
const N_ACCOUNTS: u16 = 10000;
const N_TXS: usize = 1000;
const RANDOMIZATION_SEED: u64 = 0;
Expand Down Expand Up @@ -145,7 +145,13 @@ impl TransfersGenerator {
self.sender_index = (self.sender_index + 1) % self.account_addresses.len();

let tx = self.generate_transfer(sender_address, recipient_address);
let account_tx = AccountTransaction { tx, only_query: false };
let only_query = false;
let execution_flags = ExecutionFlags {
charge_fee: enforce_fee(&tx, only_query),
only_query,
..ExecutionFlags::default()
};
let account_tx = AccountTransaction { tx, execution_flags };
txs.push(Transaction::Account(account_tx));
}
let results = self.executor.execute_txs(&txs);
Expand Down
21 changes: 17 additions & 4 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{
Executable,
ExecutableTransaction,
ExecutionFlags,
ExecutionFlags as TransactionExecutionFlags,
ValidatableTransaction,
};

Expand All @@ -77,11 +77,24 @@ mod flavors_test;
#[path = "post_execution_test.rs"]
mod post_execution_test;

#[derive(Clone, Debug, derive_more::From)]
pub struct ExecutionFlags {
pub only_query: bool,
pub charge_fee: bool,
pub validate: bool,
}

impl Default for ExecutionFlags {
fn default() -> Self {
Self { only_query: false, charge_fee: true, validate: true }
}
}

/// Represents a paid Starknet transaction.
#[derive(Clone, Debug, derive_more::From)]
pub struct AccountTransaction {
pub tx: Transaction,
pub only_query: bool,
pub execution_flags: ExecutionFlags,
}
// TODO(AvivG): create additional macro that returns a reference.
macro_rules! implement_tx_getter_calls {
Expand Down Expand Up @@ -708,7 +721,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
&self,
state: &mut TransactionalState<'_, U>,
block_context: &BlockContext,
execution_flags: ExecutionFlags,
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 Down Expand Up @@ -768,7 +781,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {

impl TransactionInfoCreator for AccountTransaction {
fn create_tx_info(&self) -> TransactionInfo {
self.tx.create_tx_info(self.only_query)
self.tx.create_tx_info(self.execution_flags.only_query)
}
}

Expand Down
18 changes: 13 additions & 5 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ use crate::test_utils::{
DEFAULT_STRK_L2_GAS_PRICE,
MAX_FEE,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{
AccountTransaction,
ExecutionFlags as AccountExecutionFlags,
};
use crate::transaction::objects::{HasRelatedFeeType, TransactionInfoCreator};
use crate::transaction::test_utils::{
account_invoke_tx,
Expand All @@ -105,7 +108,7 @@ use crate::transaction::test_utils::{
INVALID,
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{ExecutableTransaction, ExecutionFlags};
use crate::transaction::transactions::{enforce_fee, ExecutableTransaction, ExecutionFlags};
use crate::utils::u64_from_usize;

#[rstest]
Expand Down Expand Up @@ -213,7 +216,11 @@ fn test_fee_enforcement(
},
&mut NonceManager::default(),
);
let deploy_account_tx = AccountTransaction { tx, only_query: false };
let only_query = false;
let charge_fee = enforce_fee(&tx, only_query);
let execution_flags =
AccountExecutionFlags { only_query, charge_fee, ..AccountExecutionFlags::default() };
let deploy_account_tx = AccountTransaction { tx, execution_flags };

let enforce_fee = deploy_account_tx.enforce_fee();
assert_ne!(zero_bounds, enforce_fee);
Expand Down Expand Up @@ -462,7 +469,8 @@ fn test_max_fee_limit_validate(
},
class_info,
);
let account_tx = AccountTransaction { tx, only_query: false };
let execution_flags = AccountExecutionFlags::default();
let account_tx = AccountTransaction { tx, execution_flags };
account_tx.execute(&mut state, &block_context, true, true).unwrap();

// Deploy grindy account with a lot of grind in the constructor.
Expand Down Expand Up @@ -785,7 +793,7 @@ fn test_fail_declare(block_context: BlockContext, max_fee: Fee) {
};
let declare_account_tx = AccountTransaction {
tx: ApiExecutableTransaction::Declare(executable_declare),
only_query: false,
execution_flags: AccountExecutionFlags::default(),
};

// Fail execution, assert nonce and balance are unchanged.
Expand Down
23 changes: 17 additions & 6 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use starknet_api::{calldata, declare_tx_args, deploy_account_tx_args, felt, invo
use starknet_types_core::felt::Felt;
use strum::IntoEnumIterator;

use super::account_transaction::ExecutionFlags;
use crate::context::{BlockContext, ChainInfo};
use crate::state::cached_state::CachedState;
use crate::state::state_api::State;
Expand Down Expand Up @@ -115,7 +116,7 @@ pub fn deploy_and_fund_account(
// Deploy an account contract.
let deploy_account_tx = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
only_query: false,
execution_flags: ExecutionFlags::default(),
};
let account_address = deploy_account_tx.sender_address();

Expand Down Expand Up @@ -165,6 +166,9 @@ pub struct FaultyAccountTxCreatorArgs {
pub validate_constructor: bool,
// Should be used with tx_type Declare.
pub declared_contract: Option<FeatureContract>,
pub validate: bool,
pub only_query: bool,
pub charge_fee: bool,
}

impl Default for FaultyAccountTxCreatorArgs {
Expand All @@ -181,6 +185,9 @@ impl Default for FaultyAccountTxCreatorArgs {
max_fee: Fee::default(),
resource_bounds: ValidResourceBounds::create_for_testing_no_fee_enforcement(),
declared_contract: None,
validate: true,
only_query: false,
charge_fee: true,
}
}
}
Expand Down Expand Up @@ -217,6 +224,9 @@ pub fn create_account_tx_for_validate_test(
max_fee,
resource_bounds,
declared_contract,
validate,
only_query,
charge_fee,
} = faulty_account_tx_creator_args;

// The first felt of the signature is used to set the scenario. If the scenario is
Expand All @@ -226,7 +236,7 @@ pub fn create_account_tx_for_validate_test(
signature_vector.extend(additional_data);
}
let signature = TransactionSignature(signature_vector);

let execution_flags = ExecutionFlags { validate, charge_fee, only_query };
match tx_type {
TransactionType::Declare => {
let declared_contract = match declared_contract {
Expand All @@ -251,7 +261,7 @@ pub fn create_account_tx_for_validate_test(
},
class_info,
);
AccountTransaction { tx, only_query: false }
AccountTransaction { tx, execution_flags }
}
TransactionType::DeployAccount => {
// We do not use the sender address here because the transaction generates the actual
Expand All @@ -272,7 +282,7 @@ pub fn create_account_tx_for_validate_test(
},
nonce_manager,
);
AccountTransaction { tx, only_query: false }
AccountTransaction { tx, execution_flags }
}
TransactionType::InvokeFunction => {
let execute_calldata = create_calldata(sender_address, "foo", &[]);
Expand All @@ -285,7 +295,7 @@ pub fn create_account_tx_for_validate_test(
version: tx_version,
nonce: nonce_manager.next(sender_address),
});
AccountTransaction { tx, only_query: false }
AccountTransaction { tx, execution_flags }
}
_ => panic!("{tx_type:?} is not an account transaction."),
}
Expand All @@ -294,7 +304,8 @@ pub fn create_account_tx_for_validate_test(
// TODO(AvivG): Consider removing this function.
pub fn account_invoke_tx(invoke_args: InvokeTxArgs) -> AccountTransaction {
let only_query = invoke_args.only_query;
AccountTransaction { tx: invoke_tx(invoke_args), only_query }
let execution_flags = ExecutionFlags { only_query, ..ExecutionFlags::default() };
AccountTransaction { tx: invoke_tx(invoke_args), execution_flags }
}

pub fn run_invoke_tx(
Expand Down
20 changes: 17 additions & 3 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use crate::execution::entry_point::EntryPointExecutionContext;
use crate::fee::receipt::TransactionReceipt;
use crate::state::cached_state::TransactionalState;
use crate::state::state_api::UpdatableState;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{
AccountTransaction,
ExecutionFlags as AccountExecutionFlags,
};
use crate::transaction::errors::TransactionFeeError;
use crate::transaction::objects::{
TransactionExecutionInfo,
Expand All @@ -40,7 +43,10 @@ impl From<starknet_api::executable_transaction::Transaction> for Transaction {
fn from(value: starknet_api::executable_transaction::Transaction) -> Self {
match value {
starknet_api::executable_transaction::Transaction::Account(tx) => {
Transaction::Account(AccountTransaction { tx, only_query: false })
Transaction::Account(AccountTransaction {
tx,
execution_flags: AccountExecutionFlags::default(),
})
}
starknet_api::executable_transaction::Transaction::L1Handler(tx) => {
Transaction::L1Handler(tx)
Expand Down Expand Up @@ -119,7 +125,15 @@ impl Transaction {
}
_ => unimplemented!(),
};
Ok(AccountTransaction { tx: executable_tx, only_query }.into())
Ok(AccountTransaction {
tx: executable_tx,
execution_flags: AccountExecutionFlags {
only_query,
charge_fee: false,
validate: false,
},
}
.into())
}
}

Expand Down
Loading

0 comments on commit 0f38604

Please sign in to comment.