mithril_common/messages/
cardano_stake_distribution.rs

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