Skip to content

Commit 295c645

Browse files
committed
Add SnapshotDownloadMessage type
1 parent 54faa24 commit 295c645

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

mithril-common/src/messages/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod mithril_stake_distribution_list;
1111
mod register_signature;
1212
mod register_signer;
1313
mod snapshot;
14+
mod snapshot_download;
1415
mod snapshot_list;
1516

1617
pub use certificate::CertificateMessage;
@@ -28,4 +29,5 @@ pub use mithril_stake_distribution_list::{
2829
pub use register_signature::RegisterSignatureMessage;
2930
pub use register_signer::RegisterSignerMessage;
3031
pub use snapshot::SnapshotMessage;
32+
pub use snapshot_download::SnapshotDownloadMessage;
3133
pub use snapshot_list::{SnapshotListItemMessage, SnapshotListMessage};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::entities::{Beacon, CompressionAlgorithm, Epoch};
4+
5+
/// Message structure of a snapshot
6+
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
7+
pub struct SnapshotDownloadMessage {
8+
/// Digest that is signed by the signer participants
9+
pub digest: String,
10+
11+
/// Mithril beacon on the Cardano chain
12+
pub beacon: Beacon,
13+
14+
/// Size of the snapshot file in Bytes
15+
pub size: u64,
16+
17+
/// Locations where the binary content of the snapshot can be retrieved
18+
pub locations: Vec<String>,
19+
20+
/// Compression algorithm of the snapshot archive
21+
pub compression_algorithm: CompressionAlgorithm,
22+
23+
/// Cardano node version
24+
pub cardano_node_version: String,
25+
}
26+
27+
impl SnapshotDownloadMessage {
28+
/// Return a dummy test entity (test-only).
29+
pub fn dummy() -> Self {
30+
Self {
31+
digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
32+
beacon: Beacon {
33+
network: "preview".to_string(),
34+
epoch: Epoch(86),
35+
immutable_file_number: 1728,
36+
},
37+
size: 807803196,
38+
locations: vec!["https://host/certificate.tar.gz".to_string()],
39+
compression_algorithm: CompressionAlgorithm::Gzip,
40+
cardano_node_version: "0.0.1".to_string(),
41+
}
42+
}
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
fn golden_message_v1() -> SnapshotDownloadMessage {
50+
SnapshotDownloadMessage {
51+
digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
52+
beacon: Beacon {
53+
network: "preview".to_string(),
54+
epoch: Epoch(86),
55+
immutable_file_number: 1728,
56+
},
57+
size: 807803196,
58+
locations: vec!["https://host/certificate.tar.gz".to_string()],
59+
compression_algorithm: CompressionAlgorithm::Gzip,
60+
cardano_node_version: "0.0.1".to_string(),
61+
}
62+
}
63+
64+
// Test the retro compatibility with possible future upgrades.
65+
#[test]
66+
fn test_v1() {
67+
let json = r#"{
68+
"digest": "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6",
69+
"beacon": {
70+
"network": "preview",
71+
"epoch": 86,
72+
"immutable_file_number": 1728
73+
},
74+
"size": 807803196,
75+
"locations": [
76+
"https://host/certificate.tar.gz"
77+
],
78+
"compression_algorithm": "gzip",
79+
"cardano_node_version": "0.0.1"
80+
}
81+
"#;
82+
let message: SnapshotDownloadMessage = serde_json::from_str(json).expect(
83+
"This JSON is expected to be succesfully parsed into a SnapshotDownloadMessage instance.",
84+
);
85+
86+
assert_eq!(golden_message_v1(), message);
87+
}
88+
}

0 commit comments

Comments
 (0)