Skip to content

Commit 0862f54

Browse files
feat: create and use VirtualMachineConfig
1 parent f004484 commit 0862f54

7 files changed

Lines changed: 44 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Both branches support Stwo prover opcodes (Blake2s, QM31) since v2.0.0.
1313
#### Upcoming Changes
1414

1515
* Add Stwo cairo runner API [#2351](https://github.com/starkware-libs/cairo-vm/pull/2351)
16+
* feat: create and use VirtualMachineConfig [#2369](https://github.com/starkware-libs/cairo-vm/pull/2369)
17+
1618
* feat: consolidate CairoRunner ctors [#2368](https://github.com/starkware-libs/cairo-vm/pull/2368)
1719

1820
* feat: create and use CairoRunConfig as ctor arg for CairoRunner ctors [#2367](https://github.com/starkware-libs/cairo-vm/pull/2367)

hint_accountant/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn run() {
4949
whitelists.push(whitelist_file.allowed_hint_expressions);
5050
}
5151
}
52-
let mut vm = VirtualMachine::new(false, false);
52+
let mut vm = VirtualMachine::default();
5353
let mut hint_executor = BuiltinHintProcessor::new_empty();
5454
let (ap_tracking_data, reference_ids, references, mut exec_scopes, accessible_scopes) = (
5555
ApTracking::default(),

vm/src/hint_processor/builtin_hint_processor/secp/cairo0_hints.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ mod tests {
397397

398398
#[test]
399399
fn test_is_on_curve_2() {
400-
let mut vm = VirtualMachine::new(false, false);
400+
let mut vm = VirtualMachine::default();
401401
vm.set_fp(1);
402402
let ids_data = non_continuous_ids_data![("is_on_curve", -1)];
403403
vm.segments = segments![((1, 0), 1)];
@@ -428,7 +428,7 @@ mod tests {
428428

429429
#[test]
430430
fn test_compute_q_mod_prime() {
431-
let mut vm = VirtualMachine::new(false, false);
431+
let mut vm = VirtualMachine::default();
432432

433433
let ap_tracking = ApTracking::default();
434434

@@ -454,7 +454,7 @@ mod tests {
454454

455455
#[test]
456456
fn test_compute_ids_high_low() {
457-
let mut vm = VirtualMachine::new(false, false);
457+
let mut vm = VirtualMachine::default();
458458

459459
let value = BigInt::from(25);
460460
let shift = BigInt::from(12);
@@ -523,7 +523,7 @@ mod tests {
523523

524524
#[test]
525525
fn test_r1_get_point_from_x() {
526-
let mut vm = VirtualMachine::new(false, false);
526+
let mut vm = VirtualMachine::default();
527527
vm.set_fp(10);
528528

529529
let ids_data = non_continuous_ids_data![("x", -10), ("v", -7)];
@@ -582,7 +582,7 @@ mod tests {
582582

583583
#[test]
584584
fn test_reduce_value() {
585-
let mut vm = VirtualMachine::new(false, false);
585+
let mut vm = VirtualMachine::default();
586586

587587
//Initialize fp
588588
vm.run_context.fp = 10;
@@ -637,7 +637,7 @@ mod tests {
637637

638638
#[test]
639639
fn test_reduce_x() {
640-
let mut vm = VirtualMachine::new(false, false);
640+
let mut vm = VirtualMachine::default();
641641

642642
//Initialize fp
643643
vm.run_context.fp = 10;

vm/src/hint_processor/cairo_1_hint_processor/dict_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ mod tests {
249249
/// Test for relocate_all_dictionaries error cases
250250
#[test]
251251
fn test_relocate_all_dictionaries_errors() {
252-
let mut vm = VirtualMachine::new(false, false);
252+
let mut vm = VirtualMachine::default();
253253

254254
// Test 1: First segment is a temporary segment (should error)
255255
{
@@ -297,7 +297,7 @@ mod tests {
297297
/// Test for relocate_all_dictionaries when no temporary segments
298298
#[test]
299299
fn test_relocate_all_dictionaries_no_temporary_segments() {
300-
let mut vm = VirtualMachine::new(false, false);
300+
let mut vm = VirtualMachine::default();
301301
let mut dict_manager = DictManagerExecScope::new(false);
302302

303303
// Adding some trackers should not cause any errors

vm/src/utils.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub mod test_utils {
233233

234234
macro_rules! vm_with_range_check {
235235
() => {{
236-
let mut vm = VirtualMachine::new(false, false);
236+
let mut vm = VirtualMachine::default();
237237
vm.builtin_runners = vec![
238238
$crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner::<8>::new(
239239
Some(8),
@@ -418,11 +418,14 @@ pub mod test_utils {
418418

419419
macro_rules! vm {
420420
() => {{
421-
crate::vm::vm_core::VirtualMachine::new(false, false)
421+
crate::vm::vm_core::VirtualMachine::default()
422422
}};
423423

424424
($use_trace:expr) => {{
425-
crate::vm::vm_core::VirtualMachine::new($use_trace, false)
425+
crate::vm::vm_core::VirtualMachine::new(&crate::vm::vm_core::VirtualMachineConfig {
426+
trace_enabled: $use_trace,
427+
disable_trace_padding: false,
428+
})
426429
}};
427430
}
428431
pub(crate) use vm;

vm/src/vm/runners/cairo_runner.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::vm::trace::trace_entry::TraceEntry;
1+
use crate::vm::{trace::trace_entry::TraceEntry, vm_core::VirtualMachineConfig};
22

33
use std::{
44
any::Any,
@@ -201,6 +201,15 @@ impl CairoRunConfig {
201201
}
202202
}
203203

204+
impl CairoRunConfig {
205+
pub fn vm_config(&self) -> VirtualMachineConfig {
206+
VirtualMachineConfig {
207+
trace_enabled: self.trace_enabled,
208+
disable_trace_padding: self.disable_trace_padding,
209+
}
210+
}
211+
}
212+
204213
pub struct CairoRunner {
205214
pub vm: VirtualMachine,
206215
pub(crate) program: Program,
@@ -234,7 +243,7 @@ impl CairoRunner {
234243
pub fn new(program: &Program, config: &CairoRunConfig) -> CairoRunner {
235244
CairoRunner {
236245
program: program.clone(),
237-
vm: VirtualMachine::new(config.trace_enabled, config.disable_trace_padding),
246+
vm: VirtualMachine::new(&config.vm_config()),
238247
layout: config.layout.clone(),
239248
final_pc: None,
240249
program_base: None,

vm/src/vm/vm_core.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ impl DeducedOperands {
106106
}
107107
}
108108

109+
#[derive(Default)]
110+
pub struct VirtualMachineConfig {
111+
pub trace_enabled: bool,
112+
pub disable_trace_padding: bool,
113+
}
114+
109115
pub struct VirtualMachine {
110116
pub(crate) run_context: RunContext,
111117
pub builtin_runners: Vec<BuiltinRunner>,
@@ -135,15 +141,21 @@ pub struct VirtualMachine {
135141
pub(crate) relocation_table: Option<Vec<usize>>,
136142
}
137143

144+
impl Default for VirtualMachine {
145+
fn default() -> Self {
146+
Self::new(&VirtualMachineConfig::default())
147+
}
148+
}
149+
138150
impl VirtualMachine {
139-
pub fn new(trace_enabled: bool, disable_trace_padding: bool) -> VirtualMachine {
151+
pub fn new(config: &VirtualMachineConfig) -> VirtualMachine {
140152
let run_context = RunContext {
141153
pc: Relocatable::from((0, 0)),
142154
ap: 0,
143155
fp: 0,
144156
};
145157

146-
let trace = if trace_enabled {
158+
let trace = if config.trace_enabled {
147159
Some(Vec::<TraceEntry>::new())
148160
} else {
149161
None
@@ -160,7 +172,7 @@ impl VirtualMachine {
160172
segments: MemorySegmentManager::new(),
161173
rc_limits: None,
162174
run_finished: false,
163-
disable_trace_padding,
175+
disable_trace_padding: config.disable_trace_padding,
164176
instruction_cache: Vec::new(),
165177
#[cfg(feature = "test_utils")]
166178
hooks: None,
@@ -1660,7 +1672,7 @@ mod tests {
16601672
op1: MaybeRelocatable::Int(Felt252::from(10)),
16611673
};
16621674

1663-
let mut vm = VirtualMachine::new(false, false);
1675+
let mut vm = VirtualMachine::default();
16641676
vm.run_context.pc = Relocatable::from((0, 4));
16651677
vm.run_context.ap = 5;
16661678
vm.run_context.fp = 6;

0 commit comments

Comments
 (0)