mithril_common/messages/message_parts/
certificate_metadata.rs1use crate::entities::{ProtocolParameters, ProtocolVersion, StakeDistributionParty};
2
3use chrono::{DateTime, Duration, Utc};
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
8pub struct CertificateMetadataMessagePart {
9 pub network: String,
12
13 #[serde(rename = "version")]
17 pub protocol_version: ProtocolVersion,
18
19 #[serde(rename = "parameters")]
22 pub protocol_parameters: ProtocolParameters,
23
24 pub initiated_at: DateTime<Utc>,
28
29 pub sealed_at: DateTime<Utc>,
33
34 pub signers: Vec<StakeDistributionParty>,
37}
38
39impl CertificateMetadataMessagePart {
40 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}