mithril_common/messages/message_parts/
certificate_metadata.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::entities::{ProtocolParameters, ProtocolVersion, StakeDistributionParty};

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};

/// CertificateMetadata represents the metadata associated to a Certificate
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CertificateMetadataMessagePart {
    /// Cardano network
    /// part of METADATA(p,n)
    pub network: String,

    /// Protocol Version (semver)
    /// Useful to achieve backward compatibility of the certificates (including of the multi signature)
    /// part of METADATA(p,n)
    #[serde(rename = "version")]
    pub protocol_version: ProtocolVersion,

    /// Protocol parameters
    /// part of METADATA(p,n)
    #[serde(rename = "parameters")]
    pub protocol_parameters: ProtocolParameters,

    /// Date and time when the certificate was initiated
    /// Represents the time at which the single signatures registration is opened
    /// part of METADATA(p,n)
    pub initiated_at: DateTime<Utc>,

    /// Date and time when the certificate was sealed
    /// Represents the time at which the quorum of single signatures was reached so that they were aggregated into a multi signature
    /// part of METADATA(p,n)
    pub sealed_at: DateTime<Utc>,

    /// The list of the active signers with their stakes and verification keys
    /// part of METADATA(p,n)
    pub signers: Vec<StakeDistributionParty>,
}

impl CertificateMetadataMessagePart {
    /// CertificateMetadata factory
    pub fn dummy() -> Self {
        let initiated_at = DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
            .unwrap()
            .with_timezone(&Utc);

        Self {
            network: "testnet".to_string(),
            protocol_version: "0.1.0".to_string(),
            protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
            initiated_at,
            sealed_at: initiated_at + Duration::try_seconds(100).unwrap(),
            signers: vec![
                StakeDistributionParty {
                    party_id: "1".to_string(),
                    stake: 10,
                },
                StakeDistributionParty {
                    party_id: "2".to_string(),
                    stake: 20,
                },
            ],
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn golden_message_current() -> CertificateMetadataMessagePart {
        CertificateMetadataMessagePart {
            network: "testnet".to_string(),
            protocol_version: "0.1.0".to_string(),
            protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
            initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
                .unwrap()
                .with_timezone(&Utc),
            sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
                .unwrap()
                .with_timezone(&Utc),
            signers: vec![
                StakeDistributionParty {
                    party_id: "1".to_string(),
                    stake: 10,
                },
                StakeDistributionParty {
                    party_id: "2".to_string(),
                    stake: 20,
                },
            ],
        }
    }

    const CURRENT_JSON: &str = r#"{
            "network": "testnet",
            "version": "0.1.0",
            "parameters": {
                "k": 1000,
                "m": 100,
                "phi_f": 0.123
            },
            "initiated_at": "2024-02-12T13:11:47Z",
            "sealed_at": "2024-02-12T13:12:57Z",
            "signers": [
                {
                    "party_id": "1",
                    "stake": 10
                },
                {
                    "party_id": "2",
                    "stake": 20
                }
            ]
        }"#;

    #[test]
    fn test_current_json_deserialized_into_current_message() {
        let json = CURRENT_JSON;
        let message: CertificateMetadataMessagePart = serde_json::from_str(json).expect(
            "This JSON is expected to be successfully parsed into a CertificateMetadataMessagePart instance.",
        );

        assert_eq!(golden_message_current(), message);
    }
}