From 316e71c684a40f680270ed50a271c61a6fdfbc99 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Tue, 13 Aug 2024 18:18:27 +0100 Subject: [PATCH] chore: review electra branch - part 2 --- .../src/block/processOperations.ts | 2 +- .../src/signatureSets/indexedAttestation.ts | 2 +- .../src/slot/upgradeStateToElectra.ts | 2 +- .../state-transition/src/stateTransition.ts | 12 ++++++------ .../test/unit/util/deposit.test.ts | 6 +++--- packages/types/src/electra/sszTypes.ts | 18 +++++++++--------- packages/validator/src/services/attestation.ts | 3 ++- .../validator/src/services/validatorStore.ts | 7 ++----- 8 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/state-transition/src/block/processOperations.ts b/packages/state-transition/src/block/processOperations.ts index 6d4fb25b9f5b..6f61e7c242fb 100644 --- a/packages/state-transition/src/block/processOperations.ts +++ b/packages/state-transition/src/block/processOperations.ts @@ -72,7 +72,7 @@ export function processOperations( } for (const elWithdrawalRequest of bodyElectra.executionPayload.withdrawalRequests) { - processWithdrawalRequest(fork, state as CachedBeaconStateElectra, elWithdrawalRequest); + processWithdrawalRequest(fork, stateElectra, elWithdrawalRequest); } for (const elConsolidationRequest of bodyElectra.executionPayload.consolidationRequests) { diff --git a/packages/state-transition/src/signatureSets/indexedAttestation.ts b/packages/state-transition/src/signatureSets/indexedAttestation.ts index d3decaf6406f..86535fece8b8 100644 --- a/packages/state-transition/src/signatureSets/indexedAttestation.ts +++ b/packages/state-transition/src/signatureSets/indexedAttestation.ts @@ -45,7 +45,7 @@ export function getAttestationsSignatureSets( return signedBlock.message.body.attestations.map((attestation) => getIndexedAttestationSignatureSet( state, - state.epochCtx.getIndexedAttestation(state.epochCtx.config.getForkSeq(signedBlock.message.slot), attestation) + state.epochCtx.getIndexedAttestation(state.config.getForkSeq(signedBlock.message.slot), attestation) ) ); } diff --git a/packages/state-transition/src/slot/upgradeStateToElectra.ts b/packages/state-transition/src/slot/upgradeStateToElectra.ts index ef7b0bd661b4..6600ad98e80a 100644 --- a/packages/state-transition/src/slot/upgradeStateToElectra.ts +++ b/packages/state-transition/src/slot/upgradeStateToElectra.ts @@ -11,7 +11,7 @@ import {computeActivationExitEpoch} from "../util/epoch.js"; import {getActivationExitChurnLimit, getConsolidationChurnLimit} from "../util/validator.js"; /** - * Upgrade a state from Capella to Deneb. + * Upgrade a state from Deneb to Electra. */ export function upgradeStateToElectra(stateDeneb: CachedBeaconStateDeneb): CachedBeaconStateElectra { const {config} = stateDeneb; diff --git a/packages/state-transition/src/stateTransition.ts b/packages/state-transition/src/stateTransition.ts index 299235d3ab99..44cceb630225 100644 --- a/packages/state-transition/src/stateTransition.ts +++ b/packages/state-transition/src/stateTransition.ts @@ -228,20 +228,20 @@ function processSlotsWithTransientCache( epochTransitionTimer?.(); // Upgrade state if exactly at epoch boundary - const stateSlot = computeEpochAtSlot(postState.slot); - if (stateSlot === config.ALTAIR_FORK_EPOCH) { + const stateEpoch = computeEpochAtSlot(postState.slot); + if (stateEpoch === config.ALTAIR_FORK_EPOCH) { postState = upgradeStateToAltair(postState as CachedBeaconStatePhase0) as CachedBeaconStateAllForks; } - if (stateSlot === config.BELLATRIX_FORK_EPOCH) { + if (stateEpoch === config.BELLATRIX_FORK_EPOCH) { postState = upgradeStateToBellatrix(postState as CachedBeaconStateAltair) as CachedBeaconStateAllForks; } - if (stateSlot === config.CAPELLA_FORK_EPOCH) { + if (stateEpoch === config.CAPELLA_FORK_EPOCH) { postState = upgradeStateToCapella(postState as CachedBeaconStateBellatrix) as CachedBeaconStateAllForks; } - if (stateSlot === config.DENEB_FORK_EPOCH) { + if (stateEpoch === config.DENEB_FORK_EPOCH) { postState = upgradeStateToDeneb(postState as CachedBeaconStateCapella) as CachedBeaconStateAllForks; } - if (stateSlot === config.ELECTRA_FORK_EPOCH) { + if (stateEpoch === config.ELECTRA_FORK_EPOCH) { postState = upgradeStateToElectra(postState as CachedBeaconStateDeneb) as CachedBeaconStateAllForks; } } else { diff --git a/packages/state-transition/test/unit/util/deposit.test.ts b/packages/state-transition/test/unit/util/deposit.test.ts index b108de502a1f..3cfa4abb3409 100644 --- a/packages/state-transition/test/unit/util/deposit.test.ts +++ b/packages/state-transition/test/unit/util/deposit.test.ts @@ -2,7 +2,7 @@ import {describe, it, expect} from "vitest"; import {ssz} from "@lodestar/types"; import {createChainForkConfig} from "@lodestar/config"; import {MAX_DEPOSITS} from "@lodestar/params"; -import {CachedBeaconStateElectra, getEth1DepositCount} from "../../../src/index.js"; +import {getEth1DepositCount} from "../../../src/index.js"; import {createCachedBeaconStateTest} from "../../utils/state.js"; describe("getEth1DepositCount", () => { @@ -37,7 +37,7 @@ describe("getEth1DepositCount", () => { ELECTRA_FORK_EPOCH: 0, }), {skipSyncCommitteeCache: true, skipSyncPubkeys: true} - ) as CachedBeaconStateElectra; + ); if (!postElectraState.epochCtx.isPostElectra()) { throw Error("Not a post-Electra state"); @@ -71,7 +71,7 @@ describe("getEth1DepositCount", () => { ELECTRA_FORK_EPOCH: 0, }), {skipSyncCommitteeCache: true, skipSyncPubkeys: true} - ) as CachedBeaconStateElectra; + ); if (!postElectraState.epochCtx.isPostElectra()) { throw Error("Not a post-Electra state"); diff --git a/packages/types/src/electra/sszTypes.ts b/packages/types/src/electra/sszTypes.ts index a6874c7894e3..31844aac86cc 100644 --- a/packages/types/src/electra/sszTypes.ts +++ b/packages/types/src/electra/sszTypes.ts @@ -152,7 +152,7 @@ export const ExecutionPayload = new ContainerType( ...denebSsz.ExecutionPayload.fields, depositRequests: DepositRequests, // New in ELECTRA withdrawalRequests: WithdrawalRequests, // New in ELECTRA - consolidationRequests: ConsolidationRequests, // [New in Electra] + consolidationRequests: ConsolidationRequests, // New in ELECTRA }, {typeName: "ExecutionPayload", jsonCase: "eth2"} ); @@ -333,14 +333,14 @@ export const BeaconState = new ContainerType( // Deep history valid from Capella onwards historicalSummaries: capellaSsz.BeaconState.fields.historicalSummaries, depositRequestsStartIndex: UintBn64, // New in ELECTRA:EIP6110 - depositBalanceToConsume: Gwei, // New in Electra:EIP7251 - exitBalanceToConsume: Gwei, // New in Electra:EIP7251 - earliestExitEpoch: Epoch, // New in Electra:EIP7251 - consolidationBalanceToConsume: Gwei, // New in Electra:EIP7251 - earliestConsolidationEpoch: Epoch, // New in Electra:EIP7251 - pendingBalanceDeposits: PendingBalanceDeposits, // New in Electra:EIP7251 - pendingPartialWithdrawals: new ListCompositeType(PendingPartialWithdrawal, PENDING_PARTIAL_WITHDRAWALS_LIMIT), // New in Electra:EIP7251 - pendingConsolidations: new ListCompositeType(PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT), // New in Electra:EIP7251 + depositBalanceToConsume: Gwei, // New in ELECTRA:EIP7251 + exitBalanceToConsume: Gwei, // New in ELECTRA:EIP7251 + earliestExitEpoch: Epoch, // New in ELECTRA:EIP7251 + consolidationBalanceToConsume: Gwei, // New in ELECTRA:EIP7251 + earliestConsolidationEpoch: Epoch, // New in ELECTRA:EIP7251 + pendingBalanceDeposits: PendingBalanceDeposits, // New in ELECTRA:EIP7251 + pendingPartialWithdrawals: new ListCompositeType(PendingPartialWithdrawal, PENDING_PARTIAL_WITHDRAWALS_LIMIT), // New in ELECTRA:EIP7251 + pendingConsolidations: new ListCompositeType(PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT), // New in ELECTRA:EIP7251 }, {typeName: "BeaconState", jsonCase: "eth2"} ); diff --git a/packages/validator/src/services/attestation.ts b/packages/validator/src/services/attestation.ts index d6f48aeb3d1f..604f4adeb47e 100644 --- a/packages/validator/src/services/attestation.ts +++ b/packages/validator/src/services/attestation.ts @@ -1,5 +1,6 @@ import {toHexString} from "@chainsafe/ssz"; import {BLSSignature, phase0, Slot, ssz, Attestation, SignedAggregateAndProof} from "@lodestar/types"; +import {ForkSeq} from "@lodestar/params"; import {computeEpochAtSlot, isAggregatorFromCommitteeLength} from "@lodestar/state-transition"; import {sleep} from "@lodestar/utils"; import {ApiClient, routes} from "@lodestar/api"; @@ -159,7 +160,7 @@ export class AttestationService { this.metrics?.attesterStepCallProduceAggregate.observe(this.clock.secFromSlot(slot + 2 / 3)); const dutiesByCommitteeIndex = groupAttDutiesByCommitteeIndex(dutiesAll); - const isPostElectra = computeEpochAtSlot(slot) >= this.config.ELECTRA_FORK_EPOCH; + const isPostElectra = this.config.getForkSeq(slot) >= ForkSeq.electra; // Then download, sign and publish a `SignedAggregateAndProof` for each // validator that is elected to aggregate for this `slot` and `committeeIndex`. diff --git a/packages/validator/src/services/validatorStore.ts b/packages/validator/src/services/validatorStore.ts index 252d0886cc6e..fa9d855aa24a 100644 --- a/packages/validator/src/services/validatorStore.ts +++ b/packages/validator/src/services/validatorStore.ts @@ -534,8 +534,8 @@ export class ValidatorStore { return { aggregationBits: BitArray.fromSingleBit(duty.committeeLength, duty.validatorCommitteeIndex), data: attestationData, - committeeBits: BitArray.fromSingleBit(MAX_COMMITTEES_PER_SLOT, duty.committeeIndex), signature: await this.getSignature(duty.pubkey, signingRoot, signingSlot, signableMessage), + committeeBits: BitArray.fromSingleBit(MAX_COMMITTEES_PER_SLOT, duty.committeeIndex), }; } else { return { @@ -801,7 +801,7 @@ export class ValidatorStore { throw Error(`Inconsistent duties during signing: duty.slot ${duty.slot} != att.slot ${data.slot}`); } - const isPostElectra = computeEpochAtSlot(duty.slot) >= this.config.ELECTRA_FORK_EPOCH; + const isPostElectra = this.config.getForkSeq(duty.slot) >= ForkSeq.electra; if (!isPostElectra && duty.committeeIndex != data.index) { throw Error( `Inconsistent duties during signing: duty.committeeIndex ${duty.committeeIndex} != att.committeeIndex ${data.index}` @@ -810,9 +810,6 @@ export class ValidatorStore { if (isPostElectra && data.index !== 0) { throw Error(`Non-zero committee index post-electra during signing: att.committeeIndex ${data.index}`); } - if (this.config.getForkSeq(duty.slot) >= ForkSeq.electra && data.index !== 0) { - throw Error(`Attestataion data index must be 0 post electra: index ${data.index}`); - } } private assertDoppelgangerSafe(pubKey: PubkeyHex | BLSPubkey): void {