From da3048709e3ee0b3ee35c056bd9c5dae15ab806a Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Tue, 25 Jul 2023 10:13:20 +0700 Subject: [PATCH] chore: add more comments --- packages/beacon-node/src/api/impl/validator/index.ts | 4 ++-- packages/validator/src/services/blockDuties.ts | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/beacon-node/src/api/impl/validator/index.ts b/packages/beacon-node/src/api/impl/validator/index.ts index 99dd03763ab4..27efa389a11e 100644 --- a/packages/beacon-node/src/api/impl/validator/index.ts +++ b/packages/beacon-node/src/api/impl/validator/index.ts @@ -405,11 +405,11 @@ export function getValidatorApi({ const head = chain.forkChoice.getHead(); let state: CachedBeaconStateAllForks | undefined = undefined; const prepareNextSlotLookAheadMs = (config.SECONDS_PER_SLOT * 1000) / SCHEDULER_LOOKAHEAD_FACTOR; - const cpState = chain.regen.getCheckpointStateSync({rootHex: head.blockRoot, epoch}); const toNextEpochMs = msToNextEpoch(); // validators may request next epoch's duties when it's close to next epoch - // return that asap if PrepareNextSlot already compute beacon proposers for next epoch + // this is to avoid missed block proposal due to 0 epoch look ahead if (epoch === nextEpoch && toNextEpochMs < prepareNextSlotLookAheadMs) { + const cpState = chain.regen.getCheckpointStateSync({rootHex: head.blockRoot, epoch}); if (cpState) { state = cpState; metrics?.duties.requestNextEpochProposalDutiesHit.inc(); diff --git a/packages/validator/src/services/blockDuties.ts b/packages/validator/src/services/blockDuties.ts index a6bf0de3088f..65b98ce3b1f0 100644 --- a/packages/validator/src/services/blockDuties.ts +++ b/packages/validator/src/services/blockDuties.ts @@ -12,7 +12,7 @@ import {ValidatorStore} from "./validatorStore.js"; /** This polls block duties 1s before the next epoch */ // TODO: change to 6 to do it 2s before the next epoch // once we have some improvement on epoch transition time -// see https://github.com/ChainSafe/lodestar/issues/5409 +// see https://github.com/ChainSafe/lodestar/issues/5792#issuecomment-1647457442 const BLOCK_DUTIES_LOOKAHEAD_FACTOR = 12; /** Only retain `HISTORICAL_DUTIES_EPOCHS` duties prior to the current epoch */ const HISTORICAL_DUTIES_EPOCHS = 2; @@ -125,8 +125,8 @@ export class BlockDutiesService { * through the slow path every time. I.e., the proposal will only happen after we've been able to * download and process the duties from the BN. This means it is very important to ensure this * function is as fast as possible. - * - Starting from Jul 2023, if PrepareNextSlotScheduler runs well in bn we already have proposers of next epoch - * some time (< 1/3 slot) before the next epoch + * - Starting from Jul 2023, we poll proposers 1s before the next epoch thanks to PrepareNextSlotScheduler + * usually finishes in 3s. */ private async pollBeaconProposersAndNotify(currentSlot: Slot, signal: AbortSignal): Promise { const nextEpoch = computeEpochAtSlot(currentSlot) + 1; @@ -162,6 +162,10 @@ export class BlockDutiesService { } } + /** + * This is to avoid some delay on the first slot of the opoch when validators has proposal duties. + * See https://github.com/ChainSafe/lodestar/issues/5792 + */ private async pollBeaconProposersNextEpoch(currentSlot: Slot, nextEpoch: Epoch, signal: AbortSignal): Promise { const nextSlot = currentSlot + 1; const lookAheadMs = (this.config.SECONDS_PER_SLOT * 1000) / BLOCK_DUTIES_LOOKAHEAD_FACTOR;