@@ -17,22 +17,15 @@ import
1717logScope: topics = " beacnde"
1818
1919func shouldSyncOptimistically * (node: BeaconNode , wallSlot: Slot ): bool =
20- # Check whether light client is used for syncing
21- let optimisticHeader = node.lightClient.optimisticHeader.valueOr:
22- return false
23-
24- # Check whether light client is sufficiently ahead of DAG
25- const minProgress = 8 * SLOTS_PER_EPOCH # Set arbitrarily
26- let dagSlot = getStateField (node.dag.headState, slot)
27- if dagSlot + minProgress > optimisticHeader.slot:
20+ if node.eth1Monitor == nil :
2821 return false
29-
30- # Check whether light client has synced sufficiently close to wall slot
31- const maxAge = 2 * SLOTS_PER_EPOCH
32- if optimisticHeader.slot < max (wallSlot, maxAge.Slot ) - maxAge:
22+ let optimisticHeader = node.lightClient.optimisticHeader.valueOr:
3323 return false
3424
35- true
25+ shouldSyncOptimistically (
26+ optimisticSlot = optimisticHeader.slot,
27+ dagSlot = getStateField (node.dag.headState, slot),
28+ wallSlot = wallSlot)
3629
3730proc initLightClient * (
3831 node: BeaconNode ,
@@ -43,26 +36,52 @@ proc initLightClient*(
4336 genesis_validators_root: Eth2Digest ) =
4437 template config (): auto = node.config
4538
46- # Creating a light client is not dependent on `lightClientEnable `
39+ # Creating a light client is not dependent on `syncLightClient `
4740 # because the light client module also handles gossip subscriptions
4841 # for broadcasting light client data as a server.
4942
5043 let
5144 optimisticHandler = proc (signedBlock: ForkedMsgTrustedSignedBeaconBlock ):
5245 Future [void ] {.async .} =
53- debug " New LC optimistic block" ,
46+ info " New LC optimistic block" ,
5447 opt = signedBlock.toBlockId (),
5548 dag = node.dag.head.bid,
5649 wallSlot = node.currentSlot
57- return
50+ withBlck (signedBlock):
51+ when stateFork >= BeaconStateFork .Bellatrix :
52+ if blck.message.is_execution_block:
53+ template payload (): auto = blck.message.body.execution_payload
54+
55+ let eth1Monitor = node.eth1Monitor
56+ if eth1Monitor != nil and not payload.block_hash.isZero:
57+ # engine_newPayloadV1
58+ discard await eth1Monitor.newExecutionPayload (payload)
59+
60+ # Retain optimistic head for other `forkchoiceUpdated` callers.
61+ # May temporarily block `forkchoiceUpdatedV1` calls, e.g., Geth:
62+ # - Refuses `newPayload`: "Ignoring payload while snap syncing"
63+ # - Refuses `fcU`: "Forkchoice requested unknown head"
64+ # Once DAG sync catches up or as new optimistic heads are fetched
65+ # the situation recovers
66+ node.consensusManager[].setOptimisticHead (
67+ blck.toBlockId (), payload.block_hash)
68+
69+ # engine_forkchoiceUpdatedV1
70+ let beaconHead = node.attestationPool[].getBeaconHead (nil )
71+ discard await eth1Monitor.runForkchoiceUpdated (
72+ headBlockRoot = payload.block_hash,
73+ safeBlockRoot = beaconHead.safeExecutionPayloadHash,
74+ finalizedBlockRoot = beaconHead.finalizedExecutionPayloadHash)
75+ else : discard
76+
5877 optimisticProcessor = initOptimisticProcessor (
5978 getBeaconTime, optimisticHandler)
6079
6180 lightClient = createLightClient (
6281 node.network, rng, config, cfg, forkDigests, getBeaconTime,
6382 genesis_validators_root, LightClientFinalizationMode .Strict )
6483
65- if config.lightClientEnable :
84+ if config.syncLightClient :
6685 proc onFinalizedHeader (
6786 lightClient: LightClient , finalizedHeader: BeaconBlockHeader ) =
6887 optimisticProcessor.setFinalizedHeader (finalizedHeader)
@@ -73,18 +92,18 @@ proc initLightClient*(
7392
7493 lightClient.onFinalizedHeader = onFinalizedHeader
7594 lightClient.onOptimisticHeader = onOptimisticHeader
76- lightClient.trustedBlockRoot = config.lightClientTrustedBlockRoot
95+ lightClient.trustedBlockRoot = config.trustedBlockRoot
7796
78- elif config.lightClientTrustedBlockRoot .isSome:
79- warn " Ignoring `lightClientTrustedBlockRoot `, light client not enabled" ,
80- lightClientEnable = config.lightClientEnable ,
81- lightClientTrustedBlockRoot = config.lightClientTrustedBlockRoot
97+ elif config.trustedBlockRoot .isSome:
98+ warn " Ignoring `trustedBlockRoot `, light client not enabled" ,
99+ syncLightClient = config.syncLightClient ,
100+ trustedBlockRoot = config.trustedBlockRoot
82101
83102 node.optimisticProcessor = optimisticProcessor
84103 node.lightClient = lightClient
85104
86105proc startLightClient * (node: BeaconNode ) =
87- if not node.config.lightClientEnable :
106+ if not node.config.syncLightClient :
88107 return
89108
90109 node.lightClient.start ()
@@ -94,7 +113,7 @@ proc installLightClientMessageValidators*(node: BeaconNode) =
94113 if node.config.lightClientDataServe:
95114 # Process gossip using both full node and light client
96115 node.processor
97- elif node.config.lightClientEnable :
116+ elif node.config.syncLightClient :
98117 # Only process gossip using light client
99118 nil
100119 else :
@@ -116,9 +135,9 @@ proc updateLightClientGossipStatus*(
116135 node.lightClient.updateGossipStatus (slot, some isBehind)
117136
118137proc updateLightClientFromDag * (node: BeaconNode ) =
119- if not node.config.lightClientEnable :
138+ if not node.config.syncLightClient :
120139 return
121- if node.config.lightClientTrustedBlockRoot .isSome:
140+ if node.config.trustedBlockRoot .isSome:
122141 return
123142
124143 let
0 commit comments