Skip to content

Commit

Permalink
chore(blockifier): add resource bounds to faulty account transaction …
Browse files Browse the repository at this point in the history
…creator
  • Loading branch information
meship-starkware committed Aug 8, 2024
1 parent 4c06451 commit 6794b4c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
12 changes: 8 additions & 4 deletions crates/blockifier/src/blockifier/stateful_validator_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use assert_matches::assert_matches;
use rstest::rstest;
use starknet_api::transaction::{Fee, TransactionVersion};
use starknet_api::transaction::{Fee, ResourceBoundsMapping, TransactionVersion};

use crate::blockifier::stateful_validator::StatefulValidator;
use crate::context::BlockContext;
Expand All @@ -11,6 +11,7 @@ use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::test_utils::{
block_context,
create_account_tx_for_validate_test_nonce_0,
max_resource_bounds,
FaultyAccountTxCreatorArgs,
INVALID,
VALID,
Expand All @@ -32,6 +33,7 @@ fn test_transaction_validator(
#[case] validate_constructor: bool,
#[case] tx_version: TransactionVersion,
block_context: BlockContext,
max_resource_bounds: ResourceBoundsMapping,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
) {
let chain_info = &block_context.chain_info;
Expand All @@ -54,6 +56,7 @@ fn test_transaction_validator(
validate_constructor,
// TODO(Arni, 1/5/2024): Add test for insufficient max fee.
max_fee: Fee(BALANCE),
resource_bounds: max_resource_bounds,
..Default::default()
};

Expand All @@ -69,12 +72,12 @@ fn test_transaction_validator(
// Test the stateful validator.
let mut stateful_validator = StatefulValidator::create(state, block_context);

let result = stateful_validator.perform_validations(tx, false, false);
let result = stateful_validator.perform_validations(tx, false, true);
assert!(result.is_ok(), "Validation failed: {:?}", result.unwrap_err());
}

#[test]
fn test_transaction_validator_skip_validate() {
#[rstest]
fn test_transaction_validator_skip_validate(max_resource_bounds: ResourceBoundsMapping) {
let block_context = BlockContext::create_for_testing();
let faulty_account = FeatureContract::FaultyAccount(CairoVersion::Cairo1);
let state = test_state(&block_context.chain_info, BALANCE, &[(faulty_account, 1)]);
Expand All @@ -87,6 +90,7 @@ fn test_transaction_validator_skip_validate() {
sender_address: faulty_account.get_instance_address(0),
class_hash: faulty_account.get_class_hash(),
max_fee: Fee(BALANCE),
resource_bounds: max_resource_bounds,
..Default::default()
});

Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn trivial_external_entry_point_with_address(
}
}

fn default_testing_resource_bounds() -> ResourceBoundsMapping {
pub fn default_testing_resource_bounds() -> ResourceBoundsMapping {
ResourceBoundsMapping::try_from(vec![
(Resource::L1Gas, ResourceBounds { max_amount: 0, max_price_per_unit: 1 }),
// TODO(Dori, 1/2/2024): When fee market is developed, change the default price of
Expand Down
7 changes: 7 additions & 0 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::invoke::{invoke_tx, InvokeTxArgs};
use crate::test_utils::{
create_calldata,
default_testing_resource_bounds,
CairoVersion,
NonceManager,
BALANCE,
Expand Down Expand Up @@ -146,6 +147,7 @@ pub struct FaultyAccountTxCreatorArgs {
pub tx_version: TransactionVersion,
pub scenario: u64,
pub max_fee: Fee,
pub resource_bounds: ResourceBoundsMapping,
// Should be None unless scenario is CALL_CONTRACT.
pub additional_data: Option<Vec<Felt>>,
// Should be use with tx_type Declare or InvokeFunction.
Expand All @@ -172,6 +174,7 @@ impl Default for FaultyAccountTxCreatorArgs {
contract_address_salt: ContractAddressSalt::default(),
validate_constructor: false,
max_fee: Fee::default(),
resource_bounds: default_testing_resource_bounds(),
declared_contract: None,
}
}
Expand Down Expand Up @@ -207,6 +210,7 @@ pub fn create_account_tx_for_validate_test(
contract_address_salt,
validate_constructor,
max_fee,
resource_bounds,
declared_contract,
} = faulty_account_tx_creator_args;

Expand All @@ -232,6 +236,7 @@ pub fn create_account_tx_for_validate_test(
declare_tx(
declare_tx_args! {
max_fee,
resource_bounds: resource_bounds.clone(),
signature,
sender_address,
version: tx_version,
Expand All @@ -252,6 +257,7 @@ pub fn create_account_tx_for_validate_test(
let deploy_account_tx = deploy_account_tx(
deploy_account_tx_args! {
max_fee,
resource_bounds: resource_bounds.clone(),
signature,
version: tx_version,
class_hash,
Expand All @@ -266,6 +272,7 @@ pub fn create_account_tx_for_validate_test(
let execute_calldata = create_calldata(sender_address, "foo", &[]);
let invoke_tx = invoke_tx(invoke_tx_args! {
max_fee,
resource_bounds,
signature,
sender_address,
calldata: execute_calldata,
Expand Down
9 changes: 9 additions & 0 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use crate::test_utils::prices::Prices;
use crate::test_utils::{
create_calldata,
create_trivial_calldata,
default_testing_resource_bounds,
get_syscall_resources,
get_tx_resources,
test_erc20_sequencer_balance_key,
Expand Down Expand Up @@ -1552,6 +1553,7 @@ fn test_validate_accounts_tx(
* the address of
* faulty_account. */
contract_address_salt: salt_manager.next_salt(),
resource_bounds: default_testing_resource_bounds(),
..default_args
});
let error = account_tx.execute(state, block_context, false, true).unwrap_err();
Expand All @@ -1567,6 +1569,7 @@ fn test_validate_accounts_tx(
scenario: GET_BLOCK_HASH,
contract_address_salt: salt_manager.next_salt(),
additional_data: None,
resource_bounds: default_testing_resource_bounds(),
..default_args
});
let error = account_tx.execute(state, block_context, false, true).unwrap_err();
Expand All @@ -1581,6 +1584,7 @@ fn test_validate_accounts_tx(
let account_tx = create_account_tx_for_validate_test_nonce_0(FaultyAccountTxCreatorArgs {
scenario: GET_SEQUENCER_ADDRESS,
contract_address_salt: salt_manager.next_salt(),
resource_bounds: default_testing_resource_bounds(),
..default_args
});
let error = account_tx.execute(state, block_context, false, true).unwrap_err();
Expand All @@ -1603,6 +1607,7 @@ fn test_validate_accounts_tx(
contract_address_salt: salt_manager.next_salt(),
additional_data: None,
declared_contract: Some(FeatureContract::TestContract(declared_contract_cairo_version)),
resource_bounds: default_testing_resource_bounds(),
..default_args
},
);
Expand All @@ -1619,6 +1624,7 @@ fn test_validate_accounts_tx(
declared_contract: Some(FeatureContract::AccountWithLongValidate(
declared_contract_cairo_version,
)),
resource_bounds: default_testing_resource_bounds(),
..default_args
},
);
Expand All @@ -1638,6 +1644,7 @@ fn test_validate_accounts_tx(
declared_contract: Some(FeatureContract::AccountWithoutValidations(
declared_contract_cairo_version,
)),
resource_bounds: default_testing_resource_bounds(),
..default_args
},
);
Expand All @@ -1653,6 +1660,7 @@ fn test_validate_accounts_tx(
contract_address_salt: salt_manager.next_salt(),
additional_data: Some(vec![Felt::from(CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE)]),
declared_contract: Some(FeatureContract::Empty(declared_contract_cairo_version)),
resource_bounds: default_testing_resource_bounds(),
..default_args
},
);
Expand All @@ -1674,6 +1682,7 @@ fn test_validate_accounts_tx(
Felt::from(0_u64), // Sequencer address for validate.
]),
declared_contract: Some(FeatureContract::Empty(declared_contract_cairo_version)),
resource_bounds: default_testing_resource_bounds(),
..default_args
},
);
Expand Down

0 comments on commit 6794b4c

Please sign in to comment.