@@ -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) ]
415415mod 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