mithril_common/messages/
cardano_transaction_snapshot.rs

1use chrono::DateTime;
2use chrono::Utc;
3use serde::{Deserialize, Serialize};
4
5use crate::entities::{BlockNumber, Epoch};
6
7/// Message structure of a Cardano Transactions snapshot
8#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
9pub struct CardanoTransactionSnapshotMessage {
10    /// Merkle root of the Cardano transactions snapshot
11    pub merkle_root: String,
12
13    /// Epoch of the Cardano transactions snapshot
14    pub epoch: Epoch,
15
16    /// Block number of the Cardano transactions snapshot
17    pub block_number: BlockNumber,
18
19    /// Hash of the Cardano Transactions snapshot
20    pub hash: String,
21
22    /// Hash of the associated certificate
23    pub certificate_hash: String,
24
25    /// DateTime of creation
26    pub created_at: DateTime<Utc>,
27}
28
29impl CardanoTransactionSnapshotMessage {
30    cfg_test_tools! {
31        /// Return a dummy test entity (test-only).
32        pub fn dummy() -> Self {
33            Self {
34                merkle_root: "mkroot-123".to_string(),
35                epoch: Epoch(10),
36                block_number: BlockNumber(100),
37                hash: "hash-123".to_string(),
38                certificate_hash: "cert-hash-123".to_string(),
39                created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
40                    .unwrap()
41                    .with_timezone(&Utc),
42            }
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    fn golden_message_current() -> CardanoTransactionSnapshotMessage {
52        CardanoTransactionSnapshotMessage {
53            merkle_root: "mkroot-123".to_string(),
54            epoch: Epoch(8),
55            block_number: BlockNumber(6),
56            hash: "hash-123".to_string(),
57            certificate_hash: "certificate-hash-123".to_string(),
58            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
59                .unwrap()
60                .with_timezone(&Utc),
61        }
62    }
63
64    const CURRENT_JSON: &str = r#"{
65        "merkle_root": "mkroot-123",
66        "epoch": 8,
67        "block_number": 6,
68        "hash": "hash-123",
69        "certificate_hash": "certificate-hash-123",
70        "created_at": "2023-01-19T13:43:05.618857482Z"
71    }"#;
72
73    #[test]
74    fn test_current_json_deserialized_into_current_message() {
75        let json = CURRENT_JSON;
76        let message: CardanoTransactionSnapshotMessage = serde_json::from_str(json).expect(
77            "This JSON is expected to be successfully parsed into a CardanoTransactionSnapshotMessage instance.",
78        );
79
80        assert_eq!(golden_message_current(), message);
81    }
82}