mithril_common/messages/message_parts/
certificate_metadata.rsuse crate::entities::{ProtocolParameters, ProtocolVersion, StakeDistributionParty};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CertificateMetadataMessagePart {
pub network: String,
#[serde(rename = "version")]
pub protocol_version: ProtocolVersion,
#[serde(rename = "parameters")]
pub protocol_parameters: ProtocolParameters,
pub initiated_at: DateTime<Utc>,
pub sealed_at: DateTime<Utc>,
pub signers: Vec<StakeDistributionParty>,
}
impl CertificateMetadataMessagePart {
pub fn dummy() -> Self {
let initiated_at = DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
.unwrap()
.with_timezone(&Utc);
Self {
network: "testnet".to_string(),
protocol_version: "0.1.0".to_string(),
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
initiated_at,
sealed_at: initiated_at + Duration::try_seconds(100).unwrap(),
signers: vec![
StakeDistributionParty {
party_id: "1".to_string(),
stake: 10,
},
StakeDistributionParty {
party_id: "2".to_string(),
stake: 20,
},
],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn golden_message_current() -> CertificateMetadataMessagePart {
CertificateMetadataMessagePart {
network: "testnet".to_string(),
protocol_version: "0.1.0".to_string(),
protocol_parameters: ProtocolParameters::new(1000, 100, 0.123),
initiated_at: DateTime::parse_from_rfc3339("2024-02-12T13:11:47Z")
.unwrap()
.with_timezone(&Utc),
sealed_at: DateTime::parse_from_rfc3339("2024-02-12T13:12:57Z")
.unwrap()
.with_timezone(&Utc),
signers: vec![
StakeDistributionParty {
party_id: "1".to_string(),
stake: 10,
},
StakeDistributionParty {
party_id: "2".to_string(),
stake: 20,
},
],
}
}
const CURRENT_JSON: &str = r#"{
"network": "testnet",
"version": "0.1.0",
"parameters": {
"k": 1000,
"m": 100,
"phi_f": 0.123
},
"initiated_at": "2024-02-12T13:11:47Z",
"sealed_at": "2024-02-12T13:12:57Z",
"signers": [
{
"party_id": "1",
"stake": 10
},
{
"party_id": "2",
"stake": 20
}
]
}"#;
#[test]
fn test_current_json_deserialized_into_current_message() {
let json = CURRENT_JSON;
let message: CertificateMetadataMessagePart = serde_json::from_str(json).expect(
"This JSON is expected to be successfully parsed into a CertificateMetadataMessagePart instance.",
);
assert_eq!(golden_message_current(), message);
}
}