Skip to content

Commit 156a1fe

Browse files
committed
Refactor running related code
commit-id:a9dfa87d
1 parent 3e17fd5 commit 156a1fe

File tree

4 files changed

+180
-187
lines changed

4 files changed

+180
-187
lines changed

crates/forge-runner/src/gas.rs

-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ pub fn check_available_gas(
145145
match summary {
146146
TestCaseSummary::Passed {
147147
name,
148-
arguments,
149148
gas_info,
150149
debugging_trace,
151150
..
@@ -176,7 +175,6 @@ pub fn check_available_gas(
176175
"\n\tTest cost exceeded the available gas. Consumed l1_gas: ~{}, l1_data_gas: ~{}, l2_gas: ~{}",
177176
gas_info.l1_gas, gas_info.l1_data_gas, gas_info.l2_gas
178177
)),
179-
arguments,
180178
fuzzer_args: Vec::default(),
181179
test_statistics: (),
182180
debugging_trace,

crates/forge-runner/src/running.rs

+89-77
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::{Result, ensure};
77
use blockifier::execution::contract_class::TrackedResource;
88
use blockifier::execution::entry_point::EntryPointExecutionContext;
99
use blockifier::state::cached_state::CachedState;
10-
use cairo_lang_runner::{Arg, RunResult, SierraCasmRunner};
10+
use cairo_lang_runner::{Arg, RunResultValue, SierraCasmRunner};
1111
use cairo_lang_sierra::extensions::NamedType;
1212
use cairo_lang_sierra::extensions::bitwise::BitwiseType;
1313
use cairo_lang_sierra::extensions::circuit::{AddModType, MulModType};
@@ -97,7 +97,6 @@ pub fn run_test(
9797
extract_test_case_summary(
9898
run_result,
9999
&case,
100-
vec![],
101100
&test_runner_config.contracts_data,
102101
&versioned_program_path,
103102
)
@@ -138,29 +137,54 @@ pub(crate) fn run_fuzz_test(
138137
extract_test_case_summary(
139138
run_result,
140139
&case,
141-
vec![],
142140
&test_runner_config.contracts_data,
143141
&versioned_program_path,
144142
)
145143
})
146144
}
147145

148-
pub struct RunResultWithInfo {
149-
pub(crate) run_result: Result<RunResult, Box<CairoRunError>>,
146+
pub enum RunStatus {
147+
Success(Vec<Felt252>),
148+
Panic(Vec<Felt252>),
149+
}
150+
151+
impl From<RunResultValue> for RunStatus {
152+
fn from(value: RunResultValue) -> Self {
153+
match value {
154+
RunResultValue::Success(value) => Self::Success(value),
155+
RunResultValue::Panic(value) => Self::Panic(value),
156+
}
157+
}
158+
}
159+
160+
pub struct RunCompleted {
161+
pub(crate) status: RunStatus,
150162
pub(crate) call_trace: Rc<RefCell<CallTrace>>,
151163
pub(crate) gas_used: GasVector,
152164
pub(crate) used_resources: UsedResources,
153165
pub(crate) encountered_errors: EncounteredErrors,
154166
pub(crate) fuzzer_args: Vec<String>,
155167
}
156168

169+
pub struct RunError {
170+
pub(crate) error: Box<CairoRunError>,
171+
pub(crate) call_trace: Rc<RefCell<CallTrace>>,
172+
pub(crate) encountered_errors: EncounteredErrors,
173+
pub(crate) fuzzer_args: Vec<String>,
174+
}
175+
176+
pub enum RunResult {
177+
Completed(Box<RunCompleted>),
178+
Error(RunError),
179+
}
180+
157181
#[expect(clippy::too_many_lines)]
158182
pub fn run_test_case(
159183
case: &TestCaseWithResolvedConfig,
160184
casm_program: &AssembledProgramWithDebugInfo,
161185
runtime_config: &RuntimeConfig,
162186
fuzzer_rng: Option<Arc<Mutex<StdRng>>>,
163-
) -> Result<RunResultWithInfo> {
187+
) -> Result<RunResult> {
164188
ensure!(
165189
case.config
166190
.available_gas
@@ -255,7 +279,7 @@ pub fn run_test_case(
255279

256280
let ap = runner.relocated_trace.as_ref().unwrap().last().unwrap().ap;
257281

258-
let (results_data, gas_counter) = get_results_data(
282+
let results_data = get_results_data(
259283
&case.test_details.return_types,
260284
&runner.relocated_memory,
261285
ap,
@@ -274,7 +298,7 @@ pub fn run_test_case(
274298

275299
update_top_call_vm_trace(&mut forge_runtime, &mut runner);
276300

277-
Ok((gas_counter, runner.relocated_memory, value))
301+
Ok(value)
278302
}
279303
Err(err) => Err(err),
280304
};
@@ -287,7 +311,7 @@ pub fn run_test_case(
287311
.encountered_errors
288312
.clone();
289313

290-
let call_trace_ref = get_call_trace_ref(&mut forge_runtime);
314+
let call_trace = get_call_trace_ref(&mut forge_runtime);
291315

292316
update_top_call_resources(&mut forge_runtime, &tracked_resource);
293317
update_top_call_l1_resources(&mut forge_runtime);
@@ -303,25 +327,27 @@ pub fn run_test_case(
303327
let transaction_context = get_context(&forge_runtime).tx_context.clone();
304328
let used_resources =
305329
get_all_used_resources(forge_runtime, &transaction_context, tracked_resource);
306-
let gas = calculate_used_gas(
330+
let gas_used = calculate_used_gas(
307331
&transaction_context,
308332
&mut cached_state,
309333
used_resources.clone(),
310334
)?;
311335

312-
Ok(RunResultWithInfo {
313-
run_result: run_result.map(|(gas_counter, memory, value)| RunResult {
314-
used_resources: used_resources.execution_resources.clone(),
315-
gas_counter,
316-
memory,
317-
value,
318-
profiling_info: None,
336+
Ok(match run_result {
337+
Ok(result) => RunResult::Completed(Box::from(RunCompleted {
338+
status: result.into(),
339+
call_trace,
340+
gas_used,
341+
used_resources,
342+
encountered_errors,
343+
fuzzer_args,
344+
})),
345+
Err(error) => RunResult::Error(RunError {
346+
error,
347+
call_trace,
348+
encountered_errors,
349+
fuzzer_args,
319350
}),
320-
gas_used: gas,
321-
used_resources,
322-
call_trace: call_trace_ref,
323-
encountered_errors,
324-
fuzzer_args,
325351
})
326352
}
327353

@@ -333,7 +359,7 @@ pub fn get_results_data(
333359
return_types: &[(GenericTypeId, i16)],
334360
cells: &[Option<Felt252>],
335361
mut ap: usize,
336-
) -> (Vec<(GenericTypeId, Vec<Felt252>)>, Option<Felt252>) {
362+
) -> Vec<(GenericTypeId, Vec<Felt252>)> {
337363
let mut results_data = vec![];
338364
for (ty, ty_size) in return_types.iter().rev() {
339365
let size = *ty_size as usize;
@@ -345,11 +371,10 @@ pub fn get_results_data(
345371
}
346372

347373
// Handling implicits.
348-
let mut gas_counter = None;
349374
results_data.retain_mut(|(ty, values)| {
350375
let generic_ty = ty;
351376
if *generic_ty == GasBuiltinType::ID {
352-
gas_counter = Some(values.remove(0));
377+
values.remove(0);
353378
assert!(values.is_empty());
354379
false
355380
} else {
@@ -372,73 +397,60 @@ pub fn get_results_data(
372397
}
373398
});
374399

375-
(results_data, gas_counter)
400+
results_data
376401
}
377402

378403
fn extract_test_case_summary(
379-
run_result: Result<RunResultWithInfo>,
404+
run_result: Result<RunResult>,
380405
case: &TestCaseWithResolvedConfig,
381-
args: Vec<Felt>,
382406
contracts_data: &ContractsData,
383407
versioned_program_path: &Utf8Path,
384408
) -> TestCaseSummary<Single> {
385409
match run_result {
386-
Ok(result_with_info) => {
387-
match result_with_info.run_result {
388-
Ok(run_result) => TestCaseSummary::from_run_result_and_info(
389-
run_result,
390-
case,
391-
args,
392-
result_with_info.fuzzer_args,
393-
result_with_info.gas_used,
394-
result_with_info.used_resources,
395-
&result_with_info.call_trace,
396-
&result_with_info.encountered_errors,
397-
contracts_data,
398-
versioned_program_path,
399-
),
400-
// CairoRunError comes from VirtualMachineError which may come from HintException that originates in TestExecutionSyscallHandler
401-
Err(error) => {
402-
let mut message = format!(
403-
"\n {}\n",
404-
error.to_string().replace(" Custom Hint Error: ", "\n ")
405-
);
406-
if let CairoRunError::VirtualMachine(VirtualMachineError::UnfinishedExecution) =
407-
*error
408-
{
409-
message.push_str(
410-
"\n Suggestion: Consider using the flag `--max-n-steps` to increase allowed limit of steps",
411-
);
412-
}
413-
TestCaseSummary::Failed {
414-
name: case.name.clone(),
415-
msg: Some(message).map(|msg| {
416-
add_backtrace_footer(
417-
msg,
418-
contracts_data,
419-
&result_with_info.encountered_errors,
420-
)
421-
}),
422-
arguments: args,
423-
fuzzer_args: result_with_info.fuzzer_args,
424-
test_statistics: (),
425-
debugging_trace: cfg!(feature = "debugging").then(|| {
426-
debugging::Trace::new(
427-
&result_with_info.call_trace.borrow(),
428-
contracts_data,
429-
case.name.clone(),
430-
)
431-
}),
432-
}
410+
Ok(run_result) => match run_result {
411+
RunResult::Completed(run_completed) => TestCaseSummary::from_run_completed(
412+
*run_completed,
413+
case,
414+
contracts_data,
415+
versioned_program_path,
416+
),
417+
RunResult::Error(run_error) => {
418+
let mut message = format!(
419+
"\n {}\n",
420+
run_error
421+
.error
422+
.to_string()
423+
.replace(" Custom Hint Error: ", "\n ")
424+
);
425+
if let CairoRunError::VirtualMachine(VirtualMachineError::UnfinishedExecution) =
426+
*run_error.error
427+
{
428+
message.push_str(
429+
"\n Suggestion: Consider using the flag `--max-n-steps` to increase allowed limit of steps",
430+
);
431+
}
432+
TestCaseSummary::Failed {
433+
name: case.name.clone(),
434+
msg: Some(message).map(|msg| {
435+
add_backtrace_footer(msg, contracts_data, &run_error.encountered_errors)
436+
}),
437+
fuzzer_args: run_error.fuzzer_args,
438+
test_statistics: (),
439+
debugging_trace: cfg!(feature = "debugging").then(|| {
440+
debugging::Trace::new(
441+
&run_error.call_trace.borrow(),
442+
contracts_data,
443+
case.name.clone(),
444+
)
445+
}),
433446
}
434447
}
435-
}
448+
},
436449
// `ForkStateReader.get_block_info`, `get_fork_state_reader, `calculate_used_gas` may return an error
437450
// `available_gas` may be specified with Scarb ~2.4
438451
Err(error) => TestCaseSummary::Failed {
439452
name: case.name.clone(),
440453
msg: Some(error.to_string()),
441-
arguments: args,
442454
fuzzer_args: Vec::default(),
443455
test_statistics: (),
444456
debugging_trace: None,

0 commit comments

Comments
 (0)