Skip to content

Commit c30cc6e

Browse files
feat: create and use VirtualMachineConfig
1 parent fb62714 commit c30cc6e

6 files changed

Lines changed: 46 additions & 12 deletions

File tree

CHANGELOG.md

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

1313
#### Upcoming Changes
1414

15+
* feat: create and use VirtualMachineConfig [#2369](https://github.com/starkware-libs/cairo-vm/pull/2369)
16+
1517
* feat: consolidate CairoRunner ctors [#2368](https://github.com/starkware-libs/cairo-vm/pull/2368)
1618

1719
* 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/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,
@@ -165,6 +165,15 @@ pub struct CairoRunConfig {
165165
pub runner_mode: RunnerMode,
166166
}
167167

168+
impl CairoRunConfig {
169+
pub fn vm_config(&self) -> VirtualMachineConfig {
170+
VirtualMachineConfig {
171+
trace_enabled: self.trace_enabled,
172+
disable_trace_padding: self.disable_trace_padding,
173+
}
174+
}
175+
}
176+
168177
pub struct CairoRunner {
169178
pub vm: VirtualMachine,
170179
pub(crate) program: Program,
@@ -198,7 +207,7 @@ impl CairoRunner {
198207
pub fn new(program: &Program, config: &CairoRunConfig) -> CairoRunner {
199208
CairoRunner {
200209
program: program.clone(),
201-
vm: VirtualMachine::new(config.trace_enabled, config.disable_trace_padding),
210+
vm: VirtualMachine::new(&config.vm_config()),
202211
layout: config.layout.clone(),
203212
final_pc: None,
204213
program_base: None,

vm/src/vm/vm_core.rs

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

109+
pub struct VirtualMachineConfig {
110+
pub trace_enabled: bool,
111+
pub disable_trace_padding: bool,
112+
}
113+
114+
impl Default for VirtualMachineConfig {
115+
fn default() -> Self {
116+
Self {
117+
trace_enabled: false,
118+
disable_trace_padding: false,
119+
}
120+
}
121+
}
122+
109123
pub struct VirtualMachine {
110124
pub(crate) run_context: RunContext,
111125
pub builtin_runners: Vec<BuiltinRunner>,
@@ -135,15 +149,21 @@ pub struct VirtualMachine {
135149
pub(crate) relocation_table: Option<Vec<usize>>,
136150
}
137151

152+
impl Default for VirtualMachine {
153+
fn default() -> Self {
154+
Self::new(&VirtualMachineConfig::default())
155+
}
156+
}
157+
138158
impl VirtualMachine {
139-
pub fn new(trace_enabled: bool, disable_trace_padding: bool) -> VirtualMachine {
159+
pub fn new(config: &VirtualMachineConfig) -> VirtualMachine {
140160
let run_context = RunContext {
141161
pc: Relocatable::from((0, 0)),
142162
ap: 0,
143163
fp: 0,
144164
};
145165

146-
let trace = if trace_enabled {
166+
let trace = if config.trace_enabled {
147167
Some(Vec::<TraceEntry>::new())
148168
} else {
149169
None
@@ -160,7 +180,7 @@ impl VirtualMachine {
160180
segments: MemorySegmentManager::new(),
161181
rc_limits: None,
162182
run_finished: false,
163-
disable_trace_padding,
183+
disable_trace_padding: config.disable_trace_padding,
164184
instruction_cache: Vec::new(),
165185
#[cfg(feature = "test_utils")]
166186
hooks: None,
@@ -1660,7 +1680,7 @@ mod tests {
16601680
op1: MaybeRelocatable::Int(Felt252::from(10)),
16611681
};
16621682

1663-
let mut vm = VirtualMachine::new(false, false);
1683+
let mut vm = VirtualMachine::default();
16641684
vm.run_context.pc = Relocatable::from((0, 4));
16651685
vm.run_context.ap = 5;
16661686
vm.run_context.fp = 6;

0 commit comments

Comments
 (0)