1717//! - if it doesn't exist, it is inserted
1818//! 5. End
1919//!
20- use anyhow:: anyhow;
20+ use anyhow:: { Context , anyhow} ;
2121use async_trait:: async_trait;
22- use slog:: Logger ;
22+ use slog:: { Logger , debug , info } ;
2323use std:: sync:: Arc ;
2424
2525use mithril_common:: StdResult ;
@@ -111,9 +111,7 @@ impl MithrilCertificateChainSynchronizer {
111111
112112 async fn store_certificate_chain ( & self , certificate_chain : Vec < Certificate > ) -> StdResult < ( ) > {
113113 for certificate in certificate_chain {
114- self . certificate_storer
115- . insert_or_replace ( & certificate)
116- . await ?;
114+ self . certificate_storer . insert_or_replace ( & certificate) . await ?;
117115 }
118116 Ok ( ( ) )
119117 }
@@ -122,20 +120,34 @@ impl MithrilCertificateChainSynchronizer {
122120#[ async_trait]
123121impl CertificateChainSynchronizer for MithrilCertificateChainSynchronizer {
124122 async fn synchronize_certificate_chain ( & self , force : bool ) -> StdResult < ( ) > {
125- if !self . should_sync ( force) . await ? {
123+ debug ! ( self . logger, ">> synchronize_certificate_chain" ; "force" => force) ;
124+ if !self . should_sync ( force) . await . with_context ( || {
125+ format ! ( "Failed to check if certificate chain should be sync (force: `{force}`)" )
126+ } ) ? {
126127 return Ok ( ( ) ) ;
127128 }
129+ info ! ( self . logger, "Start synchronizing certificate chain" ) ;
128130
129131 let starting_point = self
130132 . remote_certificate_retriever
131133 . get_latest_certificate_details ( )
132134 . await ?
133- . ok_or ( anyhow ! ( "Remote aggregator doesn't have a chain yet" ) ) ?;
135+ . ok_or (
136+ anyhow ! ( "Remote aggregator doesn't have a chain yet" )
137+ . context ( "Failed to retrieve latest remote certificate details" ) ,
138+ ) ?;
134139 let remote_certificate_chain = self
135140 . retrieve_and_validate_remote_certificate_chain ( starting_point)
136- . await ?;
137- self . store_certificate_chain ( remote_certificate_chain) . await ?;
138-
141+ . await
142+ . with_context ( || "Failed to retrieve and validate remote certificate chain" ) ?;
143+ self . store_certificate_chain ( remote_certificate_chain)
144+ . await
145+ . with_context ( || "Failed to store remote retrieved certificate chain" ) ?;
146+
147+ info ! (
148+ self . logger,
149+ "Certificate chain synchronized with remote source"
150+ ) ;
139151 Ok ( ( ) )
140152 }
141153}
@@ -145,7 +157,9 @@ mod tests {
145157 use anyhow:: anyhow;
146158
147159 use mithril_common:: crypto_helper:: ProtocolGenesisVerificationKey ;
148- use mithril_common:: test_utils:: { fake_data, fake_keys, mock_extensions:: MockBuilder } ;
160+ use mithril_common:: test_utils:: {
161+ CertificateChainBuilder , fake_data, fake_keys, mock_extensions:: MockBuilder ,
162+ } ;
149163
150164 use crate :: services:: { MockRemoteCertificateRetriever , MockSynchronizedCertificateStorer } ;
151165 use crate :: test_tools:: TestLogger ;
@@ -349,11 +363,12 @@ mod tests {
349363 }
350364
351365 mod retrieve_validate_remote_certificate_chain {
352- use super :: * ;
353366 use mithril_common:: certificate_chain:: {
354367 FakeCertificaterRetriever , MithrilCertificateVerifier ,
355368 } ;
356369
370+ use super :: * ;
371+
357372 fn fake_verifier ( remote_certificate_chain : & [ Certificate ] ) -> Arc < dyn CertificateVerifier > {
358373 let verifier = MithrilCertificateVerifier :: new (
359374 TestLogger :: stdout ( ) ,
0 commit comments