@@ -37,7 +37,8 @@ pub struct CertificateMessage {
3737
3838 /// Mithril beacon on the Cardano chain
3939 #[ deprecated( since = "0.3.25" , note = "use epoch and/or signed_entity_type instead" ) ]
40- pub beacon : CardanoDbBeacon ,
40+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
41+ pub beacon : Option < CardanoDbBeacon > ,
4142
4243 /// Certificate metadata
4344 /// aka METADATA(p,n)
@@ -86,7 +87,7 @@ impl CertificateMessage {
8687 previous_hash: "previous_hash" . to_string( ) ,
8788 epoch,
8889 signed_entity_type: SignedEntityType :: MithrilStakeDistribution ( epoch) ,
89- beacon: CardanoDbBeacon :: new( "testnet" . to_string( ) , * epoch, 100 ) ,
90+ beacon: Some ( CardanoDbBeacon :: new( "testnet" . to_string( ) , * epoch, 100 ) ) ,
9091 metadata: CertificateMetadataMessagePart :: dummy( ) ,
9192 protocol_message: protocol_message. clone( ) ,
9293 signed_message: "signed_message" . to_string( ) ,
@@ -142,8 +143,13 @@ impl TryFrom<CertificateMessage> for Certificate {
142143 fn try_from ( certificate_message : CertificateMessage ) -> Result < Self , Self :: Error > {
143144 #[ allow( deprecated) ]
144145 let metadata = CertificateMetadata {
145- network : certificate_message. beacon . network ,
146- immutable_file_number : certificate_message. beacon . immutable_file_number ,
146+ network : certificate_message. metadata . network ,
147+ // This field is deprecated and will be removed in the future so use 0 as default
148+ // value is fine.
149+ immutable_file_number : certificate_message
150+ . beacon
151+ . map ( |b| b. immutable_file_number )
152+ . unwrap_or ( 0 ) ,
147153 protocol_version : certificate_message. metadata . protocol_version ,
148154 protocol_parameters : certificate_message. metadata . protocol_parameters ,
149155 initiated_at : certificate_message. metadata . initiated_at ,
@@ -194,7 +200,7 @@ impl TryFrom<Certificate> for CertificateMessage {
194200 type Error = StdError ;
195201
196202 fn try_from ( certificate : Certificate ) -> Result < Self , Self :: Error > {
197- let beacon = certificate. as_cardano_db_beacon ( ) ;
203+ let beacon = Some ( certificate. as_cardano_db_beacon ( ) ) ;
198204 let signed_entity_type = certificate. signed_entity_type ( ) ;
199205 let metadata = CertificateMetadataMessagePart {
200206 network : certificate. metadata . network ,
@@ -249,7 +255,23 @@ mod tests {
249255
250256 use super :: * ;
251257
252- fn golden_message ( ) -> CertificateMessage {
258+ #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
259+ struct CertificateMessagePreviousVersion {
260+ pub hash : String ,
261+ pub previous_hash : String ,
262+ pub epoch : Epoch ,
263+ pub signed_entity_type : SignedEntityType ,
264+ #[ deprecated( since = "0.3.25" , note = "use epoch and/or signed_entity_type instead" ) ]
265+ pub beacon : CardanoDbBeacon ,
266+ pub metadata : CertificateMetadataMessagePart ,
267+ pub protocol_message : ProtocolMessage ,
268+ pub signed_message : String ,
269+ pub aggregate_verification_key : String ,
270+ pub multi_signature : String ,
271+ pub genesis_signature : String ,
272+ }
273+
274+ fn golden_previous_message ( ) -> CertificateMessagePreviousVersion {
253275 let mut protocol_message = ProtocolMessage :: new ( ) ;
254276 protocol_message. set_message_part (
255277 ProtocolMessagePartKey :: SnapshotDigest ,
@@ -262,7 +284,7 @@ mod tests {
262284 let beacon = CardanoDbBeacon :: new ( "testnet" , 10 , 100 ) ;
263285
264286 #[ allow( deprecated) ]
265- CertificateMessage {
287+ CertificateMessagePreviousVersion {
266288 hash : "hash" . to_string ( ) ,
267289 previous_hash : "previous_hash" . to_string ( ) ,
268290 epoch : beacon. epoch ,
@@ -297,10 +319,55 @@ mod tests {
297319 }
298320 }
299321
300- // Test the backward compatibility with possible future upgrades.
301- #[ test]
302- fn test_v1 ( ) {
303- let json = r#"{
322+ fn golden_actual_message ( ) -> CertificateMessage {
323+ let mut protocol_message = ProtocolMessage :: new ( ) ;
324+ protocol_message. set_message_part (
325+ ProtocolMessagePartKey :: SnapshotDigest ,
326+ "snapshot-digest-123" . to_string ( ) ,
327+ ) ;
328+ protocol_message. set_message_part (
329+ ProtocolMessagePartKey :: NextAggregateVerificationKey ,
330+ "next-avk-123" . to_string ( ) ,
331+ ) ;
332+ let beacon = CardanoDbBeacon :: new ( "testnet" , 10 , 100 ) ;
333+
334+ #[ allow( deprecated) ]
335+ CertificateMessage {
336+ hash : "hash" . to_string ( ) ,
337+ previous_hash : "previous_hash" . to_string ( ) ,
338+ epoch : beacon. epoch ,
339+ signed_entity_type : SignedEntityType :: MithrilStakeDistribution ( beacon. epoch ) ,
340+ beacon : Some ( beacon. clone ( ) ) ,
341+ metadata : CertificateMetadataMessagePart {
342+ network : beacon. network ,
343+ protocol_version : "0.1.0" . to_string ( ) ,
344+ protocol_parameters : ProtocolParameters :: new ( 1000 , 100 , 0.123 ) ,
345+ initiated_at : DateTime :: parse_from_rfc3339 ( "2024-02-12T13:11:47Z" )
346+ . unwrap ( )
347+ . with_timezone ( & Utc ) ,
348+ sealed_at : DateTime :: parse_from_rfc3339 ( "2024-02-12T13:12:57Z" )
349+ . unwrap ( )
350+ . with_timezone ( & Utc ) ,
351+ signers : vec ! [
352+ StakeDistributionParty {
353+ party_id: "1" . to_string( ) ,
354+ stake: 10 ,
355+ } ,
356+ StakeDistributionParty {
357+ party_id: "2" . to_string( ) ,
358+ stake: 20 ,
359+ } ,
360+ ] ,
361+ } ,
362+ protocol_message : protocol_message. clone ( ) ,
363+ signed_message : "signed_message" . to_string ( ) ,
364+ aggregate_verification_key : "aggregate_verification_key" . to_string ( ) ,
365+ multi_signature : "multi_signature" . to_string ( ) ,
366+ genesis_signature : "genesis_signature" . to_string ( ) ,
367+ }
368+ }
369+
370+ const ACTUAL_JSON : & str = r#"{
304371 "hash": "hash",
305372 "previous_hash": "previous_hash",
306373 "epoch": 10,
@@ -344,10 +411,74 @@ mod tests {
344411 "multi_signature": "multi_signature",
345412 "genesis_signature": "genesis_signature"
346413 }"# ;
414+
415+ #[ test]
416+ fn test_actual_json_deserialized_into_previous_message ( ) {
417+ let json = ACTUAL_JSON ;
418+ let message: CertificateMessagePreviousVersion = serde_json:: from_str ( json) . unwrap ( ) ;
419+
420+ assert_eq ! ( golden_previous_message( ) , message) ;
421+ }
422+
423+ #[ test]
424+ fn test_actual_json_deserialized_into_actual_message ( ) {
425+ let json = ACTUAL_JSON ;
426+ let message: CertificateMessage = serde_json:: from_str ( json) . unwrap ( ) ;
427+
428+ assert_eq ! ( golden_actual_message( ) , message) ;
429+ }
430+
431+ #[ test]
432+ fn test_json_next_version_deserialized_into_actual_message ( ) {
433+ let json = r#"{
434+ "hash": "hash",
435+ "previous_hash": "previous_hash",
436+ "epoch": 10,
437+ "signed_entity_type": { "MithrilStakeDistribution": 10 },
438+ "metadata": {
439+ "network": "testnet",
440+ "version": "0.1.0",
441+ "parameters": {
442+ "k": 1000,
443+ "m": 100,
444+ "phi_f": 0.123
445+ },
446+ "initiated_at": "2024-02-12T13:11:47Z",
447+ "sealed_at": "2024-02-12T13:12:57Z",
448+ "signers": [
449+ {
450+ "party_id": "1",
451+ "verification_key": "7b22766b223a5b3134332c3136312c3235352c34382c37382c35372c3230342c3232302c32352c3232312c3136342c3235322c3234382c31342c35362c3132362c3138362c3133352c3232382c3138382c3134352c3138312c35322c3230302c39372c39392c3231332c34362c302c3139392c3139332c38392c3138372c38382c32392c3133352c3137332c3234342c38362c33362c38332c35342c36372c3136342c362c3133372c39342c37322c362c3130352c3132382c3132382c39332c34382c3137362c31312c342c3234362c3133382c34382c3138302c3133332c39302c3134322c3139322c32342c3139332c3131312c3134322c33312c37362c3131312c3131302c3233342c3135332c39302c3230382c3139322c33312c3132342c39352c3130322c34392c3135382c39392c35322c3232302c3136352c39342c3235312c36382c36392c3132312c31362c3232342c3139345d2c22706f70223a5b3136382c35302c3233332c3139332c31352c3133362c36352c37322c3132332c3134382c3132392c3137362c33382c3139382c3230392c34372c32382c3230342c3137362c3134342c35372c3235312c34322c32382c36362c37362c38392c39372c3135382c36332c35342c3139382c3139342c3137362c3133352c3232312c31342c3138352c3139372c3232352c3230322c39382c3234332c37342c3233332c3232352c3134332c3135312c3134372c3137372c3137302c3131372c36362c3136352c36362c36322c33332c3231362c3233322c37352c36382c3131342c3139352c32322c3130302c36352c34342c3139382c342c3136362c3130322c3233332c3235332c3234302c35392c3137352c36302c3131372c3134322c3131342c3134302c3132322c31372c38372c3131302c3138372c312c31372c31302c3139352c3135342c31332c3234392c38362c35342c3232365d7d",
452+ "stake": 10
453+ },
454+ {
455+ "party_id": "2",
456+ "verification_key": "7b22766b223a5b3134352c35362c3137352c33322c3132322c3138372c3231342c3232362c3235312c3134382c38382c392c312c3130332c3135392c3134362c38302c3136362c3130372c3234332c3235312c3233362c34312c32382c3131312c3132382c3230372c3136342c3133322c3134372c3232382c38332c3234362c3232382c3137302c36382c38392c37382c36302c32382c3132332c3133302c38382c3233342c33382c39372c34322c36352c312c3130302c35332c31382c37382c3133312c382c36312c3132322c3133312c3233382c38342c3233332c3232332c3135342c3131382c3131382c37332c32382c32372c3130312c37382c38302c3233332c3132332c3230362c3232302c3137342c3133342c3230352c37312c3131302c3131322c3138302c39372c39382c302c3131332c36392c3134352c3233312c3136382c34332c3137332c3137322c35362c3130342c3230385d2c22706f70223a5b3133372c3231342c37352c37352c3134342c3136312c3133372c37392c39342c3134302c3138312c34372c33312c38312c3231332c33312c3137312c3231362c32342c3137342c37382c3234382c3133302c37352c3235352c31312c3134352c3132342c36312c38302c3139302c32372c3231362c3130352c3130362c3234382c39312c3134332c3230342c3130322c3230332c3136322c37362c3130372c31352c35322c36312c38322c3134362c3133302c3132342c37342c382c33342c3136342c3138372c3230332c38322c36342c3130382c3139312c3138352c3138382c37372c3132322c352c3234362c3235352c3130322c3131392c3234372c3139392c3131372c36372c3234312c3134332c32392c3136382c36372c39342c3135312c37382c3132392c3133312c33302c3130312c3137332c31302c36392c36382c3137352c39382c33372c3233392c3139342c32395d7d",
457+ "stake": 20
458+ }
459+ ]
460+ },
461+ "protocol_message": {
462+ "message_parts": {
463+ "snapshot_digest": "snapshot-digest-123",
464+ "next_aggregate_verification_key": "next-avk-123"
465+ }
466+ },
467+ "signed_message": "signed_message",
468+ "aggregate_verification_key": "aggregate_verification_key",
469+ "multi_signature": "multi_signature",
470+ "genesis_signature": "genesis_signature"
471+ }"# ;
347472 let message: CertificateMessage = serde_json:: from_str ( json) . expect (
348473 "This JSON is expected to be successfully parsed into a CertificateMessage instance." ,
349474 ) ;
350475
351- assert_eq ! ( golden_message( ) , message) ;
476+ #[ allow( deprecated) ]
477+ let golden_message = CertificateMessage {
478+ beacon : None ,
479+ ..golden_actual_message ( )
480+ } ;
481+
482+ assert_eq ! ( golden_message, message) ;
352483 }
353484}
0 commit comments