Skip to content

Commit e87dfa0

Browse files
fmolettapefontana
andauthored
Compute ExecutionResources.n_steps without requiring trace (#1222)
* Get n_steps without using trace * Add cahngelog entry --------- Co-authored-by: Pedro Fontana <[email protected]>
1 parent 8e022a1 commit e87dfa0

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#### Upcoming Changes
44

5+
* BREAKING: Compute `ExecutionResources.n_steps` without requiring trace [#1222](https://github.com/lambdaclass/cairo-rs/pull/1222)
6+
7+
* `CairoRunner::get_execution_resources` return's `n_steps` field value is now set to `vm.current_step` instead of `0` if both `original_steps` and `trace` are set to `None`
8+
59
* Add `RunResources::get_n_steps` method [#1225](https://github.com/lambdaclass/cairo-rs/pull/1225)
610

711
* fix: pin Cairo compiler version [#1220](https://github.com/lambdaclass/cairo-rs/pull/1220)

src/vm/runners/cairo_runner.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,11 @@ impl CairoRunner {
859859
) -> Result<ExecutionResources, TraceError> {
860860
let n_steps = match self.original_steps {
861861
Some(x) => x,
862-
None => vm.trace.as_ref().map(|x| x.len()).unwrap_or(0),
862+
None => vm
863+
.trace
864+
.as_ref()
865+
.map(|x| x.len())
866+
.unwrap_or(vm.current_step),
863867
};
864868
let n_memory_holes = self.get_memory_holes(vm)?;
865869

@@ -1238,6 +1242,7 @@ impl MulAssign<usize> for ExecutionResources {
12381242
#[cfg(test)]
12391243
mod tests {
12401244
use super::*;
1245+
use crate::cairo_run::{cairo_run, CairoRunConfig};
12411246
use crate::stdlib::collections::{HashMap, HashSet};
12421247
use crate::vm::runners::builtin_runner::{
12431248
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME,
@@ -3492,19 +3497,54 @@ mod tests {
34923497
let program = program!();
34933498

34943499
let cairo_runner = cairo_runner!(program);
3495-
let mut vm = vm!();
3500+
let mut vm: VirtualMachine = vm!();
34963501

34973502
vm.segments.segment_used_sizes = Some(vec![4]);
3503+
vm.current_step = 10;
34983504
assert_eq!(
34993505
cairo_runner.get_execution_resources(&vm),
35003506
Ok(ExecutionResources {
3501-
n_steps: 0,
3507+
n_steps: 10,
35023508
n_memory_holes: 0,
35033509
builtin_instance_counter: HashMap::new(),
35043510
}),
35053511
);
35063512
}
35073513

3514+
#[test]
3515+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
3516+
fn get_execution_resources_run_program() {
3517+
let program_data = include_bytes!("../../../cairo_programs/fibonacci.json");
3518+
let cairo_run_config = CairoRunConfig {
3519+
entrypoint: "main",
3520+
trace_enabled: true,
3521+
relocate_mem: false,
3522+
layout: "all_cairo",
3523+
proof_mode: false,
3524+
secure_run: Some(false),
3525+
};
3526+
let mut hint_executor = BuiltinHintProcessor::new_empty();
3527+
let (runner, vm) = cairo_run(program_data, &cairo_run_config, &mut hint_executor).unwrap();
3528+
assert_eq!(runner.get_execution_resources(&vm).unwrap().n_steps, 80);
3529+
}
3530+
3531+
#[test]
3532+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
3533+
fn get_execution_resources_run_program_no_trace() {
3534+
let program_data = include_bytes!("../../../cairo_programs/fibonacci.json");
3535+
let cairo_run_config = CairoRunConfig {
3536+
entrypoint: "main",
3537+
trace_enabled: false,
3538+
relocate_mem: false,
3539+
layout: "all_cairo",
3540+
proof_mode: false,
3541+
secure_run: Some(false),
3542+
};
3543+
let mut hint_executor = BuiltinHintProcessor::new_empty();
3544+
let (runner, vm) = cairo_run(program_data, &cairo_run_config, &mut hint_executor).unwrap();
3545+
assert_eq!(runner.get_execution_resources(&vm).unwrap().n_steps, 80);
3546+
}
3547+
35083548
#[test]
35093549
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
35103550
fn get_execution_resources_empty_builtins() {

0 commit comments

Comments
 (0)