@@ -259,11 +259,19 @@ impl CompressedArchiveSnapshotter {
259259
260260 let db_ledger_dir = self . db_directory . join ( LEDGER_DIR ) ;
261261 let ledger_files = LedgerFile :: list_all_in_dir ( & db_ledger_dir) ?;
262- let last_ledger = ledger_files. last ( ) . ok_or ( anyhow ! (
263- "No ledger file found in directory: `{}`" ,
264- db_ledger_dir. display( )
265- ) ) ?;
266- files_to_snapshot. push ( PathBuf :: from ( LEDGER_DIR ) . join ( & last_ledger. filename ) ) ;
262+ let latest_ledger_files: Vec < PathBuf > = ledger_files
263+ . iter ( )
264+ . rev ( )
265+ . take ( 2 )
266+ . map ( |ledger_file| PathBuf :: from ( LEDGER_DIR ) . join ( & ledger_file. filename ) )
267+ . collect ( ) ;
268+ if latest_ledger_files. is_empty ( ) {
269+ return Err ( anyhow ! (
270+ "No ledger state snapshot found in the ledger directory: '{}'" ,
271+ db_ledger_dir. display( )
272+ ) ) ;
273+ }
274+ files_to_snapshot. extend ( latest_ledger_files) ;
267275
268276 fs:: create_dir ( target_folder. join ( IMMUTABLE_DIR ) )
269277 . with_context ( || format ! ( "Can not create folder: `{}`" , target_folder. display( ) ) ) ?;
@@ -548,6 +556,23 @@ mod tests {
548556 ) ;
549557 }
550558
559+ #[ tokio:: test]
560+ async fn getting_files_to_include_fails_when_no_ledger_file_found ( ) {
561+ let test_dir = temp_dir_create ! ( ) ;
562+ let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
563+ . with_immutables ( & [ 1 , 2 ] )
564+ . build ( ) ;
565+ let snapshotter =
566+ snapshotter_for_test ( & test_dir, cardano_db. get_dir ( ) , CompressionAlgorithm :: Gzip ) ;
567+ let ancillary_snapshot_dir = test_dir. join ( "ancillary_snapshot" ) ;
568+ fs:: create_dir ( & ancillary_snapshot_dir) . unwrap ( ) ;
569+
570+ snapshotter
571+ . get_files_and_directories_for_ancillary_snapshot ( 1 , & ancillary_snapshot_dir)
572+ . await
573+ . expect_err ( "Should fail if no ledger file found" ) ;
574+ }
575+
551576 #[ tokio:: test]
552577 async fn delete_temporary_working_directory_after_snapshot_is_created ( ) {
553578 let test_dir = temp_dir_create ! ( ) ;
@@ -603,7 +628,7 @@ mod tests {
603628 }
604629
605630 #[ tokio:: test]
606- async fn create_archive_should_embed_only_last_ledger_and_last_immutables ( ) {
631+ async fn create_archive_should_embed_only_two_last_ledgers_and_last_immutables ( ) {
607632 let test_dir = temp_dir_create ! ( ) ;
608633 let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
609634 . with_immutables ( & [ 1 , 2 , 3 ] )
@@ -627,13 +652,14 @@ mod tests {
627652 let unpack_dir = snapshot. unpack_gzip ( & test_dir) ;
628653 assert_dir_eq ! (
629654 & unpack_dir,
630- // Only the last ledger file should be included
655+ // Only the two last ledger files should be included
631656 format!(
632657 "* {IMMUTABLE_DIR}/
633658 ** 00003.chunk
634659 ** 00003.primary
635660 ** 00003.secondary
636661 * {LEDGER_DIR}/
662+ ** 637
637663 ** 737
638664 * {}" ,
639665 AncillaryFilesManifest :: ANCILLARY_MANIFEST_FILE_NAME
@@ -671,7 +697,7 @@ mod tests {
671697 let test_dir = temp_dir_create ! ( ) ;
672698 let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
673699 . with_immutables ( & [ 1 , 2 , 3 ] )
674- . with_ledger_files ( & [ "321 " , "737" ] )
700+ . with_ledger_files ( & [ "537" , "637 ", "737" ] )
675701 . with_non_immutables ( & [ "not_to_include.txt" ] )
676702 . build ( ) ;
677703 File :: create ( cardano_db. get_dir ( ) . join ( "not_to_include_as_well.txt" ) ) . unwrap ( ) ;
@@ -702,6 +728,7 @@ mod tests {
702728 & PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.chunk" ) ,
703729 & PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.primary" ) ,
704730 & PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.secondary" ) ,
731+ & PathBuf :: from( LEDGER_DIR ) . join( "637" ) ,
705732 & PathBuf :: from( LEDGER_DIR ) . join( "737" ) ,
706733 ] ,
707734 manifest. data. keys( ) . collect:: <Vec <_>>( )
0 commit comments