From 9bad0734ba8124be79b729ea007863a878762c1d Mon Sep 17 00:00:00 2001 From: harkamal Date: Sun, 21 Apr 2024 14:12:19 +0530 Subject: [PATCH] apply feedback --- .../opPools/aggregatedAttestationPool.test.ts | 2 +- .../test/perf/chain/opPools/opPool.test.ts | 2 +- .../produceBlock/produceBlockBody.test.ts | 2 +- .../opPools/aggregatedAttestationPool.test.ts | 4 +- .../unit/chain/rewards/blockRewards.test.ts | 11 ++---- .../test/unit/chain/shufflingCache.test.ts | 6 +-- .../test/utils/validationData/attestation.ts | 4 +- .../src/block/processExecutionLayerExit.ts | 39 +++++++++++-------- packages/types/src/electra/sszTypes.ts | 4 +- 9 files changed, 38 insertions(+), 36 deletions(-) diff --git a/packages/beacon-node/test/perf/chain/opPools/aggregatedAttestationPool.test.ts b/packages/beacon-node/test/perf/chain/opPools/aggregatedAttestationPool.test.ts index 0bea2bab0e72..92cfe100617f 100644 --- a/packages/beacon-node/test/perf/chain/opPools/aggregatedAttestationPool.test.ts +++ b/packages/beacon-node/test/perf/chain/opPools/aggregatedAttestationPool.test.ts @@ -32,7 +32,7 @@ describe(`getAttestationsForBlock vc=${vc}`, () => { before(function () { this.timeout(5 * 60 * 1000); // Generating the states for the first time is very slow - originalState = generatePerfTestCachedStateAltair({goBackOneSlot: true, vc}) as unknown as CachedBeaconStateAltair; + originalState = generatePerfTestCachedStateAltair({goBackOneSlot: true, vc}) as CachedBeaconStateAltair; const {blockHeader, checkpoint} = computeAnchorCheckpoint(originalState.config, originalState); // TODO figure out why getBlockRootAtSlot(originalState, justifiedSlot) is not the same to justifiedCheckpoint.root diff --git a/packages/beacon-node/test/perf/chain/opPools/opPool.test.ts b/packages/beacon-node/test/perf/chain/opPools/opPool.test.ts index 7998a204f09d..2632c593e78c 100644 --- a/packages/beacon-node/test/perf/chain/opPools/opPool.test.ts +++ b/packages/beacon-node/test/perf/chain/opPools/opPool.test.ts @@ -24,7 +24,7 @@ describe("opPool", () => { before(function () { this.timeout(2 * 60 * 1000); // Generating the states for the first time is very slow - originalState = generatePerfTestCachedStateAltair({goBackOneSlot: true}) as unknown as CachedBeaconStateAltair; + originalState = generatePerfTestCachedStateAltair({goBackOneSlot: true}) as CachedBeaconStateAltair; }); itBench({ diff --git a/packages/beacon-node/test/perf/chain/produceBlock/produceBlockBody.test.ts b/packages/beacon-node/test/perf/chain/produceBlock/produceBlockBody.test.ts index 62b5466bf891..a021e776fac5 100644 --- a/packages/beacon-node/test/perf/chain/produceBlock/produceBlockBody.test.ts +++ b/packages/beacon-node/test/perf/chain/produceBlock/produceBlockBody.test.ts @@ -25,7 +25,7 @@ describe("produceBlockBody", () => { before(async () => { db = new BeaconDb(config, await LevelDbController.create({name: ".tmpdb"}, {logger})); - state = stateOg.clone() as unknown as CachedBeaconStateAltair; + state = stateOg.clone() as CachedBeaconStateAltair; chain = new BeaconChain( { proposerBoostEnabled: true, diff --git a/packages/beacon-node/test/unit/chain/opPools/aggregatedAttestationPool.test.ts b/packages/beacon-node/test/unit/chain/opPools/aggregatedAttestationPool.test.ts index 71dfd63830c1..3c248ad4d194 100644 --- a/packages/beacon-node/test/unit/chain/opPools/aggregatedAttestationPool.test.ts +++ b/packages/beacon-node/test/unit/chain/opPools/aggregatedAttestationPool.test.ts @@ -62,9 +62,9 @@ describe("AggregatedAttestationPool", function () { epochParticipation[committee[i]] = 0b000; } } - (originalState as unknown as CachedBeaconStateAltair).previousEpochParticipation = + (originalState as CachedBeaconStateAltair).previousEpochParticipation = ssz.altair.EpochParticipation.toViewDU(epochParticipation); - (originalState as unknown as CachedBeaconStateAltair).currentEpochParticipation = + (originalState as CachedBeaconStateAltair).currentEpochParticipation = ssz.altair.EpochParticipation.toViewDU(epochParticipation); originalState.commit(); let altairState: CachedBeaconStateAllForks; diff --git a/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts b/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts index 143fed984079..f0d85ce3220f 100644 --- a/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts +++ b/packages/beacon-node/test/unit/chain/rewards/blockRewards.test.ts @@ -89,10 +89,7 @@ describe("chain / rewards / blockRewards", () => { // Populate tree root caches of the state state.hashTreeRoot(); cachedStateAltairPopulateCaches(state); - const calculatedBlockReward = await computeBlockRewards( - block.message, - state as unknown as CachedBeaconStateAllForks - ); + const calculatedBlockReward = await computeBlockRewards(block.message, state as CachedBeaconStateAllForks); const {proposerIndex, total, attestations, syncAggregate, proposerSlashings, attesterSlashings} = calculatedBlockReward; @@ -112,7 +109,7 @@ describe("chain / rewards / blockRewards", () => { expect(attesterSlashings).toBe(0); } - const postState = stateTransition(state as unknown as CachedBeaconStateAllForks, block, { + const postState = stateTransition(state as CachedBeaconStateAllForks, block, { executionPayloadStatus: ExecutionPayloadStatus.valid, dataAvailableStatus: DataAvailableStatus.available, verifyProposer: false, @@ -140,7 +137,7 @@ describe("chain / rewards / blockRewards", () => { preState.hashTreeRoot(); cachedStateAltairPopulateCaches(preState); - const postState = stateTransition(preState as unknown as CachedBeaconStateAllForks, block, { + const postState = stateTransition(preState as CachedBeaconStateAllForks, block, { executionPayloadStatus: ExecutionPayloadStatus.valid, dataAvailableStatus: DataAvailableStatus.available, verifyProposer: false, @@ -154,7 +151,7 @@ describe("chain / rewards / blockRewards", () => { const calculatedBlockReward = await computeBlockRewards( block.message, - preState as unknown as CachedBeaconStateAllForks, + preState as CachedBeaconStateAllForks, postState ); const {proposerIndex, total, attestations, syncAggregate, proposerSlashings, attesterSlashings} = diff --git a/packages/beacon-node/test/unit/chain/shufflingCache.test.ts b/packages/beacon-node/test/unit/chain/shufflingCache.test.ts index e6a7e8706bbe..035746438563 100644 --- a/packages/beacon-node/test/unit/chain/shufflingCache.test.ts +++ b/packages/beacon-node/test/unit/chain/shufflingCache.test.ts @@ -14,7 +14,7 @@ describe("ShufflingCache", function () { beforeEach(() => { shufflingCache = new ShufflingCache(null, {maxShufflingCacheEpochs: 1}); - shufflingCache.processState(state as unknown as CachedBeaconStateAllForks, currentEpoch); + shufflingCache.processState(state as CachedBeaconStateAllForks, currentEpoch); }); it("should get shuffling from cache", async function () { @@ -29,7 +29,7 @@ describe("ShufflingCache", function () { shufflingCache.insertPromise(currentEpoch, "0x00"); expect(await shufflingCache.get(currentEpoch, decisionRoot)).toEqual(state.epochCtx.currentShuffling); // insert shufflings at other epochs does prune the cache - shufflingCache.processState(state as unknown as CachedBeaconStateAllForks, currentEpoch + 1); + shufflingCache.processState(state as CachedBeaconStateAllForks, currentEpoch + 1); // the current shuffling is not available anymore expect(await shufflingCache.get(currentEpoch, decisionRoot)).toBeNull(); }); @@ -39,7 +39,7 @@ describe("ShufflingCache", function () { shufflingCache.insertPromise(currentEpoch + 1, nextDecisionRoot); const shufflingRequest0 = shufflingCache.get(currentEpoch + 1, nextDecisionRoot); const shufflingRequest1 = shufflingCache.get(currentEpoch + 1, nextDecisionRoot); - shufflingCache.processState(state as unknown as CachedBeaconStateAllForks, currentEpoch + 1); + shufflingCache.processState(state as CachedBeaconStateAllForks, currentEpoch + 1); expect(await shufflingRequest0).toEqual(state.epochCtx.nextShuffling); expect(await shufflingRequest1).toEqual(state.epochCtx.nextShuffling); }); diff --git a/packages/beacon-node/test/utils/validationData/attestation.ts b/packages/beacon-node/test/utils/validationData/attestation.ts index 02d20fd8e5bb..3b66e9befb40 100644 --- a/packages/beacon-node/test/utils/validationData/attestation.ts +++ b/packages/beacon-node/test/utils/validationData/attestation.ts @@ -82,8 +82,8 @@ export function getAttestationValidData(opts: AttestationValidDataOpts): { }; const shufflingCache = new ShufflingCache(); - shufflingCache.processState(state as unknown as CachedBeaconStateAllForks, state.epochCtx.currentShuffling.epoch); - shufflingCache.processState(state as unknown as CachedBeaconStateAllForks, state.epochCtx.nextShuffling.epoch); + shufflingCache.processState(state as CachedBeaconStateAllForks, state.epochCtx.currentShuffling.epoch); + shufflingCache.processState(state as CachedBeaconStateAllForks, state.epochCtx.nextShuffling.epoch); const dependentRoot = getShufflingDecisionBlock(state, state.epochCtx.currentShuffling.epoch); const forkChoice = { diff --git a/packages/state-transition/src/block/processExecutionLayerExit.ts b/packages/state-transition/src/block/processExecutionLayerExit.ts index 43a072d770f9..bb397b992ddf 100644 --- a/packages/state-transition/src/block/processExecutionLayerExit.ts +++ b/packages/state-transition/src/block/processExecutionLayerExit.ts @@ -1,50 +1,55 @@ -import {byteArrayEquals} from "@chainsafe/ssz"; -import {electra} from "@lodestar/types"; +import {CompositeViewDU} from "@chainsafe/ssz"; +import {electra, ssz} from "@lodestar/types"; import {ETH1_ADDRESS_WITHDRAWAL_PREFIX, FAR_FUTURE_EPOCH} from "@lodestar/params"; import {isActiveValidator} from "../util/index.js"; import {CachedBeaconStateElectra} from "../types.js"; import {initiateValidatorExit} from "./index.js"; +/** + * Validate execution Layer Exit, If a valid Exit is returned from isValidExecutionLayerExit Initiate the exit of the validator. + */ export function processExecutionLayerExit(state: CachedBeaconStateElectra, exit: electra.ExecutionLayerExit): void { - if (!isValidExecutionLayerExit(state, exit)) { + const validator = isValidExecutionLayerExit(state, exit); + if (validator === null) { return; } - const {epochCtx} = state; - const validatorIndex = epochCtx.getValidatorIndex(exit.validatorPubkey); - const validator = validatorIndex !== undefined ? state.validators.get(validatorIndex) : undefined; - if (validator === undefined) { - throw Error("Internal error validator=undefined for a valid execution layer exit"); - } initiateValidatorExit(state, validator); } -export function isValidExecutionLayerExit(state: CachedBeaconStateElectra, exit: electra.ExecutionLayerExit): boolean { +export function isValidExecutionLayerExit( + state: CachedBeaconStateElectra, + exit: electra.ExecutionLayerExit +): CompositeViewDU | null { const {config, epochCtx} = state; const validatorIndex = epochCtx.getValidatorIndex(exit.validatorPubkey); const validator = validatorIndex !== undefined ? state.validators.getReadonly(validatorIndex) : undefined; if (validator === undefined) { - return false; + return null; } const {withdrawalCredentials} = validator; if (withdrawalCredentials[0] !== ETH1_ADDRESS_WITHDRAWAL_PREFIX) { - return false; + return null; } - const executionAddress = withdrawalCredentials.slice(12, 32); - if (!byteArrayEquals(executionAddress, exit.sourceAddress)) { - return false; + const executionAddress = withdrawalCredentials.subarray(12, 32); + if (Buffer.compare(executionAddress, exit.sourceAddress) !== 0) { + return null; } const currentEpoch = epochCtx.epoch; - return ( + if ( // verify the validator is active isActiveValidator(validator, currentEpoch) && // verify exit has not been initiated validator.exitEpoch === FAR_FUTURE_EPOCH && // verify the validator had been active long enough currentEpoch >= validator.activationEpoch + config.SHARD_COMMITTEE_PERIOD - ); + ) { + return validator; + } else { + return null; + } } diff --git a/packages/types/src/electra/sszTypes.ts b/packages/types/src/electra/sszTypes.ts index 7cc7e02b6583..7b5ed51fb786 100644 --- a/packages/types/src/electra/sszTypes.ts +++ b/packages/types/src/electra/sszTypes.ts @@ -28,7 +28,7 @@ export const DepositReceipt = new ContainerType( {typeName: "DepositReceipt", jsonCase: "eth2"} ); -export const DepositReceipts = new ListCompositeType(DepositReceipt, MAX_EXECUTION_LAYER_EXITS); +export const DepositReceipts = new ListCompositeType(DepositReceipt, MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD); export const ExecutionLayerExit = new ContainerType( { @@ -37,7 +37,7 @@ export const ExecutionLayerExit = new ContainerType( }, {typeName: "ExecutionLayerExit", jsonCase: "eth2"} ); -export const ExecutionLayerExits = new ListCompositeType(ExecutionLayerExit, MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD); +export const ExecutionLayerExits = new ListCompositeType(ExecutionLayerExit, MAX_EXECUTION_LAYER_EXITS); export const ExecutionPayload = new ContainerType( {