mithril_common/messages/
certificate.rs

1use std::fmt::{Debug, Formatter};
2
3use anyhow::Context;
4use serde::{Deserialize, Serialize};
5
6use crate::entities::{
7    Certificate, CertificateMetadata, CertificateSignature, Epoch, ProtocolMessage,
8    SignedEntityType,
9};
10use crate::messages::CertificateMetadataMessagePart;
11use crate::StdError;
12#[cfg(any(test, feature = "test_tools"))]
13use crate::{entities::ProtocolMessagePartKey, test_utils::fake_keys};
14
15/// Message structure of a certificate
16#[derive(Clone, PartialEq, Serialize, Deserialize)]
17pub struct CertificateMessage {
18    /// Hash of the current certificate
19    /// Computed from the other fields of the certificate
20    /// aka H(Cp,n))
21    pub hash: String,
22
23    /// Hash of the previous certificate in the chain
24    /// This is either the hash of the first certificate of the epoch in the chain
25    /// Or the first certificate of the previous epoch in the chain (if the certificate is the first of its epoch)
26    /// aka H(FC(n))
27    pub previous_hash: String,
28
29    /// Epoch of the Cardano chain
30    pub epoch: Epoch,
31
32    /// The signed entity type of the message.
33    /// aka BEACON(p,n)
34    pub signed_entity_type: SignedEntityType,
35
36    /// Certificate metadata
37    /// aka METADATA(p,n)
38    pub metadata: CertificateMetadataMessagePart,
39
40    /// Structured message that is used to create the signed message
41    /// aka MSG(p,n) U AVK(n-1)
42    pub protocol_message: ProtocolMessage,
43
44    /// Message that is signed by the signers
45    /// aka H(MSG(p,n) || AVK(n-1))
46    pub signed_message: String,
47
48    /// Aggregate verification key
49    /// The AVK used to sign during the current epoch
50    /// aka AVK(n-2)
51    pub aggregate_verification_key: String,
52
53    /// STM multi signature created from a quorum of single signatures from the signers
54    /// aka MULTI_SIG(H(MSG(p,n) || AVK(n-1)))
55    pub multi_signature: String,
56
57    /// Genesis signature created from the original stake distribution
58    /// aka GENESIS_SIG(AVK(-1))
59    pub genesis_signature: String,
60}
61
62impl CertificateMessage {
63    cfg_test_tools! {
64        /// Return a dummy test entity (test-only).
65        pub fn dummy() -> Self {
66            let mut protocol_message = ProtocolMessage::new();
67            protocol_message.set_message_part(
68                ProtocolMessagePartKey::SnapshotDigest,
69                "snapshot-digest-123".to_string(),
70            );
71            protocol_message.set_message_part(
72                ProtocolMessagePartKey::NextAggregateVerificationKey,
73                fake_keys::aggregate_verification_key()[1].to_owned(),
74            );
75            let epoch = Epoch(10);
76
77            Self {
78                hash: "hash".to_string(),
79                previous_hash: "previous_hash".to_string(),
80                epoch,
81                signed_entity_type: SignedEntityType::MithrilStakeDistribution(epoch),
82                metadata: CertificateMetadataMessagePart::dummy(),
83                protocol_message: protocol_message.clone(),
84                signed_message: "signed_message".to_string(),
85                aggregate_verification_key: fake_keys::aggregate_verification_key()[0].to_owned(),
86                multi_signature: fake_keys::multi_signature()[0].to_owned(),
87                genesis_signature: String::new(),
88            }
89        }
90    }
91
92    /// Check that the certificate signed message match the given protocol message.
93    pub fn match_message(&self, message: &ProtocolMessage) -> bool {
94        message.compute_hash() == self.signed_message
95    }
96}
97
98impl Debug for CertificateMessage {
99    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
100        let should_be_exhaustive = f.alternate();
101        let mut debug = f.debug_struct("Certificate");
102        debug
103            .field("hash", &self.hash)
104            .field("previous_hash", &self.previous_hash)
105            .field("epoch", &format_args!("{:?}", self.epoch))
106            .field(
107                "signed_entity_type",
108                &format_args!("{:?}", self.signed_entity_type),
109            )
110            .field("metadata", &format_args!("{:?}", self.metadata))
111            .field(
112                "protocol_message",
113                &format_args!("{:?}", self.protocol_message),
114            )
115            .field("signed_message", &self.signed_message);
116
117        match should_be_exhaustive {
118            true => debug
119                .field(
120                    "aggregate_verification_key",
121                    &self.aggregate_verification_key,
122                )
123                .field("multi_signature", &self.multi_signature)
124                .field("genesis_signature", &self.genesis_signature)
125                .finish(),
126            false => debug.finish_non_exhaustive(),
127        }
128    }
129}
130
131impl TryFrom<CertificateMessage> for Certificate {
132    type Error = StdError;
133
134    fn try_from(certificate_message: CertificateMessage) -> Result<Self, Self::Error> {
135        let metadata = CertificateMetadata {
136            network: certificate_message.metadata.network,
137            protocol_version: certificate_message.metadata.protocol_version,
138            protocol_parameters: certificate_message.metadata.protocol_parameters,
139            initiated_at: certificate_message.metadata.initiated_at,
140            sealed_at: certificate_message.metadata.sealed_at,
141            signers: certificate_message.metadata.signers,
142        };
143
144        let certificate = Certificate {
145            hash: certificate_message.hash,
146            previous_hash: certificate_message.previous_hash,
147            epoch: certificate_message.epoch,
148            metadata,
149            protocol_message: certificate_message.protocol_message,
150            signed_message: certificate_message.signed_message,
151            aggregate_verification_key: certificate_message
152                .aggregate_verification_key
153                .try_into()
154                .with_context(|| {
155                "Can not convert message to certificate: can not decode the aggregate verification key"
156            })?,
157            signature: if certificate_message.genesis_signature.is_empty() {
158                CertificateSignature::MultiSignature(
159                    certificate_message.signed_entity_type,
160                    certificate_message
161                        .multi_signature
162                        .try_into()
163                        .with_context(|| {
164                            "Can not convert message to certificate: can not decode the multi-signature"
165                        })?,
166                )
167            } else {
168                CertificateSignature::GenesisSignature(
169                    certificate_message
170                        .genesis_signature
171                        .try_into()
172                        .with_context(|| {
173                            "Can not convert message to certificate: can not decode the genesis signature"
174                        })?,
175                )
176            },
177        };
178
179        Ok(certificate)
180    }
181}
182
183impl TryFrom<Certificate> for CertificateMessage {
184    type Error = StdError;
185
186    fn try_from(certificate: Certificate) -> Result<Self, Self::Error> {
187        let signed_entity_type = certificate.signed_entity_type();
188        let metadata = CertificateMetadataMessagePart {
189            network: certificate.metadata.network,
190            protocol_version: certificate.metadata.protocol_version,
191            protocol_parameters: certificate.metadata.protocol_parameters,
192            initiated_at: certificate.metadata.initiated_at,
193            sealed_at: certificate.metadata.sealed_at,
194            signers: certificate.metadata.signers,
195        };
196
197        let (multi_signature, genesis_signature) = match certificate.signature {
198            CertificateSignature::GenesisSignature(signature) => {
199                (String::new(), signature.to_bytes_hex())
200            }
201            CertificateSignature::MultiSignature(_, signature) => (
202                signature.to_json_hex().with_context(|| {
203                    "Can not convert certificate to message: can not encode the multi-signature"
204                })?,
205                String::new(),
206            ),
207        };
208
209        let message = CertificateMessage {
210            hash: certificate.hash,
211            previous_hash: certificate.previous_hash,
212            epoch: certificate.epoch,
213            signed_entity_type,
214            metadata,
215            protocol_message: certificate.protocol_message,
216            signed_message: certificate.signed_message,
217            aggregate_verification_key: certificate
218                .aggregate_verification_key
219                .to_json_hex()
220                .with_context(|| {
221                    "Can not convert certificate to message: can not encode aggregate verification key"
222                })?,
223            multi_signature,
224            genesis_signature,
225        };
226
227        Ok(message)
228    }
229}
230
231#[cfg(test)]
232mod tests {
233    use chrono::{DateTime, Utc};
234
235    use crate::entities::{CardanoDbBeacon, ProtocolParameters, StakeDistributionParty};
236
237    use super::*;
238
239    const CURRENT_JSON: &str = r#"{
240            "hash": "hash",
241            "previous_hash": "previous_hash",
242            "epoch": 10,
243            "signed_entity_type": {
244                "CardanoImmutableFilesFull": {
245                    "epoch": 10,
246                    "immutable_file_number": 1728
247                }
248            },
249            "metadata": {
250                "network": "testnet",
251                "version": "0.1.0",
252                "parameters": {
253                    "k": 1000,
254                    "m": 100,
255                    "phi_f": 0.123
256                },
257            "initiated_at": "2024-02-12T13:11:47Z",
258            "sealed_at": "2024-02-12T13:12:57Z",
259                "signers": [
260                    {
261                        "party_id": "1",
262                        "verification_key": "7b22766b223a5b3134332c3136312c3235352c34382c37382c35372c3230342c3232302c32352c3232312c3136342c3235322c3234382c31342c35362c3132362c3138362c3133352c3232382c3138382c3134352c3138312c35322c3230302c39372c39392c3231332c34362c302c3139392c3139332c38392c3138372c38382c32392c3133352c3137332c3234342c38362c33362c38332c35342c36372c3136342c362c3133372c39342c37322c362c3130352c3132382c3132382c39332c34382c3137362c31312c342c3234362c3133382c34382c3138302c3133332c39302c3134322c3139322c32342c3139332c3131312c3134322c33312c37362c3131312c3131302c3233342c3135332c39302c3230382c3139322c33312c3132342c39352c3130322c34392c3135382c39392c35322c3232302c3136352c39342c3235312c36382c36392c3132312c31362c3232342c3139345d2c22706f70223a5b3136382c35302c3233332c3139332c31352c3133362c36352c37322c3132332c3134382c3132392c3137362c33382c3139382c3230392c34372c32382c3230342c3137362c3134342c35372c3235312c34322c32382c36362c37362c38392c39372c3135382c36332c35342c3139382c3139342c3137362c3133352c3232312c31342c3138352c3139372c3232352c3230322c39382c3234332c37342c3233332c3232352c3134332c3135312c3134372c3137372c3137302c3131372c36362c3136352c36362c36322c33332c3231362c3233322c37352c36382c3131342c3139352c32322c3130302c36352c34342c3139382c342c3136362c3130322c3233332c3235332c3234302c35392c3137352c36302c3131372c3134322c3131342c3134302c3132322c31372c38372c3131302c3138372c312c31372c31302c3139352c3135342c31332c3234392c38362c35342c3232365d7d",
263                        "stake": 10
264                    },
265                    {
266                        "party_id": "2",
267                        "verification_key": "7b22766b223a5b3134352c35362c3137352c33322c3132322c3138372c3231342c3232362c3235312c3134382c38382c392c312c3130332c3135392c3134362c38302c3136362c3130372c3234332c3235312c3233362c34312c32382c3131312c3132382c3230372c3136342c3133322c3134372c3232382c38332c3234362c3232382c3137302c36382c38392c37382c36302c32382c3132332c3133302c38382c3233342c33382c39372c34322c36352c312c3130302c35332c31382c37382c3133312c382c36312c3132322c3133312c3233382c38342c3233332c3232332c3135342c3131382c3131382c37332c32382c32372c3130312c37382c38302c3233332c3132332c3230362c3232302c3137342c3133342c3230352c37312c3131302c3131322c3138302c39372c39382c302c3131332c36392c3134352c3233312c3136382c34332c3137332c3137322c35362c3130342c3230385d2c22706f70223a5b3133372c3231342c37352c37352c3134342c3136312c3133372c37392c39342c3134302c3138312c34372c33312c38312c3231332c33312c3137312c3231362c32342c3137342c37382c3234382c3133302c37352c3235352c31312c3134352c3132342c36312c38302c3139302c32372c3231362c3130352c3130362c3234382c39312c3134332c3230342c3130322c3230332c3136322c37362c3130372c31352c35322c36312c38322c3134362c3133302c3132342c37342c382c33342c3136342c3138372c3230332c38322c36342c3130382c3139312c3138352c3138382c37372c3132322c352c3234362c3235352c3130322c3131392c3234372c3139392c3131372c36372c3234312c3134332c32392c3136382c36372c39342c3135312c37382c3132392c3133312c33302c3130312c3137332c31302c36392c36382c3137352c39382c33372c3233392c3139342c32395d7d",
268                        "stake": 20
269                    }
270                ]
271            },
272            "protocol_message": {
273                "message_parts": {
274                    "snapshot_digest": "snapshot-digest-123",
275                    "next_aggregate_verification_key": "next-avk-123"
276                }
277            },
278            "signed_message": "signed_message",
279            "aggregate_verification_key": "aggregate_verification_key",
280            "multi_signature": "multi_signature",
281            "genesis_signature": "genesis_signature"
282        }"#;
283
284    fn golden_current_message() -> CertificateMessage {
285        let mut protocol_message = ProtocolMessage::new();
286        protocol_message.set_message_part(
287            ProtocolMessagePartKey::SnapshotDigest,
288            "snapshot-digest-123".to_string(),
289        );
290        protocol_message.set_message_part(
291            ProtocolMessagePartKey::NextAggregateVerificationKey,
292            "next-avk-123".to_string(),
293        );
294        let epoch = Epoch(10);
295
296        CertificateMessage {
297            hash: "hash".to_string(),
298            previous_hash: "previous_hash".to_string(),
299            epoch,
300            signed_entity_type: SignedEntityType::CardanoImmutableFilesFull(CardanoDbBeacon::new(
301                *epoch, 1728,
302            )),
303            metadata: CertificateMetadataMessagePart {
304                network: "testnet".to_string(),
305                protocol_version: "0.1.0".to_string(),
306                protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
307                initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
308                    .unwrap()
309                    .with_timezone(&Utc),
310                sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
311                    .unwrap()
312                    .with_timezone(&Utc),
313                signers: vec![
314                    StakeDistributionParty {
315                        party_id: "1".to_string(),
316                        stake: 10,
317                    },
318                    StakeDistributionParty {
319                        party_id: "2".to_string(),
320                        stake: 20,
321                    },
322                ],
323            },
324            protocol_message: protocol_message.clone(),
325            signed_message: "signed_message".to_string(),
326            aggregate_verification_key: "aggregate_verification_key".to_string(),
327            multi_signature: "multi_signature".to_string(),
328            genesis_signature: "genesis_signature".to_string(),
329        }
330    }
331
332    #[test]
333    fn test_current_json_deserialized_into_current_message() {
334        let json = CURRENT_JSON;
335        let message: CertificateMessage = serde_json::from_str(json).unwrap();
336
337        assert_eq!(golden_current_message(), message);
338    }
339}