@@ -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 ( & mut 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,24 @@ 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 ) -> Self {
44
44
PreComputeApp {
45
- chain_task_id : None ,
46
- pre_compute_args : None ,
45
+ chain_task_id,
46
+ pre_compute_args : PreComputeArgs :: default ( ) ,
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 ( & mut self ) -> Result < ( ) , ReplicateStatusCause > {
53
+ self . pre_compute_args = PreComputeArgs :: read_args ( ) ?;
55
54
self . check_output_folder ( ) ?;
56
- if self . pre_compute_args . as_ref ( ) . unwrap ( ) . is_dataset_required {
55
+ if self . pre_compute_args . is_dataset_required {
57
56
let encrypted_content = self . download_encrypted_dataset ( ) ?;
58
57
let plain_content = self . decrypt_dataset ( & encrypted_content) ?;
59
58
self . save_plain_dataset_file ( & plain_content) ?;
@@ -82,14 +81,8 @@ impl PreComputeAppTrait for PreComputeApp {
82
81
/// pre_compute_app.check_output_folder()?;
83
82
/// ```
84
83
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" ) ;
84
+ let output_dir: & str = & self . pre_compute_args . output_dir ;
85
+ let chain_task_id: & str = & self . chain_task_id ;
93
86
94
87
info ! ( "Checking output folder [chainTaskId:{chain_task_id}, path:{output_dir}]" ) ;
95
88
@@ -130,8 +123,8 @@ impl PreComputeAppTrait for PreComputeApp {
130
123
/// pre_compute_app.download_input_files()?;
131
124
/// ```
132
125
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 ( ) ;
126
+ let args = & self . pre_compute_args ;
127
+ let chain_task_id: & str = & self . chain_task_id ;
135
128
136
129
for url in & args. input_files {
137
130
info ! ( "Downloading input file [chainTaskId:{chain_task_id}, url:{url}]" ) ;
@@ -162,8 +155,8 @@ impl PreComputeAppTrait for PreComputeApp {
162
155
/// app.download_encrypted_dataset()?;
163
156
/// ```
164
157
fn download_encrypted_dataset ( & self ) -> Result < Vec < u8 > , ReplicateStatusCause > {
165
- let args = self . pre_compute_args . as_ref ( ) . unwrap ( ) ;
166
- let chain_task_id: & str = self . chain_task_id . as_ref ( ) . unwrap ( ) ;
158
+ let args = & self . pre_compute_args ;
159
+ let chain_task_id = & self . chain_task_id ;
167
160
let encrypted_dataset_url: & str = & args. encrypted_dataset_url ;
168
161
169
162
info ! (
@@ -228,11 +221,7 @@ impl PreComputeAppTrait for PreComputeApp {
228
221
/// let decrypted = app.decrypt_dataset(&encrypted)?;
229
222
/// ```
230
223
fn decrypt_dataset ( & self , encrypted_content : & [ u8 ] ) -> Result < Vec < u8 > , ReplicateStatusCause > {
231
- let base64_key: & str = & self
232
- . pre_compute_args
233
- . as_ref ( )
234
- . unwrap ( )
235
- . encrypted_dataset_base64_key ;
224
+ let base64_key: & str = & self . pre_compute_args . encrypted_dataset_base64_key ;
236
225
237
226
let key = general_purpose:: STANDARD
238
227
. decode ( base64_key)
@@ -275,8 +264,8 @@ impl PreComputeAppTrait for PreComputeApp {
275
264
/// app.save_plain_dataset_file(&plain_data)?;
276
265
/// ```
277
266
fn save_plain_dataset_file ( & self , plain_dataset : & [ u8 ] ) -> Result < ( ) , ReplicateStatusCause > {
278
- let chain_task_id: & str = self . chain_task_id . as_ref ( ) . unwrap ( ) ;
279
- let args = self . pre_compute_args . as_ref ( ) . unwrap ( ) ;
267
+ let chain_task_id: & str = & self . chain_task_id ;
268
+ let args = & self . pre_compute_args ;
280
269
let output_dir: & str = & args. output_dir ;
281
270
let plain_dataset_filename: & str = & args. plain_dataset_filename ;
282
271
@@ -325,16 +314,16 @@ mod tests {
325
314
output_dir : & str ,
326
315
) -> PreComputeApp {
327
316
PreComputeApp {
328
- chain_task_id : Some ( chain_task_id. to_string ( ) ) ,
329
- pre_compute_args : Some ( PreComputeArgs {
317
+ chain_task_id : chain_task_id. to_string ( ) ,
318
+ pre_compute_args : PreComputeArgs {
330
319
input_files : urls. into_iter ( ) . map ( String :: from) . collect ( ) ,
331
320
output_dir : output_dir. to_string ( ) ,
332
321
is_dataset_required : true ,
333
322
encrypted_dataset_url : HTTP_DATASET_URL . to_string ( ) ,
334
323
encrypted_dataset_base64_key : ENCRYPTED_DATASET_KEY . to_string ( ) ,
335
324
encrypted_dataset_checksum : DATASET_CHECKSUM . to_string ( ) ,
336
325
plain_dataset_filename : PLAIN_DATA_FILE . to_string ( ) ,
337
- } ) ,
326
+ } ,
338
327
}
339
328
}
340
329
@@ -378,19 +367,6 @@ mod tests {
378
367
) ;
379
368
}
380
369
381
- #[ test]
382
- fn check_output_folder_returns_err_with_invalid_pre_compute_args ( ) {
383
- let app = PreComputeApp {
384
- chain_task_id : Some ( CHAIN_TASK_ID . to_string ( ) ) ,
385
- pre_compute_args : None ,
386
- } ;
387
-
388
- let result = app. check_output_folder ( ) ;
389
- assert_eq ! (
390
- result,
391
- Err ( ReplicateStatusCause :: PreComputeOutputFolderNotFound )
392
- ) ;
393
- }
394
370
// endregion
395
371
396
372
// region download_input_files
@@ -495,9 +471,7 @@ mod tests {
495
471
#[ test]
496
472
fn download_encrypted_dataset_failure_with_invalid_dataset_url ( ) {
497
473
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
498
- if let Some ( args) = & mut app. pre_compute_args {
499
- args. encrypted_dataset_url = "http://bad-url" . to_string ( ) ;
500
- }
474
+ app. pre_compute_args . encrypted_dataset_url = "http://bad-url" . to_string ( ) ;
501
475
let actual_content = app. download_encrypted_dataset ( ) ;
502
476
assert_eq ! (
503
477
actual_content,
@@ -508,11 +482,9 @@ mod tests {
508
482
#[ test]
509
483
fn download_encrypted_dataset_success_with_valid_iexec_gateway ( ) {
510
484
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
511
- if let Some ( args) = & mut app. pre_compute_args {
512
- args. encrypted_dataset_url = IPFS_DATASET_URL . to_string ( ) ;
513
- args. encrypted_dataset_checksum =
514
- "0x323b1637c7999942fbebfe5d42fe15dbfe93737577663afa0181938d7ad4a2ac" . to_string ( ) ;
515
- }
485
+ app. pre_compute_args . encrypted_dataset_url = IPFS_DATASET_URL . to_string ( ) ;
486
+ app. pre_compute_args . encrypted_dataset_checksum =
487
+ "0x323b1637c7999942fbebfe5d42fe15dbfe93737577663afa0181938d7ad4a2ac" . to_string ( ) ;
516
488
let actual_content = app. download_encrypted_dataset ( ) ;
517
489
let expected_content = Ok ( "hello world !\n " . as_bytes ( ) . to_vec ( ) ) ;
518
490
assert_eq ! ( actual_content, expected_content) ;
@@ -521,9 +493,7 @@ mod tests {
521
493
#[ test]
522
494
fn download_encrypted_dataset_failure_with_invalid_gateway ( ) {
523
495
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
524
- if let Some ( args) = & mut app. pre_compute_args {
525
- args. encrypted_dataset_url = "/ipfs/INVALID_IPFS_DATASET_URL" . to_string ( ) ;
526
- }
496
+ app. pre_compute_args . encrypted_dataset_url = "/ipfs/INVALID_IPFS_DATASET_URL" . to_string ( ) ;
527
497
let actual_content = app. download_encrypted_dataset ( ) ;
528
498
let expected_content = Err ( ReplicateStatusCause :: PreComputeDatasetDownloadFailed ) ;
529
499
assert_eq ! ( actual_content, expected_content) ;
@@ -532,9 +502,7 @@ mod tests {
532
502
#[ test]
533
503
fn download_encrypted_dataset_failure_with_invalid_dataset_checksum ( ) {
534
504
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
535
- if let Some ( args) = & mut app. pre_compute_args {
536
- args. encrypted_dataset_checksum = "invalid_dataset_checksum" . to_string ( )
537
- }
505
+ app. pre_compute_args . encrypted_dataset_checksum = "invalid_dataset_checksum" . to_string ( ) ;
538
506
let actual_content = app. download_encrypted_dataset ( ) ;
539
507
let expected_content = Err ( ReplicateStatusCause :: PreComputeInvalidDatasetChecksum ) ;
540
508
assert_eq ! ( actual_content, expected_content) ;
@@ -556,9 +524,7 @@ mod tests {
556
524
#[ test]
557
525
fn decrypt_dataset_failure_with_bad_key ( ) {
558
526
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , "" ) ;
559
- if let Some ( args) = & mut app. pre_compute_args {
560
- args. encrypted_dataset_base64_key = "bad_key" . to_string ( ) ;
561
- }
527
+ app. pre_compute_args . encrypted_dataset_base64_key = "bad_key" . to_string ( ) ;
562
528
let encrypted_data = app. download_encrypted_dataset ( ) . unwrap ( ) ;
563
529
let actual_plain_data = app. decrypt_dataset ( & encrypted_data) ;
564
530
@@ -602,9 +568,7 @@ mod tests {
602
568
let output_path = temp_dir. path ( ) . to_str ( ) . unwrap ( ) ;
603
569
604
570
let mut app = get_pre_compute_app ( CHAIN_TASK_ID , vec ! [ ] , output_path) ;
605
- if let Some ( args) = & mut app. pre_compute_args {
606
- args. plain_dataset_filename = "/some-folder-123/not-found" . to_string ( ) ;
607
- }
571
+ app. pre_compute_args . plain_dataset_filename = "/some-folder-123/not-found" . to_string ( ) ;
608
572
let plain_dataset = "Some very useful data." . as_bytes ( ) . to_vec ( ) ;
609
573
let saved_dataset = app. save_plain_dataset_file ( & plain_dataset) ;
610
574
0 commit comments