Skip to content

Commit

Permalink
adding handling for ActivationEpoch
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Dec 7, 2024
1 parent 7409de1 commit 51a12a0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
12 changes: 9 additions & 3 deletions consensus-types/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ func (v Validator) IsActive(epoch math.Epoch) bool {
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#is_eligible_for_activation_queue
//
//nolint:lll
func (v Validator) IsEligibleForActivation(
finalizedEpoch math.Epoch,
) bool {
func (v Validator) IsEligibleForActivation(finalizedEpoch math.Epoch) bool {
return v.ActivationEligibilityEpoch <= finalizedEpoch &&
v.ActivationEpoch == math.Epoch(constants.FarFutureEpoch)
}
Expand Down Expand Up @@ -337,6 +335,14 @@ func (v *Validator) GetActivationEligibilityEpoch() math.Epoch {
return v.ActivationEligibilityEpoch
}

func (v *Validator) SetActivationEpoch(e math.Epoch) {
v.ActivationEpoch = e
}

func (v *Validator) GetActivationEpoch() math.Epoch {
return v.ActivationEpoch
}

// GetWithdrawalCredentials returns the withdrawal credentials of the validator.
func (v Validator) GetWithdrawalCredentials() WithdrawalCredentials {
return v.WithdrawalCredentials
Expand Down
1 change: 1 addition & 0 deletions state-transition/core/state_processor_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (sp *StateProcessor[
var idx math.ValidatorIndex
for _, val := range vals {
val.SetActivationEligibilityEpoch(0)
val.SetActivationEpoch(0)
idx, err = st.ValidatorIndexByPubkey(val.GetPubkey())
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions state-transition/core/state_processor_genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func checkValidatorNonBartio(

// checks on validators for any network but Bartio
require.Equal(t, math.Epoch(0), val.GetActivationEligibilityEpoch())
require.Equal(t, math.Epoch(0), val.GetActivationEpoch())

valBal, err := bs.GetBalance(idx)
require.NoError(t, err)
Expand Down Expand Up @@ -284,6 +285,17 @@ func checkValidatorBartio(
// checks on validators common to all networks
commonChecksValidators(t, cs, val, dep)

require.Equal(
t,
math.Epoch(constants.FarFutureEpoch),
val.GetActivationEligibilityEpoch(),
)
require.Equal(
t,
math.Epoch(constants.FarFutureEpoch),
val.GetActivationEpoch(),
)

// Bartio specific checks on validators
valBal, err := bs.GetBalance(idx)
require.NoError(t, err)
Expand Down
10 changes: 10 additions & 0 deletions state-transition/core/state_processor_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ func (sp *StateProcessor[

minEffectiveBalance := math.Gwei(sp.cs.EjectionBalance() + sp.cs.EffectiveBalanceIncrement())

// We do not currently have a cap on validator churn,
// so we can process validators in a single loop
var idx math.ValidatorIndex
for si, val := range vals {
valModified := false
if val.IsEligibleForActivationQueue(minEffectiveBalance) {
val.SetActivationEligibilityEpoch(nextEpoch)
valModified = true
}
if val.IsEligibleForActivation(currEpoch) {
val.SetActivationEpoch(nextEpoch)
valModified = true
}
if valModified {
idx, err = st.ValidatorIndexByPubkey(val.GetPubkey())
if err != nil {
return fmt.Errorf("registry update, failed loading validator index, state index %d: %w", si, err)
Expand Down
5 changes: 5 additions & 0 deletions state-transition/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ type Validator[
IsSlashed() bool

IsEligibleForActivationQueue(threshold math.Gwei) bool
IsEligibleForActivation(finalizedEpoch math.Epoch) bool

// GetPubkey returns the public key of the validator.
GetPubkey() crypto.BLSPubkey
// GetEffectiveBalance returns the effective balance of the validator in
Expand All @@ -258,6 +260,9 @@ type Validator[

GetActivationEligibilityEpoch() math.Epoch
SetActivationEligibilityEpoch(math.Epoch)

GetActivationEpoch() math.Epoch
SetActivationEpoch(math.Epoch)
}

type Validators interface {
Expand Down

0 comments on commit 51a12a0

Please sign in to comment.