mithril_common/messages/
snapshot_list.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::entities::{CardanoDbBeacon, CompressionAlgorithm, Epoch};
5
6pub type SnapshotListMessage = Vec<SnapshotListItemMessage>;
8
9#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
11pub struct SnapshotListItemMessage {
12 pub digest: String,
14
15 pub network: String,
17
18 pub beacon: CardanoDbBeacon,
20
21 pub certificate_hash: String,
23
24 pub size: u64,
26
27 #[serde(skip_serializing_if = "Option::is_none")]
29 pub ancillary_size: Option<u64>,
30
31 pub created_at: DateTime<Utc>,
33
34 pub locations: Vec<String>,
36
37 #[serde(skip_serializing_if = "Option::is_none")]
39 pub ancillary_locations: Option<Vec<String>>,
40
41 pub compression_algorithm: CompressionAlgorithm,
43
44 pub cardano_node_version: String,
46}
47
48impl SnapshotListItemMessage {
49 pub fn dummy() -> Self {
51 Self {
52 digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
53 network: "preview".to_string(),
54 beacon: CardanoDbBeacon {
55 epoch: Epoch(86),
56 immutable_file_number: 1728,
57 },
58 certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
59 .to_string(),
60 size: 807803196,
61 ancillary_size: Some(123456789),
62 created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
63 .unwrap()
64 .with_timezone(&Utc),
65 locations: vec!["https://host/certificate.tar.gz".to_string()],
66 ancillary_locations: Some(vec!["https://host/ancillary.tar.gz".to_string()]),
67 compression_algorithm: CompressionAlgorithm::default(),
68 cardano_node_version: "0.0.1".to_string(),
69 }
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 pub type SnapshotListMessageUntilV0_1_47 = Vec<SnapshotListItemMessageUntilV0_1_47>;
78
79 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
80 pub struct SnapshotListItemMessageUntilV0_1_47 {
81 pub digest: String,
82 pub network: String,
83 pub beacon: CardanoDbBeacon,
84 pub certificate_hash: String,
85 pub size: u64,
86 pub created_at: DateTime<Utc>,
87 pub locations: Vec<String>,
88 pub compression_algorithm: CompressionAlgorithm,
89 pub cardano_node_version: String,
90 }
91
92 const CURRENT_JSON: &str = r#"[{
93 "digest": "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6",
94 "network": "preview",
95 "beacon": {
96 "epoch": 86,
97 "immutable_file_number": 1728
98 },
99 "certificate_hash": "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb",
100 "size": 807803196,
101 "ancillary_size": 123456789,
102 "created_at": "2023-01-19T13:43:05.618857482Z",
103 "locations": [
104 "https://host/certificate.tar.gz"
105 ],
106 "ancillary_locations": [
107 "https://host/ancillary.tar.gz"
108 ],
109 "compression_algorithm": "zstandard",
110 "cardano_node_version": "1.0.0"
111 }]"#;
112
113 fn golden_message_until_open_api_0_1_47() -> SnapshotListMessageUntilV0_1_47 {
114 vec![SnapshotListItemMessageUntilV0_1_47 {
115 digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
116 network: "preview".to_string(),
117 beacon: CardanoDbBeacon {
118 epoch: Epoch(86),
119 immutable_file_number: 1728,
120 },
121 certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
122 .to_string(),
123 size: 807803196,
124 created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
125 .unwrap()
126 .with_timezone(&Utc),
127 locations: vec!["https://host/certificate.tar.gz".to_string()],
128 compression_algorithm: CompressionAlgorithm::Zstandard,
129 cardano_node_version: "1.0.0".to_string(),
130 }]
131 }
132
133 fn golden_message_current() -> SnapshotListMessage {
134 vec![SnapshotListItemMessage {
135 digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
136 network: "preview".to_string(),
137 beacon: CardanoDbBeacon {
138 epoch: Epoch(86),
139 immutable_file_number: 1728,
140 },
141 certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
142 .to_string(),
143 size: 807803196,
144 ancillary_size: Some(123456789),
145 created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
146 .unwrap()
147 .with_timezone(&Utc),
148 locations: vec!["https://host/certificate.tar.gz".to_string()],
149 ancillary_locations: Some(vec!["https://host/ancillary.tar.gz".to_string()]),
150 compression_algorithm: CompressionAlgorithm::Zstandard,
151 cardano_node_version: "1.0.0".to_string(),
152 }]
153 }
154
155 #[test]
156 fn test_current_json_deserialized_into_message_supported_until_open_api_0_1_47() {
157 let json = CURRENT_JSON;
158 let message: SnapshotListMessageUntilV0_1_47 = serde_json::from_str(json).unwrap();
159
160 assert_eq!(golden_message_until_open_api_0_1_47(), message);
161 }
162
163 #[test]
164 fn test_current_json_deserialized_into_current_message() {
165 let json = CURRENT_JSON;
166 let message: SnapshotListMessage = serde_json::from_str(json).unwrap();
167
168 assert_eq!(golden_message_current(), message);
169 }
170}