Skip to content

Commit

Permalink
apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Apr 21, 2024
1 parent a1b9c19 commit 9bad073
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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} =
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/test/unit/chain/shufflingCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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();
});
Expand All @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/test/utils/validationData/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
39 changes: 22 additions & 17 deletions packages/state-transition/src/block/processExecutionLayerExit.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ssz.phase0.Validator> | 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;
}
}
4 changes: 2 additions & 2 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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(
{
Expand Down

0 comments on commit 9bad073

Please sign in to comment.