mithril_common/messages/
cardano_stake_distribution_list.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::entities::Epoch;
5
6/// Message structure of a Cardano Stake Distribution list
7pub type CardanoStakeDistributionListMessage = Vec<CardanoStakeDistributionListItemMessage>;
8
9/// Message structure of a Cardano Stake Distribution list item
10#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
11pub struct CardanoStakeDistributionListItemMessage {
12    /// Epoch at the end of which the Cardano stake distribution is computed by the Cardano node
13    pub epoch: Epoch,
14
15    /// Hash of the Cardano Stake Distribution
16    pub hash: String,
17
18    /// Hash of the associated certificate
19    pub certificate_hash: String,
20
21    /// Date and time at which the Cardano Stake Distribution was created
22    pub created_at: DateTime<Utc>,
23}
24
25impl CardanoStakeDistributionListItemMessage {
26    /// Return a dummy test entity (test-only).
27    pub fn dummy() -> Self {
28        Self {
29            epoch: Epoch(1),
30            hash: "hash-123".to_string(),
31            certificate_hash: "certificate-hash-123".to_string(),
32            created_at: DateTime::parse_from_rfc3339("2024-07-29T16:15:05.618857482Z")
33                .unwrap()
34                .with_timezone(&Utc),
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    fn golden_message_current() -> CardanoStakeDistributionListMessage {
44        vec![CardanoStakeDistributionListItemMessage {
45            epoch: Epoch(1),
46            hash: "hash-123".to_string(),
47            certificate_hash: "cert-hash-123".to_string(),
48            created_at: DateTime::parse_from_rfc3339("2024-07-29T16:15:05.618857482Z")
49                .unwrap()
50                .with_timezone(&Utc),
51        }]
52    }
53
54    const CURRENT_JSON: &str = r#"[{
55        "epoch": 1,
56        "hash": "hash-123",
57        "certificate_hash": "cert-hash-123",
58        "created_at": "2024-07-29T16:15:05.618857482Z"
59    }]"#;
60
61    #[test]
62    fn test_current_json_deserialized_into_current_message() {
63        let json = CURRENT_JSON;
64        let message: CardanoStakeDistributionListMessage = serde_json::from_str(json).expect(
65            "This JSON is expected to be successfully parsed into a CardanoStakeDistributionListMessage instance.",
66        );
67
68        assert_eq!(golden_message_current(), message);
69    }
70}