Skip to content

Commit

Permalink
Move the range sync delay fix when we only have range sync enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Oct 12, 2023
1 parent 39a0c5b commit 20828d4
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions packages/beacon-node/src/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +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.");
}

// 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 epochCheckFotSyncSlot = syncStartSlot + SLOTS_PER_EPOCH;
const initiateEpochCheckForSync = (): void => {
if (this.chain.clock.currentSlot > epochCheckFotSyncSlot) {
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);
// 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 epochCheckFotSyncSlot = syncStartSlot + SLOTS_PER_EPOCH;
const initiateEpochCheckForSync = (): void => {
if (this.chain.clock.currentSlot > epochCheckFotSyncSlot) {
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 20828d4

Please sign in to comment.