Skip to content

Commit e725600

Browse files
add paths to errors (#224)
## Type - [ ] feature - [ ] bugfix - [ ] dev (no functional changes, no API changes) - [ ] fmt (formatting, renaming) - [ ] build - [ ] docs - [ ] testing ## Description ## Breaking changes? - [ ] yes - [ ] no <!-- Reviewable:start --> - - - This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/bootloader-hints/224) <!-- Reviewable:end -->
1 parent 5f10bce commit e725600

File tree

1 file changed

+51
-18
lines changed
  • crates/stwo_run_and_prove/src

1 file changed

+51
-18
lines changed

crates/stwo_run_and_prove/src/main.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,26 @@ struct Args {
104104
enum StwoRunAndProveError {
105105
#[error(transparent)]
106106
Cli(#[from] clap::Error),
107+
#[error("IO error on file '{path:?}': {source}")]
108+
PathIO {
109+
path: PathBuf,
110+
source: std::io::Error,
111+
},
107112
#[error(transparent)]
108113
IO(#[from] std::io::Error),
109114
#[error(transparent)]
110115
VmImport(#[from] VmImportError),
111116
#[error(transparent)]
112117
CairoRun(Box<CairoRunError>),
113-
#[error(transparent)]
114-
Program(#[from] ProgramError),
118+
#[error("Program error on file '{path:?}': {source}")]
119+
Program { path: PathBuf, source: ProgramError },
115120
#[error(transparent)]
116121
Runner(#[from] RunnerError),
117-
#[error(transparent)]
118-
File(#[from] IoErrorWithPath),
122+
#[error("File error on file '{path:?}': {source}")]
123+
File {
124+
path: PathBuf,
125+
source: IoErrorWithPath,
126+
},
119127
#[error(transparent)]
120128
Serializing(#[from] sonic_rs::error::Error),
121129
#[error(transparent)]
@@ -135,6 +143,24 @@ impl From<CairoRunError> for StwoRunAndProveError {
135143
}
136144
}
137145

146+
impl From<(std::io::Error, PathBuf)> for StwoRunAndProveError {
147+
fn from((source, path): (std::io::Error, PathBuf)) -> Self {
148+
StwoRunAndProveError::PathIO { path, source }
149+
}
150+
}
151+
152+
impl From<(ProgramError, PathBuf)> for StwoRunAndProveError {
153+
fn from((source, path): (ProgramError, PathBuf)) -> Self {
154+
StwoRunAndProveError::Program { path, source }
155+
}
156+
}
157+
158+
impl From<(IoErrorWithPath, PathBuf)> for StwoRunAndProveError {
159+
fn from((source, path): (IoErrorWithPath, PathBuf)) -> Self {
160+
StwoRunAndProveError::File { path, source }
161+
}
162+
}
163+
138164
struct ProveConfig {
139165
proofs_dir: PathBuf,
140166
proof_format: ProofFormat,
@@ -171,7 +197,7 @@ fn main() -> Result<(), StwoRunAndProveError> {
171197
/// Saves the proof to the specified output dir (may include multiple proofs from repeated
172198
/// attempts). If `program_output` is provided, saves the program output to that path.
173199
fn stwo_run_and_prove(
174-
program: PathBuf,
200+
program_path: PathBuf,
175201
program_input: Option<PathBuf>,
176202
program_output: Option<PathBuf>,
177203
prove_config: ProveConfig,
@@ -192,8 +218,10 @@ fn stwo_run_and_prove(
192218
false,
193219
)?;
194220

195-
let program = get_program(program.as_path())?;
196-
let program_input = get_program_input(&program_input)?;
221+
let program = get_program(program_path.as_path())
222+
.map_err(|e| StwoRunAndProveError::from((e, program_path)))?;
223+
let program_input = get_program_input(&program_input)
224+
.map_err(|e| StwoRunAndProveError::from((e, program_input.unwrap_or_default())))?;
197225
info!("Running cairo run program.");
198226
let runner = cairo_run_program(&program, program_input, cairo_run_config)?;
199227
info!("Adapting prover input.");
@@ -216,12 +244,15 @@ fn prove_with_retries(
216244
prover: Box<dyn ProverTrait>,
217245
) -> Result<usize, StwoRunAndProveError> {
218246
let prover_params = match prove_config.prover_params_json {
219-
Some(ref path) => sonic_rs::from_str(&read_to_string(path)?)?,
247+
Some(ref path) => sonic_rs::from_str(
248+
&read_to_string(path).map_err(|e| StwoRunAndProveError::from((e, path.clone())))?,
249+
)?,
220250
None => default_prod_prover_parameters(),
221251
};
222252

223-
// create the directory if it doesn't exist
224-
std::fs::create_dir_all(&prove_config.proofs_dir)?;
253+
// create the directory if it doesn't exist, attach the proofs_dir path on error.
254+
std::fs::create_dir_all(&prove_config.proofs_dir)
255+
.map_err(|e| StwoRunAndProveError::from((e, prove_config.proofs_dir.clone())))?;
225256
let proof_format = prove_config.proof_format;
226257

227258
for i in 1..=prove_config.n_proof_attempts {
@@ -344,13 +375,15 @@ where
344375
<MC::H as MerkleHasher>::Hash: CairoSerialize,
345376
{
346377
let proof = prove_cairo::<MC>(prover_input, prover_params)?;
347-
348-
let mut proof_file = create_file(&proof_file_path)?;
378+
let mut proof_file = create_file(&proof_file_path)
379+
.map_err(|e| StwoRunAndProveError::from((e, proof_file_path.clone())))?;
349380

350381
match proof_format {
351382
ProofFormat::Json => {
352383
let serialized = sonic_rs::to_string_pretty(&proof)?;
353-
proof_file.write_all(serialized.as_bytes())?;
384+
proof_file
385+
.write_all(serialized.as_bytes())
386+
.map_err(|e| StwoRunAndProveError::from((e, proof_file_path)))?;
354387
}
355388
ProofFormat::CairoSerde => {
356389
let mut serialized: Vec<starknet_ff::FieldElement> = Vec::new();
@@ -360,7 +393,9 @@ where
360393
.map(|felt| format!("0x{felt:x}"))
361394
.collect();
362395
let serialized_hex = sonic_rs::to_string_pretty(&hex_strings)?;
363-
proof_file.write_all(serialized_hex.as_bytes())?;
396+
proof_file
397+
.write_all(serialized_hex.as_bytes())
398+
.map_err(|e| StwoRunAndProveError::from((e, proof_file_path)))?;
364399
}
365400
}
366401

@@ -393,7 +428,8 @@ fn write_output_to_file(
393428
Felt252::from_dec_str(line).map_err(|_| StwoRunAndProveError::OutputParsing)
394429
})
395430
.collect::<Result<Vec<Felt252>, _>>()?;
396-
std::fs::write(output_path, sonic_rs::to_string_pretty(&output_lines)?)?;
431+
std::fs::write(&output_path, sonic_rs::to_string_pretty(&output_lines)?)
432+
.map_err(|e| StwoRunAndProveError::from((e, output_path)))?;
397433
Ok(())
398434
}
399435

@@ -616,6 +652,3 @@ mod tests {
616652
);
617653
}
618654
}
619-
620-
// TODO(nitsan): Tests -
621-
// add an inner test to choose_channel_and_prove

0 commit comments

Comments
 (0)