@@ -41,6 +41,25 @@ pub struct MithrilCertificateChainSynchronizer {
4141 logger : Logger ,
4242}
4343
44+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
45+ enum SyncStatus {
46+ Forced ,
47+ NoLocalGenesis ,
48+ RemoteGenesisMatchesLocalGenesis ,
49+ RemoteGenesisDontMatchesLocalGenesis ,
50+ }
51+
52+ impl SyncStatus {
53+ fn should_sync ( & self ) -> bool {
54+ match self {
55+ SyncStatus :: Forced => true ,
56+ SyncStatus :: NoLocalGenesis => true ,
57+ SyncStatus :: RemoteGenesisMatchesLocalGenesis => false ,
58+ SyncStatus :: RemoteGenesisDontMatchesLocalGenesis => true ,
59+ }
60+ }
61+ }
62+
4463impl MithrilCertificateChainSynchronizer {
4564 /// Create a new `MithrilCertificateChainSynchronizer` instance
4665 pub fn new (
@@ -59,9 +78,9 @@ impl MithrilCertificateChainSynchronizer {
5978 }
6079 }
6180
62- async fn should_sync ( & self , force : bool ) -> StdResult < bool > {
81+ async fn check_sync_state ( & self , force : bool ) -> StdResult < SyncStatus > {
6382 if force {
64- return Ok ( true ) ;
83+ return Ok ( SyncStatus :: Forced ) ;
6584 }
6685
6786 match self . certificate_storer . get_latest_genesis ( ) . await ? {
@@ -71,13 +90,15 @@ impl MithrilCertificateChainSynchronizer {
7190 . get_genesis_certificate_details ( )
7291 . await ?
7392 {
74- Some ( remote_genesis) => Ok ( local_genesis != remote_genesis) ,
93+ Some ( remote_genesis) if ( local_genesis == remote_genesis) => {
94+ Ok ( SyncStatus :: RemoteGenesisMatchesLocalGenesis )
95+ }
96+ Some ( _) => Ok ( SyncStatus :: RemoteGenesisDontMatchesLocalGenesis ) ,
7597 // The remote aggregator doesn't have a chain yet, we can't sync
7698 None => Err ( anyhow ! ( "Remote aggregator doesn't have a chain yet" ) ) ,
7799 }
78100 }
79- // No local genesis certificate found, always sync
80- None => Ok ( true ) ,
101+ None => Ok ( SyncStatus :: NoLocalGenesis ) ,
81102 }
82103 }
83104
@@ -121,12 +142,16 @@ impl MithrilCertificateChainSynchronizer {
121142impl CertificateChainSynchronizer for MithrilCertificateChainSynchronizer {
122143 async fn synchronize_certificate_chain ( & self , force : bool ) -> StdResult < ( ) > {
123144 debug ! ( self . logger, ">> synchronize_certificate_chain" ; "force" => force) ;
124- if !self . should_sync ( force) . await . with_context ( || {
145+
146+ let sync_state = self . check_sync_state ( force) . await . with_context ( || {
125147 format ! ( "Failed to check if certificate chain should be sync (force: `{force}`)" )
126- } ) ? {
148+ } ) ?;
149+ if sync_state. should_sync ( ) {
150+ info ! ( self . logger, "Start synchronizing certificate chain" ; "sync_state" => ?sync_state) ;
151+ } else {
152+ info ! ( self . logger, "No need to synchronize certificate chain" ; "sync_state" => ?sync_state) ;
127153 return Ok ( ( ) ) ;
128154 }
129- info ! ( self . logger, "Start synchronizing certificate chain" ) ;
130155
131156 let starting_point = self
132157 . remote_certificate_retriever
@@ -268,40 +293,71 @@ mod tests {
268293 } ;
269294 }
270295
271- mod should_sync {
296+ mod check_sync_state {
272297 use super :: * ;
273298
299+ #[ test]
300+ fn sync_state_should_sync ( ) {
301+ assert ! ( SyncStatus :: Forced . should_sync( ) ) ;
302+ assert ! ( !SyncStatus :: RemoteGenesisMatchesLocalGenesis . should_sync( ) ) ;
303+ assert ! ( SyncStatus :: RemoteGenesisDontMatchesLocalGenesis . should_sync( ) ) ;
304+ assert ! ( SyncStatus :: NoLocalGenesis . should_sync( ) ) ;
305+ }
306+
274307 #[ tokio:: test]
275- async fn should_sync_if_force_true ( ) {
308+ async fn state_when_force_true ( ) {
276309 let synchroniser = MithrilCertificateChainSynchronizer :: default_for_test ( ) ;
277310
278- let should_sync = synchroniser. should_sync ( true ) . await . unwrap ( ) ;
279- assert ! ( should_sync ) ;
311+ let sync_state = synchroniser. check_sync_state ( true ) . await . unwrap ( ) ;
312+ assert_eq ! ( SyncStatus :: Forced , sync_state ) ;
280313 }
281314
282315 #[ tokio:: test]
283- async fn should_sync_if_force_true_without_checking_genesis_certificate ( ) {
284- let synchroniser = mocked_synchroniser ! ( with_remote_genesis: Err ( anyhow!(
285- "should not fetch genesis"
286- ) ) ) ;
316+ async fn state_when_force_false_and_no_local_genesis_certificate_found ( ) {
317+ let synchroniser = mocked_synchroniser ! ( with_local_genesis: Ok ( None ) ) ;
287318
288- let should_sync = synchroniser. should_sync ( true ) . await . unwrap ( ) ;
289- assert ! ( should_sync ) ;
319+ let sync_state = synchroniser. check_sync_state ( false ) . await . unwrap ( ) ;
320+ assert_eq ! ( SyncStatus :: NoLocalGenesis , sync_state ) ;
290321 }
291322
292323 #[ tokio:: test]
293- async fn should_sync_if_false_and_no_local_genesis_certificate_found ( ) {
294- let synchroniser = mocked_synchroniser ! ( with_local_genesis: Ok ( None ) ) ;
324+ async fn state_when_force_false_and_remote_genesis_dont_matches_local_genesis ( ) {
325+ let synchroniser = mocked_synchroniser ! (
326+ with_remote_genesis: Ok ( Some ( fake_data:: genesis_certificate( "remote_genesis" ) ) ) ,
327+ with_local_genesis: Ok ( Some ( fake_data:: genesis_certificate( "local_genesis" ) ) )
328+ ) ;
295329
296- let should_sync = synchroniser. should_sync ( false ) . await . unwrap ( ) ;
297- assert ! ( should_sync) ;
330+ let sync_state = synchroniser. check_sync_state ( false ) . await . unwrap ( ) ;
331+ assert_eq ! ( SyncStatus :: RemoteGenesisDontMatchesLocalGenesis , sync_state) ;
332+ }
333+
334+ #[ tokio:: test]
335+ async fn state_when_force_false_and_remote_genesis_matches_local_genesis ( ) {
336+ let remote_genesis = fake_data:: genesis_certificate ( "genesis" ) ;
337+ let local_genesis = remote_genesis. clone ( ) ;
338+ let synchroniser = mocked_synchroniser ! (
339+ with_remote_genesis: Ok ( Some ( remote_genesis) ) ,
340+ with_local_genesis: Ok ( Some ( local_genesis) )
341+ ) ;
342+
343+ let sync_state = synchroniser. check_sync_state ( false ) . await . unwrap ( ) ;
344+ assert_eq ! ( SyncStatus :: RemoteGenesisMatchesLocalGenesis , sync_state) ;
345+ }
346+
347+ #[ tokio:: test]
348+ async fn if_force_true_it_should_not_fetch_remote_genesis_certificate ( ) {
349+ let synchroniser = mocked_synchroniser ! ( with_remote_genesis: Err ( anyhow!(
350+ "should not fetch genesis"
351+ ) ) ) ;
352+
353+ synchroniser. check_sync_state ( true ) . await . unwrap ( ) ;
298354 }
299355
300356 #[ tokio:: test]
301357 async fn should_abort_with_error_if_force_false_and_fails_to_retrieve_local_genesis ( ) {
302358 let synchroniser = mocked_synchroniser ! ( with_local_genesis: Err ( anyhow!( "failure" ) ) ) ;
303359 synchroniser
304- . should_sync ( false )
360+ . check_sync_state ( false )
305361 . await
306362 . expect_err ( "Expected an error but was:" ) ;
307363 }
@@ -313,7 +369,7 @@ mod tests {
313369 with_local_genesis: Ok ( Some ( fake_data:: genesis_certificate( "local_genesis" ) ) )
314370 ) ;
315371 synchroniser
316- . should_sync ( false )
372+ . check_sync_state ( false )
317373 . await
318374 . expect_err ( "Expected an error but was:" ) ;
319375 }
@@ -325,7 +381,7 @@ mod tests {
325381 with_local_genesis: Ok ( Some ( fake_data:: genesis_certificate( "local_genesis" ) ) )
326382 ) ;
327383 let error = synchroniser
328- . should_sync ( false )
384+ . check_sync_state ( false )
329385 . await
330386 . expect_err ( "Expected an error but was:" ) ;
331387
@@ -336,30 +392,6 @@ mod tests {
336392 "Unexpected error:\n {error:?}"
337393 ) ;
338394 }
339-
340- #[ tokio:: test]
341- async fn should_sync_if_force_false_and_remote_genesis_dont_matches_local_genesis ( ) {
342- let synchroniser = mocked_synchroniser ! (
343- with_remote_genesis: Ok ( Some ( fake_data:: genesis_certificate( "remote_genesis" ) ) ) ,
344- with_local_genesis: Ok ( Some ( fake_data:: genesis_certificate( "local_genesis" ) ) )
345- ) ;
346-
347- let should_sync = synchroniser. should_sync ( false ) . await . unwrap ( ) ;
348- assert ! ( should_sync) ;
349- }
350-
351- #[ tokio:: test]
352- async fn should_not_sync_if_force_false_and_remote_genesis_matches_local_genesis ( ) {
353- let remote_genesis = fake_data:: genesis_certificate ( "genesis" ) ;
354- let local_genesis = remote_genesis. clone ( ) ;
355- let synchroniser = mocked_synchroniser ! (
356- with_remote_genesis: Ok ( Some ( remote_genesis) ) ,
357- with_local_genesis: Ok ( Some ( local_genesis) )
358- ) ;
359-
360- let should_sync = synchroniser. should_sync ( false ) . await . unwrap ( ) ;
361- assert ! ( !should_sync) ;
362- }
363395 }
364396
365397 mod retrieve_validate_remote_certificate_chain {
0 commit comments