@@ -259,11 +259,19 @@ impl CompressedArchiveSnapshotter {
259
259
260
260
let db_ledger_dir = self . db_directory . join ( LEDGER_DIR ) ;
261
261
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) ;
267
275
268
276
fs:: create_dir ( target_folder. join ( IMMUTABLE_DIR ) )
269
277
. with_context ( || format ! ( "Can not create folder: `{}`" , target_folder. display( ) ) ) ?;
@@ -548,6 +556,23 @@ mod tests {
548
556
) ;
549
557
}
550
558
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
+
551
576
#[ tokio:: test]
552
577
async fn delete_temporary_working_directory_after_snapshot_is_created ( ) {
553
578
let test_dir = temp_dir_create ! ( ) ;
@@ -603,7 +628,7 @@ mod tests {
603
628
}
604
629
605
630
#[ 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 ( ) {
607
632
let test_dir = temp_dir_create ! ( ) ;
608
633
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
609
634
. with_immutables ( & [ 1 , 2 , 3 ] )
@@ -627,13 +652,14 @@ mod tests {
627
652
let unpack_dir = snapshot. unpack_gzip ( & test_dir) ;
628
653
assert_dir_eq ! (
629
654
& unpack_dir,
630
- // Only the last ledger file should be included
655
+ // Only the two last ledger files should be included
631
656
format!(
632
657
"* {IMMUTABLE_DIR}/
633
658
** 00003.chunk
634
659
** 00003.primary
635
660
** 00003.secondary
636
661
* {LEDGER_DIR}/
662
+ ** 637
637
663
** 737
638
664
* {}" ,
639
665
AncillaryFilesManifest :: ANCILLARY_MANIFEST_FILE_NAME
@@ -702,6 +728,7 @@ mod tests {
702
728
& PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.chunk" ) ,
703
729
& PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.primary" ) ,
704
730
& PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.secondary" ) ,
731
+ & PathBuf :: from( LEDGER_DIR ) . join( "321" ) ,
705
732
& PathBuf :: from( LEDGER_DIR ) . join( "737" ) ,
706
733
] ,
707
734
manifest. data. keys( ) . collect:: <Vec <_>>( )
0 commit comments