Skip to content

Commit b3b5ae1

Browse files
Alenarsfauvel
andcommitted
Make beacon optional in CertificateListMessage
So we can remove it gracefully in the future when enough nodes have updated. Co-authored-by: Sébastien Fauvel <[email protected]>
1 parent 6c5d517 commit b3b5ae1

File tree

2 files changed

+133
-33
lines changed

2 files changed

+133
-33
lines changed

mithril-aggregator/src/database/record/certificate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl From<CertificateRecord> for CertificateMessage {
273273

274274
impl From<CertificateRecord> for CertificateListItemMessage {
275275
fn from(value: CertificateRecord) -> Self {
276-
let beacon = value.as_cardano_db_beacon();
276+
let beacon = Some(value.as_cardano_db_beacon());
277277
let metadata = CertificateListItemMessageMetadata {
278278
network: value.network,
279279
protocol_version: value.protocol_version,

mithril-common/src/messages/certificate_list.rs

Lines changed: 132 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub struct CertificateListItemMessage {
6666

6767
/// Mithril beacon on the Cardano chain
6868
#[deprecated(since = "0.3.25", note = "use epoch and/or signed_entity_type instead")]
69-
pub beacon: CardanoDbBeacon,
69+
#[serde(skip_serializing_if = "Option::is_none")]
70+
pub beacon: Option<CardanoDbBeacon>,
7071

7172
/// Certificate metadata
7273
/// aka METADATA(p,n)
@@ -106,7 +107,7 @@ impl CertificateListItemMessage {
106107
previous_hash: "previous_hash".to_string(),
107108
epoch,
108109
signed_entity_type: SignedEntityType::MithrilStakeDistribution(epoch),
109-
beacon: CardanoDbBeacon::new("testnet", *epoch, 100),
110+
beacon: Some(CardanoDbBeacon::new("testnet", *epoch, 100)),
110111
metadata: CertificateListItemMessageMetadata {
111112
network: "testnet".to_string(),
112113
protocol_version: "0.1.0".to_string(),
@@ -161,7 +162,23 @@ impl Debug for CertificateListItemMessage {
161162
mod tests {
162163
use super::*;
163164

164-
fn golden_message() -> CertificateListMessage {
165+
type CertificateListMessagePreviousVersion = Vec<CertificateListItemMessagePreviousVersion>;
166+
167+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
168+
struct CertificateListItemMessagePreviousVersion {
169+
pub hash: String,
170+
pub previous_hash: String,
171+
pub epoch: Epoch,
172+
pub signed_entity_type: SignedEntityType,
173+
#[deprecated(since = "0.3.25", note = "use epoch and/or signed_entity_type instead")]
174+
pub beacon: CardanoDbBeacon,
175+
pub metadata: CertificateListItemMessageMetadata,
176+
pub protocol_message: ProtocolMessage,
177+
pub signed_message: String,
178+
pub aggregate_verification_key: String,
179+
}
180+
181+
fn golden_previous_message() -> CertificateListItemMessagePreviousVersion {
165182
let mut protocol_message = ProtocolMessage::new();
166183
protocol_message.set_message_part(
167184
ProtocolMessagePartKey::SnapshotDigest,
@@ -173,37 +190,69 @@ mod tests {
173190
);
174191
let epoch = Epoch(10);
175192

176-
vec![
177-
#[allow(deprecated)]
178-
CertificateListItemMessage {
179-
hash: "hash".to_string(),
180-
previous_hash: "previous_hash".to_string(),
181-
epoch,
182-
signed_entity_type: SignedEntityType::MithrilStakeDistribution(epoch),
183-
beacon: CardanoDbBeacon::new("testnet", *epoch, 100),
184-
metadata: CertificateListItemMessageMetadata {
185-
network: "testnet".to_string(),
186-
protocol_version: "0.1.0".to_string(),
187-
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
188-
initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
189-
.unwrap()
190-
.with_timezone(&Utc),
191-
sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
192-
.unwrap()
193-
.with_timezone(&Utc),
194-
total_signers: 2,
195-
},
196-
protocol_message: protocol_message.clone(),
197-
signed_message: "signed_message".to_string(),
198-
aggregate_verification_key: "aggregate_verification_key".to_string(),
193+
#[allow(deprecated)]
194+
CertificateListItemMessagePreviousVersion {
195+
hash: "hash".to_string(),
196+
previous_hash: "previous_hash".to_string(),
197+
epoch,
198+
signed_entity_type: SignedEntityType::MithrilStakeDistribution(epoch),
199+
beacon: CardanoDbBeacon::new("testnet", *epoch, 100),
200+
metadata: CertificateListItemMessageMetadata {
201+
network: "testnet".to_string(),
202+
protocol_version: "0.1.0".to_string(),
203+
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
204+
initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
205+
.unwrap()
206+
.with_timezone(&Utc),
207+
sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
208+
.unwrap()
209+
.with_timezone(&Utc),
210+
total_signers: 2,
199211
},
200-
]
212+
protocol_message: protocol_message.clone(),
213+
signed_message: "signed_message".to_string(),
214+
aggregate_verification_key: "aggregate_verification_key".to_string(),
215+
}
201216
}
202217

203-
// Test the retro compatibility with possible future upgrades.
204-
#[test]
205-
fn test_v1() {
206-
let json = r#"[{
218+
fn golden_actual_message() -> CertificateListItemMessage {
219+
let mut protocol_message = ProtocolMessage::new();
220+
protocol_message.set_message_part(
221+
ProtocolMessagePartKey::SnapshotDigest,
222+
"snapshot-digest-123".to_string(),
223+
);
224+
protocol_message.set_message_part(
225+
ProtocolMessagePartKey::NextAggregateVerificationKey,
226+
"next-avk-123".to_string(),
227+
);
228+
let epoch = Epoch(10);
229+
230+
#[allow(deprecated)]
231+
CertificateListItemMessage {
232+
hash: "hash".to_string(),
233+
previous_hash: "previous_hash".to_string(),
234+
epoch,
235+
signed_entity_type: SignedEntityType::MithrilStakeDistribution(epoch),
236+
beacon: Some(CardanoDbBeacon::new("testnet", *epoch, 100)),
237+
metadata: CertificateListItemMessageMetadata {
238+
network: "testnet".to_string(),
239+
protocol_version: "0.1.0".to_string(),
240+
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
241+
initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
242+
.unwrap()
243+
.with_timezone(&Utc),
244+
sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
245+
.unwrap()
246+
.with_timezone(&Utc),
247+
total_signers: 2,
248+
},
249+
protocol_message: protocol_message.clone(),
250+
signed_message: "signed_message".to_string(),
251+
aggregate_verification_key: "aggregate_verification_key".to_string(),
252+
}
253+
}
254+
255+
const ACTUAL_JSON: &str = r#"[{
207256
"hash": "hash",
208257
"previous_hash": "previous_hash",
209258
"epoch": 10,
@@ -235,10 +284,61 @@ mod tests {
235284
"aggregate_verification_key": "aggregate_verification_key"
236285
}]"#;
237286

287+
#[test]
288+
fn test_actual_json_deserialized_into_previous_message() {
289+
let json = ACTUAL_JSON;
290+
let message: CertificateListMessagePreviousVersion = serde_json::from_str(json).unwrap();
291+
292+
assert_eq!(vec![golden_previous_message()], message);
293+
}
294+
295+
#[test]
296+
fn test_actual_json_deserialized_into_actual_message() {
297+
let json = ACTUAL_JSON;
298+
let message: CertificateListMessage = serde_json::from_str(json).unwrap();
299+
300+
assert_eq!(vec![golden_actual_message()], message);
301+
}
302+
303+
#[test]
304+
fn test_json_next_version_deserialized_into_actual_message() {
305+
let json = r#"[{
306+
"hash": "hash",
307+
"previous_hash": "previous_hash",
308+
"epoch": 10,
309+
"signed_entity_type": { "MithrilStakeDistribution": 10 },
310+
"metadata": {
311+
"network": "testnet",
312+
"version": "0.1.0",
313+
"parameters": {
314+
"k": 1000,
315+
"m": 100,
316+
"phi_f": 0.123
317+
},
318+
"initiated_at": "2024-02-12T13:11:47Z",
319+
"sealed_at": "2024-02-12T13:12:57Z",
320+
"total_signers": 2
321+
},
322+
"protocol_message": {
323+
"message_parts": {
324+
"snapshot_digest": "snapshot-digest-123",
325+
"next_aggregate_verification_key": "next-avk-123"
326+
}
327+
},
328+
"signed_message": "signed_message",
329+
"aggregate_verification_key": "aggregate_verification_key"
330+
}]"#;
331+
238332
let message: CertificateListMessage = serde_json::from_str(json).expect(
239333
"This JSON is expected to be succesfully parsed into a CertificateListMessage instance.",
240334
);
241335

242-
assert_eq!(golden_message(), message);
336+
#[allow(deprecated)]
337+
let golden_message = vec![CertificateListItemMessage {
338+
beacon: None,
339+
..golden_actual_message()
340+
}];
341+
342+
assert_eq!(golden_message, message);
243343
}
244344
}

0 commit comments

Comments
 (0)