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 2326231 commit 68fa35c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 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
3 changes: 1 addition & 2 deletions crates/blockifier/src/concurrency/worker_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ impl<'a, S: StateReader> WorkerExecutor<'a, S> {
let tx = &self.chunk[tx_index];
let mut transactional_state =
TransactionalState::create_transactional(&mut tx_versioned_state);
let execution_flags =
ExecutionFlags { charge_fee, validate: true, concurrency_mode: true };
let execution_flags = ExecutionFlags { charge_fee, validate: true, concurrency_mode: true };
let execution_result =
tx.execute_raw(&mut transactional_state, self.block_context, execution_flags);

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
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ fn test_rc96_holes(block_context: BlockContext, max_resource_bounds: ResourceBou
assert_eq!(tx_execution_info.receipt.gas, GasVector::from_l1_gas(6598));
}



// TODO(Dori, 15/9/2023): Convert version variance to attribute macro.
#[rstest]
fn test_account_flow_test(
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 @@ -43,6 +43,7 @@ use crate::transaction::constants;
use crate::transaction::objects::{FeeType, TransactionExecutionInfo, TransactionExecutionResult};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{ExecutableTransaction, InvokeTransaction};
use crate::test_utils::default_testing_resource_bounds;
use crate::{declare_tx_args, deploy_account_tx_args, invoke_tx_args};

// Corresponding constants to the ones in faulty_account.
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 @@ -9,6 +9,7 @@ use num_traits::Pow;
use once_cell::sync::Lazy;
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use crate::test_utils::default_testing_resource_bounds;
use starknet_api::core::{ChainId, ClassHash, ContractAddress, EthAddress, Nonce, PatriciaKey};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::state::StorageKey;
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 68fa35c

Please sign in to comment.