mithril_common/messages/
cardano_database_list.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::entities::CardanoDbBeacon;
5
6/// Message structure of a Cardano database snapshot list
7pub type CardanoDatabaseSnapshotListMessage = Vec<CardanoDatabaseSnapshotListItemMessage>;
8
9/// Message structure of a Cardano database snapshot list item
10#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
11pub struct CardanoDatabaseSnapshotListItemMessage {
12    /// Hash of the Cardano database snapshot.
13    pub hash: String,
14
15    /// Merkle root of the Cardano database snapshot
16    pub merkle_root: String,
17
18    /// Mithril beacon on the Cardano chain
19    pub beacon: CardanoDbBeacon,
20
21    /// Hash of the associated certificate
22    pub certificate_hash: String,
23
24    /// Size of the uncompressed Cardano database files.
25    pub total_db_size_uncompressed: u64,
26
27    /// Version of the Cardano node used to create the snapshot.
28    pub cardano_node_version: String,
29
30    /// Date and time at which the snapshot was created
31    pub created_at: DateTime<Utc>,
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::entities::Epoch;
37
38    use super::*;
39
40    const CURRENT_JSON: &str = r#"
41    [
42        {
43            "hash": "d4071d518a3ace0f6c04a9c0745b9e9560e3e2af1b373bafc4e0398423e9abfb",
44            "merkle_root": "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6",
45            "beacon": {
46                "epoch": 123,
47                "immutable_file_number": 2345
48            },
49            "certificate_hash": "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb",
50            "total_db_size_uncompressed": 800796318,
51            "cardano_node_version": "0.0.1",
52            "created_at": "2023-01-19T13:43:05.618857482Z"
53        }
54    ]"#;
55
56    fn golden_current_message() -> CardanoDatabaseSnapshotListMessage {
57        vec![CardanoDatabaseSnapshotListItemMessage {
58            hash: "d4071d518a3ace0f6c04a9c0745b9e9560e3e2af1b373bafc4e0398423e9abfb".to_string(),
59            merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
60                .to_string(),
61            beacon: CardanoDbBeacon {
62                epoch: Epoch(123),
63                immutable_file_number: 2345,
64            },
65            certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
66                .to_string(),
67            total_db_size_uncompressed: 800796318,
68            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
69                .unwrap()
70                .with_timezone(&Utc),
71            cardano_node_version: "0.0.1".to_string(),
72        }]
73    }
74
75    #[test]
76    fn test_current_json_deserialized_into_current_message() {
77        let json = CURRENT_JSON;
78        let message: CardanoDatabaseSnapshotListMessage = serde_json::from_str(json).expect(
79            "This JSON is expected to be successfully parsed into a CardanoDatabaseSnapshotListMessage instance.",
80        );
81
82        assert_eq!(golden_current_message(), message);
83    }
84}