Skip to content

Commit 005b639

Browse files
twoethsg11tech
authored andcommitted
fix: electra fork transition spec tests (#6769)
* fix: electra fork transition * fix: merge issue * chore: remove unwanted change
1 parent c516c42 commit 005b639

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

packages/state-transition/src/slot/upgradeStateToElectra.ts

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ssz} from "@lodestar/types";
1+
import {Epoch, ValidatorIndex, ssz} from "@lodestar/types";
22
import {FAR_FUTURE_EPOCH, UNSET_DEPOSIT_RECEIPTS_START_INDEX} from "@lodestar/params";
33
import {CachedBeaconStateDeneb} from "../types.js";
44
import {CachedBeaconStateElectra, getCachedBeaconState} from "../cache/stateCache.js";
@@ -7,6 +7,8 @@ import {
77
queueEntireBalanceAndResetValidator,
88
queueExcessActiveBalance,
99
} from "../util/electra.js";
10+
import {computeActivationExitEpoch} from "../util/epoch.js";
11+
import {getActivationExitChurnLimit, getConsolidationChurnLimit} from "../util/validator.js";
1012

1113
/**
1214
* Upgrade a state from Capella to Deneb.
@@ -58,17 +60,52 @@ export function upgradeStateToElectra(stateDeneb: CachedBeaconStateDeneb): Cache
5860
// latestExecutionPayloadHeader's depositReceiptsRoot and withdrawalRequestsRoot set to zeros by default
5961
// default value of depositReceiptsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
6062
stateElectraView.depositReceiptsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
63+
stateElectraView.depositBalanceToConsume = BigInt(0);
64+
stateElectraView.exitBalanceToConsume = BigInt(0);
6165

6266
const validatorsArr = stateElectraView.validators.getAllReadonly();
67+
const exitEpochs: Epoch[] = [];
68+
69+
// [EIP-7251]: add validators that are not yet active to pending balance deposits
70+
const preActivation: ValidatorIndex[] = [];
71+
for (let validatorIndex = 0; validatorIndex < validatorsArr.length; validatorIndex++) {
72+
const {activationEpoch, exitEpoch} = validatorsArr[validatorIndex];
73+
if (activationEpoch === FAR_FUTURE_EPOCH) {
74+
preActivation.push(validatorIndex);
75+
}
76+
if (exitEpoch !== FAR_FUTURE_EPOCH) {
77+
exitEpochs.push(exitEpoch);
78+
}
79+
}
80+
81+
const currentEpochPre = stateDeneb.epochCtx.epoch;
82+
83+
if (exitEpochs.length === 0) {
84+
exitEpochs.push(currentEpochPre);
85+
}
86+
stateElectraView.earliestExitEpoch = Math.max(...exitEpochs) + 1;
87+
stateElectraView.consolidationBalanceToConsume = BigInt(0);
88+
stateElectraView.earliestConsolidationEpoch = computeActivationExitEpoch(currentEpochPre);
89+
// stateElectraView.pendingBalanceDeposits = ssz.electra.PendingBalanceDeposits.defaultViewDU();
90+
// pendingBalanceDeposits, pendingPartialWithdrawals, pendingConsolidations are default values
91+
// TODO-electra: can we improve this?
92+
stateElectraView.commit();
93+
const tmpElectraState = getCachedBeaconState(stateElectraView, stateDeneb);
94+
stateElectraView.exitBalanceToConsume = BigInt(getActivationExitChurnLimit(tmpElectraState));
95+
stateElectraView.consolidationBalanceToConsume = BigInt(getConsolidationChurnLimit(tmpElectraState));
96+
97+
preActivation.sort((i0, i1) => {
98+
const res = validatorsArr[i0].activationEligibilityEpoch - validatorsArr[i1].activationEligibilityEpoch;
99+
return res !== 0 ? res : i0 - i1;
100+
});
101+
102+
for (const validatorIndex of preActivation) {
103+
queueEntireBalanceAndResetValidator(stateElectraView as CachedBeaconStateElectra, validatorIndex);
104+
}
63105

64106
for (let i = 0; i < validatorsArr.length; i++) {
65107
const validator = validatorsArr[i];
66108

67-
// [EIP-7251]: add validators that are not yet active to pending balance deposits
68-
if (validator.activationEligibilityEpoch === FAR_FUTURE_EPOCH) {
69-
queueEntireBalanceAndResetValidator(stateElectraView as CachedBeaconStateElectra, i);
70-
}
71-
72109
// [EIP-7251]: Ensure early adopters of compounding credentials go through the activation churn
73110
const withdrawalCredential = validator.withdrawalCredentials;
74111
if (hasCompoundingWithdrawalCredential(withdrawalCredential)) {

0 commit comments

Comments
 (0)