Skip to content

Commit

Permalink
fix(state-transition): restore state sync over boonet (#2219)
Browse files Browse the repository at this point in the history
Co-authored-by: Cal Bera <[email protected]>
  • Loading branch information
abi87 and calbera authored Dec 6, 2024
1 parent d7e65c4 commit cf624db
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 4 additions & 0 deletions config/spec/special_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package spec

import "math"

// Special cased Bartio for some ad-hoc handling due to the way
// some bugs were handled on Bartio. To be removed.
const (
Expand All @@ -34,4 +36,6 @@ const (
BoonetFork1Height uint64 = 69420

BoonetFork2Height uint64 = 1722000

BoonetFork3Height uint64 = math.MaxUint64
)
64 changes: 60 additions & 4 deletions state-transition/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/log"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/constants"
"github.com/berachain/beacon-kit/primitives/crypto"
"github.com/berachain/beacon-kit/primitives/math"
"github.com/berachain/beacon-kit/primitives/transition"
Expand Down Expand Up @@ -374,15 +375,36 @@ func (sp *StateProcessor[
]) processEpoch(
st BeaconStateT,
) (transition.ValidatorUpdates, error) {
// currently no processRewardsAndPenalties
slot, err := st.GetSlot()
if err != nil {
return nil, err
}

if err := sp.processEffectiveBalanceUpdates(st); err != nil {
switch {
case sp.cs.DepositEth1ChainID() == spec.BartioChainID:
if err = sp.hollowProcessRewardsAndPenalties(st); err != nil {
return nil, err
}
case sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID &&
slot < math.U64(spec.BoonetFork3Height):
// We cannot simply drop hollowProcessRewardsAndPenalties because
// appHash accounts for the list of operations carried out
// over the state even if the operations does not affect the state
// (rewards and penalties are always zero at this stage of beaconKit)
if err = sp.hollowProcessRewardsAndPenalties(st); err != nil {
return nil, err
}
default:
// no real need to perform hollowProcessRewardsAndPenalties
}

if err = sp.processEffectiveBalanceUpdates(st); err != nil {
return nil, err
}
if err := sp.processSlashingsReset(st); err != nil {
if err = sp.processSlashingsReset(st); err != nil {
return nil, err
}
if err := sp.processRandaoMixesReset(st); err != nil {
if err = sp.processRandaoMixesReset(st); err != nil {
return nil, err
}
return sp.processValidatorsSetUpdates(st)
Expand Down Expand Up @@ -470,6 +492,40 @@ func (sp *StateProcessor[
return st.SetLatestBlockHeader(lbh)
}

func (sp *StateProcessor[
_, _, _, BeaconStateT, _, _, _, _, _, _, _, _, _, _, _, _, _,
]) hollowProcessRewardsAndPenalties(st BeaconStateT) error {
slot, err := st.GetSlot()
if err != nil {
return err
}

if sp.cs.SlotToEpoch(slot) == math.U64(constants.GenesisEpoch) {
return nil
}

// this has been simplified to make clear that
// we are not really doing anything here
valCount, err := st.GetTotalValidators()
if err != nil {
return err
}

for i := range valCount {
// Increase the balance of the validator.
if err = st.IncreaseBalance(math.ValidatorIndex(i), 0); err != nil {
return err
}

// Decrease the balance of the validator.
if err = st.DecreaseBalance(math.ValidatorIndex(i), 0); err != nil {
return err
}
}

return nil
}

// processEffectiveBalanceUpdates as defined in the Ethereum 2.0 specification.
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#effective-balances-updates
//
Expand Down
2 changes: 1 addition & 1 deletion state-transition/core/state_processor_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (sp *StateProcessor[
// We keep it for backward compatibility.
depositIndex++
case sp.cs.DepositEth1ChainID() == spec.BoonetEth1ChainID &&
slot < math.U64(spec.BoonetFork2Height):
slot != 0 && slot < math.U64(spec.BoonetFork2Height):
// Boonet pre fork2 has a bug which makes DepositEth1ChainID point to
// next deposit index, not latest processed deposit index.
// We keep it for backward compatibility.
Expand Down

0 comments on commit cf624db

Please sign in to comment.