Skip to content

Commit

Permalink
inhibit LC sync while DAG is synced (#6505)
Browse files Browse the repository at this point in the history
Normally, running LC and DAG sync at same time is fine, but on tiny
devnet where some peer may not support the LC data, we can end up in
situation where peer gets disconnected when DAG is in sync, because
DAG sync never uses any req/resp on local devnet (perfect nw conditions)
so the LC sync over minutes removes the peer as sync is stuck.

We don't need to actively sync LC from network if DAG is already synced,
preventing this specific low peer devnet issue (there are others still).
LC is still locally updated when DAG finalized checkpoint advances.
  • Loading branch information
etan-status authored Aug 22, 2024
1 parent a597fe9 commit bd09e4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
8 changes: 7 additions & 1 deletion beacon_chain/beacon_node_light_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ proc initLightClient*(
optimisticProcessor = initOptimisticProcessor(
getBeaconTime, optimisticHandler)

shouldInhibitSync = func(): bool =
if node.syncManager != nil:
not node.syncManager.inProgress # No LC sync needed if DAG is in sync
else:
false
lightClient = createLightClient(
node.network, rng, config, cfg, forkDigests, getBeaconTime,
genesis_validators_root, LightClientFinalizationMode.Strict)
genesis_validators_root, LightClientFinalizationMode.Strict,
shouldInhibitSync = shouldInhibitSync)

if config.syncLightClient:
proc onOptimisticHeader(
Expand Down
18 changes: 12 additions & 6 deletions beacon_chain/light_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ proc createLightClient(
getBeaconTime: GetBeaconTimeFn,
genesis_validators_root: Eth2Digest,
finalizationMode: LightClientFinalizationMode,
strictVerification = false
strictVerification = false,
shouldInhibitSync: light_client_manager.GetBoolCallback = nil
): LightClient =
let lightClient = LightClient(
network: network,
Expand Down Expand Up @@ -177,7 +178,8 @@ proc createLightClient(
lightClient.network, rng, getTrustedBlockRoot,
bootstrapVerifier, updateVerifier, finalityVerifier, optimisticVerifier,
isLightClientStoreInitialized, isNextSyncCommitteeKnown,
getFinalizedPeriod, getOptimisticPeriod, getBeaconTime)
getFinalizedPeriod, getOptimisticPeriod, getBeaconTime,
shouldInhibitSync = shouldInhibitSync)

lightClient.gossipState = {}

Expand All @@ -191,13 +193,15 @@ proc createLightClient*(
forkDigests: ref ForkDigests,
getBeaconTime: GetBeaconTimeFn,
genesis_validators_root: Eth2Digest,
finalizationMode: LightClientFinalizationMode
finalizationMode: LightClientFinalizationMode,
shouldInhibitSync: light_client_manager.GetBoolCallback = nil
): LightClient =
createLightClient(
network, rng,
config.dumpEnabled, config.dumpDirInvalid, config.dumpDirIncoming,
cfg, forkDigests, getBeaconTime, genesis_validators_root, finalizationMode,
strictVerification = config.strictVerification)
strictVerification = config.strictVerification,
shouldInhibitSync = shouldInhibitSync)

proc createLightClient*(
network: Eth2Node,
Expand All @@ -207,12 +211,14 @@ proc createLightClient*(
forkDigests: ref ForkDigests,
getBeaconTime: GetBeaconTimeFn,
genesis_validators_root: Eth2Digest,
finalizationMode: LightClientFinalizationMode
finalizationMode: LightClientFinalizationMode,
shouldInhibitSync: light_client_manager.GetBoolCallback = nil
): LightClient =
createLightClient(
network, rng,
dumpEnabled = false, dumpDirInvalid = ".", dumpDirIncoming = ".",
cfg, forkDigests, getBeaconTime, genesis_validators_root, finalizationMode)
cfg, forkDigests, getBeaconTime, genesis_validators_root, finalizationMode,
shouldInhibitSync = shouldInhibitSync)

proc start*(lightClient: LightClient) =
notice "Starting light client",
Expand Down
9 changes: 6 additions & 3 deletions beacon_chain/sync/light_client_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type
getFinalizedPeriod: GetSyncCommitteePeriodCallback
getOptimisticPeriod: GetSyncCommitteePeriodCallback
getBeaconTime: GetBeaconTimeFn
shouldInhibitSync: GetBoolCallback
loopFuture: Future[void].Raising([CancelledError])

func init*(
Expand All @@ -80,7 +81,8 @@ func init*(
isNextSyncCommitteeKnown: GetBoolCallback,
getFinalizedPeriod: GetSyncCommitteePeriodCallback,
getOptimisticPeriod: GetSyncCommitteePeriodCallback,
getBeaconTime: GetBeaconTimeFn
getBeaconTime: GetBeaconTimeFn,
shouldInhibitSync: GetBoolCallback = nil
): LightClientManager =
## Initialize light client manager.
LightClientManager(
Expand All @@ -95,8 +97,8 @@ func init*(
isNextSyncCommitteeKnown: isNextSyncCommitteeKnown,
getFinalizedPeriod: getFinalizedPeriod,
getOptimisticPeriod: getOptimisticPeriod,
getBeaconTime: getBeaconTime
)
getBeaconTime: getBeaconTime,
shouldInhibitSync: shouldInhibitSync)

proc isGossipSupported*(
self: LightClientManager,
Expand Down Expand Up @@ -335,6 +337,7 @@ proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} =
# Periodically wake and check for changes
let wallTime = self.getBeaconTime()
if wallTime < nextSyncTaskTime or
(self.shouldInhibitSync != nil and self.shouldInhibitSync()) or
self.network.peerPool.lenAvailable < 1:
await sleepAsync(chronos.seconds(2))
continue
Expand Down

0 comments on commit bd09e4d

Please sign in to comment.