diff --git a/packages/beacon-node/src/sync/sync.ts b/packages/beacon-node/src/sync/sync.ts index 3e325794dd64..3da201269aad 100644 --- a/packages/beacon-node/src/sync/sync.ts +++ b/packages/beacon-node/src/sync/sync.ts @@ -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));