diff --git a/beacon_chain/sync/light_client_manager.nim b/beacon_chain/sync/light_client_manager.nim index f23fa3a46a..86f3cc8209 100644 --- a/beacon_chain/sync/light_client_manager.nim +++ b/beacon_chain/sync/light_client_manager.nim @@ -332,7 +332,10 @@ template query[E]( # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.9/specs/altair/light-client/light-client.md#light-client-sync-process proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} = - var nextSyncTaskTime = self.getBeaconTime() + var + nextSyncTaskTime = self.getBeaconTime() + wasGossipSupported = false + haveFinalityUpdate = false while true: # Periodically wake and check for changes let wallTime = self.getBeaconTime() @@ -373,16 +376,32 @@ proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} = await self.query(UpdatesByRange, (startPeriod: syncTask.startPeriod, count: syncTask.count)) of LcSyncKind.FinalityUpdate: + haveFinalityUpdate = true await self.query(FinalityUpdate) of LcSyncKind.OptimisticUpdate: - await self.query(OptimisticUpdate) - - nextSyncTaskTime = wallTime + self.rng.nextLcSyncTaskDelay( - wallTime, - finalized = self.getFinalizedPeriod(), - optimistic = self.getOptimisticPeriod(), - isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown(), - didLatestSyncTaskProgress = didProgress) + if not haveFinalityUpdate: + haveFinalityUpdate = true + await self.query(FinalityUpdate) + else: + await self.query(OptimisticUpdate) + + let + finalized = self.getFinalizedPeriod() + optimistic = self.getOptimisticPeriod() + isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown() + isGossipSupported = + current.isGossipSupported(finalized, isNextSyncCommitteeKnown) + nextSyncTaskTime = + if not wasGossipSupported and isGossipSupported: + # Obtain an extra finality update after finishing sync + # to avoid having to wait several minutes for finality gossip + haveFinalityUpdate = false + wallTime + else: + wallTime + self.rng.nextLcSyncTaskDelay( + wallTime, finalized, optimistic, isNextSyncCommitteeKnown, + didLatestSyncTaskProgress = didProgress) + wasGossipSupported = isGossipSupported proc start*(self: var LightClientManager) = ## Start light client manager's loop.