Skip to content

Commit f48f558

Browse files
committed
refactor(aggregator): make storage of sync certificates works on a batch
This allow implementor to use transactions if needed.
1 parent 4ec7378 commit f48f558

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

mithril-aggregator/src/services/certificate_chain_synchroniser/interface.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ pub trait RemoteCertificateRetriever: Sync + Send {
2929
#[cfg_attr(test, mockall::automock)]
3030
#[async_trait]
3131
pub trait SynchronizedCertificateStorer: Send + Sync {
32-
/// Insert a Certificate in the database, if it already exists, it will be deleted before inserting
33-
async fn insert_or_replace(&self, certificate: &Certificate) -> StdResult<()>;
32+
/// Insert a list of Certificates in the database, if some already exists, they will be deleted before inserting
33+
async fn insert_or_replace_many(&self, certificates: Vec<Certificate>) -> StdResult<()>;
34+
3435
/// Get the latest genesis Certificate
3536
async fn get_latest_genesis(&self) -> StdResult<Option<Certificate>>;
3637
}

mithril-aggregator/src/services/certificate_chain_synchroniser/synchroniser_service.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ impl MithrilCertificateChainSynchronizer {
131131
}
132132

133133
async fn store_certificate_chain(&self, certificate_chain: Vec<Certificate>) -> StdResult<()> {
134-
for certificate in certificate_chain {
135-
self.certificate_storer.insert_or_replace(&certificate).await?;
136-
}
134+
self.certificate_storer
135+
.insert_or_replace_many(certificate_chain)
136+
.await?;
137137
Ok(())
138138
}
139139
}
@@ -481,9 +481,12 @@ mod tests {
481481

482482
#[async_trait]
483483
impl SynchronizedCertificateStorer for DumbCertificateStorer {
484-
async fn insert_or_replace(&self, certificate: &Certificate) -> StdResult<()> {
484+
async fn insert_or_replace_many(
485+
&self,
486+
certificates_chain: Vec<Certificate>,
487+
) -> StdResult<()> {
485488
let mut certificates = self.certificates.write().unwrap();
486-
certificates.push(certificate.clone());
489+
*certificates = certificates_chain;
487490
Ok(())
488491
}
489492

@@ -520,7 +523,7 @@ mod tests {
520523
let synchroniser = MithrilCertificateChainSynchronizer {
521524
certificate_storer: MockBuilder::<MockSynchronizedCertificateStorer>::configure(
522525
|mock| {
523-
mock.expect_insert_or_replace()
526+
mock.expect_insert_or_replace_many()
524527
.return_once(move |_| Err(anyhow!("failure")));
525528
},
526529
),

0 commit comments

Comments
 (0)