@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
6
6
use std:: sync:: Arc ;
7
7
8
8
use mithril_common:: digesters:: {
9
- immutable_trio_names, ImmutableFile , LedgerFile , IMMUTABLE_DIR , LEDGER_DIR ,
9
+ immutable_trio_names, ImmutableFile , LedgerStateSnapshot , IMMUTABLE_DIR , LEDGER_DIR ,
10
10
} ;
11
11
use mithril_common:: entities:: { AncillaryFilesManifest , CompressionAlgorithm , ImmutableFileNumber } ;
12
12
use mithril_common:: logging:: LoggerExtensions ;
@@ -258,12 +258,13 @@ impl CompressedArchiveSnapshotter {
258
258
. collect ( ) ;
259
259
260
260
let db_ledger_dir = self . db_directory . join ( LEDGER_DIR ) ;
261
- let ledger_files = LedgerFile :: list_all_in_dir ( & db_ledger_dir) ?;
261
+ let ledger_files = LedgerStateSnapshot :: list_all_in_dir ( & db_ledger_dir) ?;
262
262
let latest_ledger_files: Vec < PathBuf > = ledger_files
263
263
. iter ( )
264
264
. rev ( )
265
265
. take ( 2 )
266
- . map ( |ledger_file| PathBuf :: from ( LEDGER_DIR ) . join ( & ledger_file. filename ) )
266
+ . flat_map ( |ledger_state_snapshot| ledger_state_snapshot. get_files_relative_path ( ) )
267
+ . map ( |path| PathBuf :: from ( LEDGER_DIR ) . join ( path) )
267
268
. collect ( ) ;
268
269
if latest_ledger_files. is_empty ( ) {
269
270
return Err ( anyhow ! (
@@ -279,6 +280,16 @@ impl CompressedArchiveSnapshotter {
279
280
. with_context ( || format ! ( "Can not create folder: `{}`" , target_folder. display( ) ) ) ?;
280
281
281
282
for file in & files_to_snapshot {
283
+ // Some files to snapshot are in subfolders (i.e.: in-memory ledger snapshots files)
284
+ if let Some ( parent_dir) = file. parent ( ) {
285
+ let target_parent_dir = target_folder. join ( parent_dir) ;
286
+ if !target_parent_dir. exists ( ) {
287
+ fs:: create_dir_all ( & target_parent_dir) . with_context ( || {
288
+ format ! ( "Can not create folder: `{}`" , target_parent_dir. display( ) )
289
+ } ) ?;
290
+ }
291
+ }
292
+
282
293
let source = self . db_directory . join ( file) ;
283
294
let target = target_folder. join ( file) ;
284
295
tokio:: fs:: copy ( & source, & target) . await . with_context ( || {
@@ -448,7 +459,7 @@ mod tests {
448
459
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
449
460
. with_immutables ( & [ 1 , 2 , 3 ] )
450
461
. append_immutable_trio ( )
451
- . with_ledger_files ( & [ " 437" ] )
462
+ . with_legacy_ledger_snapshots ( & [ 437 ] )
452
463
. with_volatile_files ( & [ "blocks-0.dat" ] )
453
464
. with_non_immutables ( & [ "random_file.txt" , "00002.trap" ] )
454
465
. build ( ) ;
@@ -491,7 +502,7 @@ mod tests {
491
502
let test_dir = temp_dir_create ! ( ) ;
492
503
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
493
504
. with_immutables ( & [ 1 , 2 , 3 ] )
494
- . with_ledger_files ( & [ " 437" ] )
505
+ . with_legacy_ledger_snapshots ( & [ 437 ] )
495
506
. with_volatile_files ( & [ "blocks-0.dat" ] )
496
507
. with_non_immutables ( & [ "random_file.txt" , "00002.trap" ] )
497
508
. build ( ) ;
@@ -526,12 +537,12 @@ mod tests {
526
537
use super :: * ;
527
538
528
539
#[ tokio:: test]
529
- async fn getting_files_to_include_copy_them_to_a_target_directory_while_keeping_source_dir_structure (
540
+ async fn getting_files_to_include_for_legacy_ledger_snapshot_copy_them_to_a_target_directory_while_keeping_source_dir_structure (
530
541
) {
531
542
let test_dir = temp_dir_create ! ( ) ;
532
543
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
533
544
. with_immutables ( & [ 1 , 2 ] )
534
- . with_ledger_files ( & [ " 737" ] )
545
+ . with_legacy_ledger_snapshots ( & [ 737 ] )
535
546
. build ( ) ;
536
547
let snapshotter =
537
548
snapshotter_for_test ( & test_dir, cardano_db. get_dir ( ) , CompressionAlgorithm :: Gzip ) ;
@@ -556,6 +567,45 @@ mod tests {
556
567
) ;
557
568
}
558
569
570
+ #[ tokio:: test]
571
+ async fn getting_files_to_include_for_in_memory_ledger_snapshot_copy_them_to_a_target_directory_while_keeping_source_dir_structure (
572
+ ) {
573
+ let test_dir = temp_dir_create ! ( ) ;
574
+ let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
575
+ . with_immutables ( & [ 1 , 2 ] )
576
+ . with_in_memory_ledger_snapshots ( & [ 737 ] )
577
+ . build ( ) ;
578
+ let snapshotter =
579
+ snapshotter_for_test ( & test_dir, cardano_db. get_dir ( ) , CompressionAlgorithm :: Gzip ) ;
580
+ let ancillary_snapshot_dir = test_dir. join ( "ancillary_snapshot" ) ;
581
+ fs:: create_dir ( & ancillary_snapshot_dir) . unwrap ( ) ;
582
+
583
+ snapshotter
584
+ . get_files_and_directories_for_ancillary_snapshot ( 1 , & ancillary_snapshot_dir)
585
+ . await
586
+ . unwrap ( ) ;
587
+
588
+ assert_dir_eq ! (
589
+ & ancillary_snapshot_dir,
590
+ format!(
591
+ "* {IMMUTABLE_DIR}/
592
+ ** 00002.chunk
593
+ ** 00002.primary
594
+ ** 00002.secondary
595
+ * {LEDGER_DIR}/
596
+ ** 737/
597
+ *** {}/
598
+ **** {}
599
+ *** {}
600
+ *** {}" ,
601
+ LedgerStateSnapshot :: IN_MEMORY_TABLES ,
602
+ LedgerStateSnapshot :: IN_MEMORY_TVAR ,
603
+ LedgerStateSnapshot :: IN_MEMORY_META ,
604
+ LedgerStateSnapshot :: IN_MEMORY_STATE ,
605
+ )
606
+ ) ;
607
+ }
608
+
559
609
#[ tokio:: test]
560
610
async fn getting_files_to_include_fails_when_no_ledger_file_found ( ) {
561
611
let test_dir = temp_dir_create ! ( ) ;
@@ -578,7 +628,7 @@ mod tests {
578
628
let test_dir = temp_dir_create ! ( ) ;
579
629
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
580
630
. with_immutables ( & [ 1 , 2 ] )
581
- . with_ledger_files ( & [ " 637" ] )
631
+ . with_legacy_ledger_snapshots ( & [ 637 ] )
582
632
. build ( ) ;
583
633
let snapshotter = CompressedArchiveSnapshotter {
584
634
ancillary_signer : Arc :: new ( MockAncillarySigner :: that_succeeds_with_signature (
@@ -606,7 +656,7 @@ mod tests {
606
656
let test_dir = temp_dir_create ! ( ) ;
607
657
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
608
658
. with_immutables ( & [ 1 , 2 ] )
609
- . with_ledger_files ( & [ " 637" ] )
659
+ . with_legacy_ledger_snapshots ( & [ 637 ] )
610
660
. build ( ) ;
611
661
let snapshotter = CompressedArchiveSnapshotter {
612
662
ancillary_signer : Arc :: new ( MockAncillarySigner :: that_fails_with_message ( "failure" ) ) ,
@@ -632,7 +682,8 @@ mod tests {
632
682
let test_dir = temp_dir_create ! ( ) ;
633
683
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
634
684
. with_immutables ( & [ 1 , 2 , 3 ] )
635
- . with_ledger_files ( & [ "437" , "537" , "637" , "737" , "9not_included" ] )
685
+ . with_legacy_ledger_snapshots ( & [ 437 , 537 , 637 , 737 ] )
686
+ . with_non_ledger_files ( & [ "9not_included" ] )
636
687
. with_volatile_files ( & [ "blocks-0.dat" , "blocks-1.dat" , "blocks-2.dat" ] )
637
688
. build ( ) ;
638
689
fs:: create_dir ( cardano_db. get_dir ( ) . join ( "whatever" ) ) . unwrap ( ) ;
@@ -672,7 +723,7 @@ mod tests {
672
723
let test_dir = temp_dir_create ! ( ) ;
673
724
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
674
725
. with_immutables ( & [ 1 , 2 ] )
675
- . with_ledger_files ( & [ " 737" ] )
726
+ . with_legacy_ledger_snapshots ( & [ 737 ] )
676
727
. build ( ) ;
677
728
678
729
let snapshotter = CompressedArchiveSnapshotter {
@@ -693,11 +744,12 @@ mod tests {
693
744
}
694
745
695
746
#[ tokio:: test]
696
- async fn create_archive_generate_sign_and_include_manifest_file ( ) {
747
+ async fn create_archive_of_legacy_ledger_snapshot_generate_sign_and_include_manifest_file ( )
748
+ {
697
749
let test_dir = temp_dir_create ! ( ) ;
698
750
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
699
751
. with_immutables ( & [ 1 , 2 , 3 ] )
700
- . with_ledger_files ( & [ " 537" , " 637" , " 737" ] )
752
+ . with_legacy_ledger_snapshots ( & [ 537 , 637 , 737 ] )
701
753
. with_non_immutables ( & [ "not_to_include.txt" ] )
702
754
. build ( ) ;
703
755
File :: create ( cardano_db. get_dir ( ) . join ( "not_to_include_as_well.txt" ) ) . unwrap ( ) ;
@@ -742,6 +794,76 @@ mod tests {
742
794
manifest. signature
743
795
)
744
796
}
797
+
798
+ #[ tokio:: test]
799
+ async fn create_archive_of_in_memory_ledger_snapshot_generate_sign_and_include_manifest_file (
800
+ ) {
801
+ let test_dir = temp_dir_create ! ( ) ;
802
+ let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
803
+ . with_immutables ( & [ 1 , 2 , 3 ] )
804
+ . with_in_memory_ledger_snapshots ( & [ 537 , 637 , 737 ] )
805
+ . with_non_immutables ( & [ "not_to_include.txt" ] )
806
+ . build ( ) ;
807
+ File :: create ( cardano_db. get_dir ( ) . join ( "not_to_include_as_well.txt" ) ) . unwrap ( ) ;
808
+
809
+ let snapshotter = CompressedArchiveSnapshotter {
810
+ ancillary_signer : Arc :: new ( MockAncillarySigner :: that_succeeds_with_signature (
811
+ fake_keys:: signable_manifest_signature ( ) [ 0 ] ,
812
+ ) ) ,
813
+ ..snapshotter_for_test ( & test_dir, cardano_db. get_dir ( ) , CompressionAlgorithm :: Gzip )
814
+ } ;
815
+
816
+ let archive = snapshotter
817
+ . snapshot_ancillary ( 2 , "ancillary" )
818
+ . await
819
+ . unwrap ( ) ;
820
+ let unpacked = archive. unpack_gzip ( test_dir) ;
821
+ let manifest_path = unpacked. join ( AncillaryFilesManifest :: ANCILLARY_MANIFEST_FILE_NAME ) ;
822
+
823
+ assert ! ( manifest_path. exists( ) ) ;
824
+
825
+ let manifest = serde_json:: from_reader :: < _ , AncillaryFilesManifest > (
826
+ File :: open ( & manifest_path) . unwrap ( ) ,
827
+ )
828
+ . unwrap ( ) ;
829
+
830
+ assert_eq ! (
831
+ vec![
832
+ & PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.chunk" ) ,
833
+ & PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.primary" ) ,
834
+ & PathBuf :: from( IMMUTABLE_DIR ) . join( "00003.secondary" ) ,
835
+ & PathBuf :: from( LEDGER_DIR )
836
+ . join( "637" )
837
+ . join( LedgerStateSnapshot :: IN_MEMORY_META ) ,
838
+ & PathBuf :: from( LEDGER_DIR )
839
+ . join( "637" )
840
+ . join( LedgerStateSnapshot :: IN_MEMORY_STATE ) ,
841
+ & PathBuf :: from( LEDGER_DIR )
842
+ . join( "637" )
843
+ . join( LedgerStateSnapshot :: IN_MEMORY_TABLES )
844
+ . join( LedgerStateSnapshot :: IN_MEMORY_TVAR ) ,
845
+ & PathBuf :: from( LEDGER_DIR )
846
+ . join( "737" )
847
+ . join( LedgerStateSnapshot :: IN_MEMORY_META ) ,
848
+ & PathBuf :: from( LEDGER_DIR )
849
+ . join( "737" )
850
+ . join( LedgerStateSnapshot :: IN_MEMORY_STATE ) ,
851
+ & PathBuf :: from( LEDGER_DIR )
852
+ . join( "737" )
853
+ . join( LedgerStateSnapshot :: IN_MEMORY_TABLES )
854
+ . join( LedgerStateSnapshot :: IN_MEMORY_TVAR ) ,
855
+ ] ,
856
+ manifest. data. keys( ) . collect:: <Vec <_>>( )
857
+ ) ;
858
+ assert_eq ! (
859
+ Some (
860
+ fake_keys:: signable_manifest_signature( ) [ 0 ]
861
+ . try_into( )
862
+ . unwrap( )
863
+ ) ,
864
+ manifest. signature
865
+ )
866
+ }
745
867
}
746
868
747
869
mod compute_immutable_total_and_average_uncompressed_size {
@@ -757,7 +879,7 @@ mod tests {
757
879
let cardano_db = DummyCardanoDbBuilder :: new ( current_function ! ( ) )
758
880
. with_immutables ( & [ 1 , 2 , 3 ] )
759
881
. set_immutable_trio_file_size ( immutable_trio_file_size)
760
- . with_ledger_files ( & [ " 737" ] )
882
+ . with_legacy_ledger_snapshots ( & [ 737 ] )
761
883
. set_ledger_file_size ( 6666 )
762
884
. with_volatile_files ( & [ "blocks-0.dat" ] )
763
885
. set_volatile_file_size ( 99 )
0 commit comments