Skip to content

Commit e262899

Browse files
authored
Merge pull request #879 from input-output-hk/ensemble/878-open_message_domain_type
Add open message domain type
2 parents 0a209ca + e08136b commit e262899

File tree

12 files changed

+254
-96
lines changed

12 files changed

+254
-96
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/certifier_service.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ use thiserror::Error;
2020
use tokio::sync::RwLock;
2121

2222
use crate::database::provider::{
23-
CertificateRepository, OpenMessage, OpenMessageRepository, OpenMessageWithSingleSignatures,
24-
SingleSignatureRepository,
23+
CertificateRepository, OpenMessageRecord, OpenMessageRepository,
24+
OpenMessageWithSingleSignaturesRecord, SingleSignatureRepository,
2525
};
26+
use crate::entities::OpenMessage;
2627
use crate::MultiSigner;
2728

2829
#[cfg(test)]
@@ -95,7 +96,7 @@ pub trait CertifierService: Sync + Send {
9596
async fn get_open_message(
9697
&self,
9798
signed_entity_type: &SignedEntityType,
98-
) -> StdResult<Option<OpenMessageWithSingleSignatures>>;
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:?}.");
@@ -223,32 +237,37 @@ 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<OpenMessageWithSingleSignatures>> {
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(
241259
&self,
242260
signed_entity_type: &SignedEntityType,
243261
) -> StdResult<Option<Certificate>> {
244262
debug!("CertifierService::create_certificate(signed_entity_type: {signed_entity_type:?})");
245-
let open_message = self
246-
.get_open_message(signed_entity_type)
263+
let open_message_record = self
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:?}.");
250268
CertifierServiceError::NotFound(signed_entity_type.clone())
251269
})?;
270+
let open_message: OpenMessage = open_message_record.clone().into();
252271

253272
if open_message.is_certified {
254273
warn!("CertifierService::create_certificate: open message {signed_entity_type:?} is already certified, cannot create certificate.");
@@ -325,7 +344,7 @@ impl CertifierService for MithrilCertifierService {
325344
.create_certificate(certificate)
326345
.await?;
327346

328-
let mut open_message_certified: OpenMessage = open_message.into();
347+
let mut open_message_certified: OpenMessageRecord = open_message_record.into();
329348
open_message_certified.is_certified = true;
330349
self.open_message_repository
331350
.update_open_message(&open_message_certified)
@@ -521,7 +540,8 @@ mod tests {
521540
let mut certifier_service = setup_certifier_service(&fixture, &epochs_with_signers).await;
522541
certifier_service.current_epoch = Arc::new(RwLock::new(epoch));
523542
let mut open_message = certifier_service
524-
.create_open_message(&signed_entity_type, &protocol_message)
543+
.open_message_repository
544+
.create_open_message(beacon.epoch, &signed_entity_type, &protocol_message)
525545
.await
526546
.unwrap();
527547
open_message.is_certified = true;

mithril-aggregator/src/database/provider/open_message.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use mithril_common::StdError;
22

3-
use mithril_common::entities::{PartyId, ProtocolMessage, SingleSignatures};
3+
use mithril_common::entities::{ProtocolMessage, SingleSignatures};
44
use mithril_common::{
55
entities::{Epoch, SignedEntityType},
66
sqlite::{HydrationError, Projection, SqLiteEntity, WhereCondition},
@@ -25,7 +25,7 @@ type StdResult<T> = Result<T, StdError>;
2525
/// generated if possible.
2626
#[allow(dead_code)]
2727
#[derive(Debug, Clone, PartialEq, Eq)]
28-
pub struct OpenMessage {
28+
pub struct OpenMessageRecord {
2929
/// OpenMessage unique identifier
3030
pub open_message_id: Uuid,
3131

@@ -45,7 +45,7 @@ pub struct OpenMessage {
4545
pub created_at: NaiveDateTime,
4646
}
4747

48-
impl OpenMessage {
48+
impl OpenMessageRecord {
4949
#[cfg(test)]
5050
/// Create a dumb OpenMessage instance mainly for test purposes
5151
pub fn dummy() -> Self {
@@ -64,8 +64,8 @@ impl OpenMessage {
6464
}
6565
}
6666

67-
impl From<OpenMessageWithSingleSignatures> for OpenMessage {
68-
fn from(value: OpenMessageWithSingleSignatures) -> Self {
67+
impl From<OpenMessageWithSingleSignaturesRecord> for OpenMessageRecord {
68+
fn from(value: OpenMessageWithSingleSignaturesRecord) -> Self {
6969
Self {
7070
open_message_id: value.open_message_id,
7171
epoch: value.epoch,
@@ -77,7 +77,7 @@ impl From<OpenMessageWithSingleSignatures> for OpenMessage {
7777
}
7878
}
7979

80-
impl SqLiteEntity for OpenMessage {
80+
impl SqLiteEntity for OpenMessageRecord {
8181
fn hydrate(row: Row) -> Result<Self, HydrationError>
8282
where
8383
Self: Sized,
@@ -199,7 +199,7 @@ impl<'client> OpenMessageProvider<'client> {
199199
}
200200

201201
impl<'client> Provider<'client> for OpenMessageProvider<'client> {
202-
type Entity = OpenMessage;
202+
type Entity = OpenMessageRecord;
203203

204204
fn get_definition(&self, condition: &str) -> String {
205205
let aliases = SourceAlias::new(&[
@@ -245,7 +245,7 @@ impl<'client> InsertOpenMessageProvider<'client> {
245245
}
246246

247247
impl<'client> Provider<'client> for InsertOpenMessageProvider<'client> {
248-
type Entity = OpenMessage;
248+
type Entity = OpenMessageRecord;
249249

250250
fn get_connection(&'client self) -> &'client Connection {
251251
self.connection
@@ -267,7 +267,7 @@ impl<'client> UpdateOpenMessageProvider<'client> {
267267
Self { connection }
268268
}
269269

270-
fn get_update_condition(&self, open_message: &OpenMessage) -> StdResult<WhereCondition> {
270+
fn get_update_condition(&self, open_message: &OpenMessageRecord) -> StdResult<WhereCondition> {
271271
let expression = "(open_message_id, epoch_setting_id, beacon, signed_entity_type_id, protocol_message, is_certified) values (?*, ?*, ?*, ?*, ?*, ?*)";
272272
let beacon_str = open_message.signed_entity_type.get_json_beacon()?;
273273
let parameters = vec![
@@ -284,7 +284,7 @@ impl<'client> UpdateOpenMessageProvider<'client> {
284284
}
285285

286286
impl<'client> Provider<'client> for UpdateOpenMessageProvider<'client> {
287-
type Entity = OpenMessage;
287+
type Entity = OpenMessageRecord;
288288

289289
fn get_connection(&'client self) -> &'client Connection {
290290
self.connection
@@ -316,7 +316,7 @@ impl<'client> DeleteOpenMessageProvider<'client> {
316316
}
317317

318318
impl<'client> Provider<'client> for DeleteOpenMessageProvider<'client> {
319-
type Entity = OpenMessage;
319+
type Entity = OpenMessageRecord;
320320

321321
fn get_connection(&'client self) -> &'client Connection {
322322
self.connection
@@ -332,7 +332,7 @@ impl<'client> Provider<'client> for DeleteOpenMessageProvider<'client> {
332332

333333
/// Open Message with associated single signatures if any.
334334
#[derive(Debug, Clone)]
335-
pub struct OpenMessageWithSingleSignatures {
335+
pub struct OpenMessageWithSingleSignaturesRecord {
336336
/// OpenMessage unique identifier
337337
pub open_message_id: Uuid,
338338

@@ -355,17 +355,7 @@ pub struct OpenMessageWithSingleSignatures {
355355
pub created_at: NaiveDateTime,
356356
}
357357

358-
impl OpenMessageWithSingleSignatures {
359-
/// Gather all signers party_id for this open message
360-
pub fn get_signers_id(&self) -> Vec<PartyId> {
361-
self.single_signatures
362-
.iter()
363-
.map(|sig| sig.party_id.to_owned())
364-
.collect()
365-
}
366-
}
367-
368-
impl SqLiteEntity for OpenMessageWithSingleSignatures {
358+
impl SqLiteEntity for OpenMessageWithSingleSignaturesRecord {
369359
fn hydrate(row: Row) -> Result<Self, HydrationError>
370360
where
371361
Self: Sized,
@@ -378,7 +368,7 @@ impl SqLiteEntity for OpenMessageWithSingleSignatures {
378368
))
379369
})?;
380370

381-
let open_message = OpenMessage::hydrate(row)?;
371+
let open_message = OpenMessageRecord::hydrate(row)?;
382372

383373
let open_message = Self {
384374
open_message_id: open_message.open_message_id,
@@ -440,7 +430,7 @@ impl<'client> OpenMessageWithSingleSignaturesProvider<'client> {
440430
}
441431

442432
impl<'client> Provider<'client> for OpenMessageWithSingleSignaturesProvider<'client> {
443-
type Entity = OpenMessageWithSingleSignatures;
433+
type Entity = OpenMessageWithSingleSignaturesRecord;
444434

445435
fn get_definition(&self, condition: &str) -> String {
446436
let aliases = SourceAlias::new(&[
@@ -485,7 +475,7 @@ impl OpenMessageRepository {
485475
pub async fn get_open_message(
486476
&self,
487477
signed_entity_type: &SignedEntityType,
488-
) -> StdResult<Option<OpenMessage>> {
478+
) -> StdResult<Option<OpenMessageRecord>> {
489479
let lock = self.connection.lock().await;
490480
let provider = OpenMessageProvider::new(&lock);
491481
let filters = provider
@@ -502,7 +492,7 @@ impl OpenMessageRepository {
502492
epoch: Epoch,
503493
signed_entity_type: &SignedEntityType,
504494
protocol_message: &ProtocolMessage,
505-
) -> StdResult<OpenMessage> {
495+
) -> StdResult<OpenMessageRecord> {
506496
let lock = self.connection.lock().await;
507497
let provider = InsertOpenMessageProvider::new(&lock);
508498
let filters = provider.get_insert_condition(epoch, signed_entity_type, protocol_message)?;
@@ -514,7 +504,10 @@ impl OpenMessageRepository {
514504
}
515505

516506
/// Updates an [OpenMessage] in the database.
517-
pub async fn update_open_message(&self, open_message: &OpenMessage) -> StdResult<OpenMessage> {
507+
pub async fn update_open_message(
508+
&self,
509+
open_message: &OpenMessageRecord,
510+
) -> StdResult<OpenMessageRecord> {
518511
let lock = self.connection.lock().await;
519512
let provider = UpdateOpenMessageProvider::new(&lock);
520513
let filters = provider.get_update_condition(open_message)?;
@@ -540,7 +533,7 @@ impl OpenMessageRepository {
540533
pub async fn get_open_message_with_single_signatures(
541534
&self,
542535
signed_entity_type: &SignedEntityType,
543-
) -> StdResult<Option<OpenMessageWithSingleSignatures>> {
536+
) -> StdResult<Option<OpenMessageWithSingleSignaturesRecord>> {
544537
let lock = self.connection.lock().await;
545538
let provider = OpenMessageWithSingleSignaturesProvider::new(&lock);
546539
let filters = provider.get_signed_entity_type_condition(signed_entity_type);
@@ -564,7 +557,7 @@ mod tests {
564557

565558
#[test]
566559
fn open_message_with_single_signature_projection() {
567-
let projection = OpenMessageWithSingleSignatures::get_projection();
560+
let projection = OpenMessageWithSingleSignaturesRecord::get_projection();
568561
let aliases = SourceAlias::new(&[
569562
("{:open_message:}", "open_message"),
570563
("{:single_signature:}", "single_signature"),
@@ -578,7 +571,7 @@ mod tests {
578571

579572
#[test]
580573
fn open_message_projection() {
581-
let projection = OpenMessage::get_projection();
574+
let projection = OpenMessageRecord::get_projection();
582575
let aliases = SourceAlias::new(&[("{:open_message:}", "open_message")]);
583576

584577
assert_eq!(
@@ -659,7 +652,7 @@ mod tests {
659652
fn update_provider_condition() {
660653
let connection = Connection::open(":memory:").unwrap();
661654
let provider = UpdateOpenMessageProvider::new(&connection);
662-
let open_message = OpenMessage {
655+
let open_message = OpenMessageRecord {
663656
open_message_id: Uuid::new_v4(),
664657
epoch: Epoch(12),
665658
signed_entity_type: SignedEntityType::dummy(),
@@ -836,7 +829,7 @@ mod tests {
836829
setup_single_signature_db(&connection, single_signature_records.clone()).unwrap();
837830
let repository = OpenMessageRepository::new(Arc::new(Mutex::new(connection)));
838831

839-
let mut open_message = OpenMessage::dummy();
832+
let mut open_message = OpenMessageRecord::dummy();
840833
open_message.open_message_id = single_signature_records[0].open_message_id;
841834
repository.update_open_message(&open_message).await.unwrap();
842835

@@ -857,7 +850,7 @@ mod tests {
857850
setup_single_signature_db(&connection, Vec::new()).unwrap();
858851
let repository = OpenMessageRepository::new(Arc::new(Mutex::new(connection)));
859852

860-
let open_message = OpenMessage::dummy();
853+
let open_message = OpenMessageRecord::dummy();
861854
repository
862855
.create_open_message(
863856
open_message.epoch,

mithril-aggregator/src/database/provider/single_signature.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use mithril_common::StdError;
1616
use tokio::sync::Mutex;
1717
use uuid::Uuid;
1818

19-
use super::OpenMessage;
19+
use super::OpenMessageRecord;
2020

2121
/// SingleSignature record is the representation of a stored single_signature.
2222
#[derive(Debug, PartialEq, Clone)]
@@ -285,7 +285,7 @@ impl SingleSignatureRepository {
285285
pub async fn create_single_signature(
286286
&self,
287287
single_signature: &SingleSignatures,
288-
open_message: &OpenMessage,
288+
open_message: &OpenMessageRecord,
289289
) -> Result<SingleSignatureRecord, StdError> {
290290
let connection = self.connection.lock().await;
291291
let single_signature = SingleSignatureRecord::from_single_signatures(
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;

0 commit comments

Comments
 (0)