Skip to content

Commit ee585f7

Browse files
committed
feat(aggregator): implement logic to store retrieved chain
1 parent 34b578b commit ee585f7

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ impl MithrilCertificateChainSynchronizer {
110110
}
111111

112112
async fn store_certificate_chain(&self, certificate_chain: Vec<Certificate>) -> StdResult<()> {
113+
for certificate in certificate_chain {
114+
self.certificate_storer
115+
.insert_or_replace(&certificate)
116+
.await?;
117+
}
113118
Ok(())
114119
}
115120
}
@@ -410,4 +415,75 @@ mod tests {
410415
assert_eq!(remote_certificate_chain, expected);
411416
}
412417
}
418+
419+
mod store_remote_certificate_chain {
420+
use std::sync::RwLock;
421+
422+
use super::*;
423+
424+
#[derive(Default)]
425+
struct DumbCertificateStorer {
426+
certificates: RwLock<Vec<Certificate>>,
427+
}
428+
429+
impl DumbCertificateStorer {
430+
fn stored_certificates(&self) -> Vec<Certificate> {
431+
self.certificates.read().unwrap().clone()
432+
}
433+
}
434+
435+
#[async_trait]
436+
impl SynchronizedCertificateStorer for DumbCertificateStorer {
437+
async fn insert_or_replace(&self, certificate: &Certificate) -> StdResult<()> {
438+
let mut certificates = self.certificates.write().unwrap();
439+
certificates.push(certificate.clone());
440+
Ok(())
441+
}
442+
443+
async fn get_latest_genesis(&self) -> StdResult<Option<Certificate>> {
444+
unimplemented!("not needed in store_remote_certificate_chain tests")
445+
}
446+
}
447+
448+
#[tokio::test]
449+
async fn do_store_given_certificates() {
450+
let certificates_chain = vec![
451+
fake_data::genesis_certificate("genesis"),
452+
fake_data::certificate("certificate1"),
453+
fake_data::certificate("certificate2"),
454+
];
455+
let storer = Arc::new(DumbCertificateStorer::default());
456+
let synchroniser = MithrilCertificateChainSynchronizer {
457+
certificate_storer: storer.clone(),
458+
..MithrilCertificateChainSynchronizer::default_for_test()
459+
};
460+
461+
assert_eq!(Vec::<Certificate>::new(), storer.stored_certificates());
462+
463+
synchroniser
464+
.store_certificate_chain(certificates_chain.clone())
465+
.await
466+
.unwrap();
467+
468+
assert_eq!(certificates_chain, storer.stored_certificates());
469+
}
470+
471+
#[tokio::test]
472+
async fn fail_on_storer_error() {
473+
let synchroniser = MithrilCertificateChainSynchronizer {
474+
certificate_storer: MockBuilder::<MockSynchronizedCertificateStorer>::configure(
475+
|mock| {
476+
mock.expect_insert_or_replace()
477+
.return_once(move |_| Err(anyhow!("failure")));
478+
},
479+
),
480+
..MithrilCertificateChainSynchronizer::default_for_test()
481+
};
482+
483+
synchroniser
484+
.store_certificate_chain(vec![fake_data::certificate("certificate")])
485+
.await
486+
.unwrap_err();
487+
}
488+
}
413489
}

0 commit comments

Comments
 (0)