mithril_common/messages/
mithril_stake_distribution.rs

1use chrono::DateTime;
2use chrono::Utc;
3use serde::{Deserialize, Serialize};
4
5use crate::entities::Epoch;
6use crate::entities::ProtocolParameters;
7#[cfg(any(test, feature = "test_tools"))]
8use crate::test_utils::fake_data;
9
10use super::SignerWithStakeMessagePart;
11/// Message structure of a Mithril Stake Distribution
12#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
13pub struct MithrilStakeDistributionMessage {
14    /// Epoch at which the Mithril Stake Distribution is created
15    pub epoch: Epoch,
16
17    /// List of signers with stakes of the Mithril Stake Distribution
18    #[serde(rename = "signers")]
19    pub signers_with_stake: Vec<SignerWithStakeMessagePart>,
20
21    /// Hash of the Mithril Stake Distribution (different from the AVK).
22    pub hash: String,
23
24    /// Hash of the associated certificate
25    pub certificate_hash: String,
26
27    /// DateTime of creation
28    pub created_at: DateTime<Utc>,
29
30    /// Protocol parameters used to compute AVK
31    pub protocol_parameters: ProtocolParameters,
32}
33
34impl MithrilStakeDistributionMessage {
35    cfg_test_tools! {
36        /// Return a dummy test entity (test-only).
37        pub fn dummy() -> Self {
38            Self {
39                epoch: Epoch(1),
40                signers_with_stake: vec![SignerWithStakeMessagePart::dummy()],
41                hash: "hash-123".to_string(),
42                certificate_hash: "cert-hash-123".to_string(),
43                created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
44                    .unwrap()
45                    .with_timezone(&Utc),
46                protocol_parameters: fake_data::protocol_parameters(),
47            }
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    fn golden_message_current() -> MithrilStakeDistributionMessage {
57        MithrilStakeDistributionMessage {
58            epoch: Epoch(1),
59            signers_with_stake: vec![
60                SignerWithStakeMessagePart {
61                    party_id: "0".to_string(),
62                    verification_key: "7b22766b223a5b3134332c3136312c3235352c34382c37382c35372c3230342c3232302c32352c3232312c3136342c3235322c3234382c31342c35362c3132362c3138362c3133352c3232382c3138382c3134352c3138312c35322c3230302c39372c39392c3231332c34362c302c3139392c3139332c38392c3138372c38382c32392c3133352c3137332c3234342c38362c33362c38332c35342c36372c3136342c362c3133372c39342c37322c362c3130352c3132382c3132382c39332c34382c3137362c31312c342c3234362c3133382c34382c3138302c3133332c39302c3134322c3139322c32342c3139332c3131312c3134322c33312c37362c3131312c3131302c3233342c3135332c39302c3230382c3139322c33312c3132342c39352c3130322c34392c3135382c39392c35322c3232302c3136352c39342c3235312c36382c36392c3132312c31362c3232342c3139345d2c22706f70223a5b3136382c35302c3233332c3139332c31352c3133362c36352c37322c3132332c3134382c3132392c3137362c33382c3139382c3230392c34372c32382c3230342c3137362c3134342c35372c3235312c34322c32382c36362c37362c38392c39372c3135382c36332c35342c3139382c3139342c3137362c3133352c3232312c31342c3138352c3139372c3232352c3230322c39382c3234332c37342c3233332c3232352c3134332c3135312c3134372c3137372c3137302c3131372c36362c3136352c36362c36322c33332c3231362c3233322c37352c36382c3131342c3139352c32322c3130302c36352c34342c3139382c342c3136362c3130322c3233332c3235332c3234302c35392c3137352c36302c3131372c3134322c3131342c3134302c3132322c31372c38372c3131302c3138372c312c31372c31302c3139352c3135342c31332c3234392c38362c35342c3232365d7d".to_string(),
63                    verification_key_signature: None,
64                    operational_certificate: None,
65                    kes_period: None,
66                    stake: 826
67                },
68            ],
69            hash: "hash-123".to_string(),
70            certificate_hash: "cert-hash-123".to_string(),
71            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
72            .unwrap()
73            .with_timezone(&Utc),
74            protocol_parameters: fake_data::protocol_parameters(),
75        }
76    }
77
78    const CURRENT_JSON: &str = r#"{
79            "epoch": 1,
80            "signers": [
81                {
82                    "party_id": "0",
83                    "verification_key": "7b22766b223a5b3134332c3136312c3235352c34382c37382c35372c3230342c3232302c32352c3232312c3136342c3235322c3234382c31342c35362c3132362c3138362c3133352c3232382c3138382c3134352c3138312c35322c3230302c39372c39392c3231332c34362c302c3139392c3139332c38392c3138372c38382c32392c3133352c3137332c3234342c38362c33362c38332c35342c36372c3136342c362c3133372c39342c37322c362c3130352c3132382c3132382c39332c34382c3137362c31312c342c3234362c3133382c34382c3138302c3133332c39302c3134322c3139322c32342c3139332c3131312c3134322c33312c37362c3131312c3131302c3233342c3135332c39302c3230382c3139322c33312c3132342c39352c3130322c34392c3135382c39392c35322c3232302c3136352c39342c3235312c36382c36392c3132312c31362c3232342c3139345d2c22706f70223a5b3136382c35302c3233332c3139332c31352c3133362c36352c37322c3132332c3134382c3132392c3137362c33382c3139382c3230392c34372c32382c3230342c3137362c3134342c35372c3235312c34322c32382c36362c37362c38392c39372c3135382c36332c35342c3139382c3139342c3137362c3133352c3232312c31342c3138352c3139372c3232352c3230322c39382c3234332c37342c3233332c3232352c3134332c3135312c3134372c3137372c3137302c3131372c36362c3136352c36362c36322c33332c3231362c3233322c37352c36382c3131342c3139352c32322c3130302c36352c34342c3139382c342c3136362c3130322c3233332c3235332c3234302c35392c3137352c36302c3131372c3134322c3131342c3134302c3132322c31372c38372c3131302c3138372c312c31372c31302c3139352c3135342c31332c3234392c38362c35342c3232365d7d",
84                    "stake": 826
85                }
86            ],
87            "hash": "hash-123",
88            "certificate_hash": "cert-hash-123",
89            "created_at": "2023-01-19T13:43:05.618857482Z",
90            "protocol_parameters": {"k": 5, "m": 100, "phi_f": 0.65 }
91        }"#;
92
93    #[test]
94    fn test_current_json_deserialized_into_current_message() {
95        let json = CURRENT_JSON;
96        let message: MithrilStakeDistributionMessage = serde_json::from_str(json).expect(
97            "This JSON is expected to be successfully parsed into a MithrilStakeDistributionMessage instance.",
98        );
99
100        assert_eq!(golden_message_current(), message);
101    }
102}