2022-05-31 10:45:37 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-05-31 10:45:37 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-06-12 12:22:32 +00:00
|
|
|
import chronos, chronicles
|
2022-05-31 10:45:37 +00:00
|
|
|
import
|
2023-04-17 17:18:54 +00:00
|
|
|
../spec/network,
|
2022-05-31 10:45:37 +00:00
|
|
|
../networking/eth2_network,
|
|
|
|
../beacon_clock,
|
2024-01-13 09:54:24 +00:00
|
|
|
"."/[light_client_sync_helpers, light_client_protocol, sync_manager]
|
2022-05-31 10:45:37 +00:00
|
|
|
export sync_manager
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "lcman"
|
|
|
|
|
|
|
|
type
|
|
|
|
Nothing = object
|
|
|
|
ResponseError = object of CatchableError
|
|
|
|
Endpoint[K, V] =
|
|
|
|
(K, V) # https://github.com/nim-lang/Nim/issues/19531
|
|
|
|
Bootstrap =
|
2023-01-12 17:11:38 +00:00
|
|
|
Endpoint[Eth2Digest, ForkedLightClientBootstrap]
|
2022-05-31 10:45:37 +00:00
|
|
|
UpdatesByRange =
|
2023-06-20 20:21:32 +00:00
|
|
|
Endpoint[
|
|
|
|
tuple[startPeriod: SyncCommitteePeriod, count: uint64],
|
|
|
|
ForkedLightClientUpdate]
|
2022-05-31 10:45:37 +00:00
|
|
|
FinalityUpdate =
|
2023-01-12 17:11:38 +00:00
|
|
|
Endpoint[Nothing, ForkedLightClientFinalityUpdate]
|
2022-05-31 10:45:37 +00:00
|
|
|
OptimisticUpdate =
|
2023-01-12 17:11:38 +00:00
|
|
|
Endpoint[Nothing, ForkedLightClientOptimisticUpdate]
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
ValueVerifier[V] =
|
2024-01-22 16:34:54 +00:00
|
|
|
proc(v: V): Future[Result[void, VerifierError]] {.async: (raises: [CancelledError]).}
|
2022-05-31 10:45:37 +00:00
|
|
|
BootstrapVerifier* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueVerifier[ForkedLightClientBootstrap]
|
2022-05-31 10:45:37 +00:00
|
|
|
UpdateVerifier* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueVerifier[ForkedLightClientUpdate]
|
2022-05-31 10:45:37 +00:00
|
|
|
FinalityUpdateVerifier* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueVerifier[ForkedLightClientFinalityUpdate]
|
2022-05-31 10:45:37 +00:00
|
|
|
OptimisticUpdateVerifier* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueVerifier[ForkedLightClientOptimisticUpdate]
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
GetTrustedBlockRootCallback* =
|
2023-08-25 09:29:07 +00:00
|
|
|
proc(): Option[Eth2Digest] {.gcsafe, raises: [].}
|
2022-05-31 10:45:37 +00:00
|
|
|
GetBoolCallback* =
|
2023-08-25 09:29:07 +00:00
|
|
|
proc(): bool {.gcsafe, raises: [].}
|
2022-05-31 10:45:37 +00:00
|
|
|
GetSyncCommitteePeriodCallback* =
|
2023-08-25 09:29:07 +00:00
|
|
|
proc(): SyncCommitteePeriod {.gcsafe, raises: [].}
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
LightClientManager* = object
|
|
|
|
network: Eth2Node
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2022-05-31 10:45:37 +00:00
|
|
|
getTrustedBlockRoot: GetTrustedBlockRootCallback
|
|
|
|
bootstrapVerifier: BootstrapVerifier
|
|
|
|
updateVerifier: UpdateVerifier
|
|
|
|
finalityUpdateVerifier: FinalityUpdateVerifier
|
|
|
|
optimisticUpdateVerifier: OptimisticUpdateVerifier
|
|
|
|
isLightClientStoreInitialized: GetBoolCallback
|
|
|
|
isNextSyncCommitteeKnown: GetBoolCallback
|
|
|
|
getFinalizedPeriod: GetSyncCommitteePeriodCallback
|
|
|
|
getOptimisticPeriod: GetSyncCommitteePeriodCallback
|
|
|
|
getBeaconTime: GetBeaconTimeFn
|
2024-08-24 13:20:19 +00:00
|
|
|
shouldInhibitSync: GetBoolCallback
|
2024-01-22 16:34:54 +00:00
|
|
|
loopFuture: Future[void].Raising([CancelledError])
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
func init*(
|
|
|
|
T: type LightClientManager,
|
|
|
|
network: Eth2Node,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2022-05-31 10:45:37 +00:00
|
|
|
getTrustedBlockRoot: GetTrustedBlockRootCallback,
|
|
|
|
bootstrapVerifier: BootstrapVerifier,
|
|
|
|
updateVerifier: UpdateVerifier,
|
|
|
|
finalityUpdateVerifier: FinalityUpdateVerifier,
|
|
|
|
optimisticUpdateVerifier: OptimisticUpdateVerifier,
|
|
|
|
isLightClientStoreInitialized: GetBoolCallback,
|
|
|
|
isNextSyncCommitteeKnown: GetBoolCallback,
|
|
|
|
getFinalizedPeriod: GetSyncCommitteePeriodCallback,
|
|
|
|
getOptimisticPeriod: GetSyncCommitteePeriodCallback,
|
2024-08-24 13:20:19 +00:00
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
|
|
|
shouldInhibitSync: GetBoolCallback = nil
|
2022-05-31 10:45:37 +00:00
|
|
|
): LightClientManager =
|
|
|
|
## Initialize light client manager.
|
|
|
|
LightClientManager(
|
|
|
|
network: network,
|
|
|
|
rng: rng,
|
|
|
|
getTrustedBlockRoot: getTrustedBlockRoot,
|
|
|
|
bootstrapVerifier: bootstrapVerifier,
|
|
|
|
updateVerifier: updateVerifier,
|
|
|
|
finalityUpdateVerifier: finalityUpdateVerifier,
|
|
|
|
optimisticUpdateVerifier: optimisticUpdateVerifier,
|
|
|
|
isLightClientStoreInitialized: isLightClientStoreInitialized,
|
|
|
|
isNextSyncCommitteeKnown: isNextSyncCommitteeKnown,
|
|
|
|
getFinalizedPeriod: getFinalizedPeriod,
|
|
|
|
getOptimisticPeriod: getOptimisticPeriod,
|
2024-08-24 13:20:19 +00:00
|
|
|
getBeaconTime: getBeaconTime,
|
|
|
|
shouldInhibitSync: shouldInhibitSync
|
2022-05-31 10:45:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc isGossipSupported*(
|
|
|
|
self: LightClientManager,
|
|
|
|
period: SyncCommitteePeriod
|
|
|
|
): bool =
|
|
|
|
## Indicate whether the light client is sufficiently synced to accept gossip.
|
|
|
|
if not self.isLightClientStoreInitialized():
|
|
|
|
return false
|
|
|
|
|
2023-06-23 06:19:51 +00:00
|
|
|
period.isGossipSupported(
|
|
|
|
finalizedPeriod = self.getFinalizedPeriod(),
|
|
|
|
isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown())
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#getlightclientbootstrap
|
2022-05-31 10:45:37 +00:00
|
|
|
proc doRequest(
|
|
|
|
e: typedesc[Bootstrap],
|
|
|
|
peer: Peer,
|
|
|
|
blockRoot: Eth2Digest
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[NetRes[ForkedLightClientBootstrap]] {.async: (raises: [CancelledError], raw: true).} =
|
2022-05-31 10:45:37 +00:00
|
|
|
peer.lightClientBootstrap(blockRoot)
|
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#lightclientupdatesbyrange
|
2022-10-27 16:51:43 +00:00
|
|
|
type LightClientUpdatesByRangeResponse =
|
2023-01-12 17:11:38 +00:00
|
|
|
NetRes[List[ForkedLightClientUpdate, MAX_REQUEST_LIGHT_CLIENT_UPDATES]]
|
2022-05-31 10:45:37 +00:00
|
|
|
proc doRequest(
|
|
|
|
e: typedesc[UpdatesByRange],
|
|
|
|
peer: Peer,
|
2023-06-20 20:21:32 +00:00
|
|
|
key: tuple[startPeriod: SyncCommitteePeriod, count: uint64]
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[LightClientUpdatesByRangeResponse] {.async: (raises: [ResponseError, CancelledError]).} =
|
2023-06-20 20:21:32 +00:00
|
|
|
let (startPeriod, count) = key
|
|
|
|
doAssert count > 0 and count <= MAX_REQUEST_LIGHT_CLIENT_UPDATES
|
|
|
|
let response = await peer.lightClientUpdatesByRange(startPeriod, count)
|
2022-05-31 10:45:37 +00:00
|
|
|
if response.isOk:
|
2023-06-12 12:22:32 +00:00
|
|
|
let e = distinctBase(response.get)
|
2023-06-20 20:21:32 +00:00
|
|
|
.checkLightClientUpdates(startPeriod, count)
|
2023-06-12 12:22:32 +00:00
|
|
|
if e.isErr:
|
|
|
|
raise newException(ResponseError, e.error)
|
2022-05-31 10:45:37 +00:00
|
|
|
return response
|
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#getlightclientfinalityupdate
|
2022-05-31 10:45:37 +00:00
|
|
|
proc doRequest(
|
|
|
|
e: typedesc[FinalityUpdate],
|
|
|
|
peer: Peer
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[NetRes[ForkedLightClientFinalityUpdate]] {.async: (raises: [CancelledError], raw: true).} =
|
2022-05-31 10:45:37 +00:00
|
|
|
peer.lightClientFinalityUpdate()
|
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#getlightclientoptimisticupdate
|
2022-05-31 10:45:37 +00:00
|
|
|
proc doRequest(
|
|
|
|
e: typedesc[OptimisticUpdate],
|
|
|
|
peer: Peer
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[NetRes[ForkedLightClientOptimisticUpdate]] {.async: (raises: [CancelledError], raw: true).} =
|
2022-05-31 10:45:37 +00:00
|
|
|
peer.lightClientOptimisticUpdate()
|
|
|
|
|
|
|
|
template valueVerifier[E](
|
|
|
|
self: LightClientManager,
|
|
|
|
e: typedesc[E]
|
|
|
|
): ValueVerifier[E.V] =
|
2023-01-12 17:11:38 +00:00
|
|
|
when E.V is ForkedLightClientBootstrap:
|
2022-05-31 10:45:37 +00:00
|
|
|
self.bootstrapVerifier
|
2023-01-12 17:11:38 +00:00
|
|
|
elif E.V is ForkedLightClientUpdate:
|
2022-05-31 10:45:37 +00:00
|
|
|
self.updateVerifier
|
2023-01-12 17:11:38 +00:00
|
|
|
elif E.V is ForkedLightClientFinalityUpdate:
|
2022-05-31 10:45:37 +00:00
|
|
|
self.finalityUpdateVerifier
|
2023-01-12 17:11:38 +00:00
|
|
|
elif E.V is ForkedLightClientOptimisticUpdate:
|
2022-05-31 10:45:37 +00:00
|
|
|
self.optimisticUpdateVerifier
|
|
|
|
else: static: doAssert false
|
|
|
|
|
|
|
|
iterator values(v: auto): auto =
|
|
|
|
## Local helper for `workerTask` to share the same implementation for both
|
|
|
|
## scalar and aggregate values, by treating scalars as 1-length aggregates.
|
2022-10-27 16:51:43 +00:00
|
|
|
when v is List:
|
2022-05-31 10:45:37 +00:00
|
|
|
for i in v:
|
|
|
|
yield i
|
|
|
|
else:
|
|
|
|
yield v
|
|
|
|
|
|
|
|
proc workerTask[E](
|
|
|
|
self: LightClientManager,
|
|
|
|
e: typedesc[E],
|
|
|
|
key: E.K
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2022-05-31 10:45:37 +00:00
|
|
|
var
|
|
|
|
peer: Peer
|
|
|
|
didProgress = false
|
|
|
|
try:
|
|
|
|
peer = self.network.peerPool.acquireNoWait()
|
|
|
|
let value =
|
|
|
|
when E.K is Nothing:
|
|
|
|
await E.doRequest(peer)
|
|
|
|
else:
|
|
|
|
await E.doRequest(peer, key)
|
|
|
|
if value.isOk:
|
|
|
|
var applyReward = false
|
2022-10-27 16:51:43 +00:00
|
|
|
for val in value.get().values:
|
2022-05-31 10:45:37 +00:00
|
|
|
let res = await self.valueVerifier(E)(val)
|
|
|
|
if res.isErr:
|
|
|
|
case res.error
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.MissingParent:
|
2022-05-31 10:45:37 +00:00
|
|
|
# Stop, requires different request to progress
|
|
|
|
return didProgress
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.Duplicate:
|
2022-05-31 10:45:37 +00:00
|
|
|
# Ignore, a concurrent request may have already fulfilled this
|
2023-01-12 17:11:38 +00:00
|
|
|
when E.V is ForkedLightClientBootstrap:
|
2022-05-31 10:45:37 +00:00
|
|
|
didProgress = true
|
|
|
|
else:
|
|
|
|
discard
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.UnviableFork:
|
2022-05-31 10:45:37 +00:00
|
|
|
# Descore, peer is on an incompatible fork version
|
2023-01-12 17:11:38 +00:00
|
|
|
withForkyObject(val):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
notice "Received value from an unviable fork",
|
|
|
|
value = forkyObject,
|
|
|
|
endpoint = E.name, peer, peer_score = peer.getScore()
|
|
|
|
else:
|
|
|
|
notice "Received value from an unviable fork",
|
|
|
|
endpoint = E.name, peer, peer_score = peer.getScore()
|
2022-05-31 10:45:37 +00:00
|
|
|
peer.updateScore(PeerScoreUnviableFork)
|
|
|
|
return didProgress
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.Invalid:
|
2022-05-31 10:45:37 +00:00
|
|
|
# Descore, received data is malformed
|
2023-01-12 17:11:38 +00:00
|
|
|
withForkyObject(val):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
warn "Received invalid value", value = forkyObject.shortLog,
|
|
|
|
endpoint = E.name, peer, peer_score = peer.getScore()
|
|
|
|
else:
|
|
|
|
warn "Received invalid value",
|
|
|
|
endpoint = E.name, peer, peer_score = peer.getScore()
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreBadValues)
|
2022-05-31 10:45:37 +00:00
|
|
|
return didProgress
|
|
|
|
else:
|
|
|
|
# Reward, peer returned something useful
|
|
|
|
applyReward = true
|
|
|
|
didProgress = true
|
|
|
|
if applyReward:
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreGoodValues)
|
2022-05-31 10:45:37 +00:00
|
|
|
else:
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreNoValues)
|
2022-05-31 10:45:37 +00:00
|
|
|
debug "Failed to receive value on request", value,
|
|
|
|
endpoint = E.name, peer, peer_score = peer.getScore()
|
|
|
|
except ResponseError as exc:
|
|
|
|
warn "Received invalid response", error = exc.msg,
|
|
|
|
endpoint = E.name, peer, peer_score = peer.getScore()
|
2022-11-11 11:34:28 +00:00
|
|
|
peer.updateScore(PeerScoreBadValues)
|
2022-05-31 10:45:37 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except PeerPoolError as exc:
|
|
|
|
debug "Failed to acquire peer", exc = exc.msg
|
|
|
|
finally:
|
|
|
|
if peer != nil:
|
|
|
|
self.network.peerPool.release(peer)
|
|
|
|
return didProgress
|
|
|
|
|
|
|
|
proc query[E](
|
|
|
|
self: LightClientManager,
|
|
|
|
e: typedesc[E],
|
|
|
|
key: E.K
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2022-05-31 10:45:37 +00:00
|
|
|
const PARALLEL_REQUESTS = 2
|
|
|
|
var workers: array[PARALLEL_REQUESTS, Future[bool]]
|
|
|
|
|
|
|
|
let
|
2024-01-22 16:34:54 +00:00
|
|
|
progressFut = Future[void].Raising([CancelledError]).init("lcmanProgress")
|
|
|
|
doneFut = Future[void].Raising([CancelledError]).init("lcmanDone")
|
2022-05-31 10:45:37 +00:00
|
|
|
var
|
|
|
|
numCompleted = 0
|
|
|
|
maxCompleted = workers.len
|
|
|
|
|
|
|
|
proc handleFinishedWorker(future: pointer) =
|
|
|
|
try:
|
|
|
|
let didProgress = cast[Future[bool]](future).read()
|
|
|
|
if didProgress and not progressFut.finished:
|
|
|
|
progressFut.complete()
|
2023-11-11 08:49:34 +00:00
|
|
|
except CancelledError:
|
2022-05-31 10:45:37 +00:00
|
|
|
if not progressFut.finished:
|
2023-09-22 11:06:27 +00:00
|
|
|
progressFut.cancelSoon()
|
2023-11-11 08:49:34 +00:00
|
|
|
except CatchableError:
|
2022-05-31 10:45:37 +00:00
|
|
|
discard
|
|
|
|
finally:
|
|
|
|
inc numCompleted
|
|
|
|
if numCompleted == maxCompleted:
|
|
|
|
doneFut.complete()
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Start concurrent workers
|
|
|
|
for i in 0 ..< workers.len:
|
|
|
|
try:
|
|
|
|
workers[i] = self.workerTask(e, key)
|
|
|
|
workers[i].addCallback(handleFinishedWorker)
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2023-11-11 08:49:34 +00:00
|
|
|
except CatchableError:
|
2022-05-31 10:45:37 +00:00
|
|
|
workers[i] = newFuture[bool]()
|
|
|
|
workers[i].complete(false)
|
|
|
|
|
|
|
|
# Wait for any worker to report progress, or for all workers to finish
|
2024-01-22 16:34:54 +00:00
|
|
|
try:
|
|
|
|
discard await race(progressFut, doneFut)
|
|
|
|
except ValueError:
|
|
|
|
raiseAssert "race API invariant"
|
2022-05-31 10:45:37 +00:00
|
|
|
finally:
|
|
|
|
for i in 0 ..< maxCompleted:
|
|
|
|
if workers[i] == nil:
|
|
|
|
maxCompleted = i
|
|
|
|
if numCompleted == maxCompleted:
|
|
|
|
doneFut.complete()
|
|
|
|
break
|
|
|
|
if not workers[i].finished:
|
2023-09-22 11:06:27 +00:00
|
|
|
workers[i].cancelSoon()
|
2022-05-31 10:45:37 +00:00
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
await allFutures(workers[0 ..< maxCompleted])
|
|
|
|
break
|
2023-11-11 08:49:34 +00:00
|
|
|
except CancelledError:
|
2022-05-31 10:45:37 +00:00
|
|
|
continue
|
|
|
|
while true:
|
|
|
|
try:
|
|
|
|
await doneFut
|
|
|
|
break
|
2023-11-11 08:49:34 +00:00
|
|
|
except CancelledError:
|
2022-05-31 10:45:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not progressFut.finished:
|
2023-09-22 11:06:27 +00:00
|
|
|
progressFut.cancelSoon()
|
2022-05-31 10:45:37 +00:00
|
|
|
return progressFut.completed
|
|
|
|
|
|
|
|
template query[E](
|
|
|
|
self: LightClientManager,
|
|
|
|
e: typedesc[E]
|
2024-01-22 16:34:54 +00:00
|
|
|
): Future[bool].Raising([CancelledError]) =
|
2022-05-31 10:45:37 +00:00
|
|
|
self.query(e, Nothing())
|
|
|
|
|
2024-05-09 05:03:10 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.2/specs/altair/light-client/light-client.md#light-client-sync-process
|
2024-01-22 16:34:54 +00:00
|
|
|
proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} =
|
2023-06-23 06:19:51 +00:00
|
|
|
var nextSyncTaskTime = self.getBeaconTime()
|
2022-05-31 10:45:37 +00:00
|
|
|
while true:
|
|
|
|
# Periodically wake and check for changes
|
|
|
|
let wallTime = self.getBeaconTime()
|
2023-06-23 06:19:51 +00:00
|
|
|
if wallTime < nextSyncTaskTime or
|
2024-08-24 13:20:19 +00:00
|
|
|
(self.shouldInhibitSync != nil and self.shouldInhibitSync()) or
|
2022-05-31 10:45:37 +00:00
|
|
|
self.network.peerPool.lenAvailable < 1:
|
|
|
|
await sleepAsync(chronos.seconds(2))
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Obtain bootstrap data once a trusted block root is supplied
|
|
|
|
if not self.isLightClientStoreInitialized():
|
|
|
|
let trustedBlockRoot = self.getTrustedBlockRoot()
|
|
|
|
if trustedBlockRoot.isNone:
|
|
|
|
await sleepAsync(chronos.seconds(2))
|
|
|
|
continue
|
|
|
|
|
|
|
|
let didProgress = await self.query(Bootstrap, trustedBlockRoot.get)
|
2023-06-23 06:19:51 +00:00
|
|
|
nextSyncTaskTime =
|
|
|
|
if didProgress:
|
|
|
|
wallTime
|
|
|
|
else:
|
|
|
|
wallTime + self.rng.computeDelayWithJitter(chronos.seconds(0))
|
2022-05-31 10:45:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Fetch updates
|
|
|
|
let
|
|
|
|
current = wallTime.slotOrZero().sync_committee_period
|
2023-06-21 06:28:14 +00:00
|
|
|
|
|
|
|
syncTask = nextLightClientSyncTask(
|
2023-06-23 06:19:51 +00:00
|
|
|
current = current,
|
2023-06-21 06:28:14 +00:00
|
|
|
finalized = self.getFinalizedPeriod(),
|
|
|
|
optimistic = self.getOptimisticPeriod(),
|
|
|
|
isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown())
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
didProgress =
|
2023-06-21 06:28:14 +00:00
|
|
|
case syncTask.kind
|
|
|
|
of LcSyncKind.UpdatesByRange:
|
|
|
|
await self.query(UpdatesByRange,
|
|
|
|
(startPeriod: syncTask.startPeriod, count: syncTask.count))
|
|
|
|
of LcSyncKind.FinalityUpdate:
|
2022-05-31 10:45:37 +00:00
|
|
|
await self.query(FinalityUpdate)
|
2023-06-21 06:28:14 +00:00
|
|
|
of LcSyncKind.OptimisticUpdate:
|
2022-05-31 10:45:37 +00:00
|
|
|
await self.query(OptimisticUpdate)
|
|
|
|
|
2023-06-23 06:19:51 +00:00
|
|
|
nextSyncTaskTime = wallTime + self.rng.nextLcSyncTaskDelay(
|
|
|
|
wallTime,
|
|
|
|
finalized = self.getFinalizedPeriod(),
|
|
|
|
optimistic = self.getOptimisticPeriod(),
|
|
|
|
isNextSyncCommitteeKnown = self.isNextSyncCommitteeKnown(),
|
|
|
|
didLatestSyncTaskProgress = didProgress)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
proc start*(self: var LightClientManager) =
|
|
|
|
## Start light client manager's loop.
|
|
|
|
doAssert self.loopFuture == nil
|
|
|
|
self.loopFuture = self.loop()
|
|
|
|
|
2024-01-22 16:34:54 +00:00
|
|
|
proc stop*(self: var LightClientManager) {.async: (raises: []).} =
|
2022-05-31 10:45:37 +00:00
|
|
|
## Stop light client manager's loop.
|
|
|
|
if self.loopFuture != nil:
|
2024-01-22 16:34:54 +00:00
|
|
|
await noCancel self.loopFuture.cancelAndWait()
|
2022-05-31 10:45:37 +00:00
|
|
|
self.loopFuture = nil
|