Skip to content

Commit a84518a

Browse files
Add_option_to_save_run_outputs_and_execution_resources_to_file
1 parent 869a935 commit a84518a

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

.github/workflows/upload_artifacts_workflow.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- main
7+
- yairv/add_option_to_save_run_outputs_and_execution_resources_to_file
78

89
jobs:
910
artifacts-push:

crates/cairo-program-runner-lib/src/hints/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use cairo_vm::types::layout::CairoLayoutParams;
1010
use cairo_vm::types::layout_name::LayoutName;
1111
use cairo_vm::types::program::Program;
1212
use cairo_vm::vm::runners::cairo_pie::{CairoPie, StrippedProgram};
13+
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
1314
use cairo_vm::Felt252;
1415
use num_traits::ToPrimitive;
1516
use serde::de::Error as SerdeError;
@@ -539,3 +540,9 @@ pub struct PedersenMerkleInput {
539540
pub prev_leaf: Felt252,
540541
pub new_leaf: Felt252,
541542
}
543+
544+
#[derive(Serialize)]
545+
pub struct OutputER {
546+
pub output: Vec<Felt252>,
547+
pub execution_resources: ExecutionResources,
548+
}

crates/cairo-program-runner/src/bin/cairo_program_runner/main.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use std::io::{self, Write};
44
use std::path::PathBuf;
55

66
use bincode::enc::write::Writer;
7+
use cairo_program_runner_lib::types::OutputER;
78
use cairo_program_runner_lib::utils::{get_cairo_run_config, get_program, get_program_input};
89
use cairo_vm::types::layout_name::LayoutName;
910

1011
use cairo_program_runner_lib::cairo_run_program;
11-
use cairo_vm::cairo_run;
1212
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
13+
use cairo_vm::{cairo_run, Felt252};
1314
use clap::Parser;
1415
use tempfile::NamedTempFile;
1516

@@ -87,6 +88,11 @@ struct Args {
8788
the layout."
8889
)]
8990
allow_missing_builtins: bool,
91+
#[clap(
92+
long = "output_and_er_file",
93+
help = "Write program output and execution resources as JSON to this file"
94+
)]
95+
output_and_er_file: Option<PathBuf>,
9096
}
9197
struct FileWriter {
9298
buf_writer: io::BufWriter<std::fs::File>,
@@ -137,7 +143,26 @@ fn main() -> Result<(), Box<dyn Error>> {
137143
args.allow_missing_builtins,
138144
)?;
139145

140-
let runner = cairo_run_program(&program, program_input_contents, cairo_run_config)?;
146+
let mut runner = cairo_run_program(&program, program_input_contents, cairo_run_config)?;
147+
148+
if let Some(output_and_er_file) = args.output_and_er_file {
149+
let mut output_buffer = String::new();
150+
runner.vm.write_output(&mut output_buffer)?;
151+
let output_lines = output_buffer
152+
.lines()
153+
.map(|line| {
154+
Felt252::from_dec_str(line)
155+
.map_err(|_| format!("Failed to parse output line as Felt decimal: {}", line))
156+
})
157+
.collect::<Result<Vec<Felt252>, _>>()?;
158+
let er = runner.get_execution_resources()?;
159+
let to_dump = OutputER {
160+
output: output_lines,
161+
execution_resources: er,
162+
};
163+
let json = serde_json::to_string_pretty(&to_dump)?;
164+
std::fs::write(&output_and_er_file, json)?;
165+
}
141166

142167
// Handle Cairo PIE output if specified
143168
if let Some(pie_output_path) = args.cairo_pie_output {

0 commit comments

Comments
 (0)