Skip to content

Commit

Permalink
refactor: use tuyen suggestion to await for shuffling keeping state-t…
Browse files Browse the repository at this point in the history
…ransition sync
  • Loading branch information
matthewkeil committed Oct 14, 2024
1 parent 440c2a2 commit 9cb0c6f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,11 @@ export function getValidatorApi(
break;

case stateEpoch + 1:
// make sure shuffling is calculated and ready for next call to calculate nextProposers
await chain.shufflingCache.get(state.epochCtx.nextEpoch, state.epochCtx.nextDecisionRoot);
// Requesting duties for next epoch is allowed since they can be predicted with high probabilities.
// @see `epochCtx.getBeaconProposersNextEpoch` JSDocs for rationale.
indexes = await state.epochCtx.getBeaconProposersNextEpoch();
indexes = state.epochCtx.getBeaconProposersNextEpoch();
break;

case stateEpoch - 1: {
Expand Down
16 changes: 3 additions & 13 deletions packages/state-transition/src/cache/epochCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,22 +918,12 @@ export class EpochCache {
* balances are sampled to adjust the probability of the next selection (32 per epoch on average). So to invalidate
* the prediction the effective of one of those 32 samples should change and change the random_byte inequality.
*/
async getBeaconProposersNextEpoch(): Promise<ValidatorIndex[]> {
getBeaconProposersNextEpoch(): ValidatorIndex[] {
if (!this.proposersNextEpoch.computed) {
if (!this.nextShuffling) {
this.nextShuffling = (await this.shufflingCache?.get(this.nextEpoch, this.nextDecisionRoot)) ?? null;
}
if (!this.nextShuffling) {
throw new EpochCacheError({
code: EpochCacheErrorCode.NEXT_SHUFFLING_NOT_AVAILABLE,
epoch: this.nextEpoch,
decisionRoot: this.getShufflingDecisionRoot(this.nextEpoch),
});
}
const indexes = computeProposers(
this.config.getForkSeqAtEpoch(this.epoch + 1),
this.config.getForkSeqAtEpoch(this.nextEpoch),
this.proposersNextEpoch.seed,
this.nextShuffling,
this.getShufflingAtEpoch(this.nextEpoch),
this.effectiveBalanceIncrements
);
this.proposersNextEpoch = {computed: true, indexes};
Expand Down

0 comments on commit 9cb0c6f

Please sign in to comment.