mithril_common/messages/
snapshot_download.rs

1use serde::{Deserialize, Serialize};
2
3use crate::entities::{CardanoDbBeacon, CompressionAlgorithm, Epoch};
4
5/// Message structure of a snapshot
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct SnapshotDownloadMessage {
8    /// Digest that is signed by the signer participants
9    pub digest: String,
10
11    /// Cardano network
12    pub network: String,
13
14    /// Mithril beacon on the Cardano chain
15    pub beacon: CardanoDbBeacon,
16
17    /// Size of the immutables snapshot file in Bytes
18    pub size: u64,
19
20    /// Size of the ancillary files in Bytes
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub ancillary_size: Option<u64>,
23
24    /// Locations where the snapshot of the immutable files can be retrieved
25    pub locations: Vec<String>,
26
27    /// Locations where the snapshot of the ancillary files can be retrieved
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub ancillary_locations: Option<Vec<String>>,
30
31    /// Compression algorithm of the snapshot archive
32    pub compression_algorithm: CompressionAlgorithm,
33
34    /// Cardano node version
35    pub cardano_node_version: String,
36}
37
38impl SnapshotDownloadMessage {
39    /// Return a dummy test entity (test-only).
40    pub fn dummy() -> Self {
41        Self {
42            digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
43            network: "preview".to_string(),
44            beacon: CardanoDbBeacon {
45                epoch: Epoch(86),
46                immutable_file_number: 1728,
47            },
48            size: 807803196,
49            ancillary_size: Some(123456789),
50            locations: vec!["https://host/certificate.tar.gz".to_string()],
51            ancillary_locations: Some(vec!["https://host/ancillary.tar.gz".to_string()]),
52            compression_algorithm: CompressionAlgorithm::Gzip,
53            cardano_node_version: "0.0.1".to_string(),
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
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}