@@ -26,7 +26,7 @@ const AES_IV_LENGTH: usize = 16;
26
26
27
27
#[ cfg_attr( test, automock) ]
28
28
pub trait PreComputeAppTrait {
29
- fn run ( & mut self , chain_task_id : & str ) -> Result < ( ) , ReplicateStatusCause > ;
29
+ fn run ( & self ) -> Result < ( ) , ReplicateStatusCause > ;
30
30
fn check_output_folder ( & self ) -> Result < ( ) , ReplicateStatusCause > ;
31
31
fn download_input_files ( & self ) -> Result < ( ) , ReplicateStatusCause > ;
32
32
fn download_encrypted_dataset ( & self ) -> Result < Vec < u8 > , ReplicateStatusCause > ;
@@ -35,25 +35,23 @@ pub trait PreComputeAppTrait {
35
35
}
36
36
37
37
pub struct PreComputeApp {
38
- chain_task_id : Option < String > ,
39
- pre_compute_args : Option < PreComputeArgs > ,
38
+ chain_task_id : String ,
39
+ pre_compute_args : PreComputeArgs ,
40
40
}
41
41
42
42
impl PreComputeApp {
43
- pub fn new ( ) -> Self {
43
+ pub fn new ( chain_task_id : String , pre_compute_args : PreComputeArgs ) -> Self {
44
44
PreComputeApp {
45
- chain_task_id : None ,
46
- pre_compute_args : None ,
45
+ chain_task_id,
46
+ pre_compute_args,
47
47
}
48
48
}
49
49
}
50
50
51
51
impl PreComputeAppTrait for PreComputeApp {
52
- fn run ( & mut self , chain_task_id : & str ) -> Result < ( ) , ReplicateStatusCause > {
53
- self . chain_task_id = Some ( chain_task_id. to_string ( ) ) ;
54
- self . pre_compute_args = Some ( PreComputeArgs :: read_args ( ) ?) ;
52
+ fn run ( & self ) -> Result < ( ) , ReplicateStatusCause > {
55
53
self . check_output_folder ( ) ?;
56
- if self . pre_compute_args . as_ref ( ) . unwrap ( ) . is_dataset_required {
54
+ if self . pre_compute_args . is_dataset_required {
57
55
let encrypted_content = self . download_encrypted_dataset ( ) ?;
58
56
let plain_content = self . decrypt_dataset ( & encrypted_content) ?;
59
57
self . save_plain_dataset_file ( & plain_content) ?;
@@ -82,14 +80,8 @@ impl PreComputeAppTrait for PreComputeApp {
82
80
/// pre_compute_app.check_output_folder()?;
83
81
/// ```
84
82
fn check_output_folder ( & self ) -> Result < ( ) , ReplicateStatusCause > {
85
- let output_dir = self
86
- . pre_compute_args
87
- . as_ref ( )
88
- . ok_or ( ReplicateStatusCause :: PreComputeOutputFolderNotFound ) ?
89
- . output_dir
90
- . clone ( ) ;
91
-
92
- let chain_task_id = self . chain_task_id . as_deref ( ) . unwrap_or ( "unknown" ) ;
83
+ let output_dir: & str = & self . pre_compute_args . output_dir ;
84
+ let chain_task_id: & str = & self . chain_task_id ;
93
85
94
86
info ! ( "Checking output folder [chainTaskId:{chain_task_id}, path:{output_dir}]" ) ;
95
87
@@ -130,8 +122,8 @@ impl PreComputeAppTrait for PreComputeApp {
130
122
/// pre_compute_app.download_input_files()?;
131
123
/// ```
132
124
fn download_input_files ( & self ) -> Result < ( ) , ReplicateStatusCause > {
133
- let args = self . pre_compute_args . as_ref ( ) . unwrap ( ) ;
134
- let chain_task_id = self . chain_task_id . as_ref ( ) . unwrap ( ) ;
125
+ let args = & self . pre_compute_args ;
126
+ let chain_task_id: & str = & self . chain_task_id ;
135
127
136
128
for url in & args. input_files {
137
129
info ! ( "Downloading input file [chainTaskId:{chain_task_id}, url:{url}]" ) ;
@@ -162,8 +154,8 @@ impl PreComputeAppTrait for PreComputeApp {
162
154
/// app.download_encrypted_dataset()?;
163
155
/// ```
164
156
fn download_encrypted_dataset ( & self ) -> Result < Vec < u8 > , ReplicateStatusCause > {
165
- let args = self . pre_compute_args . as_ref ( ) . unwrap ( ) ;
166
- let chain_task_id = self . chain_task_id . as_ref ( ) . unwrap ( ) ;
157
+ let args = & self . pre_compute_args ;
158
+ let chain_task_id = & self . chain_task_id ;
167
159
let encrypted_dataset_url = args. encrypted_dataset_url . as_ref ( ) . unwrap ( ) ;
168
160
169
161
info ! (
@@ -233,8 +225,6 @@ impl PreComputeAppTrait for PreComputeApp {
233
225
fn decrypt_dataset ( & self , encrypted_content : & [ u8 ] ) -> Result < Vec < u8 > , ReplicateStatusCause > {
234
226
let base64_key = self
235
227
. pre_compute_args
236
- . as_ref ( )
237
- . unwrap ( )
238
228
. encrypted_dataset_base64_key
239
229
. as_ref ( )
240
230
. unwrap ( ) ;
@@ -280,10 +270,10 @@ impl PreComputeAppTrait for PreComputeApp {
280
270
/// app.save_plain_dataset_file(&plain_data)?;
281
271
/// ```
282
272
fn save_plain_dataset_file ( & self , plain_dataset : & [ u8 ] ) -> Result < ( ) , ReplicateStatusCause > {
283
- let chain_task_id = self . chain_task_id . as_ref ( ) . unwrap ( ) ;
284
- let args = self . pre_compute_args . as_ref ( ) . unwrap ( ) ;
285
- let output_dir = & args. output_dir ;
286
- let plain_dataset_filename = args. plain_dataset_filename . as_ref ( ) . unwrap ( ) ;
273
+ let chain_task_id: & str = & self . chain_task_id ;
274
+ let args = & self . pre_compute_args ;
275
+ let output_dir: & str = & args. output_dir ;
276
+ let plain_dataset_filename: & str = args. plain_dataset_filename . as_ref ( ) . unwrap ( ) ;
287
277
288
278
let mut path = PathBuf :: from ( output_dir) ;
289
279
path. push ( plain_dataset_filename) ;
@@ -330,16 +320,16 @@ mod tests {
330
320
output_dir : & str ,
331
321
) -> PreComputeApp {
332
322
PreComputeApp {
333
- chain_task_id : Some ( chain_task_id. to_string ( ) ) ,
334
- pre_compute_args : Some ( PreComputeArgs {
323
+ chain_task_id : chain_task_id. to_string ( ) ,
324
+ pre_compute_args : PreComputeArgs {
335
325
input_files : urls. into_iter ( ) . map ( String :: from) . collect ( ) ,
336
326
output_dir : output_dir. to_string ( ) ,
337
327
is_dataset_required : true ,
338
328
encrypted_dataset_url : Some ( HTTP_DATASET_URL . to_string ( ) ) ,
339
329
encrypted_dataset_base64_key : Some ( ENCRYPTED_DATASET_KEY . to_string ( ) ) ,
340
330
encrypted_dataset_checksum : Some ( DATASET_CHECKSUM . to_string ( ) ) ,
341
331
plain_dataset_filename : Some ( PLAIN_DATA_FILE . to_string ( ) ) ,
342
- } ) ,
332
+ } ,
343
333
}
344
334
}
345
335
@@ -383,19 +373,6 @@ mod tests {
383
373
) ;
384
374
}
385
375
386
- #[ test]
387
- fn check_output_folder_returns_err_with_invalid_pre_compute_args ( ) {
388
- let app = PreComputeApp {
389
- chain_task_id : Some ( CHAIN_TASK_ID . to_string ( ) ) ,
390
- pre_compute_args : None ,
391
- } ;
392
-
393
- let result = app. check_output_folder ( ) ;
394
- assert_eq ! (
395
- result,
396
- Err ( ReplicateStatusCause :: PreComputeOutputFolderNotFound )
397
- ) ;
398
- }
399
376
// endregion
400
377
401
378
// region download_input_files
@@ -500,9 +477,7 @@ mod tests {
500
477
#[ test]
501
478
fn download_encrypted_dataset_failure_with_invalid_dataset_url ( ) {
502
479
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
503
- if let Some ( args) = & mut app. pre_compute_args {
504
- args. encrypted_dataset_url = Some ( "http://bad-url" . to_string ( ) ) ;
505
- }
480
+ app. pre_compute_args . encrypted_dataset_url = Some ( "http://bad-url" . to_string ( ) ) ;
506
481
let actual_content = app. download_encrypted_dataset ( ) ;
507
482
assert_eq ! (
508
483
actual_content,
@@ -513,12 +488,9 @@ mod tests {
513
488
#[ test]
514
489
fn download_encrypted_dataset_success_with_valid_iexec_gateway ( ) {
515
490
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
516
- if let Some ( args) = & mut app. pre_compute_args {
517
- args. encrypted_dataset_url = Some ( IPFS_DATASET_URL . to_string ( ) ) ;
518
- args. encrypted_dataset_checksum = Some (
519
- "0x323b1637c7999942fbebfe5d42fe15dbfe93737577663afa0181938d7ad4a2ac" . to_string ( ) ,
520
- )
521
- }
491
+ app. pre_compute_args . encrypted_dataset_url = Some ( IPFS_DATASET_URL . to_string ( ) ) ;
492
+ app. pre_compute_args . encrypted_dataset_checksum =
493
+ Some ( "0x323b1637c7999942fbebfe5d42fe15dbfe93737577663afa0181938d7ad4a2ac" . to_string ( ) ) ;
522
494
let actual_content = app. download_encrypted_dataset ( ) ;
523
495
let expected_content = Ok ( "hello world !\n " . as_bytes ( ) . to_vec ( ) ) ;
524
496
assert_eq ! ( actual_content, expected_content) ;
@@ -527,9 +499,8 @@ mod tests {
527
499
#[ test]
528
500
fn download_encrypted_dataset_failure_with_invalid_gateway ( ) {
529
501
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
530
- if let Some ( args) = & mut app. pre_compute_args {
531
- args. encrypted_dataset_url = Some ( "/ipfs/INVALID_IPFS_DATASET_URL" . to_string ( ) ) ;
532
- }
502
+ app. pre_compute_args . encrypted_dataset_url =
503
+ Some ( "/ipfs/INVALID_IPFS_DATASET_URL" . to_string ( ) ) ;
533
504
let actual_content = app. download_encrypted_dataset ( ) ;
534
505
let expected_content = Err ( ReplicateStatusCause :: PreComputeDatasetDownloadFailed ) ;
535
506
assert_eq ! ( actual_content, expected_content) ;
@@ -538,9 +509,8 @@ mod tests {
538
509
#[ test]
539
510
fn download_encrypted_dataset_failure_with_invalid_dataset_checksum ( ) {
540
511
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
541
- if let Some ( args) = & mut app. pre_compute_args {
542
- args. encrypted_dataset_checksum = Some ( "invalid_dataset_checksum" . to_string ( ) )
543
- }
512
+ app. pre_compute_args . encrypted_dataset_checksum =
513
+ Some ( "invalid_dataset_checksum" . to_string ( ) ) ;
544
514
let actual_content = app. download_encrypted_dataset ( ) ;
545
515
let expected_content = Err ( ReplicateStatusCause :: PreComputeInvalidDatasetChecksum ) ;
546
516
assert_eq ! ( actual_content, expected_content) ;
@@ -562,9 +532,7 @@ mod tests {
562
532
#[ test]
563
533
fn decrypt_dataset_failure_with_bad_key ( ) {
564
534
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
565
- if let Some ( args) = & mut app. pre_compute_args {
566
- args. encrypted_dataset_base64_key = Some ( "bad_key" . to_string ( ) ) ;
567
- }
535
+ app. pre_compute_args . encrypted_dataset_base64_key = Some ( "bad_key" . to_string ( ) ) ;
568
536
let encrypted_data = app. download_encrypted_dataset ( ) . unwrap ( ) ;
569
537
let actual_plain_data = app. decrypt_dataset ( & encrypted_data) ;
570
538
@@ -608,9 +576,7 @@ mod tests {
608
576
let output_path = temp_dir. path ( ) . to_str ( ) . unwrap ( ) ;
609
577
610
578
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , output_path) ;
611
- if let Some ( args) = & mut app. pre_compute_args {
612
- args. plain_dataset_filename = Some ( "/some-folder-123/not-found" . to_string ( ) ) ;
613
- }
579
+ app. pre_compute_args . plain_dataset_filename = Some ( "/some-folder-123/not-found" . to_string ( ) ) ;
614
580
let plain_dataset = "Some very useful data." . as_bytes ( ) . to_vec ( ) ;
615
581
let saved_dataset = app. save_plain_dataset_file ( & plain_dataset) ;
616
582
0 commit comments