9
9
10
10
import chronos, chronicles
11
11
import
12
- eth/ p2p/ discoveryv5/ random2,
13
12
../ spec/ network,
14
13
../ networking/ eth2_network,
15
14
../ beacon_clock,
@@ -107,13 +106,9 @@ proc isGossipSupported*(
107
106
if not self.isLightClientStoreInitialized():
108
107
return false
109
108
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())
117
112
118
113
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/altair/light-client/p2p-interface.md#getlightclientbootstrap
119
114
proc doRequest(
@@ -340,45 +335,13 @@ template query[E](
340
335
): Future[bool ] =
341
336
self.query(e, Nothing())
342
337
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
-
375
338
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/altair/light-client/light-client.md#light-client-sync-process
376
339
proc loop(self: LightClientManager) {.async.} =
377
- var nextFetchTime = self.getBeaconTime()
340
+ var nextSyncTaskTime = self.getBeaconTime()
378
341
while true:
379
342
# Periodically wake and check for changes
380
343
let wallTime = self.getBeaconTime()
381
- if wallTime < nextFetchTime or
344
+ if wallTime < nextSyncTaskTime or
382
345
self.network.peerPool.lenAvailable < 1:
383
346
await sleepAsync(chronos.seconds(2))
384
347
continue
@@ -391,18 +354,21 @@ proc loop(self: LightClientManager) {.async.} =
391
354
continue
392
355
393
356
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))
396
362
continue
397
363
398
364
# Fetch updates
399
365
let
400
366
current = wallTime.slotOrZero().sync_committee_period
401
367
402
368
syncTask = nextLightClientSyncTask(
369
+ current = current,
403
370
finalized = self.getFinalizedPeriod(),
404
371
optimistic = self.getOptimisticPeriod(),
405
- current = current,
406
372
isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown())
407
373
408
374
didProgress =
@@ -415,18 +381,12 @@ proc loop(self: LightClientManager) {.async.} =
415
381
of LcSyncKind.OptimisticUpdate:
416
382
await self.query(OptimisticUpdate)
417
383
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)
430
390
431
391
proc start*(self: var LightClientManager) =
432
392
## Start light client manager's loop.
0 commit comments