Skip to content

Commit

Permalink
spec runner merge sanity and operations fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Nov 8, 2021
1 parent 5c74efd commit 9266bd3
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 16 deletions.
8 changes: 6 additions & 2 deletions packages/beacon-state-transition/src/merge/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import {allForks, altair, merge} from "@chainsafe/lodestar-types";

import {CachedBeaconState} from "../../allForks/util";
import {processBlockHeader, processEth1Data, processRandao} from "../../allForks/block";
import {processOperations} from "../../altair/block/processOperations";
import {processOperations} from "./processOperations";
import {processSyncAggregate} from "../../altair/block/processSyncCommittee";
import {processExecutionPayload} from "./processExecutionPayload";
import {ExecutionEngine} from "../executionEngine";
import {isExecutionEnabled} from "../utils";
import {processAttesterSlashing} from "./processAttesterSlashing";
import {processProposerSlashing} from "./processProposerSlashing";

export {processOperations, processAttesterSlashing, processProposerSlashing};

export function processBlock(
state: CachedBeaconState<merge.BeaconState>,
Expand All @@ -23,6 +27,6 @@ export function processBlock(

processRandao(state as CachedBeaconState<allForks.BeaconState>, block, verifySignatures);
processEth1Data(state as CachedBeaconState<allForks.BeaconState>, block.body);
processOperations((state as unknown) as CachedBeaconState<altair.BeaconState>, block.body, verifySignatures);
processOperations(state, block.body, verifySignatures);
processSyncAggregate((state as unknown) as CachedBeaconState<altair.BeaconState>, block, verifySignatures);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {allForks, merge, phase0} from "@chainsafe/lodestar-types";
import {ForkName} from "@chainsafe/lodestar-params";
import {CachedBeaconState} from "../../allForks/util";
import {processAttesterSlashing as processAttesterSlashingAllForks} from "../../allForks/block";

export function processAttesterSlashing(
state: CachedBeaconState<merge.BeaconState>,
attesterSlashing: phase0.AttesterSlashing,
verifySignatures = true
): void {
processAttesterSlashingAllForks(
ForkName.merge,
state as CachedBeaconState<allForks.BeaconState>,
attesterSlashing,
verifySignatures
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {readonlyValues} from "@chainsafe/ssz";
import {altair, merge} from "@chainsafe/lodestar-types";

import {CachedBeaconState} from "../../allForks/util";
import {processProposerSlashing} from "./processProposerSlashing";
import {processAttesterSlashing} from "./processAttesterSlashing";
import {processAttestations} from "../../altair/block/processAttestation";
import {processDeposit} from "../../altair/block/processDeposit";
import {processVoluntaryExit} from "../../altair/block/processVoluntaryExit";
import {MAX_DEPOSITS} from "@chainsafe/lodestar-params";

export function processOperations(
state: CachedBeaconState<merge.BeaconState>,
body: merge.BeaconBlockBody,
verifySignatures = true
): void {
// verify that outstanding deposits are processed up to the maximum number of deposits
const maxDeposits = Math.min(MAX_DEPOSITS, state.eth1Data.depositCount - state.eth1DepositIndex);
if (body.deposits.length !== maxDeposits) {
throw new Error(
`Block contains incorrect number of deposits: depositCount=${body.deposits.length} expected=${maxDeposits}`
);
}

for (const proposerSlashing of readonlyValues(body.proposerSlashings)) {
processProposerSlashing(state, proposerSlashing, verifySignatures);
}
for (const attesterSlashing of readonlyValues(body.attesterSlashings)) {
processAttesterSlashing(state, attesterSlashing, verifySignatures);
}

processAttestations(
(state as unknown) as CachedBeaconState<altair.BeaconState>,
Array.from(readonlyValues(body.attestations)),
verifySignatures
);

for (const deposit of readonlyValues(body.deposits)) {
processDeposit((state as unknown) as CachedBeaconState<altair.BeaconState>, deposit);
}
for (const voluntaryExit of readonlyValues(body.voluntaryExits)) {
processVoluntaryExit((state as unknown) as CachedBeaconState<altair.BeaconState>, voluntaryExit, verifySignatures);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {allForks, merge, phase0} from "@chainsafe/lodestar-types";
import {ForkName} from "@chainsafe/lodestar-params";
import {CachedBeaconState} from "../../allForks/util";
import {processProposerSlashing as processProposerSlashingAllForks} from "../../allForks/block";

export function processProposerSlashing(
state: CachedBeaconState<merge.BeaconState>,
proposerSlashing: phase0.ProposerSlashing,
verifySignatures = true
): void {
processProposerSlashingAllForks(
ForkName.merge,
state as CachedBeaconState<allForks.BeaconState>,
proposerSlashing,
verifySignatures
);
}
1 change: 1 addition & 0 deletions packages/beacon-state-transition/src/merge/epoch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {processSlashings} from "./processSlashings";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {allForks, merge} from "@chainsafe/lodestar-types";
import {ForkName} from "@chainsafe/lodestar-params";
import {CachedBeaconState, IEpochProcess} from "../../allForks/util";
import {processSlashingsAllForks} from "../../allForks/epoch/processSlashings";

export function processSlashings(state: CachedBeaconState<merge.BeaconState>, epochProcess: IEpochProcess): void {
processSlashingsAllForks(ForkName.merge, state as CachedBeaconState<allForks.BeaconState>, epochProcess);
}
5 changes: 3 additions & 2 deletions packages/beacon-state-transition/src/merge/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {processBlock} from "./block";
export {upgradeState} from "./upgradeState";
export * from "./block";
export * from "./epoch";
export * from "./upgradeState";
export * from "./utils";

// re-export merge lodestar types for ergonomic usage downstream
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {allForks, altair} from "@chainsafe/lodestar-beacon-state-transition";
import {allForks, altair, merge} from "@chainsafe/lodestar-beacon-state-transition";
import {processParticipationRecordUpdates} from "@chainsafe/lodestar-beacon-state-transition/src/phase0/epoch/processParticipationRecordUpdates";
import {ForkName} from "@chainsafe/lodestar-params";
import {EpochProcessFn, epochProcessing} from "../allForks/epochProcessing";
Expand All @@ -18,7 +18,7 @@ epochProcessing(ForkName.merge, {
randao_mixes_reset: allForks.processRandaoMixesReset,
registry_updates: allForks.processRegistryUpdates,
rewards_and_penalties: altair.processRewardsAndPenalties as EpochProcessFn,
slashings: altair.processSlashings as EpochProcessFn,
slashings: merge.processSlashings as EpochProcessFn,
slashings_reset: allForks.processSlashingsReset,
sync_committee_updates: altair.processSyncCommitteeUpdates as EpochProcessFn,
});
20 changes: 10 additions & 10 deletions packages/spec-test-runner/test/spec/merge/operations.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CachedBeaconState, allForks, altair} from "@chainsafe/lodestar-beacon-state-transition";
import {phase0, merge, ssz} from "@chainsafe/lodestar-types";
import {CachedBeaconState, allForks, altair, merge} from "@chainsafe/lodestar-beacon-state-transition";
import {phase0, ssz} from "@chainsafe/lodestar-types";
import {ForkName} from "@chainsafe/lodestar-params";
import {IBaseSpecTest} from "../type";
import {operations, BlockProcessFn} from "../allForks/operations";
Expand All @@ -9,7 +9,7 @@ import {processExecutionPayload} from "@chainsafe/lodestar-beacon-state-transiti
/* eslint-disable @typescript-eslint/naming-convention */

// Define above to re-use in sync_aggregate and sync_aggregate_random
const sync_aggregate: BlockProcessFn<altair.BeaconState> = (
const sync_aggregate: BlockProcessFn<merge.BeaconState> = (
state,
testCase: IBaseSpecTest & {sync_aggregate: altair.SyncAggregate}
) => {
Expand All @@ -19,36 +19,36 @@ const sync_aggregate: BlockProcessFn<altair.BeaconState> = (
block.slot = state.slot;
block.body.syncAggregate = testCase["sync_aggregate"];

altair.processSyncAggregate(state, block);
altair.processSyncAggregate((state as unknown) as CachedBeaconState<altair.BeaconState>, block);
};

operations<altair.BeaconState>(ForkName.merge, {
operations<merge.BeaconState>(ForkName.merge, {
attestation: (state, testCase: IBaseSpecTest & {attestation: phase0.Attestation}) => {
altair.processAttestations(state, [testCase.attestation]);
altair.processAttestations((state as unknown) as CachedBeaconState<altair.BeaconState>, [testCase.attestation]);
},

attester_slashing: (state, testCase: IBaseSpecTest & {attester_slashing: phase0.AttesterSlashing}) => {
const verify = !!testCase.meta && !!testCase.meta.blsSetting && testCase.meta.blsSetting === BigInt(1);
altair.processAttesterSlashing(state, testCase.attester_slashing, verify);
merge.processAttesterSlashing(state, testCase.attester_slashing, verify);
},

block_header: (state, testCase: IBaseSpecTest & {block: altair.BeaconBlock}) => {
allForks.processBlockHeader(state as CachedBeaconState<allForks.BeaconState>, testCase.block);
},

deposit: (state, testCase: IBaseSpecTest & {deposit: phase0.Deposit}) => {
altair.processDeposit(state, testCase.deposit);
altair.processDeposit((state as unknown) as CachedBeaconState<altair.BeaconState>, testCase.deposit);
},

proposer_slashing: (state, testCase: IBaseSpecTest & {proposer_slashing: phase0.ProposerSlashing}) => {
altair.processProposerSlashing(state, testCase.proposer_slashing);
merge.processProposerSlashing(state, testCase.proposer_slashing);
},

sync_aggregate,
sync_aggregate_random: sync_aggregate,

voluntary_exit: (state, testCase: IBaseSpecTest & {voluntary_exit: phase0.SignedVoluntaryExit}) => {
altair.processVoluntaryExit(state, testCase.voluntary_exit);
altair.processVoluntaryExit((state as unknown) as CachedBeaconState<altair.BeaconState>, testCase.voluntary_exit);
},

execution_payload: (
Expand Down

0 comments on commit 9266bd3

Please sign in to comment.