Skip to content

Commit

Permalink
Fix issues with electra upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Oct 31, 2024
1 parent c435099 commit 2314d60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
21 changes: 9 additions & 12 deletions beacon-chain/core/electra/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,15 @@ func UpgradeToElectra(beaconState state.BeaconState) (state.BeaconState, error)
}

// [New in Electra:EIP7251]
earliestExitEpoch := time.CurrentEpoch(beaconState)
farFutureEpoch := params.BeaconConfig().FarFutureEpoch
earliestExitEpoch := farFutureEpoch
preActivationIndices := make([]primitives.ValidatorIndex, 0)
compoundWithdrawalIndices := make([]primitives.ValidatorIndex, 0)
if err = beaconState.ReadFromEveryValidator(func(index int, val state.ReadOnlyValidator) error {
if val.ExitEpoch() != params.BeaconConfig().FarFutureEpoch && val.ExitEpoch() > earliestExitEpoch {
if val.ExitEpoch() < earliestExitEpoch {
earliestExitEpoch = val.ExitEpoch()
}
if val.ActivationEpoch() == params.BeaconConfig().FarFutureEpoch {
if val.ActivationEpoch() == farFutureEpoch {
preActivationIndices = append(preActivationIndices, primitives.ValidatorIndex(index))
}
if helpers.HasCompoundingWithdrawalCredential(val) {
Expand All @@ -205,14 +206,10 @@ func UpgradeToElectra(beaconState state.BeaconState) (state.BeaconState, error)
return nil, err
}

earliestExitEpoch++ // Increment to find the earliest possible exit epoch

// note: should be the same in prestate and post beaconState.
// we are deviating from the specs a bit as it calls for using the post beaconState
tab, err := helpers.TotalActiveBalance(beaconState)
if err != nil {
return nil, errors.Wrap(err, "failed to get total active balance")
if earliestExitEpoch == farFutureEpoch {
earliestExitEpoch = time.CurrentEpoch(beaconState)
}
earliestExitEpoch++ // Increment to find the earliest possible exit epoch

s := &ethpb.BeaconStateElectra{
GenesisTime: beaconState.GenesisTime(),
Expand Down Expand Up @@ -268,9 +265,9 @@ func UpgradeToElectra(beaconState state.BeaconState) (state.BeaconState, error)

DepositRequestsStartIndex: params.BeaconConfig().UnsetDepositRequestsStartIndex,
DepositBalanceToConsume: 0,
ExitBalanceToConsume: helpers.ActivationExitChurnLimit(primitives.Gwei(tab)),
ExitBalanceToConsume: 0,
EarliestExitEpoch: earliestExitEpoch,
ConsolidationBalanceToConsume: helpers.ConsolidationChurnLimit(primitives.Gwei(tab)),
ConsolidationBalanceToConsume: 0,
EarliestConsolidationEpoch: helpers.ActivationExitEpoch(slots.ToEpoch(beaconState.Slot())),
PendingDeposits: make([]*ethpb.PendingDeposit, 0),
PendingPartialWithdrawals: make([]*ethpb.PendingPartialWithdrawal, 0),
Expand Down
35 changes: 16 additions & 19 deletions beacon-chain/core/electra/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,37 @@ func TestUpgradeToElectra(t *testing.T) {
}
require.DeepEqual(t, wanted, protoHeader)

nwi, err := mSt.NextWithdrawalIndex()
nextWithdrawalIndex, err := mSt.NextWithdrawalIndex()
require.NoError(t, err)
require.Equal(t, uint64(0), nwi)
require.Equal(t, uint64(0), nextWithdrawalIndex)

lwvi, err := mSt.NextWithdrawalValidatorIndex()
nextWithdrawalValidatorIndex, err := mSt.NextWithdrawalValidatorIndex()
require.NoError(t, err)
require.Equal(t, primitives.ValidatorIndex(0), lwvi)
require.Equal(t, primitives.ValidatorIndex(0), nextWithdrawalValidatorIndex)

summaries, err := mSt.HistoricalSummaries()
require.NoError(t, err)
require.Equal(t, 0, len(summaries))

startIndex, err := mSt.DepositRequestsStartIndex()
depositRequestsStartIndex, err := mSt.DepositRequestsStartIndex()
require.NoError(t, err)
require.Equal(t, params.BeaconConfig().UnsetDepositRequestsStartIndex, startIndex)
require.Equal(t, params.BeaconConfig().UnsetDepositRequestsStartIndex, depositRequestsStartIndex)

balance, err := mSt.DepositBalanceToConsume()
depositBalanceToConsume, err := mSt.DepositBalanceToConsume()
require.NoError(t, err)
require.Equal(t, primitives.Gwei(0), balance)
require.Equal(t, primitives.Gwei(0), depositBalanceToConsume)

tab, err := helpers.TotalActiveBalance(mSt)
exitBalanceToConsume, err := mSt.ExitBalanceToConsume()
require.NoError(t, err)
require.Equal(t, primitives.Gwei(0), exitBalanceToConsume)

ebtc, err := mSt.ExitBalanceToConsume()
earliestExitEpoch, err := mSt.EarliestExitEpoch()
require.NoError(t, err)
require.Equal(t, helpers.ActivationExitChurnLimit(primitives.Gwei(tab)), ebtc)
require.Equal(t, primitives.Epoch(1), earliestExitEpoch)

eee, err := mSt.EarliestExitEpoch()
consolidationBalanceToConsume, err := mSt.ConsolidationBalanceToConsume()
require.NoError(t, err)
require.Equal(t, primitives.Epoch(1), eee)

cbtc, err := mSt.ConsolidationBalanceToConsume()
require.NoError(t, err)
require.Equal(t, helpers.ConsolidationChurnLimit(primitives.Gwei(tab)), cbtc)
require.Equal(t, primitives.Gwei(0), consolidationBalanceToConsume)

earliestConsolidationEpoch, err := mSt.EarliestConsolidationEpoch()
require.NoError(t, err)
Expand All @@ -178,8 +175,8 @@ func TestUpgradeToElectra(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(0), numPendingPartialWithdrawals)

consolidations, err := mSt.PendingConsolidations()
pendingConsolidations, err := mSt.PendingConsolidations()
require.NoError(t, err)
require.Equal(t, 0, len(consolidations))
require.Equal(t, 0, len(pendingConsolidations))

}

0 comments on commit 2314d60

Please sign in to comment.