Skip to content

Commit

Permalink
test(blockifier): parametrize more tests in transactions_test by reso…
Browse files Browse the repository at this point in the history
…urce bounds types
  • Loading branch information
dorimedini-starkware committed Nov 3, 2024
1 parent 8eaf172 commit 1234641
Showing 1 changed file with 46 additions and 31 deletions.
77 changes: 46 additions & 31 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ use crate::test_utils::{
CURRENT_BLOCK_NUMBER_FOR_VALIDATE,
CURRENT_BLOCK_TIMESTAMP,
CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE,
DEFAULT_L1_DATA_GAS_MAX_AMOUNT,
DEFAULT_L1_GAS_AMOUNT,
DEFAULT_L2_GAS_MAX_AMOUNT,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
DEFAULT_STRK_L2_GAS_PRICE,
Expand Down Expand Up @@ -1335,7 +1337,7 @@ fn test_actual_fee_gt_resource_bounds(
#[rstest]
fn test_invalid_nonce(
block_context: BlockContext,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] account_cairo_version: CairoVersion,
) {
let account_contract = FeatureContract::AccountWithoutValidations(account_cairo_version);
Expand All @@ -1348,7 +1350,7 @@ fn test_invalid_nonce(
let valid_invoke_tx_args = invoke_tx_args! {
sender_address: account_contract.get_instance_address(0),
calldata: create_trivial_calldata(test_contract.get_instance_address(0)),
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
};
let mut transactional_state = TransactionalState::create_transactional(state);

Expand Down Expand Up @@ -1459,7 +1461,7 @@ fn declare_expected_state_changes_count(version: TransactionVersion) -> StateCha
#[case(TransactionVersion::TWO, CairoVersion::Cairo1)]
#[case(TransactionVersion::THREE, CairoVersion::Cairo1)]
fn test_declare_tx(
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] account_cairo_version: CairoVersion,
#[case] tx_version: TransactionVersion,
#[case] empty_contract_version: CairoVersion,
Expand Down Expand Up @@ -1490,7 +1492,7 @@ fn test_declare_tx(
max_fee: MAX_FEE,
sender_address,
version: tx_version,
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
class_hash,
compiled_class_hash,
nonce: nonce_manager.next(sender_address),
Expand All @@ -1507,6 +1509,7 @@ fn test_declare_tx(
let fee_type = &account_tx.fee_type();
let tx_context = &block_context.to_tx_context(&account_tx);
let actual_execution_info = account_tx.execute(state, block_context, true, true).unwrap();
assert_eq!(actual_execution_info.revert_error, None);

// Build expected validate call info.
let expected_validate_call_info = declare_validate_callinfo(
Expand All @@ -1519,7 +1522,7 @@ fn test_declare_tx(
.get_runnable_class()
.tracked_resource(&versioned_constants.min_compiler_version_for_sierra_gas),
if tx_version >= TransactionVersion::THREE {
user_initial_gas_from_bounds(default_l1_resource_bounds)
user_initial_gas_from_bounds(default_all_resource_bounds)
} else {
None
},
Expand Down Expand Up @@ -1564,7 +1567,7 @@ fn test_declare_tx(
let expected_total_gas = expected_actual_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&tx_context.get_gas_vector_computation_mode(),
);

let expected_execution_info = TransactionExecutionInfo {
Expand Down Expand Up @@ -1609,7 +1612,7 @@ fn test_declare_tx(
max_fee: MAX_FEE,
sender_address,
version: tx_version,
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
class_hash,
compiled_class_hash,
nonce: nonce_manager.next(sender_address),
Expand Down Expand Up @@ -1661,7 +1664,7 @@ fn test_declare_tx_v0(default_l1_resource_bounds: ValidResourceBounds) {
fn test_deploy_account_tx(
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
#[values(false, true)] use_kzg_da: bool,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
) {
let block_context = &BlockContext::create_for_account_testing_with_kzg(use_kzg_da);
let versioned_constants = &block_context.versioned_constants;
Expand All @@ -1671,7 +1674,10 @@ fn test_deploy_account_tx(
let account_class_hash = account.get_class_hash();
let state = &mut test_state(chain_info, BALANCE, &[(account, 1)]);
let deploy_account = deploy_account_tx(
deploy_account_tx_args! { resource_bounds: default_l1_resource_bounds, class_hash: account_class_hash },
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds,
class_hash: account_class_hash
},
&mut nonce_manager,
);

Expand All @@ -1681,6 +1687,7 @@ fn test_deploy_account_tx(
let deployed_account_address = deploy_account.contract_address();
let constructor_calldata = deploy_account.constructor_calldata();
let salt = deploy_account.contract_address_salt();
let user_initial_gas = user_initial_gas_from_bounds(default_all_resource_bounds);

// Update the balance of the about to be deployed account contract in the erc20 contract, so it
// can pay for the transaction execution.
Expand Down Expand Up @@ -1714,7 +1721,7 @@ fn test_deploy_account_tx(
account
.get_runnable_class()
.tracked_resource(&versioned_constants.min_compiler_version_for_sierra_gas),
user_initial_gas_from_bounds(default_l1_resource_bounds),
user_initial_gas,
);

// Build expected execute call info.
Expand All @@ -1725,7 +1732,7 @@ fn test_deploy_account_tx(
entry_point_type: EntryPointType::Constructor,
entry_point_selector: selector_from_name(abi_constants::CONSTRUCTOR_ENTRY_POINT_NAME),
storage_address: deployed_account_address,
initial_gas: default_initial_gas_cost(),
initial_gas: user_initial_gas.unwrap_or(GasAmount(default_initial_gas_cost())).0,
..Default::default()
},
..Default::default()
Expand Down Expand Up @@ -1773,7 +1780,7 @@ fn test_deploy_account_tx(
let expected_total_gas = actual_resources.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&tx_context.get_gas_vector_computation_mode(),
);

let expected_execution_info = TransactionExecutionInfo {
Expand Down Expand Up @@ -1814,7 +1821,10 @@ fn test_deploy_account_tx(
// Negative flow.
// Deploy to an existing address.
let deploy_account = deploy_account_tx(
deploy_account_tx_args! { resource_bounds: default_l1_resource_bounds, class_hash: account_class_hash },
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds,
class_hash: account_class_hash
},
&mut nonce_manager,
);
let account_tx = AccountTransaction::DeployAccount(deploy_account);
Expand All @@ -1835,15 +1845,17 @@ fn test_deploy_account_tx(
#[rstest]
fn test_fail_deploy_account_undeclared_class_hash(
block_context: BlockContext,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
) {
let block_context = &block_context;
let chain_info = &block_context.chain_info;
let state = &mut test_state(chain_info, BALANCE, &[]);
let mut nonce_manager = NonceManager::default();
let undeclared_hash = class_hash!("0xdeadbeef");
let deploy_account = deploy_account_tx(
deploy_account_tx_args! {resource_bounds: default_l1_resource_bounds, class_hash: undeclared_hash },
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds, class_hash: undeclared_hash
},
&mut nonce_manager,
);
let tx_context = block_context.to_tx_context(&deploy_account);
Expand Down Expand Up @@ -2073,7 +2085,7 @@ fn test_validate_accounts_tx(
#[rstest]
fn test_valid_flag(
block_context: BlockContext,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] account_cairo_version: CairoVersion,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] test_contract_cairo_version: CairoVersion,
) {
Expand All @@ -2089,7 +2101,7 @@ fn test_valid_flag(
let account_tx = account_invoke_tx(invoke_tx_args! {
sender_address: account_contract.get_instance_address(0),
calldata: create_trivial_calldata(test_contract.get_instance_address(0)),
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
});

let actual_execution_info = account_tx.execute(state, block_context, true, false).unwrap();
Expand All @@ -2101,7 +2113,7 @@ fn test_valid_flag(
#[rstest]
fn test_only_query_flag(
block_context: BlockContext,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
#[values(true, false)] only_query: bool,
) {
let account_balance = BALANCE;
Expand Down Expand Up @@ -2131,13 +2143,16 @@ fn test_only_query_flag(
];

let expected_resource_bounds = vec![
Felt::TWO, // Length of ResourceBounds array.
felt!(L1Gas.to_hex()), // Resource.
felt!(DEFAULT_L1_GAS_AMOUNT.0), // Max amount.
felt!(DEFAULT_STRK_L1_GAS_PRICE.get().0), // Max price per unit.
felt!(L2Gas.to_hex()), // Resource.
Felt::ZERO, // Max amount.
Felt::ZERO, // Max price per unit.
Felt::THREE, // Length of ResourceBounds array.
felt!(L1Gas.to_hex()), // Resource.
felt!(DEFAULT_L1_GAS_AMOUNT.0), // Max amount.
felt!(DEFAULT_STRK_L1_GAS_PRICE.get().0), // Max price per unit.
felt!(L2Gas.to_hex()), // Resource.
felt!(DEFAULT_L2_GAS_MAX_AMOUNT.0), // Max amount.
felt!(DEFAULT_STRK_L2_GAS_PRICE.get().0), // Max price per unit.
felt!(L1DataGas.to_hex()), // Resource.
felt!(DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0), // Max amount.
felt!(DEFAULT_STRK_L1_DATA_GAS_PRICE.get().0), // Max price per unit.
];

let expected_unsupported_fields = vec![
Expand Down Expand Up @@ -2185,14 +2200,14 @@ fn test_only_query_flag(
);
let invoke_tx = crate::test_utils::invoke::invoke_tx(invoke_tx_args! {
calldata: execute_calldata,
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
sender_address,
only_query,
});
let account_tx = AccountTransaction::Invoke(invoke_tx);

let tx_execution_info = account_tx.execute(state, block_context, true, true).unwrap();
assert!(!tx_execution_info.is_reverted())
assert_eq!(tx_execution_info.revert_error, None);
}

#[rstest]
Expand Down Expand Up @@ -2355,7 +2370,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
#[rstest]
fn test_execute_tx_with_invalid_tx_version(
block_context: BlockContext,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
) {
let cairo_version = CairoVersion::Cairo0;
let account = FeatureContract::AccountWithoutValidations(cairo_version);
Expand All @@ -2370,7 +2385,7 @@ fn test_execute_tx_with_invalid_tx_version(
&[felt!(invalid_version)],
);
let account_tx = account_invoke_tx(invoke_tx_args! {
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
sender_address: account.get_instance_address(0),
calldata,
});
Expand Down Expand Up @@ -2428,7 +2443,7 @@ fn max_event_data() -> usize {
}))]
fn test_emit_event_exceeds_limit(
block_context: BlockContext,
default_l1_resource_bounds: ValidResourceBounds,
default_all_resource_bounds: ValidResourceBounds,
#[case] event_keys: Vec<Felt>,
#[case] event_data: Vec<Felt>,
#[case] n_emitted_events: usize,
Expand Down Expand Up @@ -2467,7 +2482,7 @@ fn test_emit_event_exceeds_limit(
let account_tx = account_invoke_tx(invoke_tx_args! {
sender_address: account_contract.get_instance_address(0),
calldata: execute_calldata,
resource_bounds: default_l1_resource_bounds,
resource_bounds: default_all_resource_bounds,
nonce: nonce!(0_u8),
});
let execution_info = account_tx.execute(state, block_context, true, true).unwrap();
Expand Down

0 comments on commit 1234641

Please sign in to comment.