Skip to content

Commit

Permalink
test(cairo_native): add native to transactions test
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Dec 31, 2024
1 parent 27b39c0 commit 9e0a752
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 29 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
};
Expand Down
56 changes: 51 additions & 5 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
110 changes: 88 additions & 22 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -162,6 +163,28 @@ use crate::{
static VERSIONED_CONSTANTS: LazyLock<VersionedConstants> =
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()
Expand Down Expand Up @@ -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())]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)]
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
) {
Expand Down

0 comments on commit 9e0a752

Please sign in to comment.