Skip to content

Commit

Permalink
fix(state-transition): make EffectiveBalance update backward compat…
Browse files Browse the repository at this point in the history
…ible (#2261)
  • Loading branch information
abi87 authored Dec 13, 2024
1 parent e7c223a commit b979363
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions state-transition/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package core

import (
"github.com/berachain/beacon-kit/config/spec"
"github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/math"
Expand Down Expand Up @@ -119,6 +120,34 @@ func (sp *StateProcessor[
return sp.createValidator(st, dep)
}

// The validator already exist and we need to update its balance.
// EffectiveBalance must be updated in processEffectiveBalanceUpdates
// However before BoonetFork2Height we mistakenly update EffectiveBalance
// every slot. We must preserve backward compatibility so we special case
// Boonet to allow proper bootstrapping.
slot, err := st.GetSlot()
if err != nil {
return err
}
if sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID &&
slot < math.U64(spec.BoonetFork2Height) {
var val ValidatorT
val, err = st.ValidatorByIndex(idx)
if err != nil {
return err
}

updatedBalance := types.ComputeEffectiveBalance(
val.GetEffectiveBalance()+dep.GetAmount(),
math.Gwei(sp.cs.EffectiveBalanceIncrement()),
math.Gwei(sp.cs.MaxEffectiveBalance(false)),
)
val.SetEffectiveBalance(updatedBalance)
if err = st.UpdateValidatorAtIndex(idx, val); err != nil {
return err
}
}

// if validator exist, just update its balance
if err = st.IncreaseBalance(idx, dep.GetAmount()); err != nil {
return err
Expand Down

0 comments on commit b979363

Please sign in to comment.