Skip to content

Commit

Permalink
chore(blockifier): add new constructor to AccountTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware committed Dec 5, 2024
1 parent c594964 commit 3204c4d
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 157 deletions.
18 changes: 4 additions & 14 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, ExecutionFlags};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::test_utils::{
block_context,
Expand All @@ -41,7 +41,6 @@ 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 @@ -132,10 +131,7 @@ fn test_declare(
},
calculate_class_info_for_testing(declared_contract.get_class()),
);
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();
let tx = AccountTransaction::new_for_sequencing(declare_tx).into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
}

Expand All @@ -156,10 +152,7 @@ fn test_deploy_account(
},
&mut NonceManager::default(),
);
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 tx = AccountTransaction::new_for_sequencing(deploy_account_tx).into();
let expected_bouncer_weights = BouncerWeights {
state_diff_size: 3,
message_segment_length: 0,
Expand Down Expand Up @@ -231,10 +224,7 @@ fn test_invoke(
calldata,
version,
});
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();
let tx = AccountTransaction::new_for_sequencing(invoke_tx).into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
}

Expand Down
15 changes: 6 additions & 9 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, ExecutionFlags};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{default_all_resource_bounds, l1_resource_bounds};
use crate::transaction::transactions::{enforce_fee, ExecutableTransaction};
use crate::transaction::transactions::ExecutableTransaction;

#[fixture]
pub fn safe_versioned_state(
Expand Down Expand Up @@ -236,9 +236,8 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
},
&mut NonceManager::default(),
);
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 deploy_account_tx_1 = AccountTransaction::new_for_sequencing(tx);
let enforce_fee = deploy_account_tx_1.enforce_fee();

let class_hash = grindy_account.get_class_hash();
let ctor_storage_arg = felt!(1_u8);
Expand All @@ -250,10 +249,8 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
constructor_calldata: constructor_calldata.clone(),
};
let nonce_manager = &mut NonceManager::default();
let delpoy_account_tx_2 = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
execution_flags,
};
let tx = deploy_account_tx(deploy_tx_args, nonce_manager);
let delpoy_account_tx_2 = AccountTransaction::new_for_sequencing(tx);

let account_address = delpoy_account_tx_2.sender_address();
let tx_context = block_context.to_tx_context(&delpoy_account_tx_2);
Expand Down
8 changes: 3 additions & 5 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, ExecutionFlags};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -549,7 +549,7 @@ fn test_deploy_before_declare(
let test_class_hash = test_contract.get_class_hash();
let test_class_info = calculate_class_info_for_testing(test_contract.get_class());
let test_compiled_class_hash = test_contract.get_compiled_class_hash();
let tx = declare_tx(
let declare_tx = AccountTransaction::new_with_default_flags(declare_tx(
declare_tx_args! {
sender_address: account_address_0,
resource_bounds: default_all_resource_bounds,
Expand All @@ -560,9 +560,7 @@ fn test_deploy_before_declare(
nonce: nonce!(0_u8),
},
test_class_info.clone(),
);
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
11 changes: 2 additions & 9 deletions crates/blockifier/src/test_utils/transfers_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ 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, ExecutionFlags};
use crate::transaction::account_transaction::AccountTransaction;
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,13 +144,7 @@ impl TransfersGenerator {
self.sender_index = (self.sender_index + 1) % self.account_addresses.len();

let tx = self.generate_transfer(sender_address, recipient_address);
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 };
let account_tx = AccountTransaction::new_for_sequencing(tx);
txs.push(Transaction::Account(account_tx));
}
let results = self.executor.execute_txs(&txs);
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use crate::transaction::objects::{
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{
enforce_fee,
Executable,
ExecutableTransaction,
ExecutionFlags as TransactionExecutionFlags,
Expand Down Expand Up @@ -128,6 +129,19 @@ impl AccountTransaction {
(paymaster_data, PaymasterData)
);

pub fn new_with_default_flags(tx: Transaction) -> Self {
Self { tx, execution_flags: ExecutionFlags::default() }
}

pub fn new_for_sequencing(tx: Transaction) -> Self {
let execution_flags = ExecutionFlags {
only_query: false,
charge_fee: enforce_fee(&tx, false),
validate: true,
};
AccountTransaction { tx, execution_flags }
}

pub fn class_hash(&self) -> Option<ClassHash> {
match &self.tx {
Transaction::Declare(tx) => Some(tx.tx.class_hash()),
Expand Down
18 changes: 6 additions & 12 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use crate::transaction::test_utils::{
INVALID,
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{enforce_fee, ExecutableTransaction, ExecutionFlags};
use crate::transaction::transactions::{ExecutableTransaction, ExecutionFlags};
use crate::utils::u64_from_usize;

#[rstest]
Expand Down Expand Up @@ -216,11 +216,7 @@ fn test_fee_enforcement(
},
&mut NonceManager::default(),
);
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 deploy_account_tx = AccountTransaction::new_for_sequencing(tx);

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

// Deploy grindy account with a lot of grind in the constructor.
Expand Down Expand Up @@ -791,10 +786,9 @@ fn test_fail_declare(block_context: BlockContext, max_fee: Fee) {
tx_hash: TransactionHash::default(),
class_info,
};
let declare_account_tx = AccountTransaction {
tx: ApiExecutableTransaction::Declare(executable_declare),
execution_flags: AccountExecutionFlags::default(),
};
let declare_account_tx = AccountTransaction::new_with_default_flags(
ApiExecutableTransaction::Declare(executable_declare),
);

// Fail execution, assert nonce and balance are unchanged.
let tx_info = declare_account_tx.create_tx_info();
Expand Down
17 changes: 7 additions & 10 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ pub fn deploy_and_fund_account(
deploy_tx_args: DeployAccountTxArgs,
) -> (AccountTransaction, ContractAddress) {
// Deploy an account contract.
let deploy_account_tx = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
execution_flags: ExecutionFlags::default(),
};
let deploy_account_tx = AccountTransaction::new_with_default_flags(deploy_account_tx(
deploy_tx_args,
nonce_manager,
));
let account_address = deploy_account_tx.sender_address();

// Update the balance of the about-to-be deployed account contract in the erc20 contract, so it
Expand Down Expand Up @@ -315,8 +315,8 @@ pub fn run_invoke_tx(
) -> TransactionExecutionResult<TransactionExecutionInfo> {
let only_query = invoke_args.only_query;
let tx = invoke_tx(invoke_args);
let charge_fee = enforce_fee(&tx, only_query);
let execution_flags = ExecutionFlags { charge_fee, only_query, ..ExecutionFlags::default() };
let execution_flags =
ExecutionFlags { only_query, charge_fee: enforce_fee(&tx, only_query), validate: true };
let account_tx = AccountTransaction { tx, execution_flags };

account_tx.execute(state, block_context)
Expand Down Expand Up @@ -394,9 +394,6 @@ pub fn emit_n_events_tx(
calldata,
nonce
});
let only_query = false;
let charge_fee = enforce_fee(&tx, only_query);
let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() };

AccountTransaction { tx, execution_flags }
AccountTransaction::new_for_sequencing(tx)
}
5 changes: 1 addition & 4 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ 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,
execution_flags: AccountExecutionFlags::default(),
})
Transaction::Account(AccountTransaction::new_with_default_flags(tx))
}
starknet_api::executable_transaction::Transaction::L1Handler(tx) => {
Transaction::L1Handler(tx)
Expand Down
Loading

0 comments on commit 3204c4d

Please sign in to comment.