Skip to content

Commit

Permalink
fix: the unknown block sync timeout (#6031)
Browse files Browse the repository at this point in the history
* Fix the unknown block sync timeout

* Remove unused variable

* Recompute the sync check initiate condition

* Move the range sync delay fix when we only have range sync enabled

* chore: rename variable

---------

Co-authored-by: Tuyen Nguyen <[email protected]>
  • Loading branch information
nazarhussain and twoeths authored Oct 13, 2023
1 parent dd57c96 commit 48f9a08
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/beacon-node/src/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,30 @@ export class BeaconSync implements IBeaconSync {
this.rangeSync.on(RangeSyncEvent.completedChain, this.updateSyncState);
this.network.events.on(NetworkEvent.peerConnected, this.addPeer);
this.network.events.on(NetworkEvent.peerDisconnected, this.removePeer);
this.chain.clock.on(ClockEvent.epoch, this.onClockEpoch);
} else {
// test code, this is needed for Unknown block sync sim test
this.unknownBlockSync.subscribeToNetwork();
this.logger.debug("RangeSync disabled.");
}

// TODO: It's okay to start this on initial sync?
this.chain.clock.on(ClockEvent.epoch, this.onClockEpoch);
// In case node is started with `rangeSync` disabled and `unknownBlockSync` is enabled.
// If the epoch boundary happens right away the `onClockEpoch` will check for the `syncDiff` and if
// it's more than 2 epoch will disable the disabling the `unknownBlockSync` as well.
// This will result into node hanging on the head slot and not syncing any blocks.
// This was the scenario in the test case `Unknown block sync` in `packages/cli/test/sim/multi_fork.test.ts`
// So we are adding a particular delay to ensure that the `unknownBlockSync` is enabled.
const syncStartSlot = this.chain.clock.currentSlot;
// Having one epoch time for the node to connect to peers and start a syncing process
const epochCheckForSyncSlot = syncStartSlot + SLOTS_PER_EPOCH;
const initiateEpochCheckForSync = (): void => {
if (this.chain.clock.currentSlot > epochCheckForSyncSlot) {
this.logger.info("Initiating epoch check for sync progress");
this.chain.clock.off(ClockEvent.slot, initiateEpochCheckForSync);
this.chain.clock.on(ClockEvent.epoch, this.onClockEpoch);
}
};
this.chain.clock.on(ClockEvent.slot, initiateEpochCheckForSync);
}

if (metrics) {
metrics.syncStatus.addCollect(() => this.scrapeMetrics(metrics));
Expand Down

0 comments on commit 48f9a08

Please sign in to comment.