2022-03-17 22:26:56 +00:00
|
|
|
# beacon_chain
|
2023-01-06 16:28:46 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-03-17 22:26:56 +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-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
stew/objects,
|
|
|
|
chronos, metrics,
|
|
|
|
../spec/light_client_sync,
|
|
|
|
../consensus_object_pools/block_pools_types,
|
2022-05-31 10:45:37 +00:00
|
|
|
".."/[beacon_clock, sszdump],
|
|
|
|
"."/[eth2_processor, gossip_validation]
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
export sszdump, eth2_processor, gossip_validation
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
logScope: topics = "gossip_lc"
|
|
|
|
|
2022-03-17 22:26:56 +00:00
|
|
|
# Light Client Processor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# The light client processor handles received light client objects
|
|
|
|
|
|
|
|
declareHistogram light_client_store_object_duration_seconds,
|
|
|
|
"storeObject() duration", buckets = [0.25, 0.5, 1, 2, 4, 8, Inf]
|
|
|
|
|
|
|
|
type
|
2022-12-08 16:24:16 +00:00
|
|
|
Nothing = object
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
GetTrustedBlockRootCallback* =
|
|
|
|
proc(): Option[Eth2Digest] {.gcsafe, raises: [Defect].}
|
|
|
|
VoidCallback* =
|
2022-03-17 22:26:56 +00:00
|
|
|
proc() {.gcsafe, raises: [Defect].}
|
|
|
|
|
2022-12-08 16:24:16 +00:00
|
|
|
ValueObserver[V] =
|
|
|
|
proc(v: V) {.gcsafe, raises: [Defect].}
|
|
|
|
BootstrapObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueObserver[ForkedLightClientBootstrap]
|
2022-12-08 16:24:16 +00:00
|
|
|
UpdateObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueObserver[ForkedLightClientUpdate]
|
2022-12-08 16:24:16 +00:00
|
|
|
FinalityUpdateObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueObserver[ForkedLightClientFinalityUpdate]
|
2022-12-08 16:24:16 +00:00
|
|
|
OptimisticUpdateObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
ValueObserver[ForkedLightClientOptimisticUpdate]
|
2022-12-08 16:24:16 +00:00
|
|
|
|
2022-07-21 09:16:10 +00:00
|
|
|
LightClientFinalizationMode* {.pure.} = enum
|
|
|
|
Strict
|
|
|
|
## Only finalize light client data that:
|
|
|
|
## - has been signed by a supermajority (2/3) of the sync committee
|
|
|
|
## - has a valid finality proof
|
|
|
|
##
|
|
|
|
## Optimizes for security, but may become stuck if there is any of:
|
|
|
|
## - non-finality for an entire sync committee period
|
|
|
|
## - low sync committee participation for an entire sync committee period
|
|
|
|
## Such periods need to be covered by an out-of-band syncing mechanism.
|
|
|
|
##
|
|
|
|
## Note that a compromised supermajority of the sync committee is able to
|
|
|
|
## sign arbitrary light client data, even after being slashed. The light
|
|
|
|
## client cannot validate the slashing status of sync committee members.
|
|
|
|
## Likewise, voluntarily exited validators may sign bad light client data
|
|
|
|
## for the sync committee periods in which they used to be selected.
|
|
|
|
|
|
|
|
Optimistic
|
|
|
|
## Attempt to finalize light client data not satisfying strict conditions
|
|
|
|
## if there is no progress for an extended period of time and if there are
|
|
|
|
## repeated messages indicating that it is the best available data on the
|
|
|
|
## network for the affected time period.
|
|
|
|
##
|
|
|
|
## Optimizes for availability of recent data, but may end up on incorrect
|
|
|
|
## forks if run in a hostile network environment (no honest peers), or if
|
|
|
|
## the low sync committee participation is being exploited by bad actors.
|
|
|
|
|
2022-03-17 22:26:56 +00:00
|
|
|
LightClientProcessor* = object
|
|
|
|
## This manages the processing of received light client objects
|
|
|
|
##
|
|
|
|
## from:
|
2022-05-23 12:02:54 +00:00
|
|
|
## - Gossip:
|
|
|
|
## - `LightClientFinalityUpdate`
|
|
|
|
## - `LightClientOptimisticUpdate`
|
|
|
|
## - `LightClientManager`:
|
|
|
|
## - `GetLightClientBootstrap`
|
|
|
|
## - `LightClientUpdatesByRange`
|
|
|
|
## - `GetLightClientFinalityUpdate`
|
|
|
|
## - `GetLightClientOptimisticUpdate`
|
2022-03-17 22:26:56 +00:00
|
|
|
##
|
|
|
|
## are then verified and added to:
|
|
|
|
## - `LightClientStore`
|
|
|
|
|
|
|
|
# Config
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
dumpEnabled: bool
|
|
|
|
dumpDirInvalid: string
|
|
|
|
dumpDirIncoming: string
|
|
|
|
|
|
|
|
# Consumer
|
|
|
|
# ----------------------------------------------------------------
|
2023-01-16 15:53:45 +00:00
|
|
|
store: ref ForkedLightClientStore
|
2022-05-31 10:45:37 +00:00
|
|
|
getBeaconTime: GetBeaconTimeFn
|
|
|
|
getTrustedBlockRoot: GetTrustedBlockRootCallback
|
|
|
|
onStoreInitialized, onFinalizedHeader, onOptimisticHeader: VoidCallback
|
2022-12-08 16:24:16 +00:00
|
|
|
bootstrapObserver: BootstrapObserver
|
|
|
|
updateObserver: UpdateObserver
|
|
|
|
finalityUpdateObserver: FinalityUpdateObserver
|
|
|
|
optimisticUpdateObserver: OptimisticUpdateObserver
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
cfg: RuntimeConfig
|
2022-04-08 16:22:49 +00:00
|
|
|
genesis_validators_root: Eth2Digest
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-07-21 09:16:10 +00:00
|
|
|
case finalizationMode: LightClientFinalizationMode
|
|
|
|
of LightClientFinalizationMode.Strict:
|
|
|
|
discard
|
|
|
|
of LightClientFinalizationMode.Optimistic:
|
|
|
|
lastProgressTick: BeaconTime # Moment when last update made progress
|
|
|
|
lastDuplicateTick: BeaconTime # Moment when last duplicate update received
|
2023-01-16 15:53:45 +00:00
|
|
|
numDupsSinceProgress: int # Number of duplicates since last progress
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
latestFinalityUpdate: ForkedLightClientOptimisticUpdate
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2022-03-17 22:26:56 +00:00
|
|
|
const
|
|
|
|
# These constants have been chosen empirically and are not backed by spec
|
|
|
|
duplicateRateLimit = chronos.seconds(5) # Rate limit for counting duplicates
|
|
|
|
duplicateCountDelay = chronos.minutes(15) # Delay to start counting duplicates
|
|
|
|
minForceUpdateDelay = chronos.minutes(30) # Minimum delay until forced-update
|
|
|
|
minForceUpdateDuplicates = 100 # Minimum duplicates until forced-update
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type LightClientProcessor,
|
|
|
|
dumpEnabled: bool,
|
|
|
|
dumpDirInvalid, dumpDirIncoming: string,
|
|
|
|
cfg: RuntimeConfig,
|
2022-05-31 10:45:37 +00:00
|
|
|
genesis_validators_root: Eth2Digest,
|
2022-07-21 09:16:10 +00:00
|
|
|
finalizationMode: LightClientFinalizationMode,
|
2023-01-16 15:53:45 +00:00
|
|
|
store: ref ForkedLightClientStore,
|
2022-03-17 22:26:56 +00:00
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
2022-05-31 10:45:37 +00:00
|
|
|
getTrustedBlockRoot: GetTrustedBlockRootCallback,
|
|
|
|
onStoreInitialized: VoidCallback = nil,
|
|
|
|
onFinalizedHeader: VoidCallback = nil,
|
2022-12-08 16:24:16 +00:00
|
|
|
onOptimisticHeader: VoidCallback = nil,
|
|
|
|
bootstrapObserver: BootstrapObserver = nil,
|
|
|
|
updateObserver: UpdateObserver = nil,
|
|
|
|
finalityUpdateObserver: FinalityUpdateObserver = nil,
|
|
|
|
optimisticUpdateObserver: OptimisticUpdateObserver = nil
|
2022-03-17 22:26:56 +00:00
|
|
|
): ref LightClientProcessor =
|
|
|
|
(ref LightClientProcessor)(
|
|
|
|
dumpEnabled: dumpEnabled,
|
|
|
|
dumpDirInvalid: dumpDirInvalid,
|
|
|
|
dumpDirIncoming: dumpDirIncoming,
|
|
|
|
store: store,
|
|
|
|
getBeaconTime: getBeaconTime,
|
2022-05-31 10:45:37 +00:00
|
|
|
getTrustedBlockRoot: getTrustedBlockRoot,
|
|
|
|
onStoreInitialized: onStoreInitialized,
|
|
|
|
onFinalizedHeader: onFinalizedHeader,
|
|
|
|
onOptimisticHeader: onOptimisticHeader,
|
2022-12-08 16:24:16 +00:00
|
|
|
bootstrapObserver: bootstrapObserver,
|
|
|
|
updateObserver: updateObserver,
|
|
|
|
finalityUpdateObserver: finalityUpdateObserver,
|
|
|
|
optimisticUpdateObserver: optimisticUpdateObserver,
|
2022-03-17 22:26:56 +00:00
|
|
|
cfg: cfg,
|
2022-07-21 09:16:10 +00:00
|
|
|
genesis_validators_root: genesis_validators_root,
|
|
|
|
finalizationMode: finalizationMode)
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
# Storage
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc dumpInvalidObject(
|
|
|
|
self: LightClientProcessor,
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkyLightClientObject) =
|
2022-03-17 22:26:56 +00:00
|
|
|
if self.dumpEnabled:
|
|
|
|
dump(self.dumpDirInvalid, obj)
|
|
|
|
|
|
|
|
proc dumpObject[T](
|
|
|
|
self: LightClientProcessor,
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkyLightClientObject,
|
2022-11-10 17:40:27 +00:00
|
|
|
res: Result[T, VerifierError]) =
|
2022-03-17 22:26:56 +00:00
|
|
|
if self.dumpEnabled and res.isErr:
|
|
|
|
case res.error
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.Invalid:
|
2022-03-17 22:26:56 +00:00
|
|
|
self.dumpInvalidObject(obj)
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.MissingParent:
|
2022-03-17 22:26:56 +00:00
|
|
|
dump(self.dumpDirIncoming, obj)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc tryForceUpdate(
|
|
|
|
self: var LightClientProcessor,
|
|
|
|
wallTime: BeaconTime) =
|
|
|
|
## Try to force-update to the next sync committee period.
|
2023-01-16 15:53:45 +00:00
|
|
|
let wallSlot = wallTime.slotOrZero()
|
|
|
|
doAssert self.finalizationMode == LightClientFinalizationMode.Optimistic
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
case forkyStore.process_light_client_store_force_update(wallSlot)
|
|
|
|
of NoUpdate:
|
|
|
|
discard
|
|
|
|
of DidUpdateWithoutSupermajority:
|
|
|
|
warn "Light client force-updated without supermajority",
|
|
|
|
finalizedSlot = forkyStore.finalized_header.beacon.slot,
|
|
|
|
optimisticSlot = forkyStore.optimistic_header.beacon.slot
|
|
|
|
of DidUpdateWithoutFinality:
|
|
|
|
warn "Light client force-updated without finality proof",
|
|
|
|
finalizedSlot = forkyStore.finalized_header.beacon.slot,
|
|
|
|
optimisticSlot = forkyStore.optimistic_header.beacon.slot
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
proc processObject(
|
2022-03-17 22:26:56 +00:00
|
|
|
self: var LightClientProcessor,
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkedLightClientObject,
|
2022-11-10 17:40:27 +00:00
|
|
|
wallTime: BeaconTime): Result[void, VerifierError] =
|
2022-03-17 22:26:56 +00:00
|
|
|
let
|
|
|
|
wallSlot = wallTime.slotOrZero()
|
2023-01-16 15:53:45 +00:00
|
|
|
res = withForkyObject(obj):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
when forkyObject is ForkyLightClientBootstrap:
|
|
|
|
if self.store[].kind > LightClientDataFork.None:
|
|
|
|
err(VerifierError.Duplicate)
|
|
|
|
else:
|
|
|
|
let trustedBlockRoot = self.getTrustedBlockRoot()
|
|
|
|
if trustedBlockRoot.isNone:
|
|
|
|
err(VerifierError.MissingParent)
|
|
|
|
else:
|
|
|
|
let initRes = initialize_light_client_store(
|
|
|
|
trustedBlockRoot.get, forkyObject, self.cfg)
|
|
|
|
if initRes.isErr:
|
|
|
|
err(initRes.error)
|
2023-01-14 21:19:50 +00:00
|
|
|
else:
|
2023-01-16 15:53:45 +00:00
|
|
|
self.store[] = ForkedLightClientStore(kind: lcDataFork)
|
|
|
|
self.store[].forky(lcDataFork) = initRes.get
|
|
|
|
ok()
|
|
|
|
elif forkyObject is SomeForkyLightClientUpdate:
|
|
|
|
if self.store[].kind == LightClientDataFork.None:
|
|
|
|
err(VerifierError.MissingParent)
|
|
|
|
else:
|
|
|
|
if lcDataFork > self.store[].kind:
|
|
|
|
info "Upgrading light client",
|
|
|
|
oldFork = self.store[].kind, newFork = lcDataFork
|
|
|
|
self.store[].migrateToDataFork(lcDataFork)
|
|
|
|
withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
let upgradedObject = obj.migratingToDataFork(lcDataFork)
|
|
|
|
process_light_client_update(
|
|
|
|
forkyStore, upgradedObject.forky(lcDataFork), wallSlot,
|
2023-01-14 21:19:50 +00:00
|
|
|
self.cfg, self.genesis_validators_root)
|
2023-01-16 15:53:45 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
|
|
|
err(VerifierError.Invalid)
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
withForkyObject(obj):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
self.dumpObject(forkyObject, res)
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
if res.isErr:
|
2023-01-12 17:11:38 +00:00
|
|
|
when obj is ForkedLightClientUpdate:
|
2022-07-21 09:16:10 +00:00
|
|
|
if self.finalizationMode == LightClientFinalizationMode.Optimistic and
|
2023-01-16 15:53:45 +00:00
|
|
|
obj.kind <= self.store[].kind:
|
|
|
|
withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
if forkyStore.best_valid_update.isSome:
|
|
|
|
# `best_valid_update` is set when supermajority / finality proof
|
|
|
|
# is unavailable. In that case, we will wait for a better update.
|
|
|
|
# If none is made available within reasonable time, light client
|
|
|
|
# is force-updated with best known data to ensure sync progress.
|
|
|
|
case res.error
|
|
|
|
of VerifierError.Duplicate:
|
|
|
|
if wallTime >= self.lastDuplicateTick + duplicateRateLimit:
|
|
|
|
if self.numDupsSinceProgress < minForceUpdateDuplicates:
|
|
|
|
let upgradedObj = obj.migratingToDataFork(lcDataFork)
|
|
|
|
if upgradedObj.forky(lcDataFork).matches(
|
|
|
|
forkyStore.best_valid_update.get):
|
|
|
|
self.lastDuplicateTick = wallTime
|
|
|
|
inc self.numDupsSinceProgress
|
|
|
|
if self.numDupsSinceProgress >= minForceUpdateDuplicates and
|
|
|
|
wallTime >= self.lastProgressTick + minForceUpdateDelay:
|
|
|
|
self.tryForceUpdate(wallTime)
|
|
|
|
self.lastProgressTick = wallTime
|
|
|
|
self.lastDuplicateTick = wallTime + duplicateCountDelay
|
|
|
|
self.numDupsSinceProgress = 0
|
|
|
|
else: discard
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
when obj is ForkedLightClientBootstrap | ForkedLightClientUpdate:
|
2022-07-21 09:16:10 +00:00
|
|
|
if self.finalizationMode == LightClientFinalizationMode.Optimistic:
|
|
|
|
self.lastProgressTick = wallTime
|
|
|
|
self.lastDuplicateTick = wallTime + duplicateCountDelay
|
2023-01-16 15:53:45 +00:00
|
|
|
self.numDupsSinceProgress = 0
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
res
|
|
|
|
|
2022-12-08 16:24:16 +00:00
|
|
|
template withReportedProgress(
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkedLightClientObject | Nothing, body: untyped): bool =
|
2022-06-07 17:01:11 +00:00
|
|
|
block:
|
|
|
|
let
|
2023-01-24 17:44:55 +00:00
|
|
|
oldIsInitialized = self.store[].kind > LightClientDataFork.None
|
2023-01-16 15:53:45 +00:00
|
|
|
oldNextCommitteeKnown = withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.is_next_sync_committee_known
|
2022-12-08 16:24:16 +00:00
|
|
|
else:
|
|
|
|
false
|
2023-01-16 15:53:45 +00:00
|
|
|
var
|
|
|
|
oldFinalized = withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var header = ForkedLightClientHeader(kind: lcDataFork)
|
|
|
|
header.forky(lcDataFork) = forkyStore.finalized_header
|
|
|
|
header
|
2022-06-07 17:01:11 +00:00
|
|
|
else:
|
2023-01-16 15:53:45 +00:00
|
|
|
default(ForkedLightClientHeader)
|
|
|
|
oldOptimistic = withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var header = ForkedLightClientHeader(kind: lcDataFork)
|
|
|
|
header.forky(lcDataFork) = forkyStore.optimistic_header
|
|
|
|
header
|
2022-06-07 17:01:11 +00:00
|
|
|
else:
|
2023-01-16 15:53:45 +00:00
|
|
|
default(ForkedLightClientHeader)
|
2022-06-07 17:01:11 +00:00
|
|
|
|
|
|
|
body
|
|
|
|
|
2022-12-08 16:24:16 +00:00
|
|
|
var
|
|
|
|
didProgress = false
|
|
|
|
didSignificantProgress = false
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2023-01-24 17:44:55 +00:00
|
|
|
let newIsInitialized = self.store[].kind > LightClientDataFork.None
|
2023-01-16 15:53:45 +00:00
|
|
|
if newIsInitialized > oldIsInitialized:
|
2022-06-07 17:01:11 +00:00
|
|
|
didProgress = true
|
2022-12-08 16:24:16 +00:00
|
|
|
didSignificantProgress = true
|
2022-06-07 17:01:11 +00:00
|
|
|
if self.onStoreInitialized != nil:
|
|
|
|
self.onStoreInitialized()
|
|
|
|
self.onStoreInitialized = nil
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
if oldOptimistic.kind <= lcDataFork:
|
|
|
|
oldOptimistic.migrateToDataFork(lcDataFork)
|
|
|
|
if forkyStore.optimistic_header != oldOptimistic.forky(lcDataFork):
|
|
|
|
didProgress = true
|
|
|
|
when obj isnot SomeForkedLightClientUpdateWithFinality:
|
|
|
|
didSignificantProgress = true
|
|
|
|
if self.onOptimisticHeader != nil:
|
|
|
|
self.onOptimisticHeader()
|
|
|
|
|
|
|
|
if oldFinalized.kind <= lcDataFork:
|
|
|
|
oldFinalized.migrateToDataFork(lcDataFork)
|
|
|
|
if forkyStore.finalized_header != oldFinalized.forky(lcDataFork):
|
|
|
|
didProgress = true
|
|
|
|
didSignificantProgress = true
|
|
|
|
if self.onFinalizedHeader != nil:
|
|
|
|
self.onFinalizedHeader()
|
|
|
|
|
|
|
|
if forkyStore.is_next_sync_committee_known != oldNextCommitteeKnown:
|
|
|
|
didProgress = true
|
2022-12-08 16:24:16 +00:00
|
|
|
|
|
|
|
if didProgress:
|
|
|
|
when obj is Nothing:
|
|
|
|
discard
|
2023-01-12 17:11:38 +00:00
|
|
|
elif obj is ForkedLightClientBootstrap:
|
2022-12-08 16:24:16 +00:00
|
|
|
if self.bootstrapObserver != nil:
|
|
|
|
self.bootstrapObserver(obj)
|
2023-01-12 17:11:38 +00:00
|
|
|
elif obj is ForkedLightClientUpdate:
|
2022-12-08 16:24:16 +00:00
|
|
|
if self.updateObserver != nil:
|
|
|
|
self.updateObserver(obj)
|
2023-01-12 17:11:38 +00:00
|
|
|
elif obj is ForkedLightClientFinalityUpdate:
|
2022-12-08 16:24:16 +00:00
|
|
|
if self.finalityUpdateObserver != nil:
|
|
|
|
self.finalityUpdateObserver(obj)
|
2023-01-12 17:11:38 +00:00
|
|
|
elif obj is ForkedLightClientOptimisticUpdate:
|
2022-12-08 16:24:16 +00:00
|
|
|
if self.optimisticUpdateObserver != nil:
|
|
|
|
self.optimisticUpdateObserver(obj)
|
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
|
|
|
|
didSignificantProgress
|
2022-06-07 17:01:11 +00:00
|
|
|
|
|
|
|
template withReportedProgress(body: untyped): bool =
|
2022-12-08 16:24:16 +00:00
|
|
|
withReportedProgress(Nothing(), body)
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
proc storeObject*(
|
|
|
|
self: var LightClientProcessor,
|
|
|
|
src: MsgSource, wallTime: BeaconTime,
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkedLightClientObject): Result[bool, VerifierError] =
|
2022-05-31 10:45:37 +00:00
|
|
|
## storeObject is the main entry point for unvalidated light client objects -
|
|
|
|
## all untrusted objects pass through here. When storing an object, we will
|
|
|
|
## update the `LightClientStore` accordingly
|
|
|
|
let
|
|
|
|
startTick = Moment.now()
|
2022-12-08 16:24:16 +00:00
|
|
|
didSignificantProgress =
|
|
|
|
withReportedProgress(obj):
|
2022-06-07 17:01:11 +00:00
|
|
|
? self.processObject(obj, wallTime)
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
let
|
|
|
|
storeObjectTick = Moment.now()
|
|
|
|
storeObjectDur = storeObjectTick - startTick
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
light_client_store_object_duration_seconds.observe(
|
|
|
|
storeObjectDur.toFloatSeconds())
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
let objSlot = withForkyObject(obj):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
when forkyObject is ForkyLightClientBootstrap:
|
2023-01-13 15:46:35 +00:00
|
|
|
forkyObject.header.beacon.slot
|
2023-01-12 17:11:38 +00:00
|
|
|
elif forkyObject is SomeForkyLightClientUpdateWithFinality:
|
2023-01-13 15:46:35 +00:00
|
|
|
forkyObject.finalized_header.beacon.slot
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
2023-01-13 15:46:35 +00:00
|
|
|
forkyObject.attested_header.beacon.slot
|
2022-06-07 17:01:11 +00:00
|
|
|
else:
|
2023-01-12 17:11:38 +00:00
|
|
|
GENESIS_SLOT
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(self.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
debug "LC object processed",
|
|
|
|
finalizedSlot = forkyStore.finalized_header.beacon.slot,
|
|
|
|
optimisticSlot = forkyStore.optimistic_header.beacon.slot,
|
|
|
|
kind = typeof(obj).name,
|
|
|
|
objectSlot = objSlot,
|
|
|
|
storeObjectDur
|
2022-12-08 16:24:16 +00:00
|
|
|
ok didSignificantProgress
|
2022-03-17 22:26:56 +00:00
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
proc resetToFinalizedHeader*(
|
|
|
|
self: var LightClientProcessor,
|
2023-01-16 15:53:45 +00:00
|
|
|
header: ForkedLightClientHeader,
|
2022-06-07 17:01:11 +00:00
|
|
|
current_sync_committee: SyncCommittee) =
|
|
|
|
discard withReportedProgress:
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyHeader(header):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
self.store[] = ForkedLightClientStore(kind: lcDataFork)
|
|
|
|
template forkyStore: untyped = self.store[].forky(lcDataFork)
|
|
|
|
forkyStore = lcDataFork.LightClientStore(
|
|
|
|
finalized_header: forkyHeader,
|
|
|
|
current_sync_committee: current_sync_committee,
|
|
|
|
optimistic_header: forkyHeader)
|
|
|
|
debug "LC reset to finalized header",
|
|
|
|
finalizedSlot = forkyStore.finalized_header.beacon.slot,
|
|
|
|
optimisticSlot = forkyStore.optimistic_header.beacon.slot
|
|
|
|
else:
|
|
|
|
self.store[].reset()
|
|
|
|
debug "LC reset"
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2022-03-17 22:26:56 +00:00
|
|
|
# Enqueue
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc addObject*(
|
|
|
|
self: var LightClientProcessor,
|
|
|
|
src: MsgSource,
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkedLightClientObject,
|
2022-11-10 17:40:27 +00:00
|
|
|
resfut: Future[Result[void, VerifierError]] = nil) =
|
2022-03-17 22:26:56 +00:00
|
|
|
## Enqueue a Gossip-validated light client object for verification
|
|
|
|
# Backpressure:
|
|
|
|
# Only one object is validated at any time -
|
|
|
|
# Light client objects are always "fast" to process
|
|
|
|
# Producers:
|
2022-05-23 12:02:54 +00:00
|
|
|
# - Gossip:
|
|
|
|
# - `LightClientFinalityUpdate`
|
|
|
|
# - `LightClientOptimisticUpdate`
|
|
|
|
# - `LightClientManager`:
|
|
|
|
# - `GetLightClientBootstrap`
|
|
|
|
# - `LightClientUpdatesByRange`
|
|
|
|
# - `GetLightClientFinalityUpdate`
|
|
|
|
# - `GetLightClientOptimisticUpdate`
|
2022-03-17 22:26:56 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
wallTime = self.getBeaconTime()
|
|
|
|
(afterGenesis, wallSlot) = wallTime.toSlot()
|
|
|
|
|
|
|
|
if not afterGenesis:
|
2022-05-31 10:45:37 +00:00
|
|
|
error "Processing LC object before genesis, clock turned back?"
|
2022-03-17 22:26:56 +00:00
|
|
|
quit 1
|
|
|
|
|
|
|
|
let res = self.storeObject(src, wallTime, obj)
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
if resfut != nil:
|
|
|
|
if res.isOk:
|
2022-11-10 17:40:27 +00:00
|
|
|
resfut.complete(Result[void, VerifierError].ok())
|
2022-05-31 10:45:37 +00:00
|
|
|
else:
|
2022-11-10 17:40:27 +00:00
|
|
|
resfut.complete(Result[void, VerifierError].err(res.error))
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
# Message validators
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func toValidationError(
|
2022-06-07 17:01:11 +00:00
|
|
|
self: var LightClientProcessor,
|
2022-11-10 17:40:27 +00:00
|
|
|
r: Result[bool, VerifierError],
|
2022-06-07 17:01:11 +00:00
|
|
|
wallTime: BeaconTime,
|
2023-01-12 17:11:38 +00:00
|
|
|
obj: SomeForkedLightClientObject): Result[void, ValidationError] =
|
2022-06-07 17:01:11 +00:00
|
|
|
if r.isOk:
|
2022-12-08 16:24:16 +00:00
|
|
|
let didSignificantProgress = r.get
|
|
|
|
if didSignificantProgress:
|
2022-06-07 17:01:11 +00:00
|
|
|
let
|
2023-01-12 17:11:38 +00:00
|
|
|
signature_slot = withForkyObject(obj):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
forkyObject.signature_slot
|
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
2022-06-07 17:01:11 +00:00
|
|
|
currentTime = wallTime + MAXIMUM_GOSSIP_CLOCK_DISPARITY
|
|
|
|
forwardTime = signature_slot.light_client_finality_update_time
|
|
|
|
if currentTime < forwardTime:
|
|
|
|
# [IGNORE] The `finality_update` is received after the block
|
|
|
|
# at `signature_slot` was given enough time to propagate through
|
|
|
|
# the network.
|
|
|
|
# [IGNORE] The `optimistic_update` is received after the block
|
|
|
|
# at `signature_slot` was given enough time to propagate through
|
|
|
|
# the network.
|
|
|
|
return errIgnore(typeof(obj).name & ": received too early")
|
2022-05-31 10:45:37 +00:00
|
|
|
ok()
|
|
|
|
else:
|
2023-01-12 17:11:38 +00:00
|
|
|
when obj is ForkedLightClientOptimisticUpdate:
|
2022-06-07 17:01:11 +00:00
|
|
|
# [IGNORE] The `optimistic_update` either matches corresponding fields
|
|
|
|
# of the most recently forwarded `LightClientFinalityUpdate` (if any),
|
|
|
|
# or it advances the `optimistic_header` of the local `LightClientStore`
|
2023-01-12 17:11:38 +00:00
|
|
|
if obj.matches(self.latestFinalityUpdate):
|
2022-06-07 17:01:11 +00:00
|
|
|
return ok()
|
|
|
|
# [IGNORE] The `finality_update` advances the `finalized_header` of the
|
|
|
|
# local `LightClientStore`.
|
|
|
|
errIgnore(typeof(obj).name & ": no significant progress")
|
2022-05-31 10:45:37 +00:00
|
|
|
else:
|
2022-06-07 17:01:11 +00:00
|
|
|
case r.error
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.Invalid:
|
2022-06-07 17:01:11 +00:00
|
|
|
# [REJECT] The `finality_update` is valid.
|
|
|
|
# [REJECT] The `optimistic_update` is valid.
|
|
|
|
errReject($r.error)
|
2022-11-10 17:40:27 +00:00
|
|
|
of VerifierError.MissingParent,
|
|
|
|
VerifierError.UnviableFork,
|
|
|
|
VerifierError.Duplicate:
|
2023-01-13 15:46:35 +00:00
|
|
|
# [IGNORE] The `finalized_header.beacon.slot` is greater than that of
|
2023-01-06 16:28:46 +00:00
|
|
|
# all previously forwarded `finality_update`s
|
2023-01-13 15:46:35 +00:00
|
|
|
# [IGNORE] The `attested_header.beacon.slot` is greater than that of all
|
2023-01-06 16:28:46 +00:00
|
|
|
# previously forwarded `optimistic_update`s
|
2022-06-07 17:01:11 +00:00
|
|
|
errIgnore($r.error)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-03-11 01:11:51 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.3/specs/altair/light-client/sync-protocol.md#process_light_client_finality_update
|
2022-07-06 16:11:44 +00:00
|
|
|
proc processLightClientFinalityUpdate*(
|
2022-05-31 10:45:37 +00:00
|
|
|
self: var LightClientProcessor, src: MsgSource,
|
2023-01-12 17:11:38 +00:00
|
|
|
finality_update: ForkedLightClientFinalityUpdate
|
2022-05-31 10:45:37 +00:00
|
|
|
): Result[void, ValidationError] =
|
|
|
|
let
|
|
|
|
wallTime = self.getBeaconTime()
|
|
|
|
r = self.storeObject(src, wallTime, finality_update)
|
2022-06-07 17:01:11 +00:00
|
|
|
v = self.toValidationError(r, wallTime, finality_update)
|
|
|
|
if v.isOk:
|
|
|
|
self.latestFinalityUpdate = finality_update.toOptimistic
|
2022-05-31 10:45:37 +00:00
|
|
|
v
|
|
|
|
|
2023-03-11 01:11:51 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.3/specs/altair/light-client/sync-protocol.md#process_light_client_finality_update
|
2022-07-06 16:11:44 +00:00
|
|
|
proc processLightClientOptimisticUpdate*(
|
2022-05-31 10:45:37 +00:00
|
|
|
self: var LightClientProcessor, src: MsgSource,
|
2023-01-12 17:11:38 +00:00
|
|
|
optimistic_update: ForkedLightClientOptimisticUpdate
|
2022-05-31 10:45:37 +00:00
|
|
|
): Result[void, ValidationError] =
|
|
|
|
let
|
|
|
|
wallTime = self.getBeaconTime()
|
|
|
|
r = self.storeObject(src, wallTime, optimistic_update)
|
2022-06-07 17:01:11 +00:00
|
|
|
v = self.toValidationError(r, wallTime, optimistic_update)
|
|
|
|
if v.isOk:
|
2023-01-12 17:11:38 +00:00
|
|
|
let
|
|
|
|
latestFinalitySlot = withForkyOptimisticUpdate(self.latestFinalityUpdate):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-13 15:46:35 +00:00
|
|
|
forkyOptimisticUpdate.attested_header.beacon.slot
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
|
|
|
attestedSlot = withForkyOptimisticUpdate(optimistic_update):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-13 15:46:35 +00:00
|
|
|
forkyOptimisticUpdate.attested_header.beacon.slot
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
|
|
|
if attestedSlot >= latestFinalitySlot:
|
2022-06-07 17:01:11 +00:00
|
|
|
self.latestFinalityUpdate.reset() # Only forward once
|
2022-05-31 10:45:37 +00:00
|
|
|
v
|