Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cairo_native): clean tests #2216

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{calldata_for_deploy_test, trivial_external_entry_point_new, CairoVersion};

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1);"VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native);"Native")
)]
fn no_constructor(deployer_contract: FeatureContract) {
#[test_case(CairoVersion::Cairo1;"VM")]
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
fn no_constructor(cairo_version: CairoVersion) {
// TODO(Yoni): share the init code of the tests in this file.

let deployer_contract = FeatureContract::TestContract(cairo_version);
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1);
let class_hash = empty_contract.get_class_hash();

Expand Down Expand Up @@ -62,12 +59,10 @@ fn no_constructor(deployer_contract: FeatureContract) {
assert_eq!(state.get_class_hash_at(deployed_contract_address).unwrap(), class_hash);
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1);"VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native);"Native")
)]
fn no_constructor_nonempty_calldata(deployer_contract: FeatureContract) {
#[test_case(CairoVersion::Cairo1;"VM")]
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
fn no_constructor_nonempty_calldata(cairo_version: CairoVersion) {
let deployer_contract = FeatureContract::TestContract(cairo_version);
let empty_contract = FeatureContract::Empty(CairoVersion::Cairo1);
let class_hash = empty_contract.get_class_hash();

Expand All @@ -92,12 +87,10 @@ fn no_constructor_nonempty_calldata(deployer_contract: FeatureContract) {
));
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1);"VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native);"Native")
)]
fn with_constructor(deployer_contract: FeatureContract) {
#[test_case(CairoVersion::Cairo1;"VM")]
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
fn with_constructor(cairo_version: CairoVersion) {
let deployer_contract = FeatureContract::TestContract(cairo_version);
let mut state = test_state(&ChainInfo::create_for_testing(), Fee(0), &[(deployer_contract, 1)]);

let class_hash = deployer_contract.get_class_hash();
Expand Down Expand Up @@ -145,12 +138,10 @@ fn with_constructor(deployer_contract: FeatureContract) {
assert_eq!(state.get_class_hash_at(contract_address).unwrap(), class_hash);
}

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1);"VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native);"Native")
)]
fn to_unavailable_address(deployer_contract: FeatureContract) {
#[test_case(CairoVersion::Cairo1;"VM")]
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
fn to_unavailable_address(cairo_version: CairoVersion) {
let deployer_contract = FeatureContract::TestContract(cairo_version);
let mut state = test_state(&ChainInfo::create_for_testing(), Fee(0), &[(deployer_contract, 1)]);

let class_hash = deployer_contract.get_class_hash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ const DATA: [Felt; 3] = [
];
const N_EMITTED_EVENTS: [Felt; 1] = [Felt::from_hex_unchecked("0x1")];

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn positive_flow(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn positive_flow(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let call_info = emit_events(test_contract, &N_EMITTED_EVENTS, &KEYS, &DATA)
.expect("emit_events failed with valued parameters");
let event = EventContent {
Expand All @@ -49,12 +47,10 @@ fn positive_flow(test_contract: FeatureContract) {
);
}

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn data_length_exceeds_limit(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn data_length_exceeds_limit(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let versioned_constants = VersionedConstants::create_for_testing();

let max_event_data_length = versioned_constants.tx_event_limits.max_data_length;
Expand All @@ -71,12 +67,10 @@ fn data_length_exceeds_limit(test_contract: FeatureContract) {
assert!(error_message.contains(&expected_error.to_string()));
}

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn keys_length_exceeds_limit(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn keys_length_exceeds_limit(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let versioned_constants = VersionedConstants::create_for_testing();

let max_event_keys_length = versioned_constants.tx_event_limits.max_keys_length;
Expand All @@ -94,10 +88,10 @@ fn keys_length_exceeds_limit(test_contract: FeatureContract) {
assert!(error_message.contains(&expected_error.to_string()));
}

#[cfg(feature = "cairo_native")]
#[test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn event_number_exceeds_limit(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn event_number_exceeds_limit(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let versioned_constants = VersionedConstants::create_for_testing();

let max_n_emitted_events = versioned_constants.tx_event_limits.max_n_emitted_events;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ fn initialize_state(test_contract: FeatureContract) -> (CachedState<DictStateRea
(state, block_number, block_hash)
}

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn positive_flow(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn positive_flow(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let (mut state, block_number, block_hash) = initialize_state(test_contract);

let calldata = calldata![block_number];
Expand All @@ -61,12 +59,10 @@ fn positive_flow(test_contract: FeatureContract) {
);
}

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn negative_flow_execution_mode_validate(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn negative_flow_execution_mode_validate(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let (mut state, block_number, _) = initialize_state(test_contract);

let calldata = calldata![block_number];
Expand Down Expand Up @@ -97,12 +93,10 @@ fn negative_flow_execution_mode_validate(test_contract: FeatureContract) {
);
}

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn negative_flow_block_number_out_of_range(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn negative_flow_block_number_out_of_range(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let (mut state, _, _) = initialize_state(test_contract);

let requested_block_number = CURRENT_BLOCK_NUMBER - constants::STORED_BLOCK_HASH_BUFFER + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE}
/// 3. Execution succeeds with expected gas for valid cases.
/// 4. Execution fails if `address` has a different `class_hash`.
/// 5. Execution succeeds and returns `class_hash` = 0 if `address` is absent.
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native"))
]
fn test_get_class_hash_at(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native;"Native"))]
#[test_case(CairoVersion::Cairo1;"VM")]
fn test_get_class_hash_at(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
let address = contract_address!("0x111");
Expand Down
10 changes: 4 additions & 6 deletions crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE};

#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
fn test_keccak(test_contract: FeatureContract) {
#[test_case(CairoVersion::Cairo1; "VM")]
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native; "Native"))]
fn test_keccak(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);

Expand Down
111 changes: 41 additions & 70 deletions crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ use crate::test_utils::{
};
use crate::versioned_constants::VersionedConstants;

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn test_library_call(test_contract: FeatureContract) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native; "Native"))]
#[test_case(CairoVersion::Cairo1; "VM")]
fn test_library_call(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);

Expand Down Expand Up @@ -63,12 +61,10 @@ fn test_library_call(test_contract: FeatureContract) {
);
}

#[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) {
#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native; "Native"))]
#[test_case(CairoVersion::Cairo1; "VM")]
fn test_library_call_assert_fails(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);
let inner_entry_point_selector = selector_from_name("assert_eq");
Expand Down Expand Up @@ -108,33 +104,10 @@ fn test_library_call_assert_fails(test_contract: FeatureContract) {
);
}

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native); "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1); "VM")]
fn test_nested_library_call(test_contract: FeatureContract) {
// 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| {
#[cfg(feature = "cairo_native")]
{
if matches!(test_contract, FeatureContract::TestContract(CairoVersion::Native)) {
native
} else {
non_native
}
}
#[cfg(not(feature = "cairo_native"))]
{
non_native
}
}
}

#[cfg_attr(feature = "cairo_native", test_case(CairoVersion::Native; "Native"))]
#[test_case(CairoVersion::Cairo1; "VM")]
fn test_nested_library_call(cairo_version: CairoVersion) {
let test_contract = FeatureContract::TestContract(cairo_version);
let chain_info = &ChainInfo::create_for_testing();
let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]);

Expand Down Expand Up @@ -188,18 +161,17 @@ fn test_nested_library_call(test_contract: FeatureContract) {
..nested_storage_entry_point
};

let first_storage_entry_point_resources = if_native(&test_contract)(
ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) },
ChargedResources::from_execution_resources(ExecutionResources {
let mut first_storage_entry_point_resources =
ChargedResources { gas_for_fee: GasAmount(0), ..Default::default() };
if cairo_version == CairoVersion::Cairo1 {
first_storage_entry_point_resources.vm_resources = ExecutionResources {
n_steps: 244,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 7)]),
}),
);
let storage_entry_point_resources = if_native(&test_contract)(
ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) },
first_storage_entry_point_resources.clone(),
);
};
}

let storage_entry_point_resources = first_storage_entry_point_resources.clone();

// The default VersionedConstants is used in the execute_directly call bellow.
let tracked_resource = test_contract.get_runnable_class().tracked_resource(
Expand All @@ -221,17 +193,16 @@ fn test_nested_library_call(test_contract: FeatureContract) {
..Default::default()
};

let library_call_resources = if_native(&test_contract)(
ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) },
ChargedResources::from_execution_resources(
&get_syscall_resources(SyscallSelector::LibraryCall)
+ &ExecutionResources {
n_steps: 377,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 15)]),
},
),
);
let mut library_call_resources =
ChargedResources { gas_for_fee: GasAmount(0), ..Default::default() };
if cairo_version == CairoVersion::Cairo1 {
library_call_resources.vm_resources = &get_syscall_resources(SyscallSelector::LibraryCall)
+ &ExecutionResources {
n_steps: 377,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 15)]),
}
}

let library_call_info = CallInfo {
call: library_entry_point,
Expand Down Expand Up @@ -260,17 +231,17 @@ fn test_nested_library_call(test_contract: FeatureContract) {
..Default::default()
};

let main_call_resources = if_native(&test_contract)(
ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) },
ChargedResources::from_execution_resources(
&(&get_syscall_resources(SyscallSelector::LibraryCall) * 3)
+ &ExecutionResources {
n_steps: 727,
n_memory_holes: 2,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 27)]),
},
),
);
let mut main_call_resources =
ChargedResources { gas_for_fee: GasAmount(0), ..Default::default() };
if cairo_version == CairoVersion::Cairo1 {
main_call_resources.vm_resources = &(&get_syscall_resources(SyscallSelector::LibraryCall)
* 3)
+ &ExecutionResources {
n_steps: 727,
n_memory_holes: 2,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 27)]),
}
}

let expected_call_info = CallInfo {
call: main_entry_point.clone(),
Expand Down
Loading
Loading