@@ -11,7 +11,7 @@ use anyhow::anyhow;
11
11
use mithril_common:: {
12
12
digesters:: { IMMUTABLE_DIR , LEDGER_DIR , VOLATILE_DIR } ,
13
13
entities:: { AncillaryLocation , CompressionAlgorithm , ImmutableFileNumber , ImmutablesLocation } ,
14
- messages:: CardanoDatabaseSnapshotMessage ,
14
+ messages:: { AncillaryMessagePart , CardanoDatabaseSnapshotMessage , ImmutablesMessagePart } ,
15
15
} ;
16
16
17
17
use crate :: feedback:: { FeedbackSender , MithrilEvent , MithrilEventCardanoDatabase } ;
@@ -121,24 +121,17 @@ impl InternalArtifactDownloader {
121
121
last_immutable_file_number,
122
122
) ?;
123
123
Self :: verify_can_write_to_target_directory ( target_dir, & download_unpack_options) ?;
124
- let immutable_locations = & cardano_database_snapshot. immutables . locations ;
125
- let average_size_uncompressed = cardano_database_snapshot
126
- . immutables
127
- . average_size_uncompressed ;
128
124
129
125
let mut tasks = VecDeque :: from ( self . build_download_tasks_for_immutables (
130
- immutable_locations ,
126
+ & cardano_database_snapshot . immutables ,
131
127
immutable_file_number_range,
132
128
target_dir,
133
- average_size_uncompressed,
134
129
& download_id,
135
130
) ?) ;
136
131
if download_unpack_options. include_ancillary {
137
- let ancillary = & cardano_database_snapshot. ancillary ;
138
132
tasks. push_back ( self . new_ancillary_download_task (
139
- & ancillary . locations ,
133
+ & cardano_database_snapshot . ancillary ,
140
134
target_dir,
141
- ancillary. size_uncompressed ,
142
135
& download_id,
143
136
) ?) ;
144
137
}
@@ -218,10 +211,9 @@ impl InternalArtifactDownloader {
218
211
219
212
fn build_download_tasks_for_immutables (
220
213
& self ,
221
- immutable_locations : & [ ImmutablesLocation ] ,
214
+ immutable_locations : & ImmutablesMessagePart ,
222
215
immutable_file_number_range : RangeInclusive < ImmutableFileNumber > ,
223
216
target_dir : & Path ,
224
- average_size_uncompressed : u64 ,
225
217
download_id : & str ,
226
218
) -> MithrilResult < Vec < DownloadTask > > {
227
219
let immutable_file_numbers_to_download = immutable_file_number_range
@@ -234,7 +226,6 @@ impl InternalArtifactDownloader {
234
226
immutable_locations,
235
227
immutable_file_number,
236
228
target_dir,
237
- average_size_uncompressed,
238
229
download_id,
239
230
) ?) ;
240
231
}
@@ -243,14 +234,13 @@ impl InternalArtifactDownloader {
243
234
244
235
fn new_immutable_download_task (
245
236
& self ,
246
- locations : & [ ImmutablesLocation ] ,
237
+ immutable_locations : & ImmutablesMessagePart ,
247
238
immutable_file_number : ImmutableFileNumber ,
248
239
immutable_files_target_dir : & Path ,
249
- average_size_uncompressed : u64 ,
250
240
download_id : & str ,
251
241
) -> MithrilResult < DownloadTask > {
252
242
let mut locations_to_try = vec ! [ ] ;
253
- let mut locations_sorted = locations . to_owned ( ) ;
243
+ let mut locations_sorted = immutable_locations . sanitized_locations ( ) ? ;
254
244
locations_sorted. sort ( ) ;
255
245
for location in locations_sorted {
256
246
let location_to_try = match location {
@@ -269,9 +259,8 @@ impl InternalArtifactDownloader {
269
259
compression_algorithm : compression_algorithm. to_owned ( ) ,
270
260
}
271
261
}
272
- ImmutablesLocation :: Unknown => {
273
- return Err ( anyhow ! ( "Unknown location type to download immutable" ) ) ;
274
- }
262
+ // Note: unknown locations should have been filtered out by `sanitized_locations`
263
+ ImmutablesLocation :: Unknown => unreachable ! ( ) ,
275
264
} ;
276
265
277
266
locations_to_try. push ( location_to_try) ;
@@ -281,7 +270,7 @@ impl InternalArtifactDownloader {
281
270
name : format ! ( "immutable_file_{:05}" , immutable_file_number) ,
282
271
locations_to_try,
283
272
target_dir : immutable_files_target_dir. to_path_buf ( ) ,
284
- size_uncompressed : average_size_uncompressed,
273
+ size_uncompressed : immutable_locations . average_size_uncompressed ,
285
274
download_event : DownloadEvent :: Immutable {
286
275
download_id : download_id. to_string ( ) ,
287
276
immutable_file_number,
@@ -291,13 +280,12 @@ impl InternalArtifactDownloader {
291
280
292
281
fn new_ancillary_download_task (
293
282
& self ,
294
- locations : & [ AncillaryLocation ] ,
283
+ locations : & AncillaryMessagePart ,
295
284
ancillary_file_target_dir : & Path ,
296
- size_uncompressed : u64 ,
297
285
download_id : & str ,
298
286
) -> MithrilResult < DownloadTask > {
299
287
let mut locations_to_try = vec ! [ ] ;
300
- let mut locations_sorted = locations. to_owned ( ) ;
288
+ let mut locations_sorted = locations. sanitized_locations ( ) ? ;
301
289
locations_sorted. sort ( ) ;
302
290
for location in locations_sorted {
303
291
let location_to_try = match location {
@@ -314,9 +302,8 @@ impl InternalArtifactDownloader {
314
302
compression_algorithm : compression_algorithm. to_owned ( ) ,
315
303
}
316
304
}
317
- AncillaryLocation :: Unknown => {
318
- return Err ( anyhow ! ( "Unknown location type to download immutable" ) ) ;
319
- }
305
+ // Note: unknown locations should have been filtered out by `sanitized_locations`
306
+ AncillaryLocation :: Unknown => unreachable ! ( ) ,
320
307
} ;
321
308
322
309
locations_to_try. push ( location_to_try) ;
@@ -326,7 +313,7 @@ impl InternalArtifactDownloader {
326
313
name : "ancillary" . to_string ( ) ,
327
314
locations_to_try,
328
315
target_dir : ancillary_file_target_dir. to_path_buf ( ) ,
329
- size_uncompressed,
316
+ size_uncompressed : locations . size_uncompressed ,
330
317
download_event : DownloadEvent :: Ancillary {
331
318
download_id : download_id. to_string ( ) ,
332
319
} ,
@@ -820,17 +807,19 @@ mod tests {
820
807
821
808
let tasks = artifact_downloader
822
809
. build_download_tasks_for_immutables (
823
- & [ ImmutablesLocation :: CloudStorage {
824
- uri : MultiFilesUri :: Template ( TemplateUri (
825
- "http://whatever/{immutable_file_number}.tar.gz" . to_string ( ) ,
826
- ) ) ,
827
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
828
- } ] ,
810
+ & ImmutablesMessagePart {
811
+ locations : vec ! [ ImmutablesLocation :: CloudStorage {
812
+ uri: MultiFilesUri :: Template ( TemplateUri (
813
+ "http://whatever/{immutable_file_number}.tar.gz" . to_string( ) ,
814
+ ) ) ,
815
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
816
+ } ] ,
817
+ average_size_uncompressed : 0 ,
818
+ } ,
829
819
immutable_file_range
830
820
. to_range_inclusive ( total_immutable_files)
831
821
. unwrap ( ) ,
832
822
& target_dir,
833
- 0 ,
834
823
"download_id" ,
835
824
)
836
825
. unwrap ( ) ;
@@ -842,7 +831,7 @@ mod tests {
842
831
}
843
832
844
833
#[ tokio:: test]
845
- async fn building_immutables_download_tasks_fails_if_location_is_unknown ( ) {
834
+ async fn building_immutables_download_tasks_fails_if_all_locations_are_unknown ( ) {
846
835
let total_immutable_files = 2 ;
847
836
let immutable_file_range = ImmutableFileRange :: Range ( 1 , total_immutable_files) ;
848
837
let target_dir = TempDir :: new ( "cardano_database_client" , "download_unpack" ) . build ( ) ;
@@ -853,18 +842,20 @@ mod tests {
853
842
) ;
854
843
855
844
let build_tasks_result = artifact_downloader. build_download_tasks_for_immutables (
856
- & [ ImmutablesLocation :: Unknown { } ] ,
845
+ & ImmutablesMessagePart {
846
+ locations : vec ! [ ImmutablesLocation :: Unknown { } ] ,
847
+ average_size_uncompressed : 0 ,
848
+ } ,
857
849
immutable_file_range
858
850
. to_range_inclusive ( total_immutable_files)
859
851
. unwrap ( ) ,
860
852
& target_dir,
861
- 0 ,
862
853
"download_id" ,
863
854
) ;
864
855
865
856
assert ! (
866
857
build_tasks_result. is_err( ) ,
867
- "building tasks should fail if a location is unknown"
858
+ "building tasks should fail if all location are unknown"
868
859
) ;
869
860
}
870
861
@@ -891,17 +882,19 @@ mod tests {
891
882
892
883
let tasks = artifact_downloader
893
884
. build_download_tasks_for_immutables (
894
- & [ ImmutablesLocation :: CloudStorage {
895
- uri : MultiFilesUri :: Template ( TemplateUri (
896
- "http://whatever-1/{immutable_file_number}.tar.gz" . to_string ( ) ,
897
- ) ) ,
898
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
899
- } ] ,
885
+ & ImmutablesMessagePart {
886
+ locations : vec ! [ ImmutablesLocation :: CloudStorage {
887
+ uri: MultiFilesUri :: Template ( TemplateUri (
888
+ "http://whatever-1/{immutable_file_number}.tar.gz" . to_string( ) ,
889
+ ) ) ,
890
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
891
+ } ] ,
892
+ average_size_uncompressed : 0 ,
893
+ } ,
900
894
immutable_file_range
901
895
. to_range_inclusive ( total_immutable_files)
902
896
. unwrap ( ) ,
903
897
& target_dir,
904
- 0 ,
905
898
"download_id" ,
906
899
)
907
900
. unwrap ( ) ;
@@ -944,25 +937,27 @@ mod tests {
944
937
945
938
let tasks = artifact_downloader
946
939
. build_download_tasks_for_immutables (
947
- & [
948
- ImmutablesLocation :: CloudStorage {
949
- uri : MultiFilesUri :: Template ( TemplateUri (
950
- "http://whatever-1/{immutable_file_number}.tar.gz" . to_string ( ) ,
951
- ) ) ,
952
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
953
- } ,
954
- ImmutablesLocation :: CloudStorage {
955
- uri : MultiFilesUri :: Template ( TemplateUri (
956
- "http://whatever-2/{immutable_file_number}.tar.gz" . to_string ( ) ,
957
- ) ) ,
958
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
959
- } ,
960
- ] ,
940
+ & ImmutablesMessagePart {
941
+ locations : vec ! [
942
+ ImmutablesLocation :: CloudStorage {
943
+ uri: MultiFilesUri :: Template ( TemplateUri (
944
+ "http://whatever-1/{immutable_file_number}.tar.gz" . to_string( ) ,
945
+ ) ) ,
946
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
947
+ } ,
948
+ ImmutablesLocation :: CloudStorage {
949
+ uri: MultiFilesUri :: Template ( TemplateUri (
950
+ "http://whatever-2/{immutable_file_number}.tar.gz" . to_string( ) ,
951
+ ) ) ,
952
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
953
+ } ,
954
+ ] ,
955
+ average_size_uncompressed : 0 ,
956
+ } ,
961
957
immutable_file_range
962
958
. to_range_inclusive ( total_immutable_files)
963
959
. unwrap ( ) ,
964
960
& target_dir,
965
- 0 ,
966
961
"download_id" ,
967
962
)
968
963
. unwrap ( ) ;
@@ -989,12 +984,14 @@ mod tests {
989
984
990
985
let task = artifact_downloader
991
986
. new_ancillary_download_task (
992
- & [ AncillaryLocation :: CloudStorage {
993
- uri : "http://whatever-1/ancillary.tar.gz" . to_string ( ) ,
994
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
995
- } ] ,
987
+ & AncillaryMessagePart {
988
+ locations : vec ! [ AncillaryLocation :: CloudStorage {
989
+ uri: "http://whatever-1/ancillary.tar.gz" . to_string( ) ,
990
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
991
+ } ] ,
992
+ size_uncompressed : 0 ,
993
+ } ,
996
994
target_dir,
997
- 0 ,
998
995
"download_id" ,
999
996
)
1000
997
. unwrap ( ) ;
@@ -1006,7 +1003,7 @@ mod tests {
1006
1003
}
1007
1004
1008
1005
#[ tokio:: test]
1009
- async fn building_ancillary_download_task_fails_if_location_is_unknown ( ) {
1006
+ async fn building_ancillary_download_tasks_fails_if_all_locations_are_unknown ( ) {
1010
1007
let target_dir = Path :: new ( "." ) ;
1011
1008
let artifact_downloader = InternalArtifactDownloader :: new (
1012
1009
Arc :: new ( MockFileDownloader :: new ( ) ) ,
@@ -1015,15 +1012,17 @@ mod tests {
1015
1012
) ;
1016
1013
1017
1014
let build_tasks_result = artifact_downloader. new_ancillary_download_task (
1018
- & [ AncillaryLocation :: Unknown { } ] ,
1015
+ & AncillaryMessagePart {
1016
+ locations : vec ! [ AncillaryLocation :: Unknown { } ] ,
1017
+ size_uncompressed : 0 ,
1018
+ } ,
1019
1019
target_dir,
1020
- 0 ,
1021
1020
"download_id" ,
1022
1021
) ;
1023
1022
1024
1023
assert ! (
1025
1024
build_tasks_result. is_err( ) ,
1026
- "building tasks should fail if a location is unknown"
1025
+ "building tasks should fail if all location are unknown"
1027
1026
) ;
1028
1027
}
1029
1028
@@ -1048,18 +1047,20 @@ mod tests {
1048
1047
1049
1048
let task = artifact_downloader
1050
1049
. new_ancillary_download_task (
1051
- & [
1052
- AncillaryLocation :: CloudStorage {
1053
- uri : "http://whatever-1/ancillary.tar.gz" . to_string ( ) ,
1054
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
1055
- } ,
1056
- AncillaryLocation :: CloudStorage {
1057
- uri : "http://whatever-2/ancillary.tar.gz" . to_string ( ) ,
1058
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
1059
- } ,
1060
- ] ,
1050
+ & AncillaryMessagePart {
1051
+ locations : vec ! [
1052
+ AncillaryLocation :: CloudStorage {
1053
+ uri: "http://whatever-1/ancillary.tar.gz" . to_string( ) ,
1054
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
1055
+ } ,
1056
+ AncillaryLocation :: CloudStorage {
1057
+ uri: "http://whatever-2/ancillary.tar.gz" . to_string( ) ,
1058
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
1059
+ } ,
1060
+ ] ,
1061
+ size_uncompressed : 0 ,
1062
+ } ,
1061
1063
target_dir,
1062
- 0 ,
1063
1064
"download_id" ,
1064
1065
)
1065
1066
. unwrap ( ) ;
@@ -1087,18 +1088,20 @@ mod tests {
1087
1088
1088
1089
let task = artifact_downloader
1089
1090
. new_ancillary_download_task (
1090
- & [
1091
- AncillaryLocation :: CloudStorage {
1092
- uri : "http://whatever-1/ancillary.tar.gz" . to_string ( ) ,
1093
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
1094
- } ,
1095
- AncillaryLocation :: CloudStorage {
1096
- uri : "http://whatever-2/ancillary.tar.gz" . to_string ( ) ,
1097
- compression_algorithm : Some ( CompressionAlgorithm :: default ( ) ) ,
1098
- } ,
1099
- ] ,
1091
+ & AncillaryMessagePart {
1092
+ locations : vec ! [
1093
+ AncillaryLocation :: CloudStorage {
1094
+ uri: "http://whatever-1/ancillary.tar.gz" . to_string( ) ,
1095
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
1096
+ } ,
1097
+ AncillaryLocation :: CloudStorage {
1098
+ uri: "http://whatever-2/ancillary.tar.gz" . to_string( ) ,
1099
+ compression_algorithm: Some ( CompressionAlgorithm :: default ( ) ) ,
1100
+ } ,
1101
+ ] ,
1102
+ size_uncompressed : 0 ,
1103
+ } ,
1100
1104
target_dir,
1101
- 0 ,
1102
1105
"download_id" ,
1103
1106
)
1104
1107
. unwrap ( ) ;
0 commit comments