mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 19:51:47 +00:00
Gossip recently computed light client data (#7023)
This commit is contained in:
@@ -40,6 +40,10 @@ pub struct LightClientServerCache<T: BeaconChainTypes> {
|
||||
latest_written_current_sync_committee: RwLock<Option<Arc<SyncCommittee<T::EthSpec>>>>,
|
||||
/// Caches state proofs by block root
|
||||
prev_block_cache: Mutex<lru::LruCache<Hash256, LightClientCachedData<T::EthSpec>>>,
|
||||
/// Tracks the latest broadcasted finality update
|
||||
latest_broadcasted_finality_update: RwLock<Option<LightClientFinalityUpdate<T::EthSpec>>>,
|
||||
/// Tracks the latest broadcasted optimistic update
|
||||
latest_broadcasted_optimistic_update: RwLock<Option<LightClientOptimisticUpdate<T::EthSpec>>>,
|
||||
}
|
||||
|
||||
impl<T: BeaconChainTypes> LightClientServerCache<T> {
|
||||
@@ -49,6 +53,8 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
|
||||
latest_optimistic_update: None.into(),
|
||||
latest_light_client_update: None.into(),
|
||||
latest_written_current_sync_committee: None.into(),
|
||||
latest_broadcasted_finality_update: None.into(),
|
||||
latest_broadcasted_optimistic_update: None.into(),
|
||||
prev_block_cache: lru::LruCache::new(PREV_BLOCK_CACHE_SIZE).into(),
|
||||
}
|
||||
}
|
||||
@@ -334,10 +340,89 @@ impl<T: BeaconChainTypes> LightClientServerCache<T> {
|
||||
Ok(new_value)
|
||||
}
|
||||
|
||||
/// Checks if we've already broadcasted the latest finality update.
|
||||
/// If we haven't, update the `latest_broadcasted_finality_update` cache
|
||||
/// and return the latest finality update for broadcasting, else return `None`.
|
||||
pub fn should_broadcast_latest_finality_update(
|
||||
&self,
|
||||
) -> Option<LightClientFinalityUpdate<T::EthSpec>> {
|
||||
if let Some(latest_finality_update) = self.get_latest_finality_update() {
|
||||
let latest_broadcasted_finality_update = self.get_latest_broadcasted_finality_update();
|
||||
match latest_broadcasted_finality_update {
|
||||
Some(latest_broadcasted_finality_update) => {
|
||||
if latest_broadcasted_finality_update != latest_finality_update {
|
||||
self.set_latest_broadcasted_finality_update(latest_finality_update.clone());
|
||||
return Some(latest_finality_update);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.set_latest_broadcasted_finality_update(latest_finality_update.clone());
|
||||
return Some(latest_finality_update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_latest_finality_update(&self) -> Option<LightClientFinalityUpdate<T::EthSpec>> {
|
||||
self.latest_finality_update.read().clone()
|
||||
}
|
||||
|
||||
pub fn get_latest_broadcasted_optimistic_update(
|
||||
&self,
|
||||
) -> Option<LightClientOptimisticUpdate<T::EthSpec>> {
|
||||
self.latest_broadcasted_optimistic_update.read().clone()
|
||||
}
|
||||
|
||||
pub fn get_latest_broadcasted_finality_update(
|
||||
&self,
|
||||
) -> Option<LightClientFinalityUpdate<T::EthSpec>> {
|
||||
self.latest_broadcasted_finality_update.read().clone()
|
||||
}
|
||||
|
||||
pub fn set_latest_broadcasted_optimistic_update(
|
||||
&self,
|
||||
optimistic_update: LightClientOptimisticUpdate<T::EthSpec>,
|
||||
) {
|
||||
*self.latest_broadcasted_optimistic_update.write() = Some(optimistic_update.clone());
|
||||
}
|
||||
|
||||
pub fn set_latest_broadcasted_finality_update(
|
||||
&self,
|
||||
finality_update: LightClientFinalityUpdate<T::EthSpec>,
|
||||
) {
|
||||
*self.latest_broadcasted_finality_update.write() = Some(finality_update.clone());
|
||||
}
|
||||
|
||||
/// Checks if we've already broadcasted the latest optimistic update.
|
||||
/// If we haven't, update the `latest_broadcasted_optimistic_update` cache
|
||||
/// and return the latest optimistic update for broadcasting, else return `None`.
|
||||
pub fn should_broadcast_latest_optimistic_update(
|
||||
&self,
|
||||
) -> Option<LightClientOptimisticUpdate<T::EthSpec>> {
|
||||
if let Some(latest_optimistic_update) = self.get_latest_optimistic_update() {
|
||||
let latest_broadcasted_optimistic_update =
|
||||
self.get_latest_broadcasted_optimistic_update();
|
||||
match latest_broadcasted_optimistic_update {
|
||||
Some(latest_broadcasted_optimistic_update) => {
|
||||
if latest_broadcasted_optimistic_update != latest_optimistic_update {
|
||||
self.set_latest_broadcasted_optimistic_update(
|
||||
latest_optimistic_update.clone(),
|
||||
);
|
||||
return Some(latest_optimistic_update);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.set_latest_broadcasted_optimistic_update(latest_optimistic_update.clone());
|
||||
return Some(latest_optimistic_update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_latest_optimistic_update(&self) -> Option<LightClientOptimisticUpdate<T::EthSpec>> {
|
||||
self.latest_optimistic_update.read().clone()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user