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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use crate::entities::{CardanoTransactionsSigningConfig, Epoch, ProtocolParameters};
use crate::messages::SignerMessagePart;
use serde::{Deserialize, Serialize};

/// EpochSettings represents the settings of an epoch
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct EpochSettingsMessage {
    /// Current Epoch
    pub epoch: Epoch,

    /// Current Protocol parameters
    #[deprecated(
        since = "0.4.77",
        note = "use only signer_registration_protocol_parameters"
    )]
    #[serde(rename = "protocol", skip_serializing_if = "Option::is_none")]
    pub protocol_parameters: Option<ProtocolParameters>,

    /// Next Protocol parameters
    #[deprecated(
        since = "0.4.77",
        note = "use only signer_registration_protocol_parameters"
    )]
    #[serde(rename = "next_protocol", skip_serializing_if = "Option::is_none")]
    pub next_protocol_parameters: Option<ProtocolParameters>,

    /// Signer Registration Protocol parameters
    #[serde(rename = "signer_registration_protocol")]
    pub signer_registration_protocol_parameters: ProtocolParameters,

    /// Current Signers
    pub current_signers: Vec<SignerMessagePart>,

    /// Signers that will be able to sign on the next epoch
    pub next_signers: Vec<SignerMessagePart>,

    /// Cardano transactions signing configuration for the current epoch
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,

    /// Cardano transactions signing configuration for the next epoch
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
}

impl EpochSettingsMessage {
    cfg_test_tools! {
        /// Dummy instance for test purposes.
        pub fn dummy() -> Self {
            #[allow(deprecated)]
            Self {
                epoch: Epoch(10),
                protocol_parameters: Some(ProtocolParameters {
                    k: 5,
                    m: 100,
                    phi_f: 0.65,
                }),
                next_protocol_parameters: Some(ProtocolParameters {
                    k: 5,
                    m: 100,
                    phi_f: 0.65,
                }),
                signer_registration_protocol_parameters: ProtocolParameters {
                    k: 5,
                    m: 100,
                    phi_f: 0.65,
                },
                current_signers: [SignerMessagePart::dummy()].to_vec(),
                next_signers: [SignerMessagePart::dummy()].to_vec(),
                cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig::dummy()),
                next_cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig::dummy()),
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use crate::entities::BlockNumber;

    use super::*;

    const ACTUAL_JSON: &str = r#"{
        "epoch": 10,
        "protocol":  { "k": 5, "m": 100, "phi_f": 0.65 },
        "next_protocol":  { "k": 50, "m": 1000, "phi_f": 0.65 },
        "signer_registration_protocol":  { "k": 500, "m": 10000, "phi_f": 0.65 },
        "current_signers":[{
            "party_id":"123",
            "verification_key":"key_123",
            "verification_key_signature":"signature_123",
            "operational_certificate":"certificate_123",
            "kes_period":12
        }],
        "next_signers": [{
            "party_id":"456",
            "verification_key":"key_456",
            "verification_key_signature":"signature_456",
            "operational_certificate":"certificate_456",
            "kes_period":45
        }],
        "cardano_transactions_signing_config": {
            "security_parameter": 70,
            "step": 20
        },
        "next_cardano_transactions_signing_config": {
            "security_parameter": 50,
            "step": 10
        }
        
        }"#;

    // Supported structure until OpenAPI version 0.1.28.
    #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    struct EpochSettingsMessageUntilV0_1_28 {
        pub epoch: Epoch,
        #[serde(rename = "protocol")]
        pub protocol_parameters: ProtocolParameters,
        #[serde(rename = "next_protocol")]
        pub next_protocol_parameters: ProtocolParameters,
    }

    // Supported structure until OpenAPI version 0.1.29.
    #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    struct EpochSettingsMessageUntilV0_1_29 {
        pub epoch: Epoch,
        #[serde(rename = "protocol")]
        pub protocol_parameters: ProtocolParameters,
        #[serde(rename = "next_protocol")]
        pub next_protocol_parameters: ProtocolParameters,
        pub current_signers: Vec<SignerMessagePart>,
        pub next_signers: Vec<SignerMessagePart>,
    }

    // Supported structure until OpenAPI version 0.1.32.
    #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
    struct EpochSettingsMessageUntilV0_1_32 {
        pub epoch: Epoch,
        #[serde(rename = "protocol")]
        pub protocol_parameters: ProtocolParameters,
        #[serde(rename = "next_protocol")]
        pub next_protocol_parameters: ProtocolParameters,
        pub current_signers: Vec<SignerMessagePart>,
        pub next_signers: Vec<SignerMessagePart>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
        #[serde(skip_serializing_if = "Option::is_none")]
        pub next_cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
    }

    fn golden_message_until_open_api_0_1_28() -> EpochSettingsMessageUntilV0_1_28 {
        EpochSettingsMessageUntilV0_1_28 {
            epoch: Epoch(10),
            protocol_parameters: ProtocolParameters {
                k: 5,
                m: 100,
                phi_f: 0.65,
            },
            next_protocol_parameters: ProtocolParameters {
                k: 50,
                m: 1000,
                phi_f: 0.65,
            },
        }
    }

    fn golden_message_until_open_api_0_1_29() -> EpochSettingsMessageUntilV0_1_29 {
        EpochSettingsMessageUntilV0_1_29 {
            epoch: Epoch(10),
            protocol_parameters: ProtocolParameters {
                k: 5,
                m: 100,
                phi_f: 0.65,
            },
            next_protocol_parameters: ProtocolParameters {
                k: 50,
                m: 1000,
                phi_f: 0.65,
            },
            current_signers: vec![SignerMessagePart {
                party_id: "123".to_string(),
                verification_key: "key_123".to_string(),
                verification_key_signature: Some("signature_123".to_string()),
                operational_certificate: Some("certificate_123".to_string()),
                kes_period: Some(12),
            }],
            next_signers: vec![SignerMessagePart {
                party_id: "456".to_string(),
                verification_key: "key_456".to_string(),
                verification_key_signature: Some("signature_456".to_string()),
                operational_certificate: Some("certificate_456".to_string()),
                kes_period: Some(45),
            }],
        }
    }

    fn golden_message_until_open_api_0_1_32() -> EpochSettingsMessageUntilV0_1_32 {
        EpochSettingsMessageUntilV0_1_32 {
            epoch: Epoch(10),
            protocol_parameters: ProtocolParameters {
                k: 5,
                m: 100,
                phi_f: 0.65,
            },
            next_protocol_parameters: ProtocolParameters {
                k: 50,
                m: 1000,
                phi_f: 0.65,
            },
            current_signers: vec![SignerMessagePart {
                party_id: "123".to_string(),
                verification_key: "key_123".to_string(),
                verification_key_signature: Some("signature_123".to_string()),
                operational_certificate: Some("certificate_123".to_string()),
                kes_period: Some(12),
            }],
            next_signers: vec![SignerMessagePart {
                party_id: "456".to_string(),
                verification_key: "key_456".to_string(),
                verification_key_signature: Some("signature_456".to_string()),
                operational_certificate: Some("certificate_456".to_string()),
                kes_period: Some(45),
            }],
            cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
                security_parameter: BlockNumber(70),
                step: BlockNumber(20),
            }),
            next_cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
                security_parameter: BlockNumber(50),
                step: BlockNumber(10),
            }),
        }
    }

    fn golden_actual_message() -> EpochSettingsMessage {
        #[allow(deprecated)]
        EpochSettingsMessage {
            epoch: Epoch(10),
            protocol_parameters: Some(ProtocolParameters {
                k: 5,
                m: 100,
                phi_f: 0.65,
            }),
            next_protocol_parameters: Some(ProtocolParameters {
                k: 50,
                m: 1000,
                phi_f: 0.65,
            }),
            signer_registration_protocol_parameters: ProtocolParameters {
                k: 500,
                m: 10000,
                phi_f: 0.65,
            },
            current_signers: vec![SignerMessagePart {
                party_id: "123".to_string(),
                verification_key: "key_123".to_string(),
                verification_key_signature: Some("signature_123".to_string()),
                operational_certificate: Some("certificate_123".to_string()),
                kes_period: Some(12),
            }],
            next_signers: vec![SignerMessagePart {
                party_id: "456".to_string(),
                verification_key: "key_456".to_string(),
                verification_key_signature: Some("signature_456".to_string()),
                operational_certificate: Some("certificate_456".to_string()),
                kes_period: Some(45),
            }],
            cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
                security_parameter: BlockNumber(70),
                step: BlockNumber(20),
            }),
            next_cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
                security_parameter: BlockNumber(50),
                step: BlockNumber(10),
            }),
        }
    }

    // Test the backward compatibility with the structure supported until OpenAPI version 0.1.28.
    #[test]
    fn test_actual_json_deserialized_into_message_supported_until_open_api_0_1_28() {
        let json = ACTUAL_JSON;
        let message: EpochSettingsMessageUntilV0_1_28 = serde_json::from_str(json).expect(
            "This JSON is expected to be successfully parsed into a EpochSettingsMessageUntilVersion0_1_28 instance.",
        );

        assert_eq!(golden_message_until_open_api_0_1_28(), message);
    }

    // Test the backward compatibility with the structure supported until OpenAPI version 0.1.29.
    #[test]
    fn test_actual_json_deserialized_into_message_supported_until_open_api_0_1_29() {
        let json = ACTUAL_JSON;
        let message: EpochSettingsMessageUntilV0_1_29 = serde_json::from_str(json).expect(
            "This JSON is expected to be successfully parsed into a EpochSettingsMessageUntilVersion0_1_29 instance.",
        );

        assert_eq!(golden_message_until_open_api_0_1_29(), message);
    }

    // Test the backward compatibility with the structure supported until OpenAPI version 0.1.32.
    #[test]
    fn test_actual_json_deserialized_into_message_supported_until_open_api_0_1_32() {
        let json = ACTUAL_JSON;
        let message: EpochSettingsMessageUntilV0_1_32 = serde_json::from_str(json).expect(
                "This JSON is expected to be successfully parsed into a EpochSettingsMessageUntilVersion0_1_32 instance.",
            );

        assert_eq!(golden_message_until_open_api_0_1_32(), message);
    }

    // Test the compatibility with current structure.
    #[test]
    fn test_actual_json_deserialized_into_actual_message() {
        let json = ACTUAL_JSON;
        let message: EpochSettingsMessage = serde_json::from_str(json).expect(
            "This JSON is expected to be successfully parsed into a EpochSettingsMessage instance.",
        );

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