99
1010import chronos, chronicles
1111import
12- eth/ p2p/ discoveryv5/ random2,
1312 ../ spec/ network,
1413 ../ networking/ eth2_network,
1514 ../ beacon_clock,
@@ -107,13 +106,9 @@ proc isGossipSupported*(
107106 if not self.isLightClientStoreInitialized ():
108107 return false
109108
110- let
111- finalizedPeriod = self.getFinalizedPeriod ()
112- isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown ()
113- if isNextSyncCommitteeKnown:
114- period <= finalizedPeriod + 1
115- else :
116- period <= finalizedPeriod
109+ period.isGossipSupported (
110+ finalizedPeriod = self.getFinalizedPeriod (),
111+ isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown ())
117112
118113# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/altair/light-client/p2p-interface.md#getlightclientbootstrap
119114proc doRequest (
@@ -340,45 +335,13 @@ template query[E](
340335): Future [bool ] =
341336 self.query (e, Nothing ())
342337
343- type SchedulingMode = enum
344- Now ,
345- Soon ,
346- CurrentPeriod ,
347- NextPeriod
348-
349- func fetchTime (
350- self: LightClientManager ,
351- wallTime: BeaconTime ,
352- schedulingMode: SchedulingMode
353- ): BeaconTime =
354- let
355- remainingTime =
356- case schedulingMode:
357- of Now :
358- return wallTime
359- of Soon :
360- chronos.seconds (0 )
361- of CurrentPeriod :
362- let
363- wallPeriod = wallTime.slotOrZero ().sync_committee_period
364- deadlineSlot = (wallPeriod + 1 ).start_slot - 1
365- deadline = deadlineSlot.start_beacon_time ()
366- chronos.nanoseconds ((deadline - wallTime).nanoseconds)
367- of NextPeriod :
368- chronos.seconds (
369- (SLOTS_PER_SYNC_COMMITTEE_PERIOD * SECONDS_PER_SLOT ).int64 )
370- minDelay = max (remainingTime div 8 , chronos.seconds (10 ))
371- jitterSeconds = (minDelay * 2 ).seconds
372- jitterDelay = chronos.seconds (self.rng[].rand (jitterSeconds).int64 )
373- return wallTime + minDelay + jitterDelay
374-
375338# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/altair/light-client/light-client.md#light-client-sync-process
376339proc loop (self: LightClientManager ) {.async .} =
377- var nextFetchTime = self.getBeaconTime ()
340+ var nextSyncTaskTime = self.getBeaconTime ()
378341 while true :
379342 # Periodically wake and check for changes
380343 let wallTime = self.getBeaconTime ()
381- if wallTime < nextFetchTime or
344+ if wallTime < nextSyncTaskTime or
382345 self.network.peerPool.lenAvailable < 1 :
383346 await sleepAsync (chronos.seconds (2 ))
384347 continue
@@ -391,18 +354,21 @@ proc loop(self: LightClientManager) {.async.} =
391354 continue
392355
393356 let didProgress = await self.query (Bootstrap , trustedBlockRoot.get)
394- if not didProgress:
395- nextFetchTime = self.fetchTime (wallTime, Soon )
357+ nextSyncTaskTime =
358+ if didProgress:
359+ wallTime
360+ else :
361+ wallTime + self.rng.computeDelayWithJitter (chronos.seconds (0 ))
396362 continue
397363
398364 # Fetch updates
399365 let
400366 current = wallTime.slotOrZero ().sync_committee_period
401367
402368 syncTask = nextLightClientSyncTask (
369+ current = current,
403370 finalized = self.getFinalizedPeriod (),
404371 optimistic = self.getOptimisticPeriod (),
405- current = current,
406372 isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown ())
407373
408374 didProgress =
@@ -415,18 +381,12 @@ proc loop(self: LightClientManager) {.async.} =
415381 of LcSyncKind .OptimisticUpdate :
416382 await self.query (OptimisticUpdate )
417383
418- schedulingMode =
419- if not self.isGossipSupported (current):
420- if didProgress:
421- Now
422- else :
423- Soon
424- elif self.getFinalizedPeriod () != self.getOptimisticPeriod ():
425- CurrentPeriod
426- else :
427- NextPeriod
428-
429- nextFetchTime = self.fetchTime (wallTime, schedulingMode)
384+ nextSyncTaskTime = wallTime + self.rng.nextLcSyncTaskDelay (
385+ wallTime,
386+ finalized = self.getFinalizedPeriod (),
387+ optimistic = self.getOptimisticPeriod (),
388+ isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown (),
389+ didLatestSyncTaskProgress = didProgress)
430390
431391proc start * (self: var LightClientManager ) =
432392 # # Start light client manager's loop.
0 commit comments