Skip to content

Commit 53d2b90

Browse files
authored
Refactorred prove_with_retries (#239)
1 parent 97afeea commit 53d2b90

File tree

1 file changed

+59
-44
lines changed
  • crates/stwo_run_and_prove/src

1 file changed

+59
-44
lines changed

crates/stwo_run_and_prove/src/main.rs

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,10 @@ fn stwo_run_and_prove(
212212
Ok(())
213213
}
214214

215-
/// Prepares the prover parameters and generates proof given the prover input and parameters.
215+
/// Prepares the prover parameters and generates a proof given the prover input and parameters.
216216
/// Verifies the proof in case the respective flag is set.
217217
/// In case the proving fails or the proof verification fails, it retries up to `n_proof_attempts`
218218
/// times.
219-
/// Returns the program output.
220219
fn prove_with_retries(
221220
prover_input: ProverInput,
222221
prove_config: ProveConfig,
@@ -226,51 +225,11 @@ fn prove_with_retries(
226225
// create the directory if it doesn't exist, attach the proofs_dir path on error.
227226
std::fs::create_dir_all(&prove_config.proofs_dir)
228227
.map_err(|e| StwoRunAndProveError::from((e, prove_config.proofs_dir.clone())))?;
229-
let proof_format = prove_config.proof_format;
230228

231229
for i in 1..=prove_config.n_proof_attempts {
232-
let _attempt_span = span!(
233-
Level::INFO,
234-
"prove_attempt",
235-
attempt = i,
236-
out_of = prove_config.n_proof_attempts
237-
)
238-
.entered();
239-
let proof_file_path = prove_config.proofs_dir.join(format!("{PROOF_PREFIX}{i}"));
240-
let proof_file_name = proof_file_path
241-
.file_name()
242-
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "no file name"))?;
243-
244-
match prover.create_and_serialize_proof(
245-
prover_input.clone(),
246-
prove_config.verify,
247-
proof_file_path.clone(),
248-
proof_format.clone(),
249-
prove_config.prover_params_json.clone(),
250-
) {
251-
Ok(()) => {
252-
info!(
253-
"Proof generated and verified successfully on attempt {}/{}",
254-
i, prove_config.n_proof_attempts
255-
);
256-
let success_path = proof_file_path.with_file_name(format!(
257-
"{}{}",
258-
proof_file_name.to_string_lossy(),
259-
SUCCESS_SUFFIX
260-
));
261-
fs::rename(proof_file_path, &success_path)?;
262-
return Ok(());
263-
}
264-
230+
match single_proving_attempt(i, &prover_input, &prove_config, prover.as_ref()) {
231+
Ok(()) => return Ok(()),
265232
Err(e) => {
266-
if proof_file_path.exists() {
267-
let failure_path = proof_file_path.with_file_name(format!(
268-
"{}{}",
269-
proof_file_name.to_string_lossy(),
270-
FAILURE_SUFFIX
271-
));
272-
fs::rename(proof_file_path, &failure_path)?;
273-
}
274233
if i < prove_config.n_proof_attempts {
275234
warn!(
276235
"Proving failed on attempt {}/{}. Retrying.",
@@ -290,6 +249,62 @@ fn prove_with_retries(
290249
panic!("Should not reach here, n_proof_attempts should be at least 1.");
291250
}
292251

252+
/// Single attempt to generate and verify a proof.
253+
fn single_proving_attempt(
254+
attempt_number: usize,
255+
prover_input: &ProverInput,
256+
prove_config: &ProveConfig,
257+
prover: &dyn ProverTrait,
258+
) -> Result<(), StwoRunAndProveError> {
259+
let _attempt_span = span!(
260+
Level::INFO,
261+
"prove_attempt",
262+
attempt = attempt_number,
263+
out_of = prove_config.n_proof_attempts
264+
)
265+
.entered();
266+
let proof_file_path = prove_config
267+
.proofs_dir
268+
.join(format!("{PROOF_PREFIX}{attempt_number}"));
269+
let proof_file_name = proof_file_path
270+
.file_name()
271+
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "no file name"))?;
272+
273+
match prover.create_and_serialize_proof(
274+
prover_input.clone(),
275+
prove_config.verify,
276+
proof_file_path.clone(),
277+
prove_config.proof_format.clone(),
278+
prove_config.prover_params_json.clone(),
279+
) {
280+
Ok(()) => {
281+
info!(
282+
"Proof generated and verified successfully on attempt {}/{}",
283+
attempt_number, prove_config.n_proof_attempts
284+
);
285+
let success_path = proof_file_path.with_file_name(format!(
286+
"{}{}",
287+
proof_file_name.to_string_lossy(),
288+
SUCCESS_SUFFIX
289+
));
290+
fs::rename(proof_file_path, &success_path)?;
291+
Ok(())
292+
}
293+
294+
Err(e) => {
295+
if proof_file_path.exists() {
296+
let failure_path = proof_file_path.with_file_name(format!(
297+
"{}{}",
298+
proof_file_name.to_string_lossy(),
299+
FAILURE_SUFFIX
300+
));
301+
fs::rename(proof_file_path, &failure_path)?;
302+
}
303+
Err(e)
304+
}
305+
}
306+
}
307+
293308
#[cfg_attr(test, automock)]
294309
trait ProverTrait {
295310
fn create_and_serialize_proof(

0 commit comments

Comments
 (0)