From 6ec6b99ef6c64c0e373e9fcea2585217f4734f54 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Wed, 21 Jun 2023 08:28:14 +0200 Subject: [PATCH] extract `nextLightClientSyncTask` helper (#5108) The `nextLightClientSyncTask` helper is generic enough to be useful for contexts besides libp2p based light client sync. Extract it. --- beacon_chain/sync/light_client_manager.nim | 36 ++++++----------- .../sync/light_client_sync_helpers.nim | 40 +++++++++++++++++++ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/beacon_chain/sync/light_client_manager.nim b/beacon_chain/sync/light_client_manager.nim index 927966aeb..078e82a44 100644 --- a/beacon_chain/sync/light_client_manager.nim +++ b/beacon_chain/sync/light_client_manager.nim @@ -393,39 +393,29 @@ proc loop(self: LightClientManager) {.async.} = continue # Fetch updates - var allowWaitNextPeriod = false let - finalized = self.getFinalizedPeriod() - optimistic = self.getOptimisticPeriod() current = wallTime.slotOrZero().sync_committee_period - isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown() + + syncTask = nextLightClientSyncTask( + finalized = self.getFinalizedPeriod(), + optimistic = self.getOptimisticPeriod(), + current = current, + isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown()) didProgress = - if finalized == optimistic and not isNextSyncCommitteeKnown: - if finalized >= current: - await self.query(UpdatesByRange, ( - startPeriod: finalized, - count: 1'u64)) - else: - await self.query(UpdatesByRange, ( - startPeriod: finalized, - count: min(current - finalized, - MAX_REQUEST_LIGHT_CLIENT_UPDATES))) - elif finalized + 1 < current: - await self.query(UpdatesByRange, ( - startPeriod: finalized + 1, - count: min(current - (finalized + 1), - MAX_REQUEST_LIGHT_CLIENT_UPDATES))) - elif finalized != optimistic: + case syncTask.kind + of LcSyncKind.UpdatesByRange: + await self.query(UpdatesByRange, + (startPeriod: syncTask.startPeriod, count: syncTask.count)) + of LcSyncKind.FinalityUpdate: await self.query(FinalityUpdate) - else: - allowWaitNextPeriod = true + of LcSyncKind.OptimisticUpdate: await self.query(OptimisticUpdate) schedulingMode = if not didProgress or not self.isGossipSupported(current): Soon - elif not allowWaitNextPeriod: + elif syncTask.kind != LcSyncKind.OptimisticUpdate: CurrentPeriod else: NextPeriod diff --git a/beacon_chain/sync/light_client_sync_helpers.nim b/beacon_chain/sync/light_client_sync_helpers.nim index aa3d59e01..1d36efad3 100644 --- a/beacon_chain/sync/light_client_sync_helpers.nim +++ b/beacon_chain/sync/light_client_sync_helpers.nim @@ -48,3 +48,43 @@ func checkLightClientUpdates*( else: return err("Invalid context bytes") ok() + +type + LcSyncKind* {.pure.} = enum + UpdatesByRange + FinalityUpdate + OptimisticUpdate + + LcSyncTask* = object + case kind*: LcSyncKind + of LcSyncKind.UpdatesByRange: + startPeriod*: SyncCommitteePeriod + count*: uint64 + of LcSyncKind.FinalityUpdate, LcSyncKind.OptimisticUpdate: + discard + +func nextLightClientSyncTask*( + finalized: SyncCommitteePeriod, + optimistic: SyncCommitteePeriod, + current: SyncCommitteePeriod, + isNextSyncCommitteeKnown: bool): LcSyncTask = + if finalized == optimistic and not isNextSyncCommitteeKnown: + if finalized >= current: + LcSyncTask( + kind: LcSyncKind.UpdatesByRange, + startPeriod: finalized, + count: 1) + else: + LcSyncTask( + kind: LcSyncKind.UpdatesByRange, + startPeriod: finalized, + count: min(current - finalized, MAX_REQUEST_LIGHT_CLIENT_UPDATES)) + elif finalized + 1 < current: + LcSyncTask( + kind: LcSyncKind.UpdatesByRange, + startPeriod: finalized + 1, + count: min(current - (finalized + 1), MAX_REQUEST_LIGHT_CLIENT_UPDATES)) + elif finalized != optimistic: + LcSyncTask(kind: LcSyncKind.FinalityUpdate) + else: + LcSyncTask(kind: LcSyncKind.OptimisticUpdate)