Skip to content

Commit 8127ef0

Browse files
committed
Use open message domain type in Aggregator service & stage machine
1 parent 015c54c commit 8127ef0

File tree

7 files changed

+177
-21
lines changed

7 files changed

+177
-21
lines changed

mithril-aggregator/src/certifier_service.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::database::provider::{
2323
CertificateRepository, OpenMessageRecord, OpenMessageRepository,
2424
OpenMessageWithSingleSignaturesRecord, SingleSignatureRepository,
2525
};
26+
use crate::entities::OpenMessage;
2627
use crate::MultiSigner;
2728

2829
#[cfg(test)]
@@ -88,14 +89,14 @@ pub trait CertifierService: Sync + Send {
8889
&self,
8990
signed_entity_type: &SignedEntityType,
9091
protocol_message: &ProtocolMessage,
91-
) -> StdResult<OpenMessageRecord>;
92+
) -> StdResult<OpenMessage>;
9293

9394
/// Return the open message at the given Beacon. If the message does not
9495
/// exist, None is returned.
9596
async fn get_open_message(
9697
&self,
9798
signed_entity_type: &SignedEntityType,
98-
) -> StdResult<Option<OpenMessageWithSingleSignaturesRecord>>;
99+
) -> StdResult<Option<OpenMessage>>;
99100

100101
/// Create a certificate if possible. If the pointed open message does
101102
/// not exist or has been already certified, an error is raised. If a multi
@@ -147,6 +148,19 @@ impl MithrilCertifierService {
147148
_logger: logger,
148149
}
149150
}
151+
152+
async fn get_open_message_record(
153+
&self,
154+
signed_entity_type: &SignedEntityType,
155+
) -> StdResult<Option<OpenMessageWithSingleSignaturesRecord>> {
156+
debug!(
157+
"CertifierService::get_open_message_record(signed_entity_type: {signed_entity_type:?})"
158+
);
159+
160+
self.open_message_repository
161+
.get_open_message_with_single_signatures(signed_entity_type)
162+
.await
163+
}
150164
}
151165

152166
#[async_trait]
@@ -177,7 +191,7 @@ impl CertifierService for MithrilCertifierService {
177191
) -> StdResult<()> {
178192
debug!("CertifierService::register_single_signature(signed_entity_type: {signed_entity_type:?}, single_signatures: {signature:?}");
179193
let open_message = self
180-
.get_open_message(signed_entity_type)
194+
.get_open_message_record(signed_entity_type)
181195
.await?
182196
.ok_or_else(|| {
183197
warn!("CertifierService::register_single_signature: OpenMessage not found for type {signed_entity_type:?}.");
@@ -209,7 +223,7 @@ impl CertifierService for MithrilCertifierService {
209223
&self,
210224
signed_entity_type: &SignedEntityType,
211225
protocol_message: &ProtocolMessage,
212-
) -> StdResult<OpenMessageRecord> {
226+
) -> StdResult<OpenMessage> {
213227
debug!("CertifierService::create_open_message(signed_entity_type: {signed_entity_type:?}, protocol_message: {protocol_message:?})");
214228
let current_epoch = self.current_epoch.read().await;
215229

@@ -223,18 +237,22 @@ impl CertifierService for MithrilCertifierService {
223237
open_message.open_message_id
224238
);
225239

226-
Ok(open_message)
240+
Ok(open_message.into())
227241
}
228242

229243
async fn get_open_message(
230244
&self,
231245
signed_entity_type: &SignedEntityType,
232-
) -> StdResult<Option<OpenMessageWithSingleSignaturesRecord>> {
246+
) -> StdResult<Option<OpenMessage>> {
233247
debug!("CertifierService::get_open_message(signed_entity_type: {signed_entity_type:?})");
234248

235-
self.open_message_repository
249+
let open_message = self
250+
.open_message_repository
236251
.get_open_message_with_single_signatures(signed_entity_type)
237-
.await
252+
.await?
253+
.map(|record| record.into());
254+
255+
Ok(open_message)
238256
}
239257

240258
async fn create_certificate(
@@ -243,7 +261,7 @@ impl CertifierService for MithrilCertifierService {
243261
) -> StdResult<Option<Certificate>> {
244262
debug!("CertifierService::create_certificate(signed_entity_type: {signed_entity_type:?})");
245263
let open_message = self
246-
.get_open_message(signed_entity_type)
264+
.get_open_message_record(signed_entity_type)
247265
.await?
248266
.ok_or_else(|| {
249267
warn!("CertifierService::create_certificate: OpenMessage not found for type {signed_entity_type:?}.");
@@ -521,7 +539,8 @@ mod tests {
521539
let mut certifier_service = setup_certifier_service(&fixture, &epochs_with_signers).await;
522540
certifier_service.current_epoch = Arc::new(RwLock::new(epoch));
523541
let mut open_message = certifier_service
524-
.create_open_message(&signed_entity_type, &protocol_message)
542+
.open_message_repository
543+
.create_open_message(beacon.epoch, &signed_entity_type, &protocol_message)
525544
.await
526545
.unwrap();
527546
open_message.is_certified = true;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Entities module
2+
//!
3+
//! This module provide domain entities for the services & state machine.
4+
mod open_message;
5+
6+
pub use open_message::OpenMessage;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}

mithril-aggregator/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod configuration;
1818
pub mod database;
1919
mod dependency;
2020
pub mod dependency_injection;
21+
pub mod entities;
2122
pub mod event_store;
2223
mod http_server;
2324
mod message_adapters;

mithril-aggregator/src/runtime/runner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::path::Path;
1616
use std::path::PathBuf;
1717
use std::sync::Arc;
1818

19-
use crate::database::provider::OpenMessageRecord;
19+
use crate::entities::OpenMessage;
2020
use crate::snapshot_uploaders::SnapshotLocation;
2121
use crate::snapshotter::OngoingSnapshot;
2222
use crate::RuntimeError;
@@ -172,7 +172,7 @@ pub trait AggregatorRunnerTrait: Sync + Send {
172172
&self,
173173
signed_entity_type: &SignedEntityType,
174174
protocol_message: &ProtocolMessage,
175-
) -> Result<OpenMessageRecord, Box<dyn StdError + Sync + Send>>;
175+
) -> Result<OpenMessage, Box<dyn StdError + Sync + Send>>;
176176
}
177177

178178
/// The runner responsibility is to expose a code API for the state machine. It
@@ -614,7 +614,7 @@ impl AggregatorRunnerTrait for AggregatorRunner {
614614
&self,
615615
signed_entity_type: &SignedEntityType,
616616
protocol_message: &ProtocolMessage,
617-
) -> Result<OpenMessageRecord, Box<dyn StdError + Sync + Send>> {
617+
) -> Result<OpenMessage, Box<dyn StdError + Sync + Send>> {
618618
self.dependencies
619619
.certifier_service
620620
.create_open_message(signed_entity_type, protocol_message)

mithril-aggregator/src/runtime/state_machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ mod tests {
359359

360360
use std::path::Path;
361361

362-
use crate::database::provider::OpenMessageRecord;
362+
use crate::entities::OpenMessage;
363363
use crate::snapshotter::OngoingSnapshot;
364364

365365
use super::super::runner::MockAggregatorRunner;
@@ -607,7 +607,7 @@ mod tests {
607607
runner
608608
.expect_create_open_message()
609609
.once()
610-
.returning(|_, _| Ok(OpenMessageRecord::dummy()));
610+
.returning(|_, _| Ok(OpenMessage::dummy()));
611611

612612
let mut runtime = init_runtime(
613613
Some(AggregatorState::Ready(ReadyState {

mithril-aggregator/tests/test_extensions/open_message_observer.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use mithril_aggregator::{
2-
certifier_service::CertifierService, database::provider::OpenMessageWithSingleSignaturesRecord,
3-
};
1+
use mithril_aggregator::{certifier_service::CertifierService, entities::OpenMessage};
42
use mithril_common::{entities::SignedEntityType, BeaconProvider};
53
use std::sync::Arc;
64

@@ -23,9 +21,7 @@ impl OpenMessageObserver {
2321
}
2422

2523
// Get the current [open message][OpenMessageWithSingleSignatures] for [cardano immutables][SignedEntityType::CardanoImmutableFilesFull]
26-
pub async fn get_current_immutable_message(
27-
&self,
28-
) -> Result<Option<OpenMessageWithSingleSignaturesRecord>, String> {
24+
pub async fn get_current_immutable_message(&self) -> Result<Option<OpenMessage>, String> {
2925
let immutable_signer_entity_type = SignedEntityType::CardanoImmutableFilesFull(
3026
self.beacon_provider
3127
.get_current_beacon()

0 commit comments

Comments
 (0)