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 2, 2024
1 parent d1f3d9d commit b74677d
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 87 deletions.
11 changes: 6 additions & 5 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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::{
account_invoke_tx,
Expand Down Expand Up @@ -121,7 +121,7 @@ fn test_declare(
let declared_contract = FeatureContract::Empty(cairo_version);
let state = test_state(&block_context.chain_info, BALANCE, &[(account_contract, 1)]);

let tx = declare_tx(
let declare_tx = declare_tx(
declare_tx_args! {
sender_address: account_contract.get_instance_address(0),
class_hash: declared_contract.get_class_hash(),
Expand All @@ -131,8 +131,9 @@ fn test_declare(
},
calculate_class_info_for_testing(declared_contract.get_class()),
);
let account_tx = AccountTransaction { tx, only_query: false }.into();
tx_executor_test_body(state, block_context, account_tx, expected_bouncer_weights);
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);
}

#[rstest]
Expand All @@ -153,7 +154,7 @@ fn test_deploy_account(
},
&mut NonceManager::default(),
),
only_query: false,
execution_flags: ExecutionFlags::default(),
}
.into();
let expected_bouncer_weights = BouncerWeights {
Expand Down
34 changes: 17 additions & 17 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 @@ -226,20 +226,19 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
let mut state_2 = TransactionalState::create_transactional(&mut versioned_state_proxy_2);

// Prepare transactions
let deploy_account_tx_1 = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
class_hash: account_without_validation.get_class_hash(),
resource_bounds: l1_resource_bounds(
u8::from(!zero_bounds).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
},
&mut NonceManager::default(),
),
only_query: false,
};
let enforce_fee = deploy_account_tx_1.enforce_fee();
let tx = deploy_account_tx(
deploy_account_tx_args! {
class_hash: account_without_validation.get_class_hash(),
resource_bounds: l1_resource_bounds(
u8::from(!zero_bounds).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
},
&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 class_hash = grindy_account.get_class_hash();
let ctor_storage_arg = felt!(1_u8);
Expand All @@ -253,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_account_tx = AccountTransaction { tx: declare_tx, only_query: false };
let execution_flags = ExecutionFlags::default();
let declare_account_tx = AccountTransaction { tx: declare_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
62 changes: 34 additions & 28 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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 @@ -104,7 +107,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 @@ -190,31 +193,33 @@ fn test_fee_enforcement(
) {
let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0);
let state = &mut test_state(&block_context.chain_info, BALANCE, &[(account, 1)]);
let deploy_account_tx = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
class_hash: account.get_class_hash(),
max_fee: Fee(if zero_bounds { 0 } else { MAX_FEE.0 }),
resource_bounds: match gas_bounds_mode {
GasVectorComputationMode::NoL2Gas => l1_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
GasVectorComputationMode::All => create_all_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L2_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L2_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_DATA_GAS_PRICE.into(),
),
},
version,
let tx = deploy_account_tx(
deploy_account_tx_args! {
class_hash: account.get_class_hash(),
max_fee: Fee(if zero_bounds { 0 } else { MAX_FEE.0 }),
resource_bounds: match gas_bounds_mode {
GasVectorComputationMode::NoL2Gas => l1_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
GasVectorComputationMode::All => create_all_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L2_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L2_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_DATA_GAS_PRICE.into(),
),
},
&mut NonceManager::default(),
),
only_query: false,
};
version,
},
&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 enforce_fee = deploy_account_tx.enforce_fee();
assert_ne!(zero_bounds, enforce_fee);
Expand Down Expand Up @@ -463,7 +468,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 @@ -786,7 +792,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 @@ -273,7 +283,7 @@ pub fn create_account_tx_for_validate_test(
},
nonce_manager,
),
only_query: false,
execution_flags,
}
}
TransactionType::InvokeFunction => {
Expand All @@ -287,15 +297,16 @@ 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."),
}
}

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
Loading

0 comments on commit b74677d

Please sign in to comment.