use chrono::{DateTime, Utc};
use mithril_common::entities::{
Epoch, PartyId, ProtocolMessage, SignedEntityType, SingleSignatures,
};
use crate::database::record::{OpenMessageRecord, OpenMessageWithSingleSignaturesRecord};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpenMessage {
pub epoch: Epoch,
pub signed_entity_type: SignedEntityType,
pub protocol_message: ProtocolMessage,
pub is_certified: bool,
pub is_expired: bool,
pub single_signatures: Vec<SingleSignatures>,
pub created_at: DateTime<Utc>,
pub expires_at: Option<DateTime<Utc>>,
}
impl OpenMessage {
pub fn get_signers_id(&self) -> Vec<PartyId> {
self.single_signatures
.iter()
.map(|sig| sig.party_id.to_owned())
.collect()
}
#[cfg(test)]
pub fn dummy() -> Self {
use mithril_common::test_utils::fake_data;
let beacon = fake_data::beacon();
let epoch = beacon.epoch;
let signed_entity_type = SignedEntityType::CardanoImmutableFilesFull(beacon);
Self {
epoch,
signed_entity_type,
protocol_message: ProtocolMessage::new(),
is_certified: false,
is_expired: false,
single_signatures: vec![
fake_data::single_signatures(vec![1, 4, 5]),
fake_data::single_signatures(vec![2, 3, 8]),
],
created_at: Utc::now(),
expires_at: None,
}
}
}
impl From<OpenMessageRecord> for OpenMessage {
fn from(record: OpenMessageRecord) -> Self {
Self {
epoch: record.epoch,
signed_entity_type: record.signed_entity_type,
protocol_message: record.protocol_message,
is_certified: record.is_certified,
is_expired: record.is_expired,
single_signatures: vec![],
created_at: record.created_at,
expires_at: record.expires_at,
}
}
}
impl From<OpenMessageWithSingleSignaturesRecord> for OpenMessage {
fn from(record: OpenMessageWithSingleSignaturesRecord) -> Self {
Self {
epoch: record.epoch,
signed_entity_type: record.signed_entity_type,
protocol_message: record.protocol_message,
is_certified: record.is_certified,
is_expired: record.is_expired,
single_signatures: record.single_signatures,
created_at: record.created_at,
expires_at: record.expires_at,
}
}
}
#[cfg(test)]
mod test {
use chrono::Utc;
use uuid::Uuid;
use mithril_common::{
entities::{Epoch, ProtocolMessage, SignedEntityType},
test_utils::fake_data,
};
use crate::database::record::{OpenMessageRecord, OpenMessageWithSingleSignaturesRecord};
use super::OpenMessage;
#[test]
fn test_from_record() {
let created_at = Utc::now();
let record = OpenMessageRecord {
open_message_id: Uuid::new_v4(),
epoch: Epoch(1),
signed_entity_type: SignedEntityType::dummy(),
protocol_message: ProtocolMessage::default(),
is_certified: false,
is_expired: false,
created_at,
expires_at: None,
};
let expected = OpenMessage {
epoch: Epoch(1),
signed_entity_type: SignedEntityType::dummy(),
protocol_message: ProtocolMessage::default(),
is_certified: false,
is_expired: false,
single_signatures: vec![],
created_at,
expires_at: None,
};
let result: OpenMessage = record.into();
assert_eq!(expected, result);
}
#[test]
fn test_from_record_with_single_signatures() {
let created_at = Utc::now();
let record = OpenMessageWithSingleSignaturesRecord {
open_message_id: Uuid::new_v4(),
epoch: Epoch(1),
signed_entity_type: SignedEntityType::dummy(),
protocol_message: ProtocolMessage::default(),
is_certified: false,
is_expired: false,
created_at,
expires_at: None,
single_signatures: vec![fake_data::single_signatures(vec![1, 4, 5])],
};
let expected = OpenMessage {
epoch: Epoch(1),
signed_entity_type: SignedEntityType::dummy(),
protocol_message: ProtocolMessage::default(),
is_certified: false,
is_expired: false,
single_signatures: vec![fake_data::single_signatures(vec![1, 4, 5])],
created_at,
expires_at: None,
};
let result: OpenMessage = record.into();
assert_eq!(expected, result);
}
}