@@ -32,7 +32,10 @@ use crate::{
32
32
cardano_database_client:: ImmutableFileRange ,
33
33
feedback:: MithrilEvent ,
34
34
file_downloader:: { DownloadEvent , FileDownloader , FileDownloaderUri } ,
35
- utils:: { create_directory_if_not_exists, delete_directory, read_files_in_directory} ,
35
+ utils:: {
36
+ TempDirectoryProvider , create_directory_if_not_exists, delete_directory,
37
+ read_files_in_directory,
38
+ } ,
36
39
} ;
37
40
38
41
/// Represents the verified digests and the Merkle tree built from them.
@@ -185,14 +188,20 @@ impl VerifiedDigests {
185
188
186
189
pub struct InternalArtifactProver {
187
190
http_file_downloader : Arc < dyn FileDownloader > ,
191
+ temp_directory_provider : Arc < dyn TempDirectoryProvider > ,
188
192
logger : slog:: Logger ,
189
193
}
190
194
191
195
impl InternalArtifactProver {
192
196
/// Constructs a new `InternalArtifactProver`.
193
- pub fn new ( http_file_downloader : Arc < dyn FileDownloader > , logger : slog:: Logger ) -> Self {
197
+ pub fn new (
198
+ http_file_downloader : Arc < dyn FileDownloader > ,
199
+ temp_directory_provider : Arc < dyn TempDirectoryProvider > ,
200
+ logger : slog:: Logger ,
201
+ ) -> Self {
194
202
Self {
195
203
http_file_downloader,
204
+ temp_directory_provider,
196
205
logger,
197
206
}
198
207
}
@@ -223,7 +232,7 @@ impl InternalArtifactProver {
223
232
certificate : & CertificateMessage ,
224
233
cardano_database_snapshot : & CardanoDatabaseSnapshotMessage ,
225
234
) -> MithrilResult < VerifiedDigests > {
226
- let digest_target_dir = Self :: digest_target_dir ( ) ;
235
+ let digest_target_dir = self . digest_target_dir ( ) ;
227
236
delete_directory ( & digest_target_dir) ?;
228
237
self . download_unpack_digest_file ( & cardano_database_snapshot. digests , & digest_target_dir)
229
238
. await ?;
@@ -423,8 +432,8 @@ impl InternalArtifactProver {
423
432
Ok ( digest_map)
424
433
}
425
434
426
- fn digest_target_dir ( ) -> PathBuf {
427
- std :: env :: temp_dir ( ) . join ( "mithril_digest" )
435
+ fn digest_target_dir ( & self ) -> PathBuf {
436
+ self . temp_directory_provider . temp_dir ( )
428
437
}
429
438
}
430
439
@@ -437,6 +446,7 @@ mod tests {
437
446
use std:: sync:: Arc ;
438
447
439
448
use mithril_common:: {
449
+ current_function,
440
450
entities:: { CardanoDbBeacon , Epoch , HexEncodedDigest } ,
441
451
messages:: CardanoDatabaseDigestListItemMessage ,
442
452
test:: { TempDir , double:: Dummy } ,
@@ -445,6 +455,7 @@ mod tests {
445
455
use crate :: {
446
456
cardano_database_client:: CardanoDatabaseClientDependencyInjector ,
447
457
file_downloader:: MockFileDownloaderBuilder , test_utils:: TestLogger ,
458
+ utils:: TimestampTempDirectoryProvider ,
448
459
} ;
449
460
450
461
use super :: * ;
@@ -561,11 +572,13 @@ mod tests {
561
572
562
573
mod download_and_verify_digests {
563
574
use mithril_common:: {
564
- StdResult ,
575
+ StdResult , current_function ,
565
576
entities:: { ProtocolMessage , ProtocolMessagePartKey } ,
566
577
messages:: DigestsMessagePart ,
567
578
} ;
568
579
580
+ use crate :: utils:: TimestampTempDirectoryProvider ;
581
+
569
582
use super :: * ;
570
583
571
584
fn write_digest_file (
@@ -610,7 +623,7 @@ mod tests {
610
623
}
611
624
612
625
#[ tokio:: test]
613
- async fn download_and_verify_digest_should_return_digest_map_acording_to_beacon ( ) {
626
+ async fn download_and_verify_digest_should_return_digest_map_according_to_beacon ( ) {
614
627
let beacon = CardanoDbBeacon {
615
628
epoch : Epoch ( 123 ) ,
616
629
immutable_file_number : 42 ,
@@ -650,22 +663,28 @@ mod tests {
650
663
} ,
651
664
..CardanoDatabaseSnapshotMessage :: dummy ( )
652
665
} ;
666
+ let temp_directory_provider =
667
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ;
668
+ let digest_target_dir = temp_directory_provider. temp_dir ( ) ;
669
+ let digest_target_dir_clone = digest_target_dir. clone ( ) ;
670
+ let http_file_downloader = Arc :: new (
671
+ MockFileDownloaderBuilder :: default ( )
672
+ . with_file_uri ( digests_location)
673
+ . with_target_dir ( digest_target_dir. clone ( ) )
674
+ . with_compression ( None )
675
+ . with_returning ( Box :: new ( move |_, _, _, _, _| {
676
+ write_digest_file (
677
+ & digest_target_dir_clone,
678
+ & build_digests_map ( hightest_immutable_number_in_digest_file) ,
679
+ ) ?;
680
+
681
+ Ok ( ( ) )
682
+ } ) )
683
+ . build ( ) ,
684
+ ) ;
653
685
let client = CardanoDatabaseClientDependencyInjector :: new ( )
654
- . with_http_file_downloader ( Arc :: new (
655
- MockFileDownloaderBuilder :: default ( )
656
- . with_file_uri ( digests_location)
657
- . with_target_dir ( InternalArtifactProver :: digest_target_dir ( ) )
658
- . with_compression ( None )
659
- . with_returning ( Box :: new ( move |_, _, _, _, _| {
660
- write_digest_file (
661
- & InternalArtifactProver :: digest_target_dir ( ) ,
662
- & build_digests_map ( hightest_immutable_number_in_digest_file) ,
663
- ) ?;
664
-
665
- Ok ( ( ) )
666
- } ) )
667
- . build ( ) ,
668
- ) )
686
+ . with_http_file_downloader ( http_file_downloader)
687
+ . with_temp_directory_provider ( temp_directory_provider)
669
688
. build_cardano_database_client ( ) ;
670
689
671
690
let verified_digests = client
@@ -681,7 +700,7 @@ mod tests {
681
700
. collect ( ) ;
682
701
assert_eq ! ( verified_digests. digests, expected_digests_in_certificate) ;
683
702
684
- assert ! ( !InternalArtifactProver :: digest_target_dir( ) . exists( ) ) ;
703
+ assert ! ( !digest_target_dir. exists( ) ) ;
685
704
}
686
705
}
687
706
@@ -704,6 +723,7 @@ mod tests {
704
723
. with_times ( 2 )
705
724
. build ( ) ,
706
725
) ,
726
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
707
727
TestLogger :: stdout ( ) ,
708
728
) ;
709
729
@@ -732,6 +752,7 @@ mod tests {
732
752
let target_dir = Path :: new ( "." ) ;
733
753
let artifact_prover = InternalArtifactProver :: new (
734
754
Arc :: new ( MockFileDownloader :: new ( ) ) ,
755
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
735
756
TestLogger :: stdout ( ) ,
736
757
) ;
737
758
@@ -760,6 +781,7 @@ mod tests {
760
781
. with_success ( )
761
782
. build ( ) ,
762
783
) ,
784
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
763
785
TestLogger :: stdout ( ) ,
764
786
) ;
765
787
@@ -794,6 +816,7 @@ mod tests {
794
816
. with_success ( )
795
817
. build ( ) ,
796
818
) ,
819
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
797
820
TestLogger :: stdout ( ) ,
798
821
) ;
799
822
@@ -828,6 +851,7 @@ mod tests {
828
851
. with_success ( )
829
852
. build ( ) ,
830
853
) ,
854
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
831
855
TestLogger :: stdout ( ) ,
832
856
) ;
833
857
@@ -884,6 +908,7 @@ mod tests {
884
908
. with_success ( )
885
909
. build ( ) ,
886
910
) ,
911
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
887
912
TestLogger :: stdout ( ) ,
888
913
) ;
889
914
artifact_prover
@@ -907,6 +932,7 @@ mod tests {
907
932
. with_success ( )
908
933
. build ( ) ,
909
934
) ,
935
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
910
936
TestLogger :: stdout ( ) ,
911
937
) ;
912
938
artifact_prover
@@ -929,6 +955,7 @@ mod tests {
929
955
. with_success ( )
930
956
. build ( ) ,
931
957
) ,
958
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
932
959
TestLogger :: stdout ( ) ,
933
960
) ;
934
961
artifact_prover
@@ -961,6 +988,7 @@ mod tests {
961
988
. with_success ( )
962
989
. build ( ) ,
963
990
) ,
991
+ Arc :: new ( TimestampTempDirectoryProvider :: new ( current_function ! ( ) ) ) ,
964
992
TestLogger :: stdout ( ) ,
965
993
) ;
966
994
0 commit comments