|
1 |
| -import {ssz} from "@lodestar/types"; |
| 1 | +import {Epoch, ValidatorIndex, ssz} from "@lodestar/types"; |
2 | 2 | import {FAR_FUTURE_EPOCH, UNSET_DEPOSIT_RECEIPTS_START_INDEX} from "@lodestar/params";
|
3 | 3 | import {CachedBeaconStateDeneb} from "../types.js";
|
4 | 4 | import {CachedBeaconStateElectra, getCachedBeaconState} from "../cache/stateCache.js";
|
|
7 | 7 | queueEntireBalanceAndResetValidator,
|
8 | 8 | queueExcessActiveBalance,
|
9 | 9 | } from "../util/electra.js";
|
| 10 | +import {computeActivationExitEpoch} from "../util/epoch.js"; |
| 11 | +import {getActivationExitChurnLimit, getConsolidationChurnLimit} from "../util/validator.js"; |
10 | 12 |
|
11 | 13 | /**
|
12 | 14 | * Upgrade a state from Capella to Deneb.
|
@@ -58,17 +60,52 @@ export function upgradeStateToElectra(stateDeneb: CachedBeaconStateDeneb): Cache
|
58 | 60 | // latestExecutionPayloadHeader's depositReceiptsRoot and withdrawalRequestsRoot set to zeros by default
|
59 | 61 | // default value of depositReceiptsStartIndex is UNSET_DEPOSIT_RECEIPTS_START_INDEX
|
60 | 62 | stateElectraView.depositReceiptsStartIndex = UNSET_DEPOSIT_RECEIPTS_START_INDEX;
|
| 63 | + stateElectraView.depositBalanceToConsume = BigInt(0); |
| 64 | + stateElectraView.exitBalanceToConsume = BigInt(0); |
61 | 65 |
|
62 | 66 | 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 | + } |
63 | 105 |
|
64 | 106 | for (let i = 0; i < validatorsArr.length; i++) {
|
65 | 107 | const validator = validatorsArr[i];
|
66 | 108 |
|
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 |
| - |
72 | 109 | // [EIP-7251]: Ensure early adopters of compounding credentials go through the activation churn
|
73 | 110 | const withdrawalCredential = validator.withdrawalCredentials;
|
74 | 111 | if (hasCompoundingWithdrawalCredential(withdrawalCredential)) {
|
|
0 commit comments