mithril_common/messages/message_parts/
certificate_metadata.rs

1use crate::entities::{ProtocolParameters, ProtocolVersion, StakeDistributionParty};
2
3use chrono::{DateTime, 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
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    fn golden_message_current() -> CertificateMetadataMessagePart {
44        CertificateMetadataMessagePart {
45            network: "testnet".to_string(),
46            protocol_version: "0.1.0".to_string(),
47            protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
48            initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
49                .unwrap()
50                .with_timezone(&Utc),
51            sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
52                .unwrap()
53                .with_timezone(&Utc),
54            signers: vec![
55                StakeDistributionParty {
56                    party_id: "1".to_string(),
57                    stake: 10,
58                },
59                StakeDistributionParty {
60                    party_id: "2".to_string(),
61                    stake: 20,
62                },
63            ],
64        }
65    }
66
67    const CURRENT_JSON: &str = r#"{
68            "network": "testnet",
69            "version": "0.1.0",
70            "parameters": {
71                "k": 1000,
72                "m": 100,
73                "phi_f": 0.123
74            },
75            "initiated_at": "2024-02-12T13:11:47Z",
76            "sealed_at": "2024-02-12T13:12:57Z",
77            "signers": [
78                {
79                    "party_id": "1",
80                    "stake": 10
81                },
82                {
83                    "party_id": "2",
84                    "stake": 20
85                }
86            ]
87        }"#;
88
89    #[test]
90    fn test_current_json_deserialized_into_current_message() {
91        let json = CURRENT_JSON;
92        let message: CertificateMetadataMessagePart = serde_json::from_str(json).expect(
93            "This JSON is expected to be successfully parsed into a CertificateMetadataMessagePart instance.",
94        );
95
96        assert_eq!(golden_message_current(), message);
97    }
98}