Skip to content

Commit

Permalink
Simplify block processor types (#2911)
Browse files Browse the repository at this point in the history
* Simplify block processor types

* Assign blockProcess.validatorExitCache
  • Loading branch information
dapplion committed Aug 3, 2021
1 parent 4eed46c commit 40358ff
Show file tree
Hide file tree
Showing 21 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ export function initiateValidatorExit(
if (validators[index].exitEpoch !== FAR_FUTURE_EPOCH) {
return;
}
const {validatorExitCache: cache} = blockProcess;

// the 1st time we process validator exit in this block
if (cache.exitQueueEpoch === undefined) {
if (blockProcess.validatorExitCache === undefined) {
const currentEpoch = epochCtx.currentShuffling.epoch;

// compute exit queue epoch
const validatorArr = validators.persistent.toArray();
const exitEpochs = [];

let exitQueueEpoch = computeActivationExitEpoch(currentEpoch);
exitEpochs.push(exitQueueEpoch);

let exitQueueChurn = 0;
for (let i = 0; i < validatorArr.length; i++) {
const {exitEpoch} = validatorArr[i];
Expand All @@ -41,32 +43,30 @@ export function initiateValidatorExit(
}
}
}

const churnLimit = getChurnLimit(config, epochCtx.currentShuffling.activeIndices.length);
if (exitQueueChurn >= churnLimit) {
// 1st validator with this exitQueueEpoch
exitQueueEpoch += 1;
exitQueueChurn = 1;
}

cache.exitQueueEpoch = exitQueueEpoch;
cache.exitQueueChurn = exitQueueChurn;
cache.churnLimit = churnLimit;
blockProcess.validatorExitCache = {
exitQueueEpoch,
exitQueueChurn,
churnLimit,
};
} else {
let {exitQueueChurn} = cache;
if (exitQueueChurn === undefined || cache.churnLimit === undefined) {
throw new Error("Invalid ValidatorExitProcess");
}
exitQueueChurn++;
if (exitQueueChurn >= cache.churnLimit) {
blockProcess.validatorExitCache.exitQueueChurn++;
if (blockProcess.validatorExitCache.exitQueueChurn >= blockProcess.validatorExitCache.churnLimit) {
// 1st validator with this exitQueueEpoch
cache.exitQueueEpoch += 1;
exitQueueChurn = 0;
blockProcess.validatorExitCache.exitQueueEpoch += 1;
blockProcess.validatorExitCache.exitQueueChurn = 0;
}
cache.exitQueueChurn = exitQueueChurn;
}

// set validator exit epoch and withdrawable epoch
const {exitQueueEpoch} = cache;
const {exitQueueEpoch} = blockProcess.validatorExitCache;
validators.update(index, {
exitEpoch: exitQueueEpoch,
withdrawableEpoch: exitQueueEpoch + config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY,
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-state-transition/src/altair/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {processDeposit} from "./processDeposit";
import {processProposerSlashing} from "./processProposerSlashing";
import {processVoluntaryExit} from "./processVoluntaryExit";
import {processSyncAggregate} from "./processSyncCommittee";
import {BlockProcess} from "../../util";

export {
processOperations,
Expand All @@ -25,7 +26,7 @@ export function processBlock(
block: altair.BeaconBlock,
verifySignatures = true
): void {
const blockProcess = {validatorExitCache: {}};
const blockProcess: BlockProcess = {};
processBlockHeader(state as CachedBeaconState<allForks.BeaconState>, block);
processRandao(state as CachedBeaconState<allForks.BeaconState>, block, verifySignatures);
processEth1Data(state as CachedBeaconState<allForks.BeaconState>, block.body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function processAttestation(
state: CachedBeaconState<altair.BeaconState>,
attestation: phase0.Attestation,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignature = true
): void {
const {epochCtx} = state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {BlockProcess} from "../../util/blockProcess";
export function processAttesterSlashing(
state: CachedBeaconState<altair.BeaconState>,
attesterSlashing: phase0.AttesterSlashing,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignatures = true
): void {
processAttesterSlashingAllForks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type OperationFunction = (
export function processOperations(
state: CachedBeaconState<altair.BeaconState>,
body: altair.BeaconBlockBody,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignatures = true
): void {
// verify that outstanding deposits are processed up to the maximum number of deposits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {BlockProcess} from "../../util/blockProcess";
export function processProposerSlashing(
state: CachedBeaconState<altair.BeaconState>,
proposerSlashing: phase0.ProposerSlashing,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignatures = true
): void {
processProposerSlashingAllForks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {BlockProcess} from "../../util/blockProcess";
export function processVoluntaryExit(
state: CachedBeaconState<altair.BeaconState>,
signedVoluntaryExit: phase0.SignedVoluntaryExit,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignature = true
): void {
processVoluntaryExitAllForks(
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-state-transition/src/phase0/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {processDeposit} from "./processDeposit";
import {processAttesterSlashing} from "./processAttesterSlashing";
import {processProposerSlashing} from "./processProposerSlashing";
import {processVoluntaryExit} from "./processVoluntaryExit";
import {BlockProcess} from "../../util";

// Extra utils used by other modules
export {isValidIndexedAttestation} from "../../allForks/block";
Expand All @@ -26,7 +27,7 @@ export function processBlock(
block: phase0.BeaconBlock,
verifySignatures = true
): void {
const blockProcess = {validatorExitCache: {}};
const blockProcess: BlockProcess = {};
processBlockHeader(state as CachedBeaconState<allForks.BeaconState>, block);
processRandao(state as CachedBeaconState<allForks.BeaconState>, block, verifySignatures);
processEth1Data(state as CachedBeaconState<allForks.BeaconState>, block.body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function processAttestation(
state: CachedBeaconState<phase0.BeaconState>,
attestation: phase0.Attestation,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignature = true
): void {
const {epochCtx} = state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {BlockProcess} from "../../util/blockProcess";
export function processAttesterSlashing(
state: CachedBeaconState<phase0.BeaconState>,
attesterSlashing: phase0.AttesterSlashing,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignatures = true
): void {
processAttesterSlashingAllForks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {BlockProcess} from "../../util/blockProcess";
export function processProposerSlashing(
state: CachedBeaconState<phase0.BeaconState>,
proposerSlashing: phase0.ProposerSlashing,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignatures = true
): void {
processProposerSlashingAllForks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {BlockProcess} from "../../util/blockProcess";
export function processVoluntaryExit(
state: CachedBeaconState<phase0.BeaconState>,
signedVoluntaryExit: phase0.SignedVoluntaryExit,
blockProcess: BlockProcess = {validatorExitCache: {}},
blockProcess: BlockProcess,
verifySignature = true
): void {
processVoluntaryExitAllForks(
Expand Down
8 changes: 4 additions & 4 deletions packages/beacon-state-transition/src/util/blockProcess.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// eslint-disable-next-line @typescript-eslint/naming-convention
export interface BlockProcess {
validatorExitCache: {
exitQueueEpoch?: number;
exitQueueChurn?: number;
churnLimit?: number;
validatorExitCache?: {
exitQueueEpoch: number;
exitQueueChurn: number;
churnLimit: number;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function runAttestations(presetName: PresetName): void {
config,
testcase.pre as TreeBacked<altair.BeaconState>
) as CachedBeaconState<altair.BeaconState>;
altair.processAttestation(wrappedState, testcase.attestation);
altair.processAttestation(wrappedState, testcase.attestation, {});
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function runAttesterSlashing(presetName: PresetName): void {
testcase.pre as TreeBacked<altair.BeaconState>
);
const verify = !!testcase.meta && !!testcase.meta.blsSetting && testcase.meta.blsSetting === BigInt(1);
altair.processAttesterSlashing(wrappedState, testcase.attester_slashing, undefined, verify);
altair.processAttesterSlashing(wrappedState, testcase.attester_slashing, {}, verify);
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function runProposerSlashing(presetName: PresetName): void {
config,
testcase.pre as TreeBacked<altair.BeaconState>
);
altair.processProposerSlashing(wrappedState, testcase.proposer_slashing);
altair.processProposerSlashing(wrappedState, testcase.proposer_slashing, {});
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function runVoluntaryExit(presetName: PresetName): void {
config,
testcase.pre as TreeBacked<altair.BeaconState>
);
altair.processVoluntaryExit(wrappedState, testcase.voluntary_exit);
altair.processVoluntaryExit(wrappedState, testcase.voluntary_exit, {});
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describeDirectorySpecTest<IProcessAttestationTestCase, phase0.BeaconState>(
config,
testcase.pre as TreeBacked<phase0.BeaconState>
);
phase0.processAttestation(wrappedState, testcase.attestation);
phase0.processAttestation(wrappedState, testcase.attestation, {});
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describeDirectorySpecTest<IProcessAttesterSlashingTestCase, phase0.BeaconState>(
testcase.pre as TreeBacked<phase0.BeaconState>
);
const verify = !!testcase.meta && !!testcase.meta.blsSetting && testcase.meta.blsSetting === BigInt(1);
phase0.processAttesterSlashing(wrappedState, testcase.attester_slashing, undefined, verify);
phase0.processAttesterSlashing(wrappedState, testcase.attester_slashing, {}, verify);
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describeDirectorySpecTest<IProcessProposerSlashingTestCase, phase0.BeaconState>(
config,
testcase.pre as TreeBacked<phase0.BeaconState>
);
phase0.processProposerSlashing(wrappedState, testcase.proposer_slashing);
phase0.processProposerSlashing(wrappedState, testcase.proposer_slashing, {});
return wrappedState;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describeDirectorySpecTest<IProcessVoluntaryExitTestCase, phase0.BeaconState>(
config,
testcase.pre as TreeBacked<phase0.BeaconState>
);
phase0.processVoluntaryExit(wrappedState, testcase.voluntary_exit);
phase0.processVoluntaryExit(wrappedState, testcase.voluntary_exit, {});
return wrappedState;
},
{
Expand Down

0 comments on commit 40358ff

Please sign in to comment.