From b835be97c8a4db8e5d125290867a0f92d5359f74 Mon Sep 17 00:00:00 2001 From: meship-starkware Date: Thu, 2 Jan 2025 13:34:20 +0200 Subject: [PATCH] test(cairo_native): add native to transactions test --- crates/blockifier/Cargo.toml | 3 +- .../src/execution/stack_trace_test.rs | 66 +------------- .../transaction/account_transactions_test.rs | 5 +- .../src/transaction/transactions_test.rs | 89 ++++++++++++++----- 4 files changed, 75 insertions(+), 88 deletions(-) diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index efd01944f3..5322c6cf84 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -14,7 +14,7 @@ cairo_native = ["dep:cairo-native", "dep:stacker", "starknet_sierra_compile/cair jemalloc = ["dep:tikv-jemallocator"] native_blockifier = [] reexecution = ["transaction_serde"] -testing = ["rand", "rstest", "starknet_api/testing"] +testing = ["rand", "rstest","rstest_reuse", "starknet_api/testing"] transaction_serde = [] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -46,6 +46,7 @@ paste.workspace = true phf = { workspace = true, features = ["macros"] } rand = { workspace = true, optional = true } rstest = { workspace = true, optional = true } +rstest_reuse = {workspace = true, optional = true} semver.workspace = true serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["arbitrary_precision"] } diff --git a/crates/blockifier/src/execution/stack_trace_test.rs b/crates/blockifier/src/execution/stack_trace_test.rs index 476cf36204..edc47dcbb9 100644 --- a/crates/blockifier/src/execution/stack_trace_test.rs +++ b/crates/blockifier/src/execution/stack_trace_test.rs @@ -2,6 +2,7 @@ use assert_matches::assert_matches; use pretty_assertions::assert_eq; use regex::Regex; use rstest::rstest; +use rstest_reuse::apply; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME; use starknet_api::core::{ @@ -44,6 +45,7 @@ use crate::execution::stack_trace::{ use crate::execution::syscalls::hint_processor::ENTRYPOINT_FAILED_ERROR; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::{fund_account, test_state}; +use crate::test_utils::test_templates::cairo_version; use crate::test_utils::{create_calldata, CairoVersion, RunnableCairo1, BALANCE}; use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use crate::transaction::test_utils::{ @@ -556,8 +558,7 @@ Error in contract (contract address: {address_felt:#064x}, class hash: {test_con // TODO(Arni, 1/5/2024): Cover version 0 declare transaction. // TODO(Arni, 1/5/2024): Consider version 0 invoke. -#[cfg(not(feature = "cairo_native"))] -#[rstest] +#[apply(cairo_version)] #[case::validate_version_1( TransactionType::InvokeFunction, VALIDATE_ENTRY_POINT_NAME, @@ -597,67 +598,6 @@ fn test_validate_trace( #[case] tx_type: TransactionType, #[case] entry_point_name: &str, #[case] tx_version: TransactionVersion, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - cairo_version: CairoVersion, -) { - test_validate_trace_fn(tx_type, entry_point_name, tx_version, cairo_version); -} - -#[cfg(feature = "cairo_native")] -#[rstest] -#[case::validate_version_1( - TransactionType::InvokeFunction, - VALIDATE_ENTRY_POINT_NAME, - TransactionVersion::ONE -)] -#[case::validate_version_3( - TransactionType::InvokeFunction, - VALIDATE_ENTRY_POINT_NAME, - TransactionVersion::THREE -)] -#[case::validate_declare_version_1( - TransactionType::Declare, - VALIDATE_DECLARE_ENTRY_POINT_NAME, - TransactionVersion::ONE -)] -#[case::validate_declare_version_2( - TransactionType::Declare, - VALIDATE_DECLARE_ENTRY_POINT_NAME, - TransactionVersion::TWO -)] -#[case::validate_declare_version_3( - TransactionType::Declare, - VALIDATE_DECLARE_ENTRY_POINT_NAME, - TransactionVersion::THREE -)] -#[case::validate_deploy_version_1( - TransactionType::DeployAccount, - VALIDATE_DEPLOY_ENTRY_POINT_NAME, - TransactionVersion::ONE -)] -#[case::validate_deploy_version_3( - TransactionType::DeployAccount, - VALIDATE_DEPLOY_ENTRY_POINT_NAME, - TransactionVersion::THREE -)] -fn test_validate_trace( - #[case] tx_type: TransactionType, - #[case] entry_point_name: &str, - #[case] tx_version: TransactionVersion, - #[values( - CairoVersion::Cairo0, - CairoVersion::Cairo1(RunnableCairo1::Casm), - CairoVersion::Cairo1(RunnableCairo1::Native) - )] - cairo_version: CairoVersion, -) { - test_validate_trace_fn(tx_type, entry_point_name, tx_version, cairo_version); -} - -fn test_validate_trace_fn( - tx_type: TransactionType, - entry_point_name: &str, - tx_version: TransactionVersion, cairo_version: CairoVersion, ) { let create_for_account_testing = &BlockContext::create_for_account_testing(); diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 36c9b10773..e506e60a5b 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -6,6 +6,7 @@ use cairo_vm::vm::runners::cairo_runner::ResourceTracker; use num_traits::Inv; use pretty_assertions::{assert_eq, assert_ne}; use rstest::rstest; +use rstest_reuse::apply; use starknet_api::abi::abi_utils::{ get_fee_token_var_address, get_storage_var_address, @@ -75,6 +76,7 @@ use crate::state::state_api::{State, StateReader}; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::{fund_account, test_state}; use crate::test_utils::syscall::build_recurse_calldata; +use crate::test_utils::test_templates::cairo_version; use crate::test_utils::{ create_calldata, create_trivial_calldata, @@ -580,13 +582,12 @@ fn test_max_fee_limit_validate( assert!(error_trace.contains("no remaining steps") | error_trace.contains("Out of gas")) } -#[rstest] +#[apply(cairo_version)] #[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())] fn test_recursion_depth_exceeded( #[case] tx_version: TransactionVersion, - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] cairo_version: CairoVersion, block_context: BlockContext, max_fee: Fee, diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 1a8aefd05a..addf585674 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; use starknet_api::abi::abi_utils::{ get_fee_token_var_address, get_storage_var_address, @@ -106,6 +107,7 @@ use crate::test_utils::dict_state_reader::DictStateReader; use crate::test_utils::initial_test_state::{fund_account, test_state}; use crate::test_utils::l1_handler::l1handler_tx; use crate::test_utils::prices::Prices; +use crate::test_utils::test_templates::cairo_version; use crate::test_utils::{ create_calldata, create_trivial_calldata, @@ -472,6 +474,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 +723,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 +893,19 @@ fn test_invoke_tx_advanced_operations( ); } -#[rstest] +#[apply(cairo_version)] #[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, + cairo_version: CairoVersion, ) { + let account_cairo_version = cairo_version; 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 +992,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 +1034,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 +1150,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 +1289,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 +1380,7 @@ fn test_insufficient_deprecated_resource_bounds_pre_validation( ); } -#[rstest] +#[apply(cairo_version)] #[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,9 +1389,9 @@ 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, + cairo_version: CairoVersion, ) { + let account_cairo_version = cairo_version; let block_context = &mut block_context; block_context.versioned_constants.allocation_cost = AllocationCost::ZERO; block_context.block_info.use_kzg_da = true; @@ -1428,11 +1463,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); @@ -1562,19 +1602,19 @@ fn declare_expected_state_changes_count(version: TransactionVersion) -> StateCha } } -#[rstest] +#[apply(cairo_version)] #[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, + cairo_version: CairoVersion, #[case] tx_version: TransactionVersion, #[case] empty_contract_version: CairoVersion, #[values(false, true)] use_kzg_da: bool, ) { + let account_cairo_version = cairo_version; let block_context = &BlockContext::create_for_account_testing_with_kzg(use_kzg_da); let versioned_constants = &block_context.versioned_constants; let empty_contract = FeatureContract::Empty(empty_contract_version); @@ -1797,6 +1837,11 @@ fn test_declare_tx_v0(default_l1_resource_bounds: ValidResourceBounds) { CairoVersion::Cairo1(RunnableCairo1::Casm), VersionedConstants::create_for_testing().os_constants.gas_costs.base.entry_point_initial_budget )] +#[cfg_attr( + feature = "cairo_native", + case::with_cairo1_native_account(CairoVersion::Cairo1(RunnableCairo1::Native), + VersionedConstants::create_for_testing().os_constants.gas_costs.base.entry_point_initial_budget) +)] fn test_deploy_account_tx( #[case] cairo_version: CairoVersion, #[case] expected_gas_consumed: u64,