Skip to content

Commit 00f9572

Browse files
add test that succeeds on retry (#201)
## 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/201) <!-- Reviewable:end -->
1 parent b2f866a commit 00f9572

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ tracing = "0.1.40"
4242
sonic-rs = "0.3.17"
4343
starknet-ff = "0.3.7"
4444
mockall = "0.13.1"
45+
rstest = "0.21"

crates/stwo_run_and_prove/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ tracing.workspace = true
2121
[dev-dependencies]
2222
tempfile.workspace = true
2323
mockall.workspace = true
24+
rstest.workspace = true

crates/stwo_run_and_prove/src/main.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn prove_with_retries(
239239
std::fs::create_dir_all(&prove_config.proofs_dir)?;
240240
let proof_format = prove_config.proof_format;
241241

242-
for i in 1..prove_config.n_proof_attempts + 1 {
242+
for i in 1..=prove_config.n_proof_attempts + 1 {
243243
info!(
244244
"Attempting to generate proof {}/{}.",
245245
i, prove_config.n_proof_attempts
@@ -414,6 +414,7 @@ fn save_output_to_file(
414414
#[cfg(test)]
415415
mod tests {
416416
use super::*;
417+
use rstest::rstest;
417418
use std::fs;
418419
use tempfile::{NamedTempFile, TempDir, TempPath};
419420

@@ -522,6 +523,37 @@ mod tests {
522523
(program_output_tempfile, proofs_tempdir)
523524
}
524525

526+
fn run_with_mock_prover_succeeds_on_retry(n_proof_attempts: usize) -> (TempPath, TempDir) {
527+
let (args, program_output_tempfile, proofs_tempdir) = prepare_args(n_proof_attempts);
528+
let mut mock_prover = Box::new(MockProverTrait::new());
529+
530+
// Create iterator that return errors for the first n-1 attempts, and a successful result
531+
// for the last attempt.
532+
let mut results = (0..n_proof_attempts.saturating_sub(1))
533+
.map(|_| Err(StwoRunAndProveError::Verification))
534+
.chain(std::iter::once(Ok(vec![ARRAY_SUM_EXPECTED_OUTPUT])));
535+
536+
mock_prover
537+
.expect_choose_channel_and_prove()
538+
.times(n_proof_attempts)
539+
.returning(move |_, proof_file, _, _, _| {
540+
let expected_proof_file = get_path(EXPECTED_PROOF_FILE_NAME);
541+
fs::copy(&expected_proof_file, &proof_file).expect("Failed to copy proof file.");
542+
results.next().unwrap()
543+
});
544+
545+
let successful_proof_attempt =
546+
run_stwo_run_and_prove(args, mock_prover).expect("failed to run stwo_run_and_prove");
547+
548+
assert_eq!(
549+
successful_proof_attempt, n_proof_attempts,
550+
"successful proof attempt should be {:?}, but got {:?}",
551+
n_proof_attempts, successful_proof_attempt
552+
);
553+
554+
(program_output_tempfile, proofs_tempdir)
555+
}
556+
525557
#[test]
526558
fn test_stwo_run_and_prove() {
527559
let (output_temp_file, proofs_temp_dir) = run_with_successful_mock_prover(1);
@@ -553,25 +585,50 @@ mod tests {
553585
}
554586

555587
#[test]
556-
fn test_stwo_run_and_prove_retries() {
588+
fn test_stwo_run_and_prove_all_retries_fail() {
557589
let n_proof_attempts = 3;
558590
let (output_temp_file, proofs_temp_dir) =
559591
run_with_verification_error_mock_prover(n_proof_attempts);
560592
let proofs_dir = proofs_temp_dir.path().to_path_buf();
561593

562-
for i in 1..n_proof_attempts + 1 {
594+
(1..=n_proof_attempts).for_each(|i| {
563595
let proof_file = proofs_dir.join(format!("proof_{}", i));
564596
assert!(
565597
proof_file.exists(),
566598
"Proof file {:?} should exist after running with verifier failures",
567599
i,
568600
);
569-
}
601+
});
602+
570603
assert!(
571604
is_file_empty(&output_temp_file.to_path_buf()).unwrap(),
572605
"Output file should be empty after running with verifier failures",
573606
);
574607
}
608+
609+
#[rstest]
610+
#[case::two_tries(2)]
611+
#[case::three_tries(3)]
612+
fn test_stwo_run_and_prove_succeeds_on_retry(#[case] n_proof_attempts: usize) {
613+
let (output_temp_file, proofs_temp_dir) =
614+
run_with_mock_prover_succeeds_on_retry(n_proof_attempts);
615+
let proofs_dir = proofs_temp_dir.path().to_path_buf();
616+
617+
(1..=n_proof_attempts).for_each(|i| {
618+
let proof_file = proofs_dir.join(format!("proof_{}", i));
619+
assert!(
620+
proof_file.exists(),
621+
"Proof file {:?} should exist after a run that succeeds on attempt {:?} of proof and verify",
622+
i, n_proof_attempts,
623+
);
624+
});
625+
626+
assert!(
627+
!is_file_empty(&output_temp_file.to_path_buf()).unwrap(),
628+
"Output file should not be empty after a run that succeeds on attempt {:?} of proof and verify",
629+
n_proof_attempts,
630+
);
631+
}
575632
}
576633

577634
// TODO(nitsan): Tests -

0 commit comments

Comments
 (0)