mithril-client 0.14.5

Mithril client library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use std::path::Path;

use anyhow::anyhow;
use async_trait::async_trait;

use mithril_common::{
    StdError, StdResult,
    entities::{
        AncillaryLocation, CompressionAlgorithm, DigestLocation, FileUri, ImmutableFileNumber,
    },
};

use crate::feedback::{MithrilEvent, MithrilEventCardanoDatabase};

/// A file downloader URI
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum FileDownloaderUri {
    /// A single file URI
    FileUri(FileUri),
}

impl FileDownloaderUri {
    /// Get the URI as a string
    pub fn as_str(&self) -> &str {
        match self {
            FileDownloaderUri::FileUri(file_uri) => file_uri.0.as_str(),
        }
    }
}

impl From<String> for FileDownloaderUri {
    fn from(location: String) -> Self {
        Self::FileUri(FileUri(location))
    }
}

impl From<FileUri> for FileDownloaderUri {
    fn from(file_uri: FileUri) -> Self {
        Self::FileUri(file_uri)
    }
}

impl TryFrom<AncillaryLocation> for FileDownloaderUri {
    type Error = StdError;

    fn try_from(location: AncillaryLocation) -> Result<Self, Self::Error> {
        match location {
            AncillaryLocation::CloudStorage {
                uri,
                compression_algorithm: _,
            } => Ok(Self::FileUri(FileUri(uri))),
            AncillaryLocation::Unknown => {
                Err(anyhow!("Unknown location type to download ancillary"))
            }
        }
    }
}

impl TryFrom<DigestLocation> for FileDownloaderUri {
    type Error = StdError;

    fn try_from(location: DigestLocation) -> Result<Self, Self::Error> {
        match location {
            DigestLocation::CloudStorage {
                uri,
                compression_algorithm: _,
            }
            | DigestLocation::Aggregator { uri } => Ok(Self::FileUri(FileUri(uri))),
            DigestLocation::Unknown => Err(anyhow!("Unknown location type to download digest")),
        }
    }
}

/// A download event
///
/// The `download_id` is a unique identifier that allow
/// [feedback receivers][crate::feedback::FeedbackReceiver] to track concurrent downloads.
#[derive(Debug, Clone)]
pub enum DownloadEvent {
    /// Immutable file download
    Immutable {
        /// Unique download identifier
        download_id: String,
        /// Immutable file number
        immutable_file_number: ImmutableFileNumber,
    },
    /// Ancillary file download
    Ancillary {
        /// Unique download identifier
        download_id: String,
    },
    /// Digest file download
    Digest {
        /// Unique download identifier
        download_id: String,
    },
    /// Database download of all immutable files together
    Full {
        /// Unique download identifier
        download_id: String,
        /// Digest of the downloaded snapshot
        digest: String,
    },
    /// Download of the ancillary file associated with a full immutables download
    FullAncillary {
        /// Unique download identifier
        download_id: String,
    },
}

impl DownloadEvent {
    /// Get the unique download identifier
    pub fn download_id(&self) -> &str {
        match self {
            DownloadEvent::Immutable { download_id, .. }
            | DownloadEvent::Ancillary { download_id }
            | DownloadEvent::Digest { download_id }
            | DownloadEvent::Full { download_id, .. }
            | DownloadEvent::FullAncillary { download_id } => download_id,
        }
    }

    /// Build a download started event
    pub fn build_download_started_event(&self, size: u64) -> MithrilEvent {
        match self {
            DownloadEvent::Immutable {
                download_id,
                immutable_file_number,
            } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::ImmutableDownloadStarted {
                    download_id: download_id.to_string(),
                    immutable_file_number: *immutable_file_number,
                    size,
                },
            ),
            DownloadEvent::Ancillary { download_id } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::AncillaryDownloadStarted {
                    download_id: download_id.to_string(),
                    size,
                },
            ),
            DownloadEvent::Digest { download_id } => {
                MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::DigestDownloadStarted {
                    download_id: download_id.to_string(),
                    size,
                })
            }
            DownloadEvent::Full {
                download_id,
                digest,
            } => MithrilEvent::SnapshotDownloadStarted {
                download_id: download_id.to_string(),
                digest: digest.to_string(),
                size,
            },
            DownloadEvent::FullAncillary { download_id } => {
                MithrilEvent::SnapshotAncillaryDownloadStarted {
                    download_id: download_id.to_string(),
                    size,
                }
            }
        }
    }

    /// Build a download started event
    pub fn build_download_progress_event(
        &self,
        downloaded_bytes: u64,
        total_bytes: u64,
    ) -> MithrilEvent {
        match self {
            DownloadEvent::Immutable {
                immutable_file_number,
                download_id,
            } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::ImmutableDownloadProgress {
                    download_id: download_id.to_string(),
                    downloaded_bytes,
                    size: total_bytes,
                    immutable_file_number: *immutable_file_number,
                },
            ),
            DownloadEvent::Ancillary { download_id } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::AncillaryDownloadProgress {
                    download_id: download_id.to_string(),
                    downloaded_bytes,
                    size: total_bytes,
                },
            ),
            DownloadEvent::Digest { download_id } => {
                MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::DigestDownloadProgress {
                    download_id: download_id.to_string(),
                    downloaded_bytes,
                    size: total_bytes,
                })
            }
            DownloadEvent::Full { download_id, .. } => MithrilEvent::SnapshotDownloadProgress {
                download_id: download_id.to_string(),
                downloaded_bytes,
                size: total_bytes,
            },
            DownloadEvent::FullAncillary { download_id } => {
                MithrilEvent::SnapshotAncillaryDownloadProgress {
                    download_id: download_id.to_string(),
                    downloaded_bytes,
                    size: total_bytes,
                }
            }
        }
    }

    /// Build a download completed event
    pub fn build_download_completed_event(&self) -> MithrilEvent {
        match self {
            DownloadEvent::Immutable {
                download_id,
                immutable_file_number,
            } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::ImmutableDownloadCompleted {
                    download_id: download_id.to_string(),
                    immutable_file_number: *immutable_file_number,
                },
            ),
            DownloadEvent::Ancillary { download_id } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::AncillaryDownloadCompleted {
                    download_id: download_id.to_string(),
                },
            ),
            DownloadEvent::Digest { download_id } => MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::DigestDownloadCompleted {
                    download_id: download_id.to_string(),
                },
            ),
            DownloadEvent::Full { download_id, .. } => MithrilEvent::SnapshotDownloadCompleted {
                download_id: download_id.to_string(),
            },
            DownloadEvent::FullAncillary { download_id, .. } => {
                MithrilEvent::SnapshotAncillaryDownloadCompleted {
                    download_id: download_id.to_string(),
                }
            }
        }
    }
}

/// A file downloader
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait FileDownloader: Sync + Send {
    /// Download and unpack (if necessary) a file on the disk.
    ///
    async fn download_unpack(
        &self,
        location: &FileDownloaderUri,
        file_size: u64,
        target_dir: &Path,
        compression_algorithm: Option<CompressionAlgorithm>,
        download_event_type: DownloadEvent,
    ) -> StdResult<()>;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn download_event_type_builds_started_event() {
        let download_event_type = DownloadEvent::Immutable {
            download_id: "download-123".to_string(),
            immutable_file_number: 123,
        };
        let event = download_event_type.build_download_started_event(1234);
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::ImmutableDownloadStarted {
                immutable_file_number: 123,
                download_id: "download-123".to_string(),
                size: 1234,
            }),
            event,
        );

        let download_event_type = DownloadEvent::Ancillary {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_started_event(1234);
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::AncillaryDownloadStarted {
                download_id: "download-123".to_string(),
                size: 1234,
            }),
            event,
        );

        let download_event_type = DownloadEvent::Digest {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_started_event(1234);
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::DigestDownloadStarted {
                download_id: "download-123".to_string(),
                size: 1234,
            }),
            event,
        );

        let download_event_type = DownloadEvent::Full {
            download_id: "download-123".to_string(),
            digest: "digest-123".to_string(),
        };
        let event = download_event_type.build_download_started_event(1234);
        assert_eq!(
            MithrilEvent::SnapshotDownloadStarted {
                digest: "digest-123".to_string(),
                download_id: "download-123".to_string(),
                size: 1234,
            },
            event,
        );

        let download_event_type = DownloadEvent::FullAncillary {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_started_event(1234);
        assert_eq!(
            MithrilEvent::SnapshotAncillaryDownloadStarted {
                download_id: "download-123".to_string(),
                size: 1234,
            },
            event,
        );
    }

    #[test]
    fn download_event_type_builds_progress_event() {
        let download_event_type = DownloadEvent::Immutable {
            download_id: "download-123".to_string(),
            immutable_file_number: 123,
        };
        let event = download_event_type.build_download_progress_event(123, 1234);
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::ImmutableDownloadProgress {
                immutable_file_number: 123,
                download_id: "download-123".to_string(),
                downloaded_bytes: 123,
                size: 1234,
            }),
            event,
        );

        let download_event_type = DownloadEvent::Ancillary {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_progress_event(123, 1234);
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::AncillaryDownloadProgress {
                download_id: "download-123".to_string(),
                downloaded_bytes: 123,
                size: 1234,
            }),
            event,
        );

        let download_event_type = DownloadEvent::Digest {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_progress_event(123, 1234);
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::DigestDownloadProgress {
                download_id: "download-123".to_string(),
                downloaded_bytes: 123,
                size: 1234,
            }),
            event,
        );

        let download_event_type = DownloadEvent::Full {
            download_id: "download-123".to_string(),
            digest: "whatever".to_string(),
        };
        let event = download_event_type.build_download_progress_event(123, 1234);
        assert_eq!(
            MithrilEvent::SnapshotDownloadProgress {
                download_id: "download-123".to_string(),
                downloaded_bytes: 123,
                size: 1234,
            },
            event,
        );

        let download_event_type = DownloadEvent::FullAncillary {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_progress_event(123, 1234);
        assert_eq!(
            MithrilEvent::SnapshotAncillaryDownloadProgress {
                download_id: "download-123".to_string(),
                downloaded_bytes: 123,
                size: 1234,
            },
            event,
        );
    }

    #[test]
    fn file_downloader_uri_from_ancillary_location() {
        let location = AncillaryLocation::CloudStorage {
            uri: "http://whatever/ancillary-1".to_string(),
            compression_algorithm: Some(CompressionAlgorithm::Gzip),
        };
        let file_downloader_uri: FileDownloaderUri = location.try_into().unwrap();

        assert_eq!(
            FileDownloaderUri::FileUri(FileUri("http://whatever/ancillary-1".to_string())),
            file_downloader_uri
        );
    }
    #[test]
    fn file_downloader_uri_from_unknown_ancillary_location() {
        let location = AncillaryLocation::Unknown;
        let file_downloader_uri: StdResult<FileDownloaderUri> = location.try_into();

        file_downloader_uri.expect_err("try_into should fail on Unknown ancillary location");
    }

    #[test]
    fn file_downloader_uri_from_digest_location() {
        let location = DigestLocation::CloudStorage {
            uri: "http://whatever/digest-1".to_string(),
            compression_algorithm: None,
        };
        let file_downloader_uri: FileDownloaderUri = location.try_into().unwrap();

        assert_eq!(
            FileDownloaderUri::FileUri(FileUri("http://whatever/digest-1".to_string())),
            file_downloader_uri
        );
    }
    #[test]
    fn file_downloader_uri_from_unknown_digest_location() {
        let location = DigestLocation::Unknown;
        let file_downloader_uri: StdResult<FileDownloaderUri> = location.try_into();

        file_downloader_uri.expect_err("try_into should fail on Unknown digest location");
    }

    #[test]
    fn download_event_type_builds_completed_event() {
        let download_event_type = DownloadEvent::Immutable {
            download_id: "download-123".to_string(),
            immutable_file_number: 123,
        };
        let event = download_event_type.build_download_completed_event();
        assert_eq!(
            MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::ImmutableDownloadCompleted {
                    immutable_file_number: 123,
                    download_id: "download-123".to_string()
                }
            ),
            event,
        );

        let download_event_type = DownloadEvent::Ancillary {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_completed_event();
        assert_eq!(
            MithrilEvent::CardanoDatabase(
                MithrilEventCardanoDatabase::AncillaryDownloadCompleted {
                    download_id: "download-123".to_string(),
                }
            ),
            event,
        );

        let download_event_type = DownloadEvent::Digest {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_completed_event();
        assert_eq!(
            MithrilEvent::CardanoDatabase(MithrilEventCardanoDatabase::DigestDownloadCompleted {
                download_id: "download-123".to_string(),
            }),
            event,
        );

        let download_event_type = DownloadEvent::Full {
            download_id: "download-123".to_string(),
            digest: "whatever".to_string(),
        };
        let event = download_event_type.build_download_completed_event();
        assert_eq!(
            MithrilEvent::SnapshotDownloadCompleted {
                download_id: "download-123".to_string(),
            },
            event,
        );

        let download_event_type = DownloadEvent::FullAncillary {
            download_id: "download-123".to_string(),
        };
        let event = download_event_type.build_download_completed_event();
        assert_eq!(
            MithrilEvent::SnapshotAncillaryDownloadCompleted {
                download_id: "download-123".to_string(),
            },
            event,
        );
    }
}