From 9e0a752ecafde98e2e9ee7a093cd065d421a7bda Mon Sep 17 00:00:00 2001 From: meship-starkware Date: Tue, 31 Dec 2024 11:40:18 +0200 Subject: [PATCH] test(cairo_native): add native to transactions test --- Cargo.lock | 12 ++ Cargo.toml | 1 + crates/blockifier/Cargo.toml | 1 + .../src/execution/native/syscall_handler.rs | 5 +- .../transaction/account_transactions_test.rs | 56 ++++++++- .../src/transaction/transactions_test.rs | 110 ++++++++++++++---- 6 files changed, 156 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a47052fbd3..1e9a30f2bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1507,6 +1507,7 @@ dependencies = [ "rand 0.8.5", "regex", "rstest", + "rstest_reuse", "semver 1.0.23", "serde", "serde_json", @@ -9243,6 +9244,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "rstest_reuse" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a8fb4672e840a587a66fc577a5491375df51ddb88f2a2c2a792598c326fe14" +dependencies = [ + "quote", + "rand 0.8.5", + "syn 2.0.90", +] + [[package]] name = "rtnetlink" version = "0.10.1" diff --git a/Cargo.toml b/Cargo.toml index a51d19f4d0..7d686bfd4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -201,6 +201,7 @@ replace_with = "0.1.7" reqwest = "0.11" retry = "2.0.0" rstest = "0.17.0" +rstest_reuse = "0.7.0" rustc-hex = "2.1.0" schemars = "0.8.12" semver = "1.0.23" diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index 66b5470e51..efd01944f3 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -70,6 +70,7 @@ pretty_assertions.workspace = true rand.workspace = true regex.workspace = true rstest.workspace = true +rstest_reuse.workspace = true starknet_api = { workspace = true, features = ["testing"] } test-case.workspace = true diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index cc5e24f8e0..d4ca814f92 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -356,8 +356,9 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> { let class_hash = self .base + .state .get_class_hash_at(contract_address) - .map_err(|e| self.handle_error(remaining_gas, e))?; + .map_err(|e| self.handle_error(remaining_gas, e.into()))?; if self.base.context.execution_mode == ExecutionMode::Validate && self.base.call.storage_address != contract_address { @@ -377,7 +378,7 @@ impl StarknetSyscallHandler for &mut NativeSyscallHandler<'_> { entry_point_selector: EntryPointSelector(entry_point_selector), calldata: wrapper_calldata, storage_address: contract_address, - caller_address: self.base.call.caller_address, + caller_address: self.base.call.storage_address, call_type: CallType::Call, initial_gas: *remaining_gas, }; diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index e09f3948a7..ed8875b4cc 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -581,13 +581,59 @@ fn test_max_fee_limit_validate( } #[rstest] -#[case::v1(TransactionVersion::ONE, default_all_resource_bounds())] -#[case::l1_bounds(TransactionVersion::THREE, default_l1_resource_bounds())] -#[case::all_bounds(TransactionVersion::THREE, default_all_resource_bounds())] +#[case::v1_cairo0(TransactionVersion::ONE, CairoVersion::Cairo0, default_all_resource_bounds())] +#[case::l1_bounds_cairo0( + TransactionVersion::THREE, + CairoVersion::Cairo0, + default_l1_resource_bounds() +)] +#[case::all_bounds_cairo0( + TransactionVersion::THREE, + CairoVersion::Cairo0, + default_all_resource_bounds() +)] +#[case::v1_cairo1_casm( + TransactionVersion::ONE, + CairoVersion::Cairo1(RunnableCairo1::Casm), + default_all_resource_bounds() +)] +#[case::l1_bounds_cairo1_casm( + TransactionVersion::THREE, + CairoVersion::Cairo1(RunnableCairo1::Casm), + default_l1_resource_bounds() +)] +#[case::all_bounds_cairo1_casm( + TransactionVersion::THREE, + CairoVersion::Cairo1(RunnableCairo1::Casm), + default_all_resource_bounds() +)] +#[cfg_attr( + feature = "cairo_native", + case::v1_cairo1_native( + TransactionVersion::ONE, + CairoVersion::Cairo1(RunnableCairo1::Native), + default_all_resource_bounds() + ) +)] +#[cfg_attr( + feature = "cairo_native", + case::l1_bounds_cairo1_native( + TransactionVersion::THREE, + CairoVersion::Cairo1(RunnableCairo1::Native), + default_l1_resource_bounds() + ) +)] +#[cfg_attr( + feature = "cairo_native", + case::all_bounds_cairo1_native( + TransactionVersion::THREE, + CairoVersion::Cairo1(RunnableCairo1::Native), + default_all_resource_bounds() + ) +)] fn test_recursion_depth_exceeded( #[case] tx_version: TransactionVersion, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - cairo_version: CairoVersion, + #[case] cairo_version: CairoVersion, block_context: BlockContext, max_fee: Fee, #[case] resource_bounds: ValidResourceBounds, diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index e905d828bf..ba134042b9 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -7,6 +7,7 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_bigint::BigUint; use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; +use rstest_reuse::{apply, template}; use starknet_api::abi::abi_utils::{ get_fee_token_var_address, get_storage_var_address, @@ -162,6 +163,28 @@ use crate::{ static VERSIONED_CONSTANTS: LazyLock = LazyLock::new(VersionedConstants::create_for_testing); +#[cfg(not(feature = "cairo_native"))] +#[template] +#[rstest] +fn cairo_version_no_native( + #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] + account_cairo_version: CairoVersion, +) { +} + +#[cfg(feature = "cairo_native")] +#[template] +#[rstest] +fn cairo_version_with_native( + #[values( + CairoVersion::Cairo0, + CairoVersion::Cairo1(RunnableCairo1::Casm), + CairoVersion::Cairo1(RunnableCairo1::Native) + )] + account_cairo_version: CairoVersion, +) { +} + #[fixture] fn infinite_gas_for_vm_mode() -> u64 { VERSIONED_CONSTANTS.infinite_gas_for_vm_mode() @@ -472,6 +495,14 @@ fn add_kzg_da_resources_to_resources_mapping( execute_gas_consumed: 112080, }, CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr(feature = "cairo_native", case::with_cairo1_native_account( + ExpectedResultTestInvokeTx{ + resources: ExecutionResources::default(), + validate_gas_consumed: 4740, // The gas consumption results from parsing the input + // arguments. + execute_gas_consumed: 112080, + }, + CairoVersion::Cairo1(RunnableCairo1::Native)))] // TODO(Tzahi): Add calls to cairo1 test contracts (where gas flows to and from the inner call). fn test_invoke_tx( #[values(default_l1_resource_bounds(), default_all_resource_bounds())] @@ -713,11 +744,16 @@ fn verify_storage_after_invoke_advanced_operations( } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_invoke_tx_advanced_operations( block_context: BlockContext, default_all_resource_bounds: ValidResourceBounds, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - cairo_version: CairoVersion, + #[case] cairo_version: CairoVersion, ) { let block_context = &block_context; let account = FeatureContract::AccountWithoutValidations(cairo_version); @@ -878,19 +914,19 @@ fn test_invoke_tx_advanced_operations( ); } -#[rstest] +#[cfg_attr(not(feature = "cairo_native"), apply(cairo_version_no_native))] +#[cfg_attr(feature = "cairo_native", apply(cairo_version_with_native))] #[case(TransactionVersion::ONE, FeeType::Eth)] #[case(TransactionVersion::THREE, FeeType::Strk)] fn test_state_get_fee_token_balance( block_context: BlockContext, #[case] tx_version: TransactionVersion, #[case] fee_type: FeeType, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - account_version: CairoVersion, + account_cairo_version: CairoVersion, ) { let block_context = &block_context; let chain_info = &block_context.chain_info; - let account = FeatureContract::AccountWithoutValidations(account_version); + let account = FeatureContract::AccountWithoutValidations(account_cairo_version); let test_contract = FeatureContract::TestContract(CairoVersion::Cairo0); let state = &mut test_state(chain_info, BALANCE, &[(account, 1), (test_contract, 1)]); let account_address = account.get_instance_address(0); @@ -977,13 +1013,18 @@ fn assert_resource_bounds_exceed_balance_failure( } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_estimate_minimal_gas_vector( mut block_context: BlockContext, #[values(true, false)] use_kzg_da: bool, #[values(GasVectorComputationMode::NoL2Gas, GasVectorComputationMode::All)] gas_vector_computation_mode: GasVectorComputationMode, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - account_cairo_version: CairoVersion, + #[case] account_cairo_version: CairoVersion, ) { block_context.block_info.use_kzg_da = use_kzg_da; let block_context = &block_context; @@ -1014,13 +1055,18 @@ fn test_estimate_minimal_gas_vector( } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_max_fee_exceeds_balance( mut block_context: BlockContext, #[values(default_l1_resource_bounds(), default_all_resource_bounds())] resource_bounds: ValidResourceBounds, #[values(true, false)] use_kzg_da: bool, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - account_cairo_version: CairoVersion, + #[case] account_cairo_version: CairoVersion, ) { block_context.block_info.use_kzg_da = use_kzg_da; let block_context = &block_context; @@ -1125,11 +1171,16 @@ fn test_max_fee_exceeds_balance( } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_insufficient_new_resource_bounds_pre_validation( mut block_context: BlockContext, #[values(true, false)] use_kzg_da: bool, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - account_cairo_version: CairoVersion, + #[case] account_cairo_version: CairoVersion, ) { block_context.block_info.use_kzg_da = use_kzg_da; let block_context = &block_context; @@ -1259,10 +1310,15 @@ fn test_insufficient_new_resource_bounds_pre_validation( } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_insufficient_deprecated_resource_bounds_pre_validation( block_context: BlockContext, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - account_cairo_version: CairoVersion, + #[case] account_cairo_version: CairoVersion, ) { let block_context = &block_context; let account_contract = FeatureContract::AccountWithoutValidations(account_cairo_version); @@ -1345,7 +1401,8 @@ fn test_insufficient_deprecated_resource_bounds_pre_validation( ); } -#[rstest] +#[cfg_attr(not(feature = "cairo_native"), apply(cairo_version_no_native))] +#[cfg_attr(feature = "cairo_native", apply(cairo_version_with_native))] #[case::l1_bounds(default_l1_resource_bounds(), Resource::L1Gas)] #[case::all_bounds_l1_gas_overdraft(default_all_resource_bounds(), Resource::L1Gas)] #[case::all_bounds_l2_gas_overdraft(default_all_resource_bounds(), Resource::L2Gas)] @@ -1354,7 +1411,6 @@ fn test_actual_fee_gt_resource_bounds( mut block_context: BlockContext, #[case] resource_bounds: ValidResourceBounds, #[case] overdraft_resource: Resource, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] account_cairo_version: CairoVersion, ) { let block_context = &mut block_context; @@ -1428,11 +1484,16 @@ fn test_actual_fee_gt_resource_bounds( } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_invalid_nonce( block_context: BlockContext, default_all_resource_bounds: ValidResourceBounds, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - account_cairo_version: CairoVersion, + #[case] account_cairo_version: CairoVersion, ) { let account_contract = FeatureContract::AccountWithoutValidations(account_cairo_version); let test_contract = FeatureContract::TestContract(CairoVersion::Cairo0); @@ -1552,14 +1613,14 @@ fn declare_expected_state_changes_count(version: TransactionVersion) -> StateCha } } -#[rstest] +#[cfg_attr(not(feature = "cairo_native"), apply(cairo_version_no_native))] +#[cfg_attr(feature = "cairo_native", apply(cairo_version_with_native))] #[case(TransactionVersion::ZERO, CairoVersion::Cairo0)] #[case(TransactionVersion::ONE, CairoVersion::Cairo0)] #[case(TransactionVersion::TWO, CairoVersion::Cairo1(RunnableCairo1::Casm))] #[case(TransactionVersion::THREE, CairoVersion::Cairo1(RunnableCairo1::Casm))] fn test_declare_tx( default_all_resource_bounds: ValidResourceBounds, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] account_cairo_version: CairoVersion, #[case] tx_version: TransactionVersion, #[case] empty_contract_version: CairoVersion, @@ -1764,9 +1825,14 @@ fn test_declare_tx_v0(default_l1_resource_bounds: ValidResourceBounds) { } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0)] +#[case::with_cairo1_account(CairoVersion::Cairo1(RunnableCairo1::Casm))] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native)) +)] fn test_deploy_account_tx( - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - cairo_version: CairoVersion, + #[case] cairo_version: CairoVersion, #[values(false, true)] use_kzg_da: bool, default_all_resource_bounds: ValidResourceBounds, ) {