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
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    fn golden_message_current() -> CardanoStakeDistributionMessage {
32        CardanoStakeDistributionMessage {
33            epoch: Epoch(1),
34            hash: "hash-123".to_string(),
35            certificate_hash: "cert-hash-123".to_string(),
36            stake_distribution: StakeDistribution::from([
37                ("pool-123".to_string(), 1000),
38                ("pool-456".to_string(), 2000),
39            ]),
40            created_at: DateTime::parse_from_rfc3339("2024-07-29T16:15:05.618857482Z")
41                .unwrap()
42                .with_timezone(&Utc),
43        }
44    }
45
46    const CURRENT_JSON: &str = r#"{
47        "epoch": 1,
48        "hash": "hash-123",
49        "certificate_hash": "cert-hash-123",
50        "stake_distribution": { "pool-123": 1000, "pool-456": 2000 },
51        "created_at": "2024-07-29T16:15:05.618857482Z"
52    }"#;
53
54    #[test]
55    fn test_current_json_deserialized_into_current_message() {
56        let json = CURRENT_JSON;
57        let message: CardanoStakeDistributionMessage = serde_json::from_str(json).expect(
58            "This JSON is expected to be successfully parsed into a CardanoStakeDistributionMessage instance.",
59        );
60
61        assert_eq!(golden_message_current(), message);
62    }
63}