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
|
|
|
|
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
./gossip_processing/light_client_processor,
|
2022-08-25 03:53:59 +00:00
|
|
|
./networking/[eth2_network, topic_params],
|
2022-05-31 10:45:37 +00:00
|
|
|
./spec/datatypes/altair,
|
|
|
|
./spec/helpers,
|
|
|
|
./sync/light_client_manager,
|
|
|
|
"."/[beacon_clock, conf_light_client]
|
|
|
|
|
2022-07-21 09:16:10 +00:00
|
|
|
export LightClientFinalizationMode, eth2_network, conf_light_client
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
logScope: topics = "lightcl"
|
|
|
|
|
|
|
|
type
|
2022-08-25 03:53:59 +00:00
|
|
|
LightClientHeaderCallback* =
|
2023-01-16 15:53:45 +00:00
|
|
|
proc(lightClient: LightClient, header: ForkedLightClientHeader) {.
|
2023-08-25 09:29:07 +00:00
|
|
|
gcsafe, raises: [].}
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2022-12-08 16:24:16 +00:00
|
|
|
LightClientValueObserver[V] =
|
2023-08-25 09:29:07 +00:00
|
|
|
proc(lightClient: LightClient, v: V) {.gcsafe, raises: [].}
|
2022-12-08 16:24:16 +00:00
|
|
|
LightClientBootstrapObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
LightClientValueObserver[ForkedLightClientBootstrap]
|
2022-12-08 16:24:16 +00:00
|
|
|
LightClientUpdateObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
LightClientValueObserver[ForkedLightClientUpdate]
|
2022-12-08 16:24:16 +00:00
|
|
|
LightClientFinalityUpdateObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
LightClientValueObserver[ForkedLightClientFinalityUpdate]
|
2022-12-08 16:24:16 +00:00
|
|
|
LightClientOptimisticUpdateObserver* =
|
2023-01-12 17:11:38 +00:00
|
|
|
LightClientValueObserver[ForkedLightClientOptimisticUpdate]
|
2022-12-08 16:24:16 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
LightClient* = ref object
|
|
|
|
network: Eth2Node
|
|
|
|
cfg: RuntimeConfig
|
|
|
|
forkDigests: ref ForkDigests
|
|
|
|
getBeaconTime: GetBeaconTimeFn
|
2023-01-16 15:53:45 +00:00
|
|
|
store: ref ForkedLightClientStore
|
2022-05-31 10:45:37 +00:00
|
|
|
processor: ref LightClientProcessor
|
|
|
|
manager: LightClientManager
|
|
|
|
gossipState: GossipState
|
2022-08-25 03:53:59 +00:00
|
|
|
onFinalizedHeader*, onOptimisticHeader*: LightClientHeaderCallback
|
2022-12-08 16:24:16 +00:00
|
|
|
bootstrapObserver*: LightClientBootstrapObserver
|
|
|
|
updateObserver*: LightClientUpdateObserver
|
|
|
|
finalityUpdateObserver*: LightClientFinalityUpdateObserver
|
|
|
|
optimisticUpdateObserver*: LightClientOptimisticUpdateObserver
|
2022-05-31 10:45:37 +00:00
|
|
|
trustedBlockRoot*: Option[Eth2Digest]
|
|
|
|
|
2023-01-13 15:46:35 +00:00
|
|
|
func finalizedHeader*(
|
2023-01-16 15:53:45 +00:00
|
|
|
lightClient: LightClient): ForkedLightClientHeader =
|
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-10-04 16:11:45 +00:00
|
|
|
ForkedLightClientHeader.init(forkyStore.finalized_header)
|
2023-01-16 15:53:45 +00:00
|
|
|
else:
|
|
|
|
default(ForkedLightClientHeader)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-01-13 15:46:35 +00:00
|
|
|
func optimisticHeader*(
|
2023-01-16 15:53:45 +00:00
|
|
|
lightClient: LightClient): ForkedLightClientHeader =
|
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-10-04 16:11:45 +00:00
|
|
|
ForkedLightClientHeader.init(forkyStore.optimistic_header)
|
2023-01-16 15:53:45 +00:00
|
|
|
else:
|
|
|
|
default(ForkedLightClientHeader)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2022-11-30 03:45:03 +00:00
|
|
|
func finalizedSyncCommittee*(
|
|
|
|
lightClient: LightClient): Opt[altair.SyncCommittee] =
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
ok forkyStore.current_sync_committee
|
|
|
|
else:
|
|
|
|
Opt.none(altair.SyncCommittee)
|
2022-11-30 03:45:03 +00:00
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
proc createLightClient(
|
|
|
|
network: Eth2Node,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2022-05-31 10:45:37 +00:00
|
|
|
dumpEnabled: bool,
|
|
|
|
dumpDirInvalid, dumpDirIncoming: string,
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
forkDigests: ref ForkDigests,
|
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
2022-07-21 09:16:10 +00:00
|
|
|
genesis_validators_root: Eth2Digest,
|
2023-05-02 11:06:02 +00:00
|
|
|
finalizationMode: LightClientFinalizationMode,
|
|
|
|
strictVerification = false
|
2022-05-31 10:45:37 +00:00
|
|
|
): LightClient =
|
|
|
|
let lightClient = LightClient(
|
|
|
|
network: network,
|
|
|
|
cfg: cfg,
|
|
|
|
forkDigests: forkDigests,
|
|
|
|
getBeaconTime: getBeaconTime,
|
2023-01-16 15:53:45 +00:00
|
|
|
store: (ref ForkedLightClientStore)())
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
func getTrustedBlockRoot(): Option[Eth2Digest] =
|
|
|
|
lightClient.trustedBlockRoot
|
|
|
|
|
|
|
|
proc onStoreInitialized() =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc onFinalizedHeader() =
|
|
|
|
if lightClient.onFinalizedHeader != nil:
|
2022-08-25 03:53:59 +00:00
|
|
|
lightClient.onFinalizedHeader(
|
2023-01-16 15:53:45 +00:00
|
|
|
lightClient, lightClient.finalizedHeader)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
proc onOptimisticHeader() =
|
|
|
|
if lightClient.onOptimisticHeader != nil:
|
2022-08-25 03:53:59 +00:00
|
|
|
lightClient.onOptimisticHeader(
|
2023-01-16 15:53:45 +00:00
|
|
|
lightClient, lightClient.optimisticHeader)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
proc bootstrapObserver(obj: ForkedLightClientBootstrap) =
|
2022-12-08 16:24:16 +00:00
|
|
|
if lightClient.bootstrapObserver != nil:
|
|
|
|
lightClient.bootstrapObserver(lightClient, obj)
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
proc updateObserver(obj: ForkedLightClientUpdate) =
|
2022-12-08 16:24:16 +00:00
|
|
|
if lightClient.updateObserver != nil:
|
|
|
|
lightClient.updateObserver(lightClient, obj)
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
proc finalityObserver(obj: ForkedLightClientFinalityUpdate) =
|
2022-12-08 16:24:16 +00:00
|
|
|
if lightClient.finalityUpdateObserver != nil:
|
|
|
|
lightClient.finalityUpdateObserver(lightClient, obj)
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
proc optimisticObserver(obj: ForkedLightClientOptimisticUpdate) =
|
2022-12-08 16:24:16 +00:00
|
|
|
if lightClient.optimisticUpdateObserver != nil:
|
|
|
|
lightClient.optimisticUpdateObserver(lightClient, obj)
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.processor = LightClientProcessor.new(
|
|
|
|
dumpEnabled, dumpDirInvalid, dumpDirIncoming,
|
2022-07-21 09:16:10 +00:00
|
|
|
cfg, genesis_validators_root, finalizationMode,
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.store, getBeaconTime, getTrustedBlockRoot,
|
2022-12-08 16:24:16 +00:00
|
|
|
onStoreInitialized, onFinalizedHeader, onOptimisticHeader,
|
2023-05-02 11:06:02 +00:00
|
|
|
bootstrapObserver, updateObserver, finalityObserver, optimisticObserver,
|
|
|
|
strictVerification)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
proc lightClientVerifier(obj: SomeForkedLightClientObject):
|
2022-11-10 17:40:27 +00:00
|
|
|
Future[Result[void, VerifierError]] =
|
|
|
|
let resfut = newFuture[Result[void, VerifierError]]("lightClientVerifier")
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.processor[].addObject(MsgSource.gossip, obj, resfut)
|
|
|
|
resfut
|
2023-01-12 17:11:38 +00:00
|
|
|
proc bootstrapVerifier(obj: ForkedLightClientBootstrap): auto =
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClientVerifier(obj)
|
2023-01-12 17:11:38 +00:00
|
|
|
proc updateVerifier(obj: ForkedLightClientUpdate): auto =
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClientVerifier(obj)
|
2023-01-12 17:11:38 +00:00
|
|
|
proc finalityVerifier(obj: ForkedLightClientFinalityUpdate): auto =
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClientVerifier(obj)
|
2023-01-12 17:11:38 +00:00
|
|
|
proc optimisticVerifier(obj: ForkedLightClientOptimisticUpdate): auto =
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClientVerifier(obj)
|
|
|
|
|
|
|
|
func isLightClientStoreInitialized(): bool =
|
2023-01-16 15:53:45 +00:00
|
|
|
lightClient.store[].kind > LightClientDataFork.None
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
func isNextSyncCommitteeKnown(): bool =
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.is_next_sync_committee_known
|
|
|
|
else:
|
|
|
|
false
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
func getFinalizedPeriod(): SyncCommitteePeriod =
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.finalized_header.beacon.slot.sync_committee_period
|
|
|
|
else:
|
|
|
|
GENESIS_SLOT.sync_committee_period
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
func getOptimisticPeriod(): SyncCommitteePeriod =
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.optimistic_header.beacon.slot.sync_committee_period
|
|
|
|
else:
|
|
|
|
GENESIS_SLOT.sync_committee_period
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
lightClient.manager = LightClientManager.init(
|
|
|
|
lightClient.network, rng, getTrustedBlockRoot,
|
|
|
|
bootstrapVerifier, updateVerifier, finalityVerifier, optimisticVerifier,
|
|
|
|
isLightClientStoreInitialized, isNextSyncCommitteeKnown,
|
|
|
|
getFinalizedPeriod, getOptimisticPeriod, getBeaconTime)
|
|
|
|
|
|
|
|
lightClient.gossipState = {}
|
|
|
|
|
|
|
|
lightClient
|
|
|
|
|
|
|
|
proc createLightClient*(
|
|
|
|
network: Eth2Node,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2022-05-31 10:45:37 +00:00
|
|
|
config: BeaconNodeConf,
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
forkDigests: ref ForkDigests,
|
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
2022-07-21 09:16:10 +00:00
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
finalizationMode: LightClientFinalizationMode
|
2022-05-31 10:45:37 +00:00
|
|
|
): LightClient =
|
|
|
|
createLightClient(
|
|
|
|
network, rng,
|
|
|
|
config.dumpEnabled, config.dumpDirInvalid, config.dumpDirIncoming,
|
2023-05-02 11:06:02 +00:00
|
|
|
cfg, forkDigests, getBeaconTime, genesis_validators_root, finalizationMode,
|
|
|
|
strictVerification = config.strictVerification)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
proc createLightClient*(
|
|
|
|
network: Eth2Node,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2022-05-31 10:45:37 +00:00
|
|
|
config: LightClientConf,
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
forkDigests: ref ForkDigests,
|
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
2022-07-21 09:16:10 +00:00
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
finalizationMode: LightClientFinalizationMode
|
2022-05-31 10:45:37 +00:00
|
|
|
): LightClient =
|
|
|
|
createLightClient(
|
|
|
|
network, rng,
|
|
|
|
dumpEnabled = false, dumpDirInvalid = ".", dumpDirIncoming = ".",
|
2022-07-21 09:16:10 +00:00
|
|
|
cfg, forkDigests, getBeaconTime, genesis_validators_root, finalizationMode)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
proc start*(lightClient: LightClient) =
|
|
|
|
notice "Starting light client",
|
|
|
|
trusted_block_root = lightClient.trustedBlockRoot
|
|
|
|
lightClient.manager.start()
|
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
proc resetToFinalizedHeader*(
|
|
|
|
lightClient: LightClient,
|
2023-01-16 15:53:45 +00:00
|
|
|
header: ForkedLightClientHeader,
|
|
|
|
current_sync_committee: altair.SyncCommittee) =
|
2022-06-07 17:01:11 +00:00
|
|
|
lightClient.processor[].resetToFinalizedHeader(header, current_sync_committee)
|
|
|
|
|
|
|
|
import metrics
|
|
|
|
|
|
|
|
from
|
|
|
|
./gossip_processing/eth2_processor
|
|
|
|
import
|
2022-07-06 16:11:44 +00:00
|
|
|
processLightClientFinalityUpdate, processLightClientOptimisticUpdate
|
2022-06-07 17:01:11 +00:00
|
|
|
|
|
|
|
declareCounter beacon_light_client_finality_updates_received,
|
|
|
|
"Number of valid LC finality updates processed by this node"
|
|
|
|
declareCounter beacon_light_client_finality_updates_dropped,
|
|
|
|
"Number of invalid LC finality updates dropped by this node", labels = ["reason"]
|
|
|
|
declareCounter beacon_light_client_optimistic_updates_received,
|
|
|
|
"Number of valid LC optimistic updates processed by this node"
|
|
|
|
declareCounter beacon_light_client_optimistic_updates_dropped,
|
|
|
|
"Number of invalid LC optimistic updates dropped by this node", labels = ["reason"]
|
|
|
|
|
|
|
|
template logReceived(
|
2023-01-12 17:11:38 +00:00
|
|
|
msg: ForkyLightClientFinalityUpdate) =
|
2022-06-07 17:01:11 +00:00
|
|
|
debug "LC finality update received", finality_update = msg
|
|
|
|
|
|
|
|
template logValidated(
|
2023-01-12 17:11:38 +00:00
|
|
|
msg: ForkyLightClientFinalityUpdate) =
|
2022-06-07 17:01:11 +00:00
|
|
|
trace "LC finality update validated", finality_update = msg
|
|
|
|
beacon_light_client_finality_updates_received.inc()
|
|
|
|
|
|
|
|
proc logDropped(
|
2023-01-12 17:11:38 +00:00
|
|
|
msg: ForkyLightClientFinalityUpdate, es: varargs[ValidationError]) =
|
2022-06-07 17:01:11 +00:00
|
|
|
for e in es:
|
|
|
|
debug "Dropping LC finality update", finality_update = msg, error = e
|
|
|
|
beacon_light_client_finality_updates_dropped.inc(1, [$es[0][0]])
|
|
|
|
|
|
|
|
template logReceived(
|
2023-01-12 17:11:38 +00:00
|
|
|
msg: ForkyLightClientOptimisticUpdate) =
|
2022-06-07 17:01:11 +00:00
|
|
|
debug "LC optimistic update received", optimistic_update = msg
|
|
|
|
|
|
|
|
template logValidated(
|
2023-01-12 17:11:38 +00:00
|
|
|
msg: ForkyLightClientOptimisticUpdate) =
|
2022-06-07 17:01:11 +00:00
|
|
|
trace "LC optimistic update validated", optimistic_update = msg
|
|
|
|
beacon_light_client_optimistic_updates_received.inc()
|
|
|
|
|
|
|
|
proc logDropped(
|
2023-01-12 17:11:38 +00:00
|
|
|
msg: ForkyLightClientOptimisticUpdate, es: varargs[ValidationError]) =
|
2022-06-07 17:01:11 +00:00
|
|
|
for e in es:
|
|
|
|
debug "Dropping LC optimistic update", optimistic_update = msg, error = e
|
|
|
|
beacon_light_client_optimistic_updates_dropped.inc(1, [$es[0][0]])
|
|
|
|
|
|
|
|
proc installMessageValidators*(
|
|
|
|
lightClient: LightClient, eth2Processor: ref Eth2Processor = nil) =
|
|
|
|
# When registering multiple message validators, IGNORE results take precedence
|
|
|
|
# over ACCEPT results. However, because the opposite behaviour is needed here,
|
|
|
|
# we handle both full node and light client validation in this module
|
2022-05-31 10:45:37 +00:00
|
|
|
template getLocalWallPeriod(): auto =
|
|
|
|
lightClient.getBeaconTime().slotOrZero().sync_committee_period
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
template validate[T: SomeForkyLightClientObject](
|
|
|
|
msg: T,
|
2023-01-28 19:53:41 +00:00
|
|
|
contextFork: ConsensusFork,
|
2023-01-12 17:11:38 +00:00
|
|
|
validatorProcName: untyped): ValidationResult =
|
2022-06-07 17:01:11 +00:00
|
|
|
msg.logReceived()
|
|
|
|
|
2023-02-16 09:32:12 +00:00
|
|
|
if contextFork != lightClient.cfg.consensusForkAtEpoch(msg.contextEpoch):
|
2023-01-14 21:19:50 +00:00
|
|
|
msg.logDropped(
|
|
|
|
(ValidationResult.Reject, cstring "Invalid context fork"))
|
2023-01-12 17:11:38 +00:00
|
|
|
return ValidationResult.Reject
|
|
|
|
|
2023-10-04 16:11:45 +00:00
|
|
|
let obj = T.Forked.init(msg)
|
2023-01-12 17:11:38 +00:00
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
var
|
|
|
|
ignoreErrors {.noinit.}: array[2, ValidationError]
|
|
|
|
numIgnoreErrors = 0
|
|
|
|
|
|
|
|
let res1 =
|
|
|
|
if eth2Processor != nil:
|
|
|
|
let
|
2023-01-12 17:11:38 +00:00
|
|
|
v = eth2Processor[].`validatorProcName`(MsgSource.gossip, obj)
|
2022-06-07 17:01:11 +00:00
|
|
|
res = v.toValidationResult()
|
|
|
|
if res == ValidationResult.Reject:
|
|
|
|
msg.logDropped(v.error)
|
|
|
|
return res
|
|
|
|
if res == ValidationResult.Ignore:
|
|
|
|
ignoreErrors[numIgnoreErrors] = v.error
|
|
|
|
inc numIgnoreErrors
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
ValidationResult.Ignore
|
|
|
|
|
|
|
|
let res2 =
|
|
|
|
if lightClient.manager.isGossipSupported(getLocalWallPeriod()):
|
|
|
|
let
|
2023-01-12 17:11:38 +00:00
|
|
|
v = lightClient.processor[].`validatorProcName`(MsgSource.gossip, obj)
|
2022-06-07 17:01:11 +00:00
|
|
|
res = v.toValidationResult()
|
|
|
|
if res == ValidationResult.Reject:
|
|
|
|
msg.logDropped(v.error)
|
|
|
|
return res
|
|
|
|
if res == ValidationResult.Ignore:
|
|
|
|
ignoreErrors[numIgnoreErrors] = v.error
|
|
|
|
inc numIgnoreErrors
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
ValidationResult.Ignore
|
|
|
|
|
|
|
|
if res1 == ValidationResult.Accept or res2 == ValidationResult.Accept:
|
|
|
|
msg.logValidated()
|
|
|
|
return ValidationResult.Accept
|
|
|
|
|
|
|
|
doAssert res1 == ValidationResult.Ignore and res2 == ValidationResult.Ignore
|
|
|
|
if numIgnoreErrors == 0:
|
|
|
|
ignoreErrors[numIgnoreErrors] = static:
|
|
|
|
(ValidationResult.Ignore, cstring T.name & ": irrelevant")
|
|
|
|
inc numIgnoreErrors
|
|
|
|
msg.logDropped(ignoreErrors.toOpenArray(0, numIgnoreErrors - 1))
|
|
|
|
ValidationResult.Ignore
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
let forkDigests = lightClient.forkDigests
|
2023-03-11 00:35:52 +00:00
|
|
|
for consensusFork in ConsensusFork:
|
2023-03-11 20:09:21 +00:00
|
|
|
withLcDataFork(lcDataForkAtConsensusFork(consensusFork)):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-16 15:53:45 +00:00
|
|
|
let
|
2023-03-11 00:35:52 +00:00
|
|
|
contextFork = consensusFork # Avoid capturing `Deneb` (Nim 1.6)
|
2023-03-11 14:39:29 +00:00
|
|
|
digest = forkDigests[].atConsensusFork(contextFork)
|
2023-01-14 21:19:50 +00:00
|
|
|
|
2023-05-16 07:46:41 +00:00
|
|
|
# light_client_optimistic_update
|
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#light_client_finality_update
|
2023-01-14 21:19:50 +00:00
|
|
|
lightClient.network.addValidator(
|
2023-05-16 07:46:41 +00:00
|
|
|
getLightClientFinalityUpdateTopic(digest), proc (
|
|
|
|
msg: lcDataFork.LightClientFinalityUpdate
|
|
|
|
): ValidationResult =
|
2023-01-16 15:53:45 +00:00
|
|
|
validate(msg, contextFork, processLightClientFinalityUpdate))
|
2023-01-14 21:19:50 +00:00
|
|
|
|
2023-05-16 07:46:41 +00:00
|
|
|
# light_client_optimistic_update
|
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#light_client_optimistic_update
|
2023-01-14 21:19:50 +00:00
|
|
|
lightClient.network.addValidator(
|
2023-05-16 07:46:41 +00:00
|
|
|
getLightClientOptimisticUpdateTopic(digest), proc (
|
|
|
|
msg: lcDataFork.LightClientOptimisticUpdate
|
|
|
|
): ValidationResult =
|
2023-01-16 15:53:45 +00:00
|
|
|
validate(msg, contextFork, processLightClientOptimisticUpdate))
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
proc updateGossipStatus*(
|
|
|
|
lightClient: LightClient, slot: Slot, dagIsBehind = default(Option[bool])) =
|
2022-06-07 17:01:11 +00:00
|
|
|
template cfg(): auto = lightClient.cfg
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
let
|
2022-06-07 17:01:11 +00:00
|
|
|
epoch = slot.epoch
|
|
|
|
|
|
|
|
lcBehind =
|
|
|
|
not lightClient.manager.isGossipSupported(slot.sync_committee_period)
|
|
|
|
dagBehind =
|
|
|
|
# While separate message validators can be installed for both
|
|
|
|
# full node and light client (both are called unless one rejects msg),
|
|
|
|
# only a single subscription is supported per topic.
|
|
|
|
# The full node subscription is also handled in this module, even though
|
|
|
|
# it does not directly relate to the client side of the LC sync protocol
|
|
|
|
dagIsBehind.get(true)
|
|
|
|
isBehind = lcBehind and dagBehind
|
|
|
|
|
|
|
|
currentEpochTargetGossipState = getTargetGossipState(
|
2022-12-04 07:42:03 +00:00
|
|
|
epoch, cfg.ALTAIR_FORK_EPOCH, cfg.BELLATRIX_FORK_EPOCH,
|
2023-02-15 14:44:09 +00:00
|
|
|
cfg.CAPELLA_FORK_EPOCH, cfg.DENEB_FORK_EPOCH, isBehind)
|
2022-05-31 10:45:37 +00:00
|
|
|
targetGossipState =
|
2022-06-07 17:01:11 +00:00
|
|
|
if lcBehind or epoch < 1:
|
|
|
|
currentEpochTargetGossipState
|
|
|
|
else:
|
|
|
|
# The fork digest for light client topics depends on the attested slot,
|
|
|
|
# which is in the past relative to the signature slot (current slot).
|
|
|
|
# Therefore, LC topic subscriptions are kept for 1 extra epoch.
|
|
|
|
let previousEpochTargetGossipState = getTargetGossipState(
|
2022-12-04 07:42:03 +00:00
|
|
|
epoch - 1, cfg.ALTAIR_FORK_EPOCH, cfg.BELLATRIX_FORK_EPOCH,
|
2023-02-15 14:44:09 +00:00
|
|
|
cfg.CAPELLA_FORK_EPOCH, cfg.DENEB_FORK_EPOCH, isBehind)
|
2022-06-07 17:01:11 +00:00
|
|
|
currentEpochTargetGossipState + previousEpochTargetGossipState
|
|
|
|
|
2022-05-31 10:45:37 +00:00
|
|
|
template currentGossipState(): auto = lightClient.gossipState
|
|
|
|
if currentGossipState == targetGossipState:
|
|
|
|
return
|
|
|
|
|
|
|
|
if currentGossipState.card == 0 and targetGossipState.card > 0:
|
|
|
|
debug "Enabling LC topic subscriptions",
|
|
|
|
wallSlot = slot, targetGossipState
|
|
|
|
elif currentGossipState.card > 0 and targetGossipState.card == 0:
|
|
|
|
debug "Disabling LC topic subscriptions",
|
|
|
|
wallSlot = slot
|
|
|
|
else:
|
|
|
|
# Individual forks added / removed
|
|
|
|
discard
|
|
|
|
|
|
|
|
let
|
|
|
|
newGossipForks = targetGossipState - currentGossipState
|
|
|
|
oldGossipForks = currentGossipState - targetGossipState
|
|
|
|
|
|
|
|
for gossipFork in oldGossipForks:
|
2023-01-28 19:53:41 +00:00
|
|
|
if gossipFork >= ConsensusFork.Altair:
|
2023-03-11 14:39:29 +00:00
|
|
|
let forkDigest = lightClient.forkDigests[].atConsensusFork(gossipFork)
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.network.unsubscribe(
|
|
|
|
getLightClientFinalityUpdateTopic(forkDigest))
|
2022-06-07 17:01:11 +00:00
|
|
|
lightClient.network.unsubscribe(
|
|
|
|
getLightClientOptimisticUpdateTopic(forkDigest))
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
for gossipFork in newGossipForks:
|
2023-01-28 19:53:41 +00:00
|
|
|
if gossipFork >= ConsensusFork.Altair:
|
2023-03-11 14:39:29 +00:00
|
|
|
let forkDigest = lightClient.forkDigests[].atConsensusFork(gossipFork)
|
2022-06-07 17:01:11 +00:00
|
|
|
lightClient.network.subscribe(
|
|
|
|
getLightClientFinalityUpdateTopic(forkDigest),
|
2022-08-25 03:53:59 +00:00
|
|
|
basicParams)
|
2022-05-31 10:45:37 +00:00
|
|
|
lightClient.network.subscribe(
|
|
|
|
getLightClientOptimisticUpdateTopic(forkDigest),
|
2022-08-25 03:53:59 +00:00
|
|
|
basicParams)
|
2022-05-31 10:45:37 +00:00
|
|
|
|
|
|
|
lightClient.gossipState = targetGossipState
|