1
1
use anyhow:: Context ;
2
2
use async_trait:: async_trait;
3
3
use futures:: Future ;
4
- use indicatif:: { MultiProgress , ProgressBar , ProgressDrawTarget , ProgressState , ProgressStyle } ;
4
+ use indicatif:: { MultiProgress , ProgressBar , ProgressState , ProgressStyle } ;
5
5
use slog_scope:: { debug, warn} ;
6
6
use std:: {
7
7
fmt:: Write ,
@@ -24,7 +24,10 @@ use mithril_common::{
24
24
25
25
use crate :: {
26
26
aggregator_client:: { AggregatorHTTPClientError , CertificateClient , SnapshotClient } ,
27
- utils:: { SnapshotUnpacker , SnapshotUnpackerError } ,
27
+ utils:: {
28
+ DownloadProgressReporter , ProgressOutputType , ProgressPrinter , SnapshotUnpacker ,
29
+ SnapshotUnpackerError ,
30
+ } ,
28
31
} ;
29
32
30
33
/// [SnapshotService] related errors.
@@ -72,7 +75,7 @@ pub trait SnapshotService: Sync + Send {
72
75
snapshot_entity : & SignedEntity < Snapshot > ,
73
76
pathdir : & Path ,
74
77
genesis_verification_key : & str ,
75
- progress_target : ProgressDrawTarget ,
78
+ progress_output_type : ProgressOutputType ,
76
79
) -> StdResult < PathBuf > ;
77
80
}
78
81
@@ -210,20 +213,20 @@ impl SnapshotService for MithrilClientSnapshotService {
210
213
snapshot_entity : & SignedEntity < Snapshot > ,
211
214
download_dir : & Path ,
212
215
genesis_verification_key : & str ,
213
- progress_target : ProgressDrawTarget ,
216
+ progress_output_type : ProgressOutputType ,
214
217
) -> StdResult < PathBuf > {
215
218
debug ! ( "Snapshot service: download." ) ;
216
219
217
220
let db_dir = download_dir. join ( "db" ) ;
218
- let progress_bar = MultiProgress :: with_draw_target ( progress_target ) ;
219
- progress_bar. println ( "1/7 - Checking local disk info…") ?;
221
+ let progress_bar = ProgressPrinter :: new ( progress_output_type , 7 ) ;
222
+ progress_bar. report_step ( 1 , " Checking local disk info…") ?;
220
223
let unpacker = SnapshotUnpacker ;
221
224
222
225
if let Err ( e) = unpacker. check_prerequisites ( & db_dir, snapshot_entity. artifact . size ) {
223
226
self . check_disk_space_error ( e) ?;
224
227
}
225
228
226
- progress_bar. println ( "2/7 - Fetching the certificate's information…") ?;
229
+ progress_bar. report_step ( 2 , " Fetching the certificate's information…") ?;
227
230
let certificate = self
228
231
. certificate_client
229
232
. get ( & snapshot_entity. certificate_id )
@@ -234,23 +237,27 @@ impl SnapshotService for MithrilClientSnapshotService {
234
237
)
235
238
} ) ?;
236
239
237
- progress_bar. println ( "3/7 - Verifying the certificate chain…") ?;
240
+ progress_bar. report_step ( 3 , " Verifying the certificate chain…") ?;
238
241
let verifier = self . verify_certificate_chain ( genesis_verification_key, & certificate) ;
239
242
self . wait_spinner ( & progress_bar, verifier) . await ?;
240
243
241
- progress_bar. println ( "4/7 - Downloading the snapshot…") ?;
244
+ progress_bar. report_step ( 4 , " Downloading the snapshot…") ?;
242
245
let pb = progress_bar. add ( ProgressBar :: new ( snapshot_entity. artifact . size ) ) ;
243
246
pb. set_style ( ProgressStyle :: with_template ( "{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})" )
244
247
. unwrap ( )
245
248
. with_key ( "eta" , |state : & ProgressState , w : & mut dyn Write | write ! ( w, "{:.1}s" , state. eta( ) . as_secs_f64( ) ) . unwrap ( ) )
246
249
. progress_chars ( "#>-" ) ) ;
247
250
let snapshot_path = self
248
251
. snapshot_client
249
- . download ( & snapshot_entity. artifact , download_dir, pb)
252
+ . download (
253
+ & snapshot_entity. artifact ,
254
+ download_dir,
255
+ DownloadProgressReporter :: new ( pb, progress_output_type) ,
256
+ )
250
257
. await
251
258
. with_context ( || format ! ( "Could not download file in '{}'" , download_dir. display( ) ) ) ?;
252
259
253
- progress_bar. println ( "5/7 - Unpacking the snapshot…") ?;
260
+ progress_bar. report_step ( 5 , " Unpacking the snapshot…") ?;
254
261
let unpacker = unpacker. unpack_snapshot ( & snapshot_path, & db_dir) ;
255
262
self . wait_spinner ( & progress_bar, unpacker) . await ?;
256
263
@@ -262,14 +269,14 @@ impl SnapshotService for MithrilClientSnapshotService {
262
269
) ;
263
270
} ;
264
271
265
- progress_bar. println ( "6/7 - Computing the snapshot digest…") ?;
272
+ progress_bar. report_step ( 6 , " Computing the snapshot digest…") ?;
266
273
let unpacked_snapshot_digest = self
267
274
. immutable_digester
268
275
. compute_digest ( & db_dir, & certificate. beacon )
269
276
. await
270
277
. with_context ( || format ! ( "Could not compute digest in '{}'" , db_dir. display( ) ) ) ?;
271
278
272
- progress_bar. println ( "7/7 - Verifying the snapshot signature…") ?;
279
+ progress_bar. report_step ( 7 , " Verifying the snapshot signature…") ?;
273
280
let expected_message = {
274
281
let mut protocol_message = certificate. protocol_message . clone ( ) ;
275
282
protocol_message. set_message_part (
@@ -550,7 +557,7 @@ mod tests {
550
557
& snapshot,
551
558
& test_path,
552
559
& genesis_verification_key. to_json_hex ( ) . unwrap ( ) ,
553
- ProgressDrawTarget :: hidden ( ) ,
560
+ ProgressOutputType :: Hidden ,
554
561
)
555
562
. await
556
563
. expect ( "Snapshot download should succeed." ) ;
@@ -590,7 +597,7 @@ mod tests {
590
597
& snapshot,
591
598
& test_path,
592
599
& genesis_verification_key. to_json_hex ( ) . unwrap ( ) ,
593
- ProgressDrawTarget :: hidden ( ) ,
600
+ ProgressOutputType :: Hidden ,
594
601
)
595
602
. await
596
603
. expect ( "Snapshot download should succeed." ) ;
@@ -636,7 +643,7 @@ mod tests {
636
643
& signed_entity,
637
644
& test_path,
638
645
& genesis_verification_key. to_json_hex ( ) . unwrap ( ) ,
639
- ProgressDrawTarget :: hidden ( ) ,
646
+ ProgressOutputType :: Hidden ,
640
647
)
641
648
. await
642
649
. expect_err ( "Snapshot digest comparison should fail." ) ;
@@ -684,7 +691,7 @@ mod tests {
684
691
& snapshot,
685
692
& test_path,
686
693
& genesis_verification_key. to_json_hex ( ) . unwrap ( ) ,
687
- ProgressDrawTarget :: hidden ( ) ,
694
+ ProgressOutputType :: Hidden ,
688
695
)
689
696
. await
690
697
. expect_err ( "Snapshot download should fail." ) ;
0 commit comments