mithril_common/messages/
snapshot_download.rs

1use serde::{Deserialize, Serialize};
2
3use crate::entities::{CardanoDbBeacon, CompressionAlgorithm};
4use crate::messages::SnapshotMessage;
5
6/// Message structure of a snapshot
7#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
8pub struct SnapshotDownloadMessage {
9    /// Digest that is signed by the signer participants
10    pub digest: String,
11
12    /// Cardano network
13    pub network: String,
14
15    /// Mithril beacon on the Cardano chain
16    pub beacon: CardanoDbBeacon,
17
18    /// Size of the immutables snapshot file in Bytes
19    pub size: u64,
20
21    /// Size of the ancillary files in Bytes
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub ancillary_size: Option<u64>,
24
25    /// Locations where the snapshot of the immutable files can be retrieved
26    pub locations: Vec<String>,
27
28    /// Locations where the snapshot of the ancillary files can be retrieved
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub ancillary_locations: Option<Vec<String>>,
31
32    /// Compression algorithm of the snapshot archive
33    pub compression_algorithm: CompressionAlgorithm,
34
35    /// Cardano node version
36    pub cardano_node_version: String,
37}
38
39impl From<SnapshotMessage> for SnapshotDownloadMessage {
40    fn from(message: SnapshotMessage) -> Self {
41        Self {
42            digest: message.digest,
43            network: message.network,
44            beacon: message.beacon,
45            size: message.size,
46            ancillary_size: message.ancillary_size,
47            locations: message.locations,
48            ancillary_locations: message.ancillary_locations,
49            compression_algorithm: message.compression_algorithm,
50            cardano_node_version: message.cardano_node_version,
51        }
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use crate::entities::Epoch;
58    use crate::test::double::Dummy;
59
60    use super::*;
61
62    #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
63    pub struct SnapshotDownloadMessageUntilV0_1_47 {
64        pub digest: String,
65        pub network: String,
66        pub beacon: CardanoDbBeacon,
67        pub size: u64,
68        pub locations: Vec<String>,
69        pub compression_algorithm: CompressionAlgorithm,
70        pub cardano_node_version: String,
71    }
72
73    const CURRENT_JSON: &str = r#"{
74        "digest": "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6",
75        "network": "preview",
76        "beacon": {
77            "epoch": 86,
78            "immutable_file_number": 1728
79        },
80        "size": 807803196,
81        "ancillary_size": 123456789,
82        "locations": [
83            "https://host/certificate.tar.gz"
84        ],
85        "ancillary_locations": [
86            "https://host/ancillary.tar.gz"
87        ],
88        "compression_algorithm": "gzip",
89        "cardano_node_version": "0.0.1"
90    }"#;
91
92    fn golden_message_until_open_api_0_1_47() -> SnapshotDownloadMessageUntilV0_1_47 {
93        SnapshotDownloadMessageUntilV0_1_47 {
94            digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
95            network: "preview".to_string(),
96            beacon: CardanoDbBeacon {
97                epoch: Epoch(86),
98                immutable_file_number: 1728,
99            },
100            size: 807803196,
101            locations: vec!["https://host/certificate.tar.gz".to_string()],
102            compression_algorithm: CompressionAlgorithm::Gzip,
103            cardano_node_version: "0.0.1".to_string(),
104        }
105    }
106
107    fn golden_message_current() -> SnapshotDownloadMessage {
108        SnapshotDownloadMessage {
109            digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
110            network: "preview".to_string(),
111            beacon: CardanoDbBeacon {
112                epoch: Epoch(86),
113                immutable_file_number: 1728,
114            },
115            size: 807803196,
116            ancillary_size: Some(123456789),
117            locations: vec!["https://host/certificate.tar.gz".to_string()],
118            ancillary_locations: Some(vec!["https://host/ancillary.tar.gz".to_string()]),
119            compression_algorithm: CompressionAlgorithm::Gzip,
120            cardano_node_version: "0.0.1".to_string(),
121        }
122    }
123
124    #[test]
125    fn test_current_json_deserialized_into_message_supported_until_open_api_0_1_47() {
126        let json = CURRENT_JSON;
127        let message: SnapshotDownloadMessageUntilV0_1_47 = serde_json::from_str(json).unwrap();
128
129        assert_eq!(golden_message_until_open_api_0_1_47(), message);
130    }
131
132    #[test]
133    fn test_current_json_deserialized_into_current_message() {
134        let json = CURRENT_JSON;
135        let message: SnapshotDownloadMessage = serde_json::from_str(json).unwrap();
136
137        assert_eq!(golden_message_current(), message);
138    }
139
140    #[test]
141    fn convert_from_snapshot_message() {
142        let snapshot_message = SnapshotMessage::dummy();
143        let snapshot_download_message = SnapshotDownloadMessage::from(snapshot_message.clone());
144
145        assert_eq!(
146            snapshot_download_message,
147            SnapshotDownloadMessage {
148                digest: snapshot_message.digest,
149                network: snapshot_message.network,
150                beacon: snapshot_message.beacon,
151                size: snapshot_message.size,
152                ancillary_size: snapshot_message.ancillary_size,
153                locations: snapshot_message.locations,
154                ancillary_locations: snapshot_message.ancillary_locations,
155                compression_algorithm: snapshot_message.compression_algorithm,
156                cardano_node_version: snapshot_message.cardano_node_version,
157            }
158        )
159    }
160}