Skip to content

Commit

Permalink
fix: tests, problems while compiling without cairo_native feature
Browse files Browse the repository at this point in the history
  • Loading branch information
varex83 committed Oct 30, 2024
1 parent f79e37a commit d3176d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
21 changes: 14 additions & 7 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ use std::hash::RandomState;
use std::sync::Arc;

use cairo_native::starknet::{
ExecutionInfo, ExecutionInfoV2, Secp256k1Point, Secp256r1Point, StarknetSyscallHandler,
SyscallResult, U256,
ExecutionInfo,
ExecutionInfoV2,
Secp256k1Point,
Secp256r1Point,
StarknetSyscallHandler,
SyscallResult,
U256,
};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::core::{ClassHash, EntryPointSelector};
use starknet_api::state::StorageKey;
use starknet_api::transaction::Calldata;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -111,8 +116,10 @@ impl<'state> NativeSyscallHandler<'state> {

if *remaining_gas < required_gas {
// Out of gas failure.
return Err(vec![Felt::from_hex(OUT_OF_GAS_ERROR)
.expect("Failed to parse OUT_OF_GAS_ERROR hex string")]);
return Err(vec![
Felt::from_hex(OUT_OF_GAS_ERROR)
.expect("Failed to parse OUT_OF_GAS_ERROR hex string"),
]);
}

*remaining_gas -= required_gas;
Expand Down Expand Up @@ -180,8 +187,8 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
entry_point_selector: EntryPointSelector(function_selector),
calldata: wrapper_calldata,
// The call context remains the same in a library call.
storage_address: self.contract_address,
caller_address: self.caller_address,
storage_address: self.call.storage_address,
caller_address: self.call.caller_address,
call_type: CallType::Delegate,
initial_gas: u64::try_from(*remaining_gas)
.expect("Failed to convert gas (u128 -> u64)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use crate::test_utils::{
};
use crate::versioned_constants::VersionedConstants;

#[test_case(FeatureContract::TestContract(CairoVersion::Native), 189470; "Native")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native), 189470; "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), REQUIRED_GAS_LIBRARY_CALL_TEST; "VM")]
fn test_library_call(test_contract: FeatureContract, expected_gas: u64) {
let chain_info = &ChainInfo::create_for_testing();
Expand Down Expand Up @@ -61,7 +64,10 @@ fn test_library_call(test_contract: FeatureContract, expected_gas: u64) {
);
}

#[test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn test_library_call_assert_fails(test_contract: FeatureContract) {
let chain_info = &ChainInfo::create_for_testing();
Expand Down Expand Up @@ -89,22 +95,34 @@ fn test_library_call_assert_fails(test_contract: FeatureContract) {
"(0x7820213d2079 ('x != y'), 0x454e545259504f494e545f4641494c4544 \
('ENTRYPOINT_FAILED'))"
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => "0x7820213d2079 ('x != y')",
};
assert_eq!(format_panic_data(&call_info.execution.retdata.0), expected_err);
}

#[test_case(FeatureContract::TestContract(CairoVersion::Native), 518110; "Native")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native), 518110; "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 478110; "VM")]
fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
// Todo(pwhite) 2024/10/28: Execution resources from the VM & Native are mesaured differently
// helper function to change the expected resource values from both of executions
// When gas is changed to be the same between VM and Native this should be removed.
#[cfg_attr(not(feature = "cairo_native"), allow(unused_variables))]
fn if_native<T>(test_contract: &FeatureContract) -> impl Fn(T, T) -> T + '_ {
move |native: T, non_native: T| {
if matches!(test_contract, FeatureContract::TestContract(CairoVersion::Native)) {
native
} else {
#[cfg(feature = "cairo_native")]
{
if matches!(test_contract, FeatureContract::TestContract(CairoVersion::Native)) {
native
} else {
non_native
}
}
#[cfg(not(feature = "cairo_native"))]
{
non_native
}
}
Expand Down

0 comments on commit d3176d7

Please sign in to comment.