mithril_common/messages/message_parts/
certificate_metadata.rs

1use crate::entities::{ProtocolParameters, ProtocolVersion, StakeDistributionParty};
2
3use chrono::{DateTime, Duration, Utc};
4use serde::{Deserialize, Serialize};
5
6/// CertificateMetadata represents the metadata associated to a Certificate
7#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
8pub struct CertificateMetadataMessagePart {
9    /// Cardano network
10    /// part of METADATA(p,n)
11    pub network: String,
12
13    /// Protocol Version (semver)
14    /// Useful to achieve backward compatibility of the certificates (including of the multi signature)
15    /// part of METADATA(p,n)
16    #[serde(rename = "version")]
17    pub protocol_version: ProtocolVersion,
18
19    /// Protocol parameters
20    /// part of METADATA(p,n)
21    #[serde(rename = "parameters")]
22    pub protocol_parameters: ProtocolParameters,
23
24    /// Date and time when the certificate was initiated
25    /// Represents the time at which the single signatures registration is opened
26    /// part of METADATA(p,n)
27    pub initiated_at: DateTime<Utc>,
28
29    /// Date and time when the certificate was sealed
30    /// Represents the time at which the quorum of single signatures was reached so that they were aggregated into a multi signature
31    /// part of METADATA(p,n)
32    pub sealed_at: DateTime<Utc>,
33
34    /// The list of the active signers with their stakes and verification keys
35    /// part of METADATA(p,n)
36    pub signers: Vec<StakeDistributionParty>,
37}
38
39impl CertificateMetadataMessagePart {
40    /// CertificateMetadata factory
41    pub fn dummy() -> Self {
42        let initiated_at = DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
43            .unwrap()
44            .with_timezone(&Utc);
45
46        Self {
47            network: "testnet".to_string(),
48            protocol_version: "0.1.0".to_string(),
49            protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
50            initiated_at,
51            sealed_at: initiated_at + Duration::try_seconds(100).unwrap(),
52            signers: vec![
53                StakeDistributionParty {
54                    party_id: "1".to_string(),
55                    stake: 10,
56                },
57                StakeDistributionParty {
58                    party_id: "2".to_string(),
59                    stake: 20,
60                },
61            ],
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    fn golden_message_current() -> CertificateMetadataMessagePart {
71        CertificateMetadataMessagePart {
72            network: "testnet".to_string(),
73            protocol_version: "0.1.0".to_string(),
74            protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
75            initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
76                .unwrap()
77                .with_timezone(&Utc),
78            sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
79                .unwrap()
80                .with_timezone(&Utc),
81            signers: vec![
82                StakeDistributionParty {
83                    party_id: "1".to_string(),
84                    stake: 10,
85                },
86                StakeDistributionParty {
87                    party_id: "2".to_string(),
88                    stake: 20,
89                },
90            ],
91        }
92    }
93
94    const CURRENT_JSON: &str = r#"{
95            "network": "testnet",
96            "version": "0.1.0",
97            "parameters": {
98                "k": 1000,
99                "m": 100,
100                "phi_f": 0.123
101            },
102            "initiated_at": "2024-02-12T13:11:47Z",
103            "sealed_at": "2024-02-12T13:12:57Z",
104            "signers": [
105                {
106                    "party_id": "1",
107                    "stake": 10
108                },
109                {
110                    "party_id": "2",
111                    "stake": 20
112                }
113            ]
114        }"#;
115
116    #[test]
117    fn test_current_json_deserialized_into_current_message() {
118        let json = CURRENT_JSON;
119        let message: CertificateMetadataMessagePart = serde_json::from_str(json).expect(
120            "This JSON is expected to be successfully parsed into a CertificateMetadataMessagePart instance.",
121        );
122
123        assert_eq!(golden_message_current(), message);
124    }
125}