From b97936385800db5eb5bf4cf464e9bc4cc1998201 Mon Sep 17 00:00:00 2001 From: Alberto Benegiamo Date: Fri, 13 Dec 2024 14:31:39 -0500 Subject: [PATCH] fix(state-transition): make `EffectiveBalance` update backward compatible (#2261) --- .../core/state_processor_staking.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/state-transition/core/state_processor_staking.go b/state-transition/core/state_processor_staking.go index 4ad29f238e..b7e62de497 100644 --- a/state-transition/core/state_processor_staking.go +++ b/state-transition/core/state_processor_staking.go @@ -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" @@ -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