@@ -40,6 +40,10 @@ pub struct LightClientServerCache<T: BeaconChainTypes> {
4040 latest_written_current_sync_committee : RwLock < Option < Arc < SyncCommittee < T :: EthSpec > > > > ,
4141 /// Caches state proofs by block root
4242 prev_block_cache : Mutex < lru:: LruCache < Hash256 , LightClientCachedData < T :: EthSpec > > > ,
43+ /// Tracks the latest broadcasted finality update
44+ latest_broadcasted_finality_update : RwLock < Option < LightClientFinalityUpdate < T :: EthSpec > > > ,
45+ /// Tracks the latest broadcasted optimistic update
46+ latest_broadcasted_optimistic_update : RwLock < Option < LightClientOptimisticUpdate < T :: EthSpec > > > ,
4347}
4448
4549impl < T : BeaconChainTypes > LightClientServerCache < T > {
@@ -49,6 +53,8 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
4953 latest_optimistic_update : None . into ( ) ,
5054 latest_light_client_update : None . into ( ) ,
5155 latest_written_current_sync_committee : None . into ( ) ,
56+ latest_broadcasted_finality_update : None . into ( ) ,
57+ latest_broadcasted_optimistic_update : None . into ( ) ,
5258 prev_block_cache : lru:: LruCache :: new ( PREV_BLOCK_CACHE_SIZE ) . into ( ) ,
5359 }
5460 }
@@ -333,10 +339,66 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
333339 Ok ( new_value)
334340 }
335341
342+ pub fn should_broadcast_latest_finality_update (
343+ & self ,
344+ ) -> Option < LightClientFinalityUpdate < T :: EthSpec > > {
345+ if let Some ( latest_finality_update) = self . get_latest_finality_update ( ) {
346+ let latest_broadcasted_finality_update = self
347+ . latest_broadcasted_finality_update
348+ . read ( )
349+ . clone ( )
350+ . take ( ) ;
351+ match latest_broadcasted_finality_update {
352+ Some ( latest_broadcasted_finality_update) => {
353+ if latest_broadcasted_finality_update != latest_finality_update {
354+ * self . latest_broadcasted_finality_update . write ( ) =
355+ Some ( latest_finality_update. clone ( ) ) ;
356+ return Some ( latest_finality_update) ;
357+ }
358+ }
359+ None => {
360+ * self . latest_broadcasted_finality_update . write ( ) =
361+ Some ( latest_finality_update. clone ( ) ) ;
362+ return Some ( latest_finality_update) ;
363+ }
364+ }
365+ }
366+
367+ return None ;
368+ }
369+
336370 pub fn get_latest_finality_update ( & self ) -> Option < LightClientFinalityUpdate < T :: EthSpec > > {
337371 self . latest_finality_update . read ( ) . clone ( )
338372 }
339373
374+ /// Checks if we've already broadcasted the latest optimistic update.
375+ /// If we haven't, update the `latest_broadcasted_optimistic_update` cache
376+ /// and return the latest optimistic update for broadcasting
377+ pub fn should_broadcast_latest_optimistic_update (
378+ & self ,
379+ ) -> Option < LightClientOptimisticUpdate < T :: EthSpec > > {
380+ if let Some ( latest_optimistic_update) = self . get_latest_optimistic_update ( ) {
381+ let latest_broadcasted_optimistic_update =
382+ self . latest_optimistic_update . read ( ) . clone ( ) . take ( ) ;
383+ match latest_broadcasted_optimistic_update {
384+ Some ( latest_broadcasted_optimistic_update) => {
385+ if latest_broadcasted_optimistic_update != latest_optimistic_update {
386+ * self . latest_broadcasted_optimistic_update . write ( ) =
387+ Some ( latest_optimistic_update. clone ( ) ) ;
388+ return Some ( latest_optimistic_update) ;
389+ }
390+ }
391+ None => {
392+ * self . latest_broadcasted_optimistic_update . write ( ) =
393+ Some ( latest_optimistic_update. clone ( ) ) ;
394+ return Some ( latest_optimistic_update) ;
395+ }
396+ }
397+ }
398+
399+ return None ;
400+ }
401+
340402 pub fn get_latest_optimistic_update ( & self ) -> Option < LightClientOptimisticUpdate < T :: EthSpec > > {
341403 self . latest_optimistic_update . read ( ) . clone ( )
342404 }
0 commit comments