Skip to content

Commit

Permalink
Improve rotateEpochs and exitQueueEndChurn (#2831)
Browse files Browse the repository at this point in the history
* Improve rotateEpochs()

* Improve exitQueueEndChurn in EpochProcess

* EpochProcess: no need to push validator to array manually
  • Loading branch information
twoeths authored Jul 19, 2021
1 parent 87b1e2e commit 5c04112
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -204,17 +203,12 @@ export function computeSyncParticipantReward(config: IBeaconConfig, totalActiveB
export function rotateEpochs(
epochCtx: EpochContext,
state: allForks.BeaconState,
validators: CachedValidatorList<phase0.Validator>
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);

Expand Down
23 changes: 13 additions & 10 deletions packages/beacon-state-transition/src/allForks/util/epochProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -77,6 +79,7 @@ export function createIEpochProcess(): IEpochProcess {
churnLimit: 0,
statuses: [],
validators: [],
indicesBounded: [],
};
}

Expand All @@ -96,7 +99,8 @@ export function prepareEpochProcessState<T extends allForks.BeaconState>(state:

let activeCount = 0;

validators.forEach((v, i) => {
out.validators = validators.persistent.toArray();
out.validators.forEach((v, i) => {
const status = createIAttesterStatus();

if (v.slashed) {
Expand All @@ -118,8 +122,13 @@ export function prepareEpochProcessState<T extends allForks.BeaconState>(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) {
Expand All @@ -135,7 +144,7 @@ export function prepareEpochProcessState<T extends allForks.BeaconState>(state:
}

out.statuses.push(status);
out.validators.push(v);
out.indicesBounded.push([i, v.activationEpoch, v.exitEpoch]);
});

if (out.totalActiveStake < EFFECTIVE_BALANCE_INCREMENT) {
Expand All @@ -150,12 +159,6 @@ export function prepareEpochProcessState<T extends allForks.BeaconState>(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;
Expand Down

0 comments on commit 5c04112

Please sign in to comment.