Skip to content

Commit d7f290e

Browse files
committed
Add snapshot list message
1 parent f8ee73d commit d7f290e

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

mithril-common/src/messages/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ mod epoch_settings;
66
mod register_signature;
77
mod register_signer;
88
mod snapshot;
9+
mod snapshot_list;
910

1011
pub use certificate::CertificateMessage;
1112
pub use certificate_pending::{CertificatePendingMessage, SignerMessage};
1213
pub use epoch_settings::EpochSettingsMessage;
1314
pub use register_signature::RegisterSignatureMessage;
1415
pub use register_signer::RegisterSignerMessage;
1516
pub use snapshot::SnapshotMessage;
17+
pub use snapshot_list::{SnapshotListItemMessage, SnapshotListMessage};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
// TODO: We should probably not rely on entities when defining the message
4+
use crate::entities::Beacon;
5+
6+
// TODO: We should probably not rely on entities when defining the message
7+
#[cfg(any(test, feature = "test_only"))]
8+
use crate::entities::Epoch;
9+
/// Message structure of a snapshot list
10+
pub type SnapshotListMessage = Vec<SnapshotListItemMessage>;
11+
12+
/// Message structure of a snapshot list item
13+
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
14+
pub struct SnapshotListItemMessage {
15+
/// Digest that is signed by the signer participants
16+
pub digest: String,
17+
18+
/// Mithril beacon on the Cardano chain
19+
pub beacon: Beacon,
20+
21+
/// Hash of the associated certificate
22+
pub certificate_hash: String,
23+
24+
/// Size of the snapshot file in Bytes
25+
pub size: u64,
26+
27+
/// Date and time at which the snapshot was created
28+
pub created_at: String,
29+
30+
/// Locations where the binary content of the snapshot can be retrieved
31+
pub locations: Vec<String>,
32+
}
33+
34+
impl SnapshotListItemMessage {
35+
#[cfg(any(test, feature = "test_only"))]
36+
/// Return a dummy test entity (test-only).
37+
pub fn dummy() -> Self {
38+
Self {
39+
digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
40+
beacon: Beacon {
41+
network: "preview".to_string(),
42+
epoch: Epoch(86),
43+
immutable_file_number: 1728,
44+
},
45+
certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
46+
.to_string(),
47+
size: 807803196,
48+
created_at: "2023-01-19T13:43:05.618857482Z".to_string(),
49+
locations: vec!["https://host/certificate.tar.gz".to_string()],
50+
}
51+
}
52+
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
fn golden_message() -> SnapshotListMessage {
59+
vec![SnapshotListItemMessage {
60+
digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
61+
beacon: Beacon {
62+
network: "preview".to_string(),
63+
epoch: Epoch(86),
64+
immutable_file_number: 1728,
65+
},
66+
certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
67+
.to_string(),
68+
size: 807803196,
69+
created_at: "2023-01-19T13:43:05.618857482Z".to_string(),
70+
locations: vec!["https://host/certificate.tar.gz".to_string()],
71+
}]
72+
}
73+
74+
// Test the retro compatibility with possible future upgrades.
75+
#[test]
76+
fn test_v1() {
77+
let json = r#"[{
78+
"digest": "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6",
79+
"beacon": {
80+
"network": "preview",
81+
"epoch": 86,
82+
"immutable_file_number": 1728
83+
},
84+
"certificate_hash": "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb",
85+
"size": 807803196,
86+
"created_at": "2023-01-19T13:43:05.618857482Z",
87+
"locations": [
88+
"https://host/certificate.tar.gz"
89+
]
90+
}]"#;
91+
let message: SnapshotListMessage = serde_json::from_str(json).expect(
92+
"This JSON is expected to be succesfully parsed into a SnapshotListMessage instance.",
93+
);
94+
95+
assert_eq!(golden_message(), message);
96+
}
97+
}

0 commit comments

Comments
 (0)