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

feat: unify test utils to a single feature #1755

Merged
merged 5 commits into from
May 9, 2024
Merged
Changes from 2 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

#### Upcoming Changes

* feat: unify `hooks`, `print` and `skip_next_instruction_hint` features as a single `test_utils` feature [#1755](https://github.com/lambdaclass/cairo-vm/pull/1755)
* BREAKING: removed the above features

* feat: Add hint `U256InvModN` to `Cairo1HintProcessor` [#1744](https://github.com/lambdaclass/cairo-vm/pull/1744)

* perf: use a more compact representation for `MemoryCell` [#1672](https://github.com/lambdaclass/cairo-vm/pull/1672)
9 changes: 1 addition & 8 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -31,18 +31,11 @@ tracer = []
mod_builtin = []

# Note that these features are not retro-compatible with the cairo Python VM.
test_utils = [
"skip_next_instruction_hint",
"hooks",
"print",
] # This feature will reference every test-oriented feature
skip_next_instruction_hint = []
hooks = []
test_utils = ["std"] # This feature will reference every test-oriented feature
arbitrary = ["dep:arbitrary", "std", "starknet-types-core/arbitrary", "starknet-types-core/std"]
# Allows extending the set of hints for the current vm run from within a hint.
# For a usage example checkout vm/src/tests/run_deprecated_contract_class_simplified.rs
extensive_hints = []
print = ["std"]

[dependencies]
zip = {version = "0.6.6", optional = true }
Original file line number Diff line number Diff line change
@@ -114,10 +114,10 @@ use crate::{
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};

#[cfg(feature = "skip_next_instruction_hint")]
#[cfg(feature = "test_utils")]
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;

#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
use crate::hint_processor::builtin_hint_processor::print::{print_array, print_dict, print_felt};
use crate::hint_processor::builtin_hint_processor::secp::secp_utils::{
SECP256R1_ALPHA, SECP256R1_P,
@@ -856,13 +856,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
constants,
)
}
#[cfg(feature = "skip_next_instruction_hint")]
#[cfg(feature = "test_utils")]
hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm),
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
hint_code::PRINT_FELT => print_felt(vm, &hint_data.ids_data, &hint_data.ap_tracking),
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
hint_code::PRINT_ARR => print_array(vm, &hint_data.ids_data, &hint_data.ap_tracking),
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
hint_code::PRINT_DICT => {
print_dict(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
5 changes: 4 additions & 1 deletion vm/src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
@@ -1427,15 +1427,18 @@ if x % 2 != 0:
x = PRIME - x
ids.x.low = x & ((1<<128)-1)
ids.x.high = x >> 128";
#[cfg(feature = "skip_next_instruction_hint")]
#[cfg(feature = "test_utils")]
pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()";

#[cfg(feature = "test_utils")]
pub const PRINT_FELT: &str = "print(ids.x)";

#[cfg(feature = "test_utils")]
pub const PRINT_ARR: &str = r#"print(bytes.fromhex(f"{ids.name:062x}").decode().replace('\x00',''))
arr = [memory[ids.arr + i] for i in range(ids.arr_len)]
print(arr)"#;

#[cfg(feature = "test_utils")]
pub const PRINT_DICT: &str = r#"print(bytes.fromhex(f"{ids.name:062x}").decode().replace('\x00',''))
data = __dict_manager.get_dict(ids.dict_ptr)
print(
7 changes: 4 additions & 3 deletions vm/src/hint_processor/builtin_hint_processor/mod.rs
Original file line number Diff line number Diff line change
@@ -19,15 +19,16 @@ pub mod memset_utils;
mod mod_circuit;
pub mod poseidon_utils;
pub mod pow_utils;
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "test_utils")))]
pub mod print;
pub mod secp;
pub mod segments;
pub mod set;
pub mod sha256_utils;
pub mod signature;
#[cfg(feature = "skip_next_instruction_hint")]
#[cfg_attr(docsrs, doc(cfg(feature = "skip_next_instruction_hint")))]
#[cfg(feature = "test_utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "test_utils")))]
pub mod skip_next_instruction;
pub mod squash_dict_utils;
pub mod uint256_utils;
4 changes: 1 addition & 3 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2,9 +2,7 @@
//!
//! ## Feature Flags
//! - `std`: Enables usage of the [`std`] standard library. Enabled by default.
//! - `skip_next_instruction_hint`: Enable the `skip_next_instruction()` hint. Not enabled by default.
//! - `hooks`: Enable [`Hooks`](crate::vm::hooks::Hooks) support for the [VirtualMachine](vm::vm_core::VirtualMachine). Not enabled by default.
//! - `test_utils`: Enables test utils (`hooks` and `skip_next_instruction` features). Not enabled by default.
//! - `test_utils`: Enables test utils ([`Hooks`](crate::vm::hooks::Hooks) support for the [VirtualMachine](vm::vm_core::VirtualMachine) and the `print_*` and `skip_next_instruction()` hints). Not enabled by default.
//! - `cairo-1-hints`: Enable hints that were introduced in Cairo 1. Not enabled by default.
//! - `arbitrary`: Enables implementations of [`arbitrary::Arbitrary`](https://docs.rs/arbitrary/latest/arbitrary/) for some structs. Not enabled by default.

8 changes: 4 additions & 4 deletions vm/src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
@@ -1043,28 +1043,28 @@ fn divmod_igcdex_not_one() {
}

#[test]
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
fn cairo_run_print_felt() {
let program_data = include_bytes!("../../../cairo_programs/print_feature/print_felt.json");
run_program_simple(program_data);
}

#[test]
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
fn cairo_run_print_array() {
let program_data = include_bytes!("../../../cairo_programs/print_feature/print_array.json");
run_program_simple(program_data);
}

#[test]
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
fn cairo_run_print_dict_felt() {
let program_data = include_bytes!("../../../cairo_programs/print_feature/print_dict_felt.json");
run_program_simple(program_data);
}

#[test]
#[cfg(feature = "print")]
#[cfg(feature = "test_utils")]
fn cairo_run_print_dict_array() {
let program_data =
include_bytes!("../../../cairo_programs/print_feature/print_dict_array.json");
2 changes: 1 addition & 1 deletion vm/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ mod pedersen_test;
mod struct_test;

mod cairo_pie_test;
#[cfg(feature = "skip_next_instruction_hint")]
#[cfg(feature = "test_utils")]
mod skip_instruction_test;

//For simple programs that should just succeed and have no special needs.
4 changes: 2 additions & 2 deletions vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,6 @@ pub mod trace;
pub mod vm_core;
pub mod vm_memory;

#[cfg(feature = "hooks")]
#[cfg_attr(docsrs, doc(cfg(feature = "hooks")))]
#[cfg(feature = "test_utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "test_utils")))]
pub mod hooks;
2 changes: 1 addition & 1 deletion vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
@@ -672,7 +672,7 @@ impl CairoRunner {
.hints_collection
.hints_ranges
.clone();
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
vm.execute_before_first_step(self, &hint_data)?;
while vm.run_context.pc != address && !hint_processor.consumed() {
vm.step(
22 changes: 11 additions & 11 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ pub struct VirtualMachine {
skip_instruction_execution: bool,
run_finished: bool,
instruction_cache: Vec<Option<Instruction>>,
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
pub(crate) hooks: crate::vm::hooks::Hooks,
pub(crate) relocation_table: Option<Vec<usize>>,
}
@@ -119,7 +119,7 @@ impl VirtualMachine {
rc_limits: None,
run_finished: false,
instruction_cache: Vec::new(),
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
hooks: Default::default(),
relocation_table: None,
}
@@ -562,10 +562,10 @@ impl VirtualMachine {
constants,
)?;

#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
self.execute_pre_step_instruction(hint_processor, exec_scopes, hint_datas, constants)?;
self.step_instruction()?;
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
self.execute_post_step_instruction(hint_processor, exec_scopes, hint_datas, constants)?;

Ok(())
@@ -1165,7 +1165,7 @@ pub struct VirtualMachineBuilder {
pub(crate) current_step: usize,
skip_instruction_execution: bool,
run_finished: bool,
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
pub(crate) hooks: crate::vm::hooks::Hooks,
}

@@ -1185,7 +1185,7 @@ impl Default for VirtualMachineBuilder {
skip_instruction_execution: false,
segments: MemorySegmentManager::new(),
run_finished: false,
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
hooks: Default::default(),
}
}
@@ -1230,7 +1230,7 @@ impl VirtualMachineBuilder {
self
}

#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
pub fn hooks(mut self, hooks: crate::vm::hooks::Hooks) -> VirtualMachineBuilder {
self.hooks = hooks;
self
@@ -1247,7 +1247,7 @@ impl VirtualMachineBuilder {
rc_limits: None,
run_finished: self.run_finished,
instruction_cache: Vec::new(),
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
hooks: self.hooks,
relocation_table: None,
}
@@ -4384,15 +4384,15 @@ mod tests {
fp: 1,
}]));

#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
fn before_first_step_hook(
_vm: &mut VirtualMachine,
_runner: &mut CairoRunner,
_hint_data: &[Box<dyn Any>],
) -> Result<(), VirtualMachineError> {
Err(VirtualMachineError::Unexpected)
}
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
let virtual_machine_builder = virtual_machine_builder.hooks(crate::vm::hooks::Hooks::new(
Some(std::sync::Arc::new(before_first_step_hook)),
None,
@@ -4426,7 +4426,7 @@ mod tests {
fp: 1,
}])
);
#[cfg(feature = "hooks")]
#[cfg(feature = "test_utils")]
{
let program = crate::types::program::Program::from_bytes(
include_bytes!("../../../cairo_programs/sqrt.json"),
Loading