Skip to content

Commit

Permalink
EpochProces: Prepare activeValidatorIndices for the next epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 22, 2021
1 parent 5428329 commit 7145a84
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 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, process.indicesBounded);
rotateEpochs(postState.epochCtx, postState, process.indicesBounded, process.nextEpochActiveValidatorIndices);
} finally {
if (timer) timer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
computeStartSlotAtEpoch,
getSeed,
getTotalActiveBalance,
getTotalBalance,
isAggregatorFromCommitteeLength,
zipIndexesCommitteeBits,
} from "../../util";
Expand Down Expand Up @@ -203,7 +204,8 @@ export function computeSyncParticipantReward(config: IBeaconConfig, totalActiveB
export function rotateEpochs(
epochCtx: EpochContext,
state: allForks.BeaconState,
indicesBounded: [ValidatorIndex, Epoch, Epoch][]
indicesBounded: [ValidatorIndex, Epoch, Epoch][],
activeValidatorIndices: ValidatorIndex[]
): void {
epochCtx.previousShuffling = epochCtx.currentShuffling;
epochCtx.currentShuffling = epochCtx.nextShuffling;
Expand All @@ -213,7 +215,9 @@ export function rotateEpochs(
epochCtx.proposers = computeProposers(state, epochCtx.currentShuffling);

if (currEpoch >= epochCtx.config.ALTAIR_FORK_EPOCH) {
const totalActiveBalance = getTotalActiveBalance(state);
// calling getTotalActiveBalance would cause 1 extra validators loop
// here we leverage EpochProcess to prepare activeValidatorIndices
const totalActiveBalance = getTotalBalance(state, activeValidatorIndices);
epochCtx.syncParticipantReward = computeSyncParticipantReward(epochCtx.config, totalActiveBalance);
epochCtx.syncProposerReward =
(epochCtx.syncParticipantReward * PROPOSER_WEIGHT) / (WEIGHT_DENOMINATOR - PROPOSER_WEIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface IEpochProcess {
balances?: BigUint64Array;
// to be used for rotateEpochs()
indicesBounded: [ValidatorIndex, Epoch, Epoch][];
nextEpochActiveValidatorIndices: ValidatorIndex[];
}

export function createIEpochProcess(): IEpochProcess {
Expand All @@ -80,6 +81,7 @@ export function createIEpochProcess(): IEpochProcess {
statuses: [],
validators: [],
indicesBounded: [],
nextEpochActiveValidatorIndices: [],
};
}

Expand All @@ -90,6 +92,7 @@ export function prepareEpochProcessState<T extends allForks.BeaconState>(state:
const forkName = config.getForkName(state.slot);
const currentEpoch = epochCtx.currentShuffling.epoch;
const prevEpoch = epochCtx.previousShuffling.epoch;
const nextEpoch = currentEpoch + 1;
out.currentEpoch = currentEpoch;
out.prevEpoch = prevEpoch;

Expand Down Expand Up @@ -145,6 +148,11 @@ export function prepareEpochProcessState<T extends allForks.BeaconState>(state:

out.statuses.push(status);
out.indicesBounded.push([i, v.activationEpoch, v.exitEpoch]);
if (nextEpoch >= config.ALTAIR_FORK_EPOCH) {
if (isActiveValidator(v, nextEpoch)) {
out.nextEpochActiveValidatorIndices.push(i);
}
}
});

if (out.totalActiveStake < EFFECTIVE_BALANCE_INCREMENT) {
Expand Down

0 comments on commit 7145a84

Please sign in to comment.