mithril_common/messages/
cardano_database_list.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::entities::{CardanoDbBeacon, Epoch};
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
34impl CardanoDatabaseSnapshotListItemMessage {
35    /// Return a dummy test entity (test-only).
36    pub fn dummy() -> Self {
37        Self {
38            hash: "d4071d518a3ace0f6c04a9c0745b9e9560e3e2af1b373bafc4e0398423e9abfb".to_string(),
39            merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
40                .to_string(),
41            beacon: CardanoDbBeacon {
42                epoch: Epoch(123),
43                immutable_file_number: 2345,
44            },
45            certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
46                .to_string(),
47            total_db_size_uncompressed: 800796318,
48            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
49                .unwrap()
50                .with_timezone(&Utc),
51            cardano_node_version: "0.0.1".to_string(),
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    const CURRENT_JSON: &str = r#"
61    [
62        {
63            "hash": "d4071d518a3ace0f6c04a9c0745b9e9560e3e2af1b373bafc4e0398423e9abfb",
64            "merkle_root": "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6",
65            "beacon": {
66                "epoch": 123,
67                "immutable_file_number": 2345
68            },
69            "certificate_hash": "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb",
70            "total_db_size_uncompressed": 800796318,
71            "cardano_node_version": "0.0.1",
72            "created_at": "2023-01-19T13:43:05.618857482Z"
73        }
74    ]"#;
75
76    fn golden_current_message() -> CardanoDatabaseSnapshotListMessage {
77        vec![CardanoDatabaseSnapshotListItemMessage {
78            hash: "d4071d518a3ace0f6c04a9c0745b9e9560e3e2af1b373bafc4e0398423e9abfb".to_string(),
79            merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
80                .to_string(),
81            beacon: CardanoDbBeacon {
82                epoch: Epoch(123),
83                immutable_file_number: 2345,
84            },
85            certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
86                .to_string(),
87            total_db_size_uncompressed: 800796318,
88            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
89                .unwrap()
90                .with_timezone(&Utc),
91            cardano_node_version: "0.0.1".to_string(),
92        }]
93    }
94
95    #[test]
96    fn test_current_json_deserialized_into_current_message() {
97        let json = CURRENT_JSON;
98        let message: CardanoDatabaseSnapshotListMessage = serde_json::from_str(json).expect(
99            "This JSON is expected to be successfully parsed into a CardanoDatabaseSnapshotListMessage instance.",
100        );
101
102        assert_eq!(golden_current_message(), message);
103    }
104}