Skip to content

Commit 1add0f5

Browse files
ensi321nazarhussain
authored andcommitted
fix: fix electra genesis spec test (#6764)
* process pending deposit from eth1 * Fix the genesis params * fix * Fix * clean up --------- Co-authored-by: Nazar Hussain <nazarhussain@gmail.com>
1 parent eaf550f commit 1add0f5

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

packages/beacon-node/src/chain/genesis/genesis.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ export class GenesisBuilder implements IGenesisBuilder {
174174
};
175175
});
176176

177-
const {activatedValidatorCount} = applyDeposits(this.config, this.state, newDeposits, this.depositTree);
177+
const {activatedValidatorCount} = applyDeposits(
178+
this.config.getForkSeq(this.state.slot),
179+
this.config,
180+
this.state,
181+
newDeposits,
182+
this.depositTree
183+
);
178184
this.activatedValidatorCount += activatedValidatorCount;
179185

180186
// TODO: If necessary persist deposits here to this.db.depositData, this.db.depositDataRoot

packages/state-transition/src/util/genesis.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ import {
44
EFFECTIVE_BALANCE_INCREMENT,
55
EPOCHS_PER_HISTORICAL_VECTOR,
66
ForkName,
7+
ForkSeq,
78
GENESIS_EPOCH,
89
GENESIS_SLOT,
910
MAX_EFFECTIVE_BALANCE,
1011
UNSET_DEPOSIT_RECEIPTS_START_INDEX,
1112
} from "@lodestar/params";
1213
import {Bytes32, phase0, Root, ssz, TimeSeconds} from "@lodestar/types";
1314

14-
import {CachedBeaconStateAllForks, BeaconStateAllForks} from "../types.js";
15+
import {CachedBeaconStateAllForks, BeaconStateAllForks, CachedBeaconStateElectra} from "../types.js";
1516
import {createCachedBeaconState} from "../cache/stateCache.js";
1617
import {EpochCacheImmutableData} from "../cache/epochCache.js";
1718
import {processDeposit} from "../block/processDeposit.js";
19+
import {increaseBalance} from "../index.js";
1820
import {computeEpochAtSlot} from "./epoch.js";
1921
import {getActiveValidatorIndices} from "./validator.js";
2022
import {getTemporaryBlockHeader} from "./blockRoot.js";
@@ -127,6 +129,7 @@ export function applyTimestamp(config: ChainForkConfig, state: CachedBeaconState
127129
* @returns active validator indices
128130
*/
129131
export function applyDeposits(
132+
fork: ForkSeq,
130133
config: ChainForkConfig,
131134
state: CachedBeaconStateAllForks,
132135
newDeposits: phase0.Deposit[],
@@ -164,6 +167,16 @@ export function applyDeposits(
164167
processDeposit(fork, state, deposit);
165168
}
166169

170+
// Process deposit balance updates
171+
if (fork >= ForkSeq.electra) {
172+
const stateElectra = state as CachedBeaconStateElectra;
173+
stateElectra.commit();
174+
for (const {index: validatorIndex, amount} of stateElectra.pendingBalanceDeposits.getAllReadonly()) {
175+
increaseBalance(state, validatorIndex, Number(amount));
176+
}
177+
stateElectra.pendingBalanceDeposits = ssz.electra.PendingBalanceDeposits.defaultViewDU();
178+
}
179+
167180
// Process activations
168181
const {epochCtx} = state;
169182
const balancesArr = state.balances.getAll();
@@ -226,6 +239,8 @@ export function initializeBeaconStateFromEth1(
226239
getTemporaryBlockHeader(config, config.getForkTypes(GENESIS_SLOT).BeaconBlock.defaultValue())
227240
);
228241

242+
const fork = config.getForkSeq(GENESIS_SLOT);
243+
229244
// We need a CachedBeaconState to run processDeposit() which uses various caches.
230245
// However at this point the state's syncCommittees are not known.
231246
// This function can be called by:
@@ -240,13 +255,13 @@ export function initializeBeaconStateFromEth1(
240255
applyEth1BlockHash(state, eth1BlockHash);
241256

242257
// Process deposits
243-
applyDeposits(config, state, deposits, fullDepositDataRootList);
258+
applyDeposits(fork, config, state, deposits, fullDepositDataRootList);
244259

245260
// Commit before reading all validators in `getActiveValidatorIndices()`
246261
state.commit();
247262
const activeValidatorIndices = getActiveValidatorIndices(state, computeEpochAtSlot(GENESIS_SLOT));
248263

249-
if (GENESIS_SLOT >= config.ALTAIR_FORK_EPOCH) {
264+
if (fork >= ForkSeq.altair) {
250265
const {syncCommittee} = getNextSyncCommittee(
251266
state,
252267
activeValidatorIndices,
@@ -259,7 +274,7 @@ export function initializeBeaconStateFromEth1(
259274
stateAltair.nextSyncCommittee = ssz.altair.SyncCommittee.toViewDU(syncCommittee);
260275
}
261276

262-
if (GENESIS_SLOT >= config.BELLATRIX_FORK_EPOCH) {
277+
if (fork >= ForkSeq.bellatrix) {
263278
const stateBellatrix = state as CompositeViewDU<typeof ssz.bellatrix.BeaconState>;
264279
stateBellatrix.fork.previousVersion = config.BELLATRIX_FORK_VERSION;
265280
stateBellatrix.fork.currentVersion = config.BELLATRIX_FORK_VERSION;
@@ -268,7 +283,7 @@ export function initializeBeaconStateFromEth1(
268283
ssz.bellatrix.ExecutionPayloadHeader.defaultViewDU();
269284
}
270285

271-
if (GENESIS_SLOT >= config.CAPELLA_FORK_EPOCH) {
286+
if (fork >= ForkSeq.capella) {
272287
const stateCapella = state as CompositeViewDU<typeof ssz.capella.BeaconState>;
273288
stateCapella.fork.previousVersion = config.CAPELLA_FORK_VERSION;
274289
stateCapella.fork.currentVersion = config.CAPELLA_FORK_VERSION;
@@ -277,7 +292,7 @@ export function initializeBeaconStateFromEth1(
277292
ssz.capella.ExecutionPayloadHeader.defaultViewDU();
278293
}
279294

280-
if (GENESIS_SLOT >= config.DENEB_FORK_EPOCH) {
295+
if (fork >= ForkSeq.deneb) {
281296
const stateDeneb = state as CompositeViewDU<typeof ssz.deneb.BeaconState>;
282297
stateDeneb.fork.previousVersion = config.DENEB_FORK_VERSION;
283298
stateDeneb.fork.currentVersion = config.DENEB_FORK_VERSION;
@@ -286,7 +301,7 @@ export function initializeBeaconStateFromEth1(
286301
ssz.deneb.ExecutionPayloadHeader.defaultViewDU();
287302
}
288303

289-
if (GENESIS_SLOT >= config.ELECTRA_FORK_EPOCH) {
304+
if (fork >= ForkSeq.electra) {
290305
const stateElectra = state as CompositeViewDU<typeof ssz.electra.BeaconState>;
291306
stateElectra.fork.previousVersion = config.ELECTRA_FORK_VERSION;
292307
stateElectra.fork.currentVersion = config.ELECTRA_FORK_VERSION;

packages/types/src/electra/sszTypes.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ export const PendingBalanceDeposit = new ContainerType(
275275
{typeName: "PendingBalanceDeposit", jsonCase: "eth2"}
276276
);
277277

278+
export const PendingBalanceDeposits = new ListCompositeType(
279+
PendingBalanceDeposit,
280+
Number(PENDING_BALANCE_DEPOSITS_LIMIT)
281+
);
282+
278283
export const PartialWithdrawal = new ContainerType(
279284
{
280285
index: ValidatorIndex,
@@ -341,7 +346,7 @@ export const BeaconState = new ContainerType(
341346
earliestExitEpoch: Epoch, // New in Electra:EIP7251
342347
consolidationBalanceToConsume: Gwei, // New in Electra:EIP7251
343348
earliestConsolidationEpoch: Epoch, // New in Electra:EIP7251
344-
pendingBalanceDeposits: new ListCompositeType(PendingBalanceDeposit, Number(PENDING_BALANCE_DEPOSITS_LIMIT)), // new in electra:eip7251
349+
pendingBalanceDeposits: PendingBalanceDeposits, // new in electra:eip7251
345350
pendingPartialWithdrawals: new ListCompositeType(PartialWithdrawal, Number(PENDING_PARTIAL_WITHDRAWALS_LIMIT)), // New in Electra:EIP7251
346351
pendingConsolidations: new ListCompositeType(PendingConsolidation, Number(PENDING_CONSOLIDATIONS_LIMIT)), // new in electra:eip7251
347352
},

0 commit comments

Comments
 (0)