Skip to content

Commit

Permalink
pending consolidations
Browse files Browse the repository at this point in the history
  • Loading branch information
domiwei committed Nov 3, 2024
1 parent 03a0b02 commit 54eaa81
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cl/abstract/beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type BeaconStateExtension interface {
GetDepositBalanceToConsume() uint64
GetPendingDeposits() *solid.ListSSZ[*solid.PendingDeposit]
GetDepositRequestsStartIndex() uint64
GetPendingConsolidations() *solid.ListSSZ[*solid.PendingConsolidation]
}

type BeaconStateBasic interface {
Expand Down Expand Up @@ -131,6 +132,7 @@ type BeaconStateMutator interface {
SetPendingPartialWithdrawals(*solid.ListSSZ[*solid.PendingPartialWithdrawal])
SetPendingDeposits(*solid.ListSSZ[*solid.PendingDeposit])
SetDepositBalanceToConsume(uint64)
SetPendingConsolidations(consolidations *solid.ListSSZ[*solid.PendingConsolidation])

AddEth1DataVote(vote *cltypes.Eth1Data)
AddValidator(validator solid.Validator, balance uint64)
Expand Down
5 changes: 5 additions & 0 deletions cl/phase1/core/state/raw/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ func (b *BeaconState) SetPendingDeposits(deposits *solid.ListSSZ[*solid.PendingD
b.markLeaf(PendingDepositsLeafIndex)
}

func (b *BeaconState) SetPendingConsolidations(consolidations *solid.ListSSZ[*solid.PendingConsolidation]) {
b.pendingConsolidations = consolidations
b.markLeaf(PendingConsolidationsLeafIndex)
}

func (b *BeaconState) SetDepositBalanceToConsume(balance uint64) {
b.depositBalanceToConsume = balance
b.markLeaf(DepositBalanceToConsumeLeafIndex)
Expand Down
4 changes: 4 additions & 0 deletions cl/phase1/core/state/raw/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,7 @@ func (b *BeaconState) GetPendingDeposits() *solid.ListSSZ[*solid.PendingDeposit]
func (b *BeaconState) GetDepositRequestsStartIndex() uint64 {
return b.depositRequestsStartIndex
}

func (b *BeaconState) GetPendingConsolidations() *solid.ListSSZ[*solid.PendingConsolidation] {
return b.pendingConsolidations
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
package statechange

import (
"errors"

"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/cl/abstract"
"github.com/erigontech/erigon/cl/cltypes/solid"
"github.com/erigontech/erigon/cl/phase1/core/state"
)

func ProcessPendingConsolidations(s abstract.BeaconState) error {
return errors.New("not implemented")
func ProcessPendingConsolidations(s abstract.BeaconState) {
nextEpoch := s.Slot()/s.BeaconConfig().SlotsPerEpoch + 1
nextConsolidationIndex := 0
s.GetPendingConsolidations().Range(func(i int, c *solid.PendingConsolidation, length int) bool {
sourceValidator, err := s.ValidatorForValidatorIndex(int(c.SourceIndex))
if err != nil {
log.Warn("Failed to get source validator for consolidation", "index", c.SourceIndex)
nextConsolidationIndex++
return true
}
if sourceValidator.Slashed() {
nextConsolidationIndex++
return true
}
if sourceValidator.WithdrawableEpoch() > nextEpoch {
return false // stop processing
}
// Calculate the consolidated balance
maxEffectiveBalance := state.GetMaxEffectiveBalanceByVersion(sourceValidator, s.BeaconConfig(), s.Version())
vBalance, err := s.ValidatorBalance(int(c.SourceIndex))
if err != nil {
log.Warn("Failed to get validator balance for consolidation", "index", c.SourceIndex)
nextConsolidationIndex++
return true
}
sourceEffectiveBalance := min(vBalance, maxEffectiveBalance)
// Move active balance to target. Excess balance is withdrawable.
state.DecreaseBalance(s, c.SourceIndex, sourceEffectiveBalance)
state.IncreaseBalance(s, c.TargetIndex, sourceEffectiveBalance)
nextConsolidationIndex++
return true
})
pendingConsolidations := s.GetPendingConsolidations().ShallowCopy()
pendingConsolidations.Cut(nextConsolidationIndex)
s.SetPendingConsolidations(pendingConsolidations)
}

0 comments on commit 54eaa81

Please sign in to comment.