From 5c0411220e69b49f4bf7c9e86bc23c5aaf9dcf20 Mon Sep 17 00:00:00 2001 From: tuyennhv Date: Mon, 19 Jul 2021 23:26:22 +0700 Subject: [PATCH] Improve rotateEpochs and exitQueueEndChurn (#2831) * Improve rotateEpochs() * Improve exitQueueEndChurn in EpochProcess * EpochProcess: no need to push validator to array manually --- .../src/allForks/stateTransition.ts | 2 +- .../src/allForks/util/epochContext.ts | 8 +------ .../src/allForks/util/epochProcess.ts | 23 +++++++++++-------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/beacon-state-transition/src/allForks/stateTransition.ts b/packages/beacon-state-transition/src/allForks/stateTransition.ts index 8b8551b2e244..bd176d55934c 100644 --- a/packages/beacon-state-transition/src/allForks/stateTransition.ts +++ b/packages/beacon-state-transition/src/allForks/stateTransition.ts @@ -144,7 +144,7 @@ function processSlotsWithTransientCache( metrics?.registerValidatorStatuses(process.currentEpoch, process.statuses); postState.slot++; - rotateEpochs(postState.epochCtx, postState, postState.validators); + rotateEpochs(postState.epochCtx, postState, process.indicesBounded); } finally { if (timer) timer(); } diff --git a/packages/beacon-state-transition/src/allForks/util/epochContext.ts b/packages/beacon-state-transition/src/allForks/util/epochContext.ts index a6d4986579c2..cec2d902da4f 100644 --- a/packages/beacon-state-transition/src/allForks/util/epochContext.ts +++ b/packages/beacon-state-transition/src/allForks/util/epochContext.ts @@ -36,7 +36,6 @@ import { } from "../../util"; import {computeEpochShuffling, IEpochShuffling} from "./epochShuffling"; import {MutableVector} from "@chainsafe/persistent-ts"; -import {CachedValidatorList} from "./cachedValidatorList"; import {computeBaseRewardPerIncrement} from "../../altair/misc"; export type AttesterDuty = { @@ -204,17 +203,12 @@ export function computeSyncParticipantReward(config: IBeaconConfig, totalActiveB export function rotateEpochs( epochCtx: EpochContext, state: allForks.BeaconState, - validators: CachedValidatorList + indicesBounded: [ValidatorIndex, Epoch, Epoch][] ): void { epochCtx.previousShuffling = epochCtx.currentShuffling; epochCtx.currentShuffling = epochCtx.nextShuffling; const currEpoch = epochCtx.currentShuffling.epoch; const nextEpoch = currEpoch + 1; - const indicesBounded: [ValidatorIndex, Epoch, Epoch][] = validators.map((v, i) => [ - i, - v.activationEpoch, - v.exitEpoch, - ]); epochCtx.nextShuffling = computeEpochShuffling(state, indicesBounded, nextEpoch); epochCtx.proposers = computeProposers(state, epochCtx.currentShuffling); diff --git a/packages/beacon-state-transition/src/allForks/util/epochProcess.ts b/packages/beacon-state-transition/src/allForks/util/epochProcess.ts index 13a790868244..db5e9795c54a 100644 --- a/packages/beacon-state-transition/src/allForks/util/epochProcess.ts +++ b/packages/beacon-state-transition/src/allForks/util/epochProcess.ts @@ -54,6 +54,8 @@ export interface IEpochProcess { statuses: IAttesterStatus[]; validators: phase0.Validator[]; balances?: BigUint64Array; + // to be used for rotateEpochs() + indicesBounded: [ValidatorIndex, Epoch, Epoch][]; } export function createIEpochProcess(): IEpochProcess { @@ -77,6 +79,7 @@ export function createIEpochProcess(): IEpochProcess { churnLimit: 0, statuses: [], validators: [], + indicesBounded: [], }; } @@ -96,7 +99,8 @@ export function prepareEpochProcessState(state: let activeCount = 0; - validators.forEach((v, i) => { + out.validators = validators.persistent.toArray(); + out.validators.forEach((v, i) => { const status = createIAttesterStatus(); if (v.slashed) { @@ -118,8 +122,13 @@ export function prepareEpochProcessState(state: activeCount += 1; } - if (v.exitEpoch !== FAR_FUTURE_EPOCH && v.exitEpoch > exitQueueEnd) { - exitQueueEnd = v.exitEpoch; + if (v.exitEpoch !== FAR_FUTURE_EPOCH) { + if (v.exitEpoch > exitQueueEnd) { + exitQueueEnd = v.exitEpoch; + exitQueueEndChurn = 1; + } else if (v.exitEpoch === exitQueueEnd) { + exitQueueEndChurn += 1; + } } if (v.activationEligibilityEpoch === FAR_FUTURE_EPOCH && v.effectiveBalance === MAX_EFFECTIVE_BALANCE) { @@ -135,7 +144,7 @@ export function prepareEpochProcessState(state: } out.statuses.push(status); - out.validators.push(v); + out.indicesBounded.push([i, v.activationEpoch, v.exitEpoch]); }); if (out.totalActiveStake < EFFECTIVE_BALANCE_INCREMENT) { @@ -150,12 +159,6 @@ export function prepareEpochProcessState(state: (a, b) => out.validators[a].activationEligibilityEpoch - out.validators[b].activationEligibilityEpoch || a - b ); - for (const validator of out.validators) { - if (validator.exitEpoch === exitQueueEnd) { - exitQueueEndChurn += 1; - } - } - const churnLimit = getChurnLimit(config, activeCount); if (exitQueueEndChurn >= churnLimit) { exitQueueEnd += 1;