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

Commit

Permalink
test(concurrency): add ERC20 cairo 1 contract for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Jun 17, 2024
1 parent b898b1c commit d85da65
Show file tree
Hide file tree
Showing 15 changed files with 36,813 additions and 503 deletions.

Large diffs are not rendered by default.

700 changes: 700 additions & 0 deletions crates/blockifier/ERC20/ERC20_Cairo1/ERC20_cairo1.cairo

Large diffs are not rendered by default.

35,587 changes: 35,587 additions & 0 deletions crates/blockifier/ERC20/ERC20_Cairo1/erc20.casm.json

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions crates/blockifier/src/concurrency/fee_utils_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use crate::concurrency::test_utils::create_fee_transfer_call_info;
use crate::context::BlockContext;
use crate::invoke_tx_args;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::initial_test_state::{fund_account, test_state_inner};
use crate::test_utils::{
create_trivial_calldata, CairoVersion, BALANCE, MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE,
};
use crate::transaction::test_utils::{account_invoke_tx, block_context, l1_resource_bounds};

#[rstest]
pub fn test_fill_sequencer_balance_reads(block_context: BlockContext) {
pub fn test_fill_sequencer_balance_reads(
block_context: BlockContext,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] erc20_version: CairoVersion,
) {
let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
let account_tx = account_invoke_tx(invoke_tx_args! {
sender_address: account.get_instance_address(0),
Expand All @@ -23,7 +26,7 @@ pub fn test_fill_sequencer_balance_reads(block_context: BlockContext) {
version: TransactionVersion::THREE
});
let chain_info = &block_context.chain_info;
let state = &mut test_state(chain_info, BALANCE, &[(account, 1)]);
let state = &mut test_state_inner(chain_info, BALANCE, &[(account, 1)], erc20_version);

let sequencer_balance = 100;
let sequencer_address = block_context.block_info.sequencer_address;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ fn test_worker_execute() {
let account_balance = BALANCE - result.transaction_receipt.fee.0;
assert!(!result.is_reverted());

let erc20 = FeatureContract::ERC20;
let erc20 = FeatureContract::ERC20(CairoVersion::Cairo0);
let erc_contract_address = contract_address!(TEST_ERC20_CONTRACT_ADDRESS);
let account_balance_key_low = get_fee_token_var_address(account_address);
let account_balance_key_high = next_storage_key(&account_balance_key_low).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub const TEST_ERC20_CONTRACT_ADDRESS2: &str = "0x1002";
pub const TEST_ERC20_CONTRACT_CLASS_HASH: &str = "0x1010";

// Paths.
pub const ERC20_CONTRACT_PATH: &str =
"./ERC20_without_some_syscalls/ERC20/erc20_contract_without_some_syscalls_compiled.json";
pub const ERC20_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo0/ERC20_without_some_syscalls/ERC20/\
erc20_contract_without_some_syscalls_compiled.json";

#[derive(Clone, Copy, Debug)]
pub enum CairoVersion {
Expand Down
31 changes: 20 additions & 11 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ const SECURITY_TEST_CONTRACT_NAME: &str = "security_tests_contract";
const TEST_CONTRACT_NAME: &str = "test_contract";

// ERC20 contract is in a unique location.
const ERC20_CONTRACT_PATH: &str =
"./ERC20_without_some_syscalls/ERC20/erc20_contract_without_some_syscalls_compiled.json";
const ERC20_CAIRO0_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo0/ERC20_without_some_syscalls/ERC20/\
erc20_contract_without_some_syscalls_compiled.json";
const ERC20_CAIRO1_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo1/erc20.casm.json";

/// Enum representing all feature contracts.
/// The contracts that are implemented in both Cairo versions include a version field.
#[derive(Clone, Copy, Debug, EnumIter)]
pub enum FeatureContract {
AccountWithLongValidate(CairoVersion),
AccountWithoutValidations(CairoVersion),
ERC20,
ERC20(CairoVersion),
Empty(CairoVersion),
FaultyAccount(CairoVersion),
LegacyTestContract,
Expand All @@ -81,8 +82,9 @@ impl FeatureContract {
| Self::AccountWithoutValidations(version)
| Self::Empty(version)
| Self::FaultyAccount(version)
| Self::TestContract(version) => *version,
Self::SecurityTests | Self::ERC20 => CairoVersion::Cairo0,
| Self::TestContract(version)
| Self::ERC20(version) => *version,
Self::SecurityTests => CairoVersion::Cairo0,
Self::LegacyTestContract => CairoVersion::Cairo1,
}
}
Expand All @@ -93,8 +95,9 @@ impl FeatureContract {
| Self::AccountWithoutValidations(_)
| Self::Empty(_)
| Self::FaultyAccount(_)
| Self::TestContract(_) => true,
Self::SecurityTests | Self::ERC20 | Self::LegacyTestContract => false,
| Self::TestContract(_)
| Self::ERC20(_) => true,
Self::SecurityTests | Self::LegacyTestContract => false,
}
}

Expand All @@ -112,7 +115,7 @@ impl FeatureContract {
Self::AccountWithLongValidate(_) => ACCOUNT_LONG_VALIDATE_BASE,
Self::AccountWithoutValidations(_) => ACCOUNT_WITHOUT_VALIDATIONS_BASE,
Self::Empty(_) => EMPTY_CONTRACT_BASE,
Self::ERC20 => ERC20_CONTRACT_BASE,
Self::ERC20(_) => ERC20_CONTRACT_BASE,
Self::FaultyAccount(_) => FAULTY_ACCOUNT_BASE,
Self::LegacyTestContract => LEGACY_CONTRACT_BASE,
Self::SecurityTests => SECURITY_TEST_CONTRACT_BASE,
Expand All @@ -131,7 +134,12 @@ impl FeatureContract {
Self::SecurityTests => SECURITY_TEST_CONTRACT_NAME,
Self::TestContract(_) => TEST_CONTRACT_NAME,
// ERC20 is a special case - not in the feature_contracts directory.
Self::ERC20 => return ERC20_CONTRACT_PATH.into(),
Self::ERC20(_) => {
return match cairo_version {
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_PATH.into(),
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_PATH.into(),
};
}
};
format!(
"feature_contracts/cairo{}/compiled/{}{}.json",
Expand All @@ -153,8 +161,9 @@ impl FeatureContract {
| Self::AccountWithoutValidations(v)
| Self::Empty(v)
| Self::FaultyAccount(v)
| Self::TestContract(v) => *v = version,
Self::ERC20 | Self::LegacyTestContract | Self::SecurityTests => {
| Self::TestContract(v)
| Self::ERC20(v) => *v = version,
Self::LegacyTestContract | Self::SecurityTests => {
panic!("{self:?} contract has no configurable version.")
}
}
Expand Down
14 changes: 12 additions & 2 deletions crates/blockifier/src/test_utils/initial_test_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::context::ChainInfo;
use crate::state::cached_state::CachedState;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::CairoVersion;
use crate::transaction::objects::FeeType;

/// Utility to fund an account.
Expand Down Expand Up @@ -37,16 +38,17 @@ pub fn fund_account(
/// * "Declares" the input list of contracts.
/// * "Deploys" the requested number of instances of each input contract.
/// * Makes each input account contract privileged.
pub fn test_state(
pub fn test_state_inner(
chain_info: &ChainInfo,
initial_balances: u128,
contract_instances: &[(FeatureContract, u16)],
erc20_contract_version: CairoVersion,
) -> CachedState<DictStateReader> {
let mut class_hash_to_class = HashMap::new();
let mut address_to_class_hash = HashMap::new();

// Declare and deploy account and ERC20 contracts.
let erc20 = FeatureContract::ERC20;
let erc20 = FeatureContract::ERC20(erc20_contract_version);
class_hash_to_class.insert(erc20.get_class_hash(), erc20.get_class());
address_to_class_hash
.insert(chain_info.fee_token_address(&FeeType::Eth), erc20.get_class_hash());
Expand Down Expand Up @@ -83,3 +85,11 @@ pub fn test_state(

CachedState::from(state_reader)
}

pub fn test_state(
chain_info: &ChainInfo,
initial_balances: u128,
contract_instances: &[(FeatureContract, u16)],
) -> CachedState<DictStateReader> {
test_state_inner(chain_info, initial_balances, contract_instances, CairoVersion::Cairo0)
}
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn deploy_and_fund_account(
pub fn create_test_init_data(chain_info: &ChainInfo, cairo_version: CairoVersion) -> TestInitData {
let account = FeatureContract::AccountWithoutValidations(cairo_version);
let test_contract = FeatureContract::TestContract(cairo_version);
let erc20 = FeatureContract::ERC20;
let erc20 = FeatureContract::ERC20(CairoVersion::Cairo0);
let state = test_state(chain_info, BALANCE, &[(account, 1), (erc20, 1), (test_contract, 1)]);
TestInitData {
state,
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ fn test_invoke_tx(
&tx_context,
sender_address,
expected_actual_fee,
FeatureContract::ERC20.get_class_hash(),
FeatureContract::ERC20(CairoVersion::Cairo0).get_class_hash(),
);

let da_gas = starknet_resources.get_state_changes_cost(use_kzg_da);
Expand Down Expand Up @@ -1160,7 +1160,7 @@ fn test_declare_tx(
tx_context,
sender_address,
expected_actual_fee,
FeatureContract::ERC20.get_class_hash(),
FeatureContract::ERC20(CairoVersion::Cairo0).get_class_hash(),
);

let da_gas = starknet_resources.get_state_changes_cost(use_kzg_da);
Expand Down Expand Up @@ -1303,7 +1303,7 @@ fn test_deploy_account_tx(
tx_context,
deployed_account_address,
expected_actual_fee,
FeatureContract::ERC20.get_class_hash(),
FeatureContract::ERC20(CairoVersion::Cairo0).get_class_hash(),
);
let starknet_resources =
actual_execution_info.transaction_receipt.resources.starknet_resources.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ fn verify_and_get_files(cairo_version: CairoVersion) -> Vec<(String, String, Str
fn verify_feature_contracts_match_enum() {
let mut compiled_paths_from_enum: Vec<String> = FeatureContract::all_contracts()
// ERC20 is a special case - not in the feature_contracts directory.
.filter(|contract| !matches!(contract, FeatureContract::ERC20))
.filter(|contract| !matches!(contract, FeatureContract::ERC20(CairoVersion::Cairo0) |
FeatureContract::ERC20(CairoVersion::Cairo1)))
.map(|contract| contract.get_compiled_path())
.collect();
let mut compiled_paths_on_filesystem: Vec<String> = verify_and_get_files(CairoVersion::Cairo0)
Expand Down

0 comments on commit d85da65

Please sign in to comment.