Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(blockifier): add resource bounds to faulty account transaction creator #365

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 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::{DeprecatedResourceBoundsMapping, Fee, 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: DeprecatedResourceBoundsMapping,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
) {
let chain_info = &block_context.chain_info;
Expand All @@ -44,16 +46,15 @@ fn test_transaction_validator(

let mut state = test_state(chain_info, account_balance, &[(faulty_account, 1)]);

// TODO(Arni, 1/5/2024): Cover resource bounds in version 3 txs. Test the validator validate
// enough "fee" in version 3 txs.
let transaction_args = FaultyAccountTxCreatorArgs {
tx_type,
tx_version,
sender_address,
class_hash,
validate_constructor,
// TODO(Arni, 1/5/2024): Add test for insufficient max fee.
// TODO(Arni, 1/5/2024): Add test for insufficient maximal resources.
max_fee: Fee(BALANCE),
resource_bounds: max_resource_bounds,
..Default::default()
};

Expand All @@ -68,13 +69,13 @@ 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);
let skip_validate = false;
let result = stateful_validator.perform_validations(tx, skip_validate);
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: DeprecatedResourceBoundsMapping) {
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 @@ -86,7 +87,7 @@ fn test_transaction_validator_skip_validate() {
tx_version: TransactionVersion::THREE,
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 @@ -211,7 +211,7 @@ pub fn trivial_external_entry_point_with_address(
}
}

fn default_testing_resource_bounds() -> DeprecatedResourceBoundsMapping {
pub fn default_testing_resource_bounds() -> DeprecatedResourceBoundsMapping {
DeprecatedResourceBoundsMapping::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 @@ -145,6 +146,7 @@ pub struct FaultyAccountTxCreatorArgs {
pub tx_version: TransactionVersion,
pub scenario: u64,
pub max_fee: Fee,
pub resource_bounds: DeprecatedResourceBoundsMapping,
// 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 @@ -171,6 +173,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 @@ -206,6 +209,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 @@ -231,6 +235,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 @@ -251,6 +256,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 @@ -265,6 +271,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 @@ -66,6 +66,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 @@ -1556,6 +1557,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, true, true).unwrap_err();
Expand All @@ -1571,6 +1573,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, true, true).unwrap_err();
Expand All @@ -1585,6 +1588,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, true, true).unwrap_err();
Expand All @@ -1607,6 +1611,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 @@ -1623,6 +1628,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 @@ -1642,6 +1648,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 @@ -1657,6 +1664,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 @@ -1678,6 +1686,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
Loading