|
| 1 | +use mithril_common::entities::{Epoch, ProtocolMessage, SignedEntityType, SingleSignatures}; |
| 2 | + |
| 3 | +use crate::database::provider::{OpenMessageRecord, OpenMessageWithSingleSignaturesRecord}; |
| 4 | + |
| 5 | +/// ## OpenMessage |
| 6 | +/// |
| 7 | +/// An open message is a message open for signatures. Every signer may send a |
| 8 | +/// single signature for this message from which a multi signature will be |
| 9 | +/// generated if possible. |
| 10 | +#[derive(Debug, Clone, PartialEq)] |
| 11 | +pub struct OpenMessage { |
| 12 | + /// OpenMessage unique identifier |
| 13 | + // pub open_message_id: Uuid, // do we need it in the entity ? |
| 14 | + |
| 15 | + /// Epoch |
| 16 | + pub epoch: Epoch, |
| 17 | + |
| 18 | + /// Type of message |
| 19 | + pub signed_entity_type: SignedEntityType, |
| 20 | + |
| 21 | + /// Message used by the Mithril Protocol |
| 22 | + pub protocol_message: ProtocolMessage, |
| 23 | + |
| 24 | + /// Has this message been converted into a Certificate? |
| 25 | + pub is_certified: bool, |
| 26 | + |
| 27 | + /// associated single signatures |
| 28 | + pub single_signatures: Vec<SingleSignatures>, |
| 29 | +} |
| 30 | + |
| 31 | +impl OpenMessage { |
| 32 | + #[cfg(test)] |
| 33 | + /// Create a dumb OpenMessage instance mainly for test purposes |
| 34 | + pub fn dummy() -> Self { |
| 35 | + use mithril_common::test_utils::fake_data; |
| 36 | + |
| 37 | + let beacon = mithril_common::test_utils::fake_data::beacon(); |
| 38 | + let epoch = beacon.epoch; |
| 39 | + let signed_entity_type = SignedEntityType::CardanoImmutableFilesFull(beacon); |
| 40 | + |
| 41 | + Self { |
| 42 | + epoch, |
| 43 | + signed_entity_type, |
| 44 | + protocol_message: ProtocolMessage::new(), |
| 45 | + is_certified: false, |
| 46 | + single_signatures: vec![ |
| 47 | + fake_data::single_signatures(vec![1, 4, 5]), |
| 48 | + fake_data::single_signatures(vec![2, 3, 8]), |
| 49 | + ], |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl From<OpenMessageRecord> for OpenMessage { |
| 55 | + fn from(record: OpenMessageRecord) -> Self { |
| 56 | + Self { |
| 57 | + epoch: record.epoch, |
| 58 | + signed_entity_type: record.signed_entity_type, |
| 59 | + protocol_message: record.protocol_message, |
| 60 | + is_certified: record.is_certified, |
| 61 | + single_signatures: vec![], |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl From<OpenMessageWithSingleSignaturesRecord> for OpenMessage { |
| 67 | + fn from(record: OpenMessageWithSingleSignaturesRecord) -> Self { |
| 68 | + Self { |
| 69 | + epoch: record.epoch, |
| 70 | + signed_entity_type: record.signed_entity_type, |
| 71 | + protocol_message: record.protocol_message, |
| 72 | + is_certified: record.is_certified, |
| 73 | + single_signatures: record.single_signatures, |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod test { |
| 80 | + use crate::database::provider::{OpenMessageRecord, OpenMessageWithSingleSignaturesRecord}; |
| 81 | + use mithril_common::{ |
| 82 | + entities::{Epoch, ProtocolMessage, SignedEntityType}, |
| 83 | + test_utils::fake_data, |
| 84 | + }; |
| 85 | + use std::vec; |
| 86 | + use uuid::Uuid; |
| 87 | + |
| 88 | + use super::OpenMessage; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_from_record() { |
| 92 | + let record = OpenMessageRecord { |
| 93 | + open_message_id: Uuid::new_v4(), |
| 94 | + epoch: Epoch(1), |
| 95 | + signed_entity_type: SignedEntityType::dummy(), |
| 96 | + protocol_message: ProtocolMessage::default(), |
| 97 | + is_certified: false, |
| 98 | + created_at: chrono::Local::now().naive_local(), |
| 99 | + }; |
| 100 | + let expected = OpenMessage { |
| 101 | + epoch: Epoch(1), |
| 102 | + signed_entity_type: SignedEntityType::dummy(), |
| 103 | + protocol_message: ProtocolMessage::default(), |
| 104 | + is_certified: false, |
| 105 | + single_signatures: vec![], |
| 106 | + }; |
| 107 | + let result: OpenMessage = record.into(); |
| 108 | + |
| 109 | + assert_eq!(expected, result); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_from_record_with_single_signatures() { |
| 114 | + let record = OpenMessageWithSingleSignaturesRecord { |
| 115 | + open_message_id: Uuid::new_v4(), |
| 116 | + epoch: Epoch(1), |
| 117 | + signed_entity_type: SignedEntityType::dummy(), |
| 118 | + protocol_message: ProtocolMessage::default(), |
| 119 | + is_certified: false, |
| 120 | + created_at: chrono::Local::now().naive_local(), |
| 121 | + single_signatures: vec![fake_data::single_signatures(vec![1, 4, 5])], |
| 122 | + }; |
| 123 | + let expected = OpenMessage { |
| 124 | + epoch: Epoch(1), |
| 125 | + signed_entity_type: SignedEntityType::dummy(), |
| 126 | + protocol_message: ProtocolMessage::default(), |
| 127 | + is_certified: false, |
| 128 | + single_signatures: vec![fake_data::single_signatures(vec![1, 4, 5])], |
| 129 | + }; |
| 130 | + let result: OpenMessage = record.into(); |
| 131 | + |
| 132 | + assert_eq!(expected, result); |
| 133 | + } |
| 134 | +} |
0 commit comments