Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
test: add version 2 and 3 declare tx support (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored May 1, 2024
1 parent e64bf80 commit 3300b13
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
13 changes: 13 additions & 0 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata, ContractAddressSalt, Resource, ResourceBounds, ResourceBoundsMapping,
TransactionVersion,
};
use starknet_api::{contract_address, patricia_key, stark_felt};

Expand Down Expand Up @@ -61,6 +62,18 @@ impl Default for CairoVersion {
}
}

impl CairoVersion {
// A declare transaction of the given version, can be used to declare contracts of the returned
// cairo version.
pub fn from_declare_tx_version(tx_version: TransactionVersion) -> Self {
match tx_version {
TransactionVersion::ZERO | TransactionVersion::ONE => CairoVersion::Cairo0,
TransactionVersion::TWO | TransactionVersion::THREE => CairoVersion::Cairo1,
_ => panic!("Transaction version {:?} is not supported.", tx_version),
}
}
}

// Storage keys.
pub fn test_erc20_sequencer_balance_key() -> StorageKey {
get_fee_token_var_address(contract_address!(TEST_SEQUENCER_ADDRESS))
Expand Down
18 changes: 11 additions & 7 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub struct FaultyAccountTxCreatorArgs {
pub tx_type: TransactionType,
pub tx_version: TransactionVersion,
pub scenario: u64,
pub max_fee: Fee,
// Should be None unless scenario is CALL_CONTRACT.
pub additional_data: Option<Vec<StarkFelt>>,
// Should be use with tx_type Declare or InvokeFunction.
Expand All @@ -140,7 +141,8 @@ pub struct FaultyAccountTxCreatorArgs {
pub contract_address_salt: ContractAddressSalt,
// Should be used with tx_type DeployAccount.
pub validate_constructor: bool,
pub max_fee: Fee,
// Should be used with tx_type Declare.
pub declared_contract: Option<FeatureContract>,
}

impl Default for FaultyAccountTxCreatorArgs {
Expand All @@ -155,6 +157,7 @@ impl Default for FaultyAccountTxCreatorArgs {
contract_address_salt: ContractAddressSalt::default(),
validate_constructor: false,
max_fee: Fee::default(),
declared_contract: None,
}
}
}
Expand All @@ -176,6 +179,7 @@ pub fn create_account_tx_for_validate_test(
contract_address_salt,
validate_constructor,
max_fee,
declared_contract,
} = faulty_account_tx_creator_args;

// The first felt of the signature is used to set the scenario. If the scenario is
Expand All @@ -188,13 +192,13 @@ pub fn create_account_tx_for_validate_test(

match tx_type {
TransactionType::Declare => {
// It does not matter which class is declared for this test.
let declared_contract_cairo_version = match tx_version {
TransactionVersion::ZERO | TransactionVersion::ONE => CairoVersion::Cairo0,
TransactionVersion::TWO | TransactionVersion::THREE => CairoVersion::Cairo1,
_ => panic!("Transaction version {:?} is not supported.", tx_version),
let declared_contract = match declared_contract {
Some(declared_contract) => declared_contract,
None => {
// It does not matter which class is declared for this test.
FeatureContract::TestContract(CairoVersion::from_declare_tx_version(tx_version))
}
};
let declared_contract = FeatureContract::TestContract(declared_contract_cairo_version);
let class_hash = declared_contract.get_class_hash();
let class_info = calculate_class_info_for_testing(declared_contract.get_class());
declare_tx(
Expand Down
12 changes: 12 additions & 0 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,8 @@ fn test_fail_deploy_account_undeclared_class_hash(block_context: BlockContext) {
#[case::validate_version_1(TransactionType::InvokeFunction, false, TransactionVersion::ONE)]
#[case::validate_version_3(TransactionType::InvokeFunction, false, TransactionVersion::THREE)]
#[case::validate_declare_version_1(TransactionType::Declare, false, TransactionVersion::ONE)]
#[case::validate_declare_version_2(TransactionType::Declare, false, TransactionVersion::TWO)]
#[case::validate_declare_version_3(TransactionType::Declare, false, TransactionVersion::THREE)]
#[case::validate_deploy_version_1(TransactionType::DeployAccount, false, TransactionVersion::ONE)]
#[case::validate_deploy_version_3(TransactionType::DeployAccount, false, TransactionVersion::THREE)]
#[case::constructor_version_1(TransactionType::DeployAccount, true, TransactionVersion::ONE)]
Expand Down Expand Up @@ -1513,12 +1515,14 @@ fn test_validate_accounts_tx(

// Valid logic.
let nonce_manager = &mut NonceManager::default();
let declared_contract_cairo_version = CairoVersion::from_declare_tx_version(tx_version);
let account_tx = create_account_tx_for_validate_test(
nonce_manager,
FaultyAccountTxCreatorArgs {
scenario: VALID,
contract_address_salt: salt_manager.next_salt(),
additional_data: None,
declared_contract: Some(FeatureContract::TestContract(declared_contract_cairo_version)),
..default_args
},
);
Expand All @@ -1532,6 +1536,9 @@ fn test_validate_accounts_tx(
FaultyAccountTxCreatorArgs {
scenario: CALL_CONTRACT,
additional_data: Some(vec![*sender_address.0.key()]),
declared_contract: Some(FeatureContract::AccountWithLongValidate(
declared_contract_cairo_version,
)),
..default_args
},
);
Expand All @@ -1548,6 +1555,9 @@ fn test_validate_accounts_tx(
scenario: GET_BLOCK_NUMBER,
contract_address_salt: salt_manager.next_salt(),
additional_data: Some(vec![StarkFelt::from(CURRENT_BLOCK_NUMBER_FOR_VALIDATE)]),
declared_contract: Some(FeatureContract::AccountWithoutValidations(
declared_contract_cairo_version,
)),
..default_args
},
);
Expand All @@ -1562,6 +1572,7 @@ fn test_validate_accounts_tx(
scenario: GET_BLOCK_TIMESTAMP,
contract_address_salt: salt_manager.next_salt(),
additional_data: Some(vec![StarkFelt::from(CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE)]),
declared_contract: Some(FeatureContract::Empty(declared_contract_cairo_version)),
..default_args
},
);
Expand All @@ -1582,6 +1593,7 @@ fn test_validate_accounts_tx(
StarkFelt::from(CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE),
StarkFelt::from(0_u64), // Sequencer address for validate.
]),
declared_contract: Some(FeatureContract::Empty(declared_contract_cairo_version)),
..default_args
},
);
Expand Down

0 comments on commit 3300b13

Please sign in to comment.