Skip to content

Commit

Permalink
Update Certificate List message
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed May 30, 2023
1 parent 09e5bab commit 1193c63
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 76 deletions.
@@ -1,5 +1,7 @@
use mithril_common::entities::Certificate;
use mithril_common::messages::{CertificateListItemMessage, CertificateListMessage};
use mithril_common::messages::{
CertificateListItemMessage, CertificateListItemMessageMetadata, CertificateListMessage,
};

/// Adapter to convert a list of [Certificate] to [CertificateListMessage] instances
pub struct ToCertificateListMessageAdapter;
Expand All @@ -13,9 +15,16 @@ impl ToCertificateListMessageAdapter {
hash: certificate.hash,
previous_hash: certificate.previous_hash,
beacon: certificate.beacon,
metadata: CertificateListItemMessageMetadata {
protocol_version: certificate.metadata.protocol_version,
protocol_parameters: certificate.metadata.protocol_parameters,
initiated_at: certificate.metadata.initiated_at,
sealed_at: certificate.metadata.sealed_at,
total_signers: certificate.metadata.signers.len(),
},
protocol_message: certificate.protocol_message,
signed_message: certificate.signed_message,
initiated_at: certificate.metadata.initiated_at,
sealed_at: certificate.metadata.sealed_at,
aggregate_verification_key: certificate.aggregate_verification_key,
})
.collect()
}
Expand All @@ -37,9 +46,16 @@ mod tests {
hash: certificate.hash,
previous_hash: certificate.previous_hash,
beacon: certificate.beacon,
metadata: CertificateListItemMessageMetadata {
protocol_version: certificate.metadata.protocol_version,
protocol_parameters: certificate.metadata.protocol_parameters,
initiated_at: certificate.metadata.initiated_at,
sealed_at: certificate.metadata.sealed_at,
total_signers: certificate.metadata.signers.len(),
},
protocol_message: certificate.protocol_message,
signed_message: certificate.signed_message,
initiated_at: certificate.metadata.initiated_at,
sealed_at: certificate.metadata.sealed_at,
aggregate_verification_key: certificate.aggregate_verification_key,
}];

assert_eq!(certificate_list_message_expected, certificate_list_message);
Expand Down
155 changes: 115 additions & 40 deletions mithril-common/src/messages/certificate_list.rs
Expand Up @@ -2,14 +2,45 @@ use serde::{Deserialize, Serialize};

use crate::entities::Beacon;

use crate::entities::Epoch;
use crate::entities::ProtocolMessage;
use crate::entities::ProtocolMessagePartKey;
use crate::entities::ProtocolParameters;
use crate::entities::ProtocolVersion;

/// Message structure of a certificate list
pub type CertificateListMessage = Vec<CertificateListItemMessage>;

/// CertificateListItemMessage represents the metadata associated to a CertificateListItemMessage
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct CertificateListItemMessageMetadata {
/// Protocol Version (semver)
/// Useful to achieve backward compatibility of the certificates (including of the multi signature)
/// part of METADATA(p,n)
#[serde(rename = "version")]
pub protocol_version: ProtocolVersion,

/// Protocol parameters
/// part of METADATA(p,n)
#[serde(rename = "parameters")]
pub protocol_parameters: ProtocolParameters,

/// Date and time when the certificate was initiated
/// Represents the time at which the single signatures registration is opened
/// part of METADATA(p,n)
pub initiated_at: String,

/// Date and time when the certificate was sealed
/// Represents the time at which the quorum of single signatures was reached so that they were aggregated into a multi signature
/// part of METADATA(p,n)
pub sealed_at: String,

/// The number of signers that contributed to the certificate
/// part of METADATA(p,n)
pub total_signers: usize,
}

/// Message structure of a certificate list item
// TODO: select final fields for the list
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct CertificateListItemMessage {
/// Hash of the current certificate
/// Computed from the other fields of the certificate
Expand All @@ -26,37 +57,50 @@ pub struct CertificateListItemMessage {
/// aka BEACON(p,n)
pub beacon: Beacon,

/// Certificate metadata
/// aka METADATA(p,n)
pub metadata: CertificateListItemMessageMetadata,

/// Structured message that is used to created the signed message
/// aka MSG(p,n) U AVK(n-1)
pub protocol_message: ProtocolMessage,

/// Message that is signed by the signers
/// aka H(MSG(p,n) || AVK(n-1))
pub signed_message: String,

/// Date and time when the certificate was initiated
/// Represents the time at which the single signatures registration is opened
/// part of METADATA(p,n)
pub initiated_at: String,

/// Date and time when the certificate was sealed
/// Represents the time at which the quorum of single signatures was reached so that they were aggregated into a multi signature
/// part of METADATA(p,n)
pub sealed_at: String,
/// Aggregate verification key
/// The AVK used to sign during the current epoch
/// aka AVK(n-2)
pub aggregate_verification_key: String,
}

impl CertificateListItemMessage {
/// Return a dummy test entity (test-only).
pub fn dummy() -> Self {
let mut protocol_message = ProtocolMessage::new();
protocol_message.set_message_part(
ProtocolMessagePartKey::SnapshotDigest,
"snapshot-digest-123".to_string(),
);
protocol_message.set_message_part(
ProtocolMessagePartKey::NextAggregateVerificationKey,
"next-avk-123".to_string(),
);
Self {
hash: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
previous_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
.to_string(),
beacon: Beacon {
network: "preview".to_string(),
epoch: Epoch(86),
immutable_file_number: 1728,
hash: "hash".to_string(),
previous_hash: "previous_hash".to_string(),
beacon: Beacon::new("testnet".to_string(), 10, 100),
metadata: CertificateListItemMessageMetadata {
protocol_version: "0.1.0".to_string(),
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
initiated_at: "initiated_at".to_string(),
sealed_at: "sealed_at".to_string(),
total_signers: 2,
},
signed_message: "eca9866c06a9fb98a34686449ff0c03f75f8ddd9a126840e5cdd77cda2ffc4de"
.to_string(),
initiated_at: "2023-01-19T13:43:05.618857482Z".to_string(),
sealed_at: "2023-01-19T13:45:17.628757381Z".to_string(),
protocol_message: protocol_message.clone(),
signed_message: "signed_message".to_string(),
aggregate_verification_key: "aggregate_verification_key".to_string(),
}
}
}
Expand All @@ -66,33 +110,64 @@ mod tests {
use super::*;

fn golden_message() -> CertificateListMessage {
let mut protocol_message = ProtocolMessage::new();
protocol_message.set_message_part(
ProtocolMessagePartKey::SnapshotDigest,
"snapshot-digest-123".to_string(),
);
protocol_message.set_message_part(
ProtocolMessagePartKey::NextAggregateVerificationKey,
"next-avk-123".to_string(),
);
vec![CertificateListItemMessage {
hash: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(),
previous_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb"
.to_string(),
beacon: Beacon {
network: "preview".to_string(),
epoch: Epoch(86),
immutable_file_number: 1728,
hash: "hash".to_string(),
previous_hash: "previous_hash".to_string(),
beacon: Beacon::new("testnet".to_string(), 10, 100),
metadata: CertificateListItemMessageMetadata {
protocol_version: "0.1.0".to_string(),
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
initiated_at: "initiated_at".to_string(),
sealed_at: "sealed_at".to_string(),
total_signers: 2,
},
signed_message: "eca9866c06a9fb98a34686449ff0c03f75f8ddd9a126840e5cdd77cda2ffc4de"
.to_string(),
initiated_at: "2023-01-19T13:43:05.618857482Z".to_string(),
sealed_at: "2023-01-19T13:45:17.628757381Z".to_string(),
protocol_message: protocol_message.clone(),
signed_message: "signed_message".to_string(),
aggregate_verification_key: "aggregate_verification_key".to_string(),
}]
}

// Test the retro compatibility with possible future upgrades.
#[test]
fn test_v1() {
let json = r#"[{
"hash":"0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6",
"previous_hash":"d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb",
"beacon":{"network":"preview","epoch":86,"immutable_file_number":1728},
"signed_message":"eca9866c06a9fb98a34686449ff0c03f75f8ddd9a126840e5cdd77cda2ffc4de",
"initiated_at":"2023-01-19T13:43:05.618857482Z",
"sealed_at":"2023-01-19T13:45:17.628757381Z"
"hash": "hash",
"previous_hash": "previous_hash",
"beacon": {
"network": "testnet",
"epoch": 10,
"immutable_file_number": 100
},
"metadata": {
"version": "0.1.0",
"parameters": {
"k": 1000,
"m": 100,
"phi_f": 0.123
},
"initiated_at": "initiated_at",
"sealed_at": "sealed_at",
"total_signers": 2
},
"protocol_message": {
"message_parts": {
"snapshot_digest": "snapshot-digest-123",
"next_aggregate_verification_key": "next-avk-123"
}
},
"signed_message": "signed_message",
"aggregate_verification_key": "aggregate_verification_key"
}]"#;

let message: CertificateListMessage = serde_json::from_str(json).expect(
"This JSON is expected to be succesfully parsed into a CertificateListMessage instance.",
);
Expand Down
4 changes: 3 additions & 1 deletion mithril-common/src/messages/mod.rs
Expand Up @@ -13,7 +13,9 @@ mod snapshot;
mod snapshot_list;

pub use certificate::CertificateMessage;
pub use certificate_list::{CertificateListItemMessage, CertificateListMessage};
pub use certificate_list::{
CertificateListItemMessage, CertificateListItemMessageMetadata, CertificateListMessage,
};
pub use certificate_pending::{CertificatePendingMessage, SignerMessage};
pub use epoch_settings::EpochSettingsMessage;
pub use interface::MessageAdapter;
Expand Down

0 comments on commit 1193c63

Please sign in to comment.