Skip to content

Commit f7d25dd

Browse files
committed
Add missing open message tests
1 parent 07942ed commit f7d25dd

File tree

4 files changed

+234
-176
lines changed

4 files changed

+234
-176
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod signer;
77
mod signer_registration;
88
mod single_signature;
99
mod stake_pool;
10+
#[cfg(test)]
11+
mod test_helper;
1012

1113
pub use certificate::*;
1214
pub use epoch_setting::*;
@@ -16,3 +18,5 @@ pub use signer::*;
1618
pub use signer_registration::*;
1719
pub use single_signature::*;
1820
pub use stake_pool::*;
21+
#[cfg(test)]
22+
pub use test_helper::*;

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

Lines changed: 134 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -330,89 +330,6 @@ impl<'client> Provider<'client> for DeleteOpenMessageProvider<'client> {
330330
}
331331
}
332332

333-
/// ## Open message repository
334-
///
335-
/// This is a business oriented layer to perform actions on the database through
336-
/// providers.
337-
pub struct OpenMessageRepository {
338-
connection: Arc<Mutex<Connection>>,
339-
}
340-
341-
impl OpenMessageRepository {
342-
/// Instanciate service
343-
pub fn new(connection: Arc<Mutex<Connection>>) -> Self {
344-
Self { connection }
345-
}
346-
347-
/// Return the latest [OpenMessage] for the given Epoch and [SignedEntityType].
348-
pub async fn get_open_message(
349-
&self,
350-
signed_entity_type: &SignedEntityType,
351-
) -> StdResult<Option<OpenMessage>> {
352-
let lock = self.connection.lock().await;
353-
let provider = OpenMessageProvider::new(&lock);
354-
let filters = provider
355-
.get_epoch_condition(signed_entity_type.get_epoch())
356-
.and_where(provider.get_signed_entity_type_condition(signed_entity_type));
357-
let mut messages = provider.find(filters)?;
358-
359-
Ok(messages.next())
360-
}
361-
362-
/// Create a new [OpenMessage] in the database.
363-
pub async fn create_open_message(
364-
&self,
365-
epoch: Epoch,
366-
signed_entity_type: &SignedEntityType,
367-
protocol_message: &ProtocolMessage,
368-
) -> StdResult<OpenMessage> {
369-
let lock = self.connection.lock().await;
370-
let provider = InsertOpenMessageProvider::new(&lock);
371-
let filters = provider.get_insert_condition(epoch, signed_entity_type, protocol_message)?;
372-
let mut cursor = provider.find(filters)?;
373-
374-
cursor
375-
.next()
376-
.ok_or_else(|| panic!("Inserting an open_message should not return nothing."))
377-
}
378-
379-
/// Updates an [OpenMessage] in the database.
380-
pub async fn update_open_message(&self, open_message: &OpenMessage) -> StdResult<OpenMessage> {
381-
let lock = self.connection.lock().await;
382-
let provider = UpdateOpenMessageProvider::new(&lock);
383-
let filters = provider.get_update_condition(open_message)?;
384-
let mut cursor = provider.find(filters)?;
385-
386-
cursor
387-
.next()
388-
.ok_or_else(|| panic!("Updating an open_message should not return nothing."))
389-
}
390-
391-
/// Remove all the [OpenMessage] for the given Epoch in the database.
392-
/// It returns the number of messages removed.
393-
pub async fn clean_epoch(&self, epoch: Epoch) -> StdResult<usize> {
394-
let lock = self.connection.lock().await;
395-
let provider = DeleteOpenMessageProvider::new(&lock);
396-
let filters = provider.get_epoch_condition(epoch);
397-
let cursor = provider.find(filters)?;
398-
399-
Ok(cursor.count())
400-
}
401-
402-
/// Return an open message with its associated single signatures if any.
403-
pub async fn get_open_message_with_single_signatures(
404-
&self,
405-
signed_entity_type: &SignedEntityType,
406-
) -> StdResult<Option<OpenMessageWithSingleSignatures>> {
407-
let lock = self.connection.lock().await;
408-
let provider = OpenMessageWithSingleSignaturesProvider::new(&lock);
409-
let filters = provider.get_signed_entity_type_condition(signed_entity_type);
410-
let mut messages = provider.find(filters)?;
411-
412-
Ok(messages.next())
413-
}
414-
}
415-
416333
/// Open Message with associated single signatures if any.
417334
#[derive(Debug, Clone)]
418335
pub struct OpenMessageWithSingleSignatures {
@@ -550,12 +467,99 @@ order by open_message.created_at desc, open_message.rowid desc
550467
}
551468
}
552469

470+
/// ## Open message repository
471+
///
472+
/// This is a business oriented layer to perform actions on the database through
473+
/// providers.
474+
pub struct OpenMessageRepository {
475+
connection: Arc<Mutex<Connection>>,
476+
}
477+
478+
impl OpenMessageRepository {
479+
/// Instanciate service
480+
pub fn new(connection: Arc<Mutex<Connection>>) -> Self {
481+
Self { connection }
482+
}
483+
484+
/// Return the latest [OpenMessage] for the given Epoch and [SignedEntityType].
485+
pub async fn get_open_message(
486+
&self,
487+
signed_entity_type: &SignedEntityType,
488+
) -> StdResult<Option<OpenMessage>> {
489+
let lock = self.connection.lock().await;
490+
let provider = OpenMessageProvider::new(&lock);
491+
let filters = provider
492+
.get_epoch_condition(signed_entity_type.get_epoch())
493+
.and_where(provider.get_signed_entity_type_condition(signed_entity_type));
494+
let mut messages = provider.find(filters)?;
495+
496+
Ok(messages.next())
497+
}
498+
499+
/// Create a new [OpenMessage] in the database.
500+
pub async fn create_open_message(
501+
&self,
502+
epoch: Epoch,
503+
signed_entity_type: &SignedEntityType,
504+
protocol_message: &ProtocolMessage,
505+
) -> StdResult<OpenMessage> {
506+
let lock = self.connection.lock().await;
507+
let provider = InsertOpenMessageProvider::new(&lock);
508+
let filters = provider.get_insert_condition(epoch, signed_entity_type, protocol_message)?;
509+
let mut cursor = provider.find(filters)?;
510+
511+
cursor
512+
.next()
513+
.ok_or_else(|| panic!("Inserting an open_message should not return nothing."))
514+
}
515+
516+
/// Updates an [OpenMessage] in the database.
517+
pub async fn update_open_message(&self, open_message: &OpenMessage) -> StdResult<OpenMessage> {
518+
let lock = self.connection.lock().await;
519+
let provider = UpdateOpenMessageProvider::new(&lock);
520+
let filters = provider.get_update_condition(open_message)?;
521+
let mut cursor = provider.find(filters)?;
522+
523+
cursor
524+
.next()
525+
.ok_or_else(|| panic!("Updating an open_message should not return nothing."))
526+
}
527+
528+
/// Remove all the [OpenMessage] for the given Epoch in the database.
529+
/// It returns the number of messages removed.
530+
pub async fn clean_epoch(&self, epoch: Epoch) -> StdResult<usize> {
531+
let lock = self.connection.lock().await;
532+
let provider = DeleteOpenMessageProvider::new(&lock);
533+
let filters = provider.get_epoch_condition(epoch);
534+
let cursor = provider.find(filters)?;
535+
536+
Ok(cursor.count())
537+
}
538+
539+
/// Return an open message with its associated single signatures if any.
540+
pub async fn get_open_message_with_single_signatures(
541+
&self,
542+
signed_entity_type: &SignedEntityType,
543+
) -> StdResult<Option<OpenMessageWithSingleSignatures>> {
544+
let lock = self.connection.lock().await;
545+
let provider = OpenMessageWithSingleSignaturesProvider::new(&lock);
546+
let filters = provider.get_signed_entity_type_condition(signed_entity_type);
547+
let mut messages = provider.find(filters)?;
548+
549+
Ok(messages.next())
550+
}
551+
}
552+
553553
#[cfg(test)]
554554
mod tests {
555555
use mithril_common::{entities::Beacon, sqlite::SourceAlias};
556556

557557
use crate::{dependency_injection::DependenciesBuilder, Configuration};
558558

559+
use crate::database::provider::test_helper::{
560+
setup_single_signature_db, setup_single_signature_records,
561+
};
562+
559563
use super::*;
560564

561565
#[test]
@@ -824,9 +828,52 @@ mod tests {
824828
assert_eq!(2, count);
825829
}
826830

827-
/*
828831
#[tokio::test]
829-
async fn repository_get_open_message_with_single_signatures() {
830-
todo!()
831-
} */
832+
async fn repository_get_open_message_with_single_signatures_when_signatures_exist() {
833+
let single_signature_records = setup_single_signature_records(1, 1, 4);
834+
835+
let connection = Connection::open(":memory:").unwrap();
836+
setup_single_signature_db(&connection, single_signature_records.clone()).unwrap();
837+
let repository = OpenMessageRepository::new(Arc::new(Mutex::new(connection)));
838+
839+
let mut open_message = OpenMessage::dummy();
840+
open_message.open_message_id = single_signature_records[0].open_message_id;
841+
repository.update_open_message(&open_message).await.unwrap();
842+
843+
let open_message_with_single_signatures = repository
844+
.get_open_message_with_single_signatures(&open_message.signed_entity_type)
845+
.await
846+
.unwrap()
847+
.unwrap();
848+
assert_eq!(
849+
4,
850+
open_message_with_single_signatures.single_signatures.len()
851+
)
852+
}
853+
854+
#[tokio::test]
855+
async fn repository_get_open_message_with_single_signatures_when_signatures_not_exist() {
856+
let connection = Connection::open(":memory:").unwrap();
857+
setup_single_signature_db(&connection, Vec::new()).unwrap();
858+
let repository = OpenMessageRepository::new(Arc::new(Mutex::new(connection)));
859+
860+
let open_message = OpenMessage::dummy();
861+
repository
862+
.create_open_message(
863+
open_message.epoch,
864+
&open_message.signed_entity_type,
865+
&open_message.protocol_message,
866+
)
867+
.await
868+
.unwrap();
869+
870+
let open_message_with_single_signatures = repository
871+
.get_open_message_with_single_signatures(&open_message.signed_entity_type)
872+
.await
873+
.unwrap()
874+
.unwrap();
875+
assert!(open_message_with_single_signatures
876+
.single_signatures
877+
.is_empty())
878+
}
832879
}

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

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'conn> UpdateSingleSignatureRecordProvider<'conn> {
216216
Self { connection }
217217
}
218218

219-
fn get_update_condition(
219+
pub(crate) fn get_update_condition(
220220
&self,
221221
single_signature_record: &SingleSignatureRecord,
222222
) -> WhereCondition {
@@ -303,97 +303,10 @@ impl SingleSignatureRepository {
303303
mod tests {
304304
use mithril_common::test_utils::fake_data;
305305

306-
use crate::database::migration::get_migrations;
306+
use crate::database::provider::{setup_single_signature_db, setup_single_signature_records};
307307

308308
use super::*;
309309

310-
fn setup_single_signature_records(
311-
total_epoch: u64,
312-
total_open_message: u64,
313-
total_signer: u64,
314-
) -> Vec<SingleSignatureRecord> {
315-
let mut single_signature_records = Vec::new();
316-
for epoch in 1..=total_epoch {
317-
for open_message_idx in 1..=total_open_message {
318-
let open_message_id = Uuid::new_v4();
319-
for signer_idx in 1..=total_signer {
320-
let single_signature_id = epoch
321-
+ (epoch + 1) * open_message_idx
322-
+ (epoch + 1) * (open_message_idx + 1) * signer_idx;
323-
single_signature_records.push(SingleSignatureRecord {
324-
open_message_id,
325-
signer_id: format!("signer-{signer_idx}"),
326-
registration_epoch_setting_id: Epoch(epoch),
327-
lottery_indexes: (1..=single_signature_id).collect(),
328-
signature: format!("signature-{single_signature_id}"),
329-
created_at: format!("created-at-{single_signature_id}"),
330-
});
331-
}
332-
}
333-
}
334-
single_signature_records
335-
}
336-
337-
pub fn setup_single_signature_db(
338-
connection: &Connection,
339-
single_signature_records: Vec<SingleSignatureRecord>,
340-
) -> Result<(), StdError> {
341-
for migration in get_migrations() {
342-
connection.execute(&migration.alterations)?;
343-
}
344-
345-
if single_signature_records.is_empty() {
346-
return Ok(());
347-
}
348-
349-
let query = {
350-
// leverage the expanded parameter from this provider which is unit
351-
// tested on its own above.
352-
let update_provider = UpdateSingleSignatureRecordProvider::new(connection);
353-
let (sql_values, _) = update_provider
354-
.get_update_condition(single_signature_records.first().unwrap())
355-
.expand();
356-
format!("insert into single_signature {sql_values}")
357-
};
358-
359-
for single_signature_record in single_signature_records {
360-
let mut statement = connection.prepare(&query)?;
361-
362-
statement
363-
.bind(
364-
1,
365-
single_signature_record.open_message_id.to_string().as_str(),
366-
)
367-
.unwrap();
368-
statement
369-
.bind(2, single_signature_record.signer_id.as_str())
370-
.unwrap();
371-
statement
372-
.bind(
373-
3,
374-
single_signature_record.registration_epoch_setting_id.0 as i64,
375-
)
376-
.unwrap();
377-
statement
378-
.bind(
379-
4,
380-
serde_json::to_string(&single_signature_record.lottery_indexes)
381-
.unwrap()
382-
.as_str(),
383-
)
384-
.unwrap();
385-
statement
386-
.bind(5, single_signature_record.signature.as_str())
387-
.unwrap();
388-
statement
389-
.bind(6, single_signature_record.created_at.as_str())
390-
.unwrap();
391-
statement.next().unwrap();
392-
}
393-
394-
Ok(())
395-
}
396-
397310
#[test]
398311
fn test_convert_single_signatures() {
399312
let single_signature = fake_data::single_signatures(vec![1, 3, 4, 6, 7, 9]);

0 commit comments

Comments
 (0)