Compare commits

...

2 Commits

Author SHA1 Message Date
Potuz
636beebd3c Merge branch 'develop' into active_validator_count_helper 2024-05-08 14:49:43 -03:00
Potuz
e9029beaf5 Add helper to get active validator count 2024-05-08 14:34:53 -03:00
3 changed files with 54 additions and 0 deletions

View File

@@ -131,6 +131,7 @@ type ReadOnlyValidators interface {
PublicKeys() ([][fieldparams.BLSPubkeyLength]byte, error)
PubkeyAtIndex(idx primitives.ValidatorIndex) [fieldparams.BLSPubkeyLength]byte
NumValidators() int
NumActiveValidators() int
ReadFromEveryValidator(f func(idx int, val ReadOnlyValidator) error) error
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)
// Validators participating in consensus on the beacon chain.
@@ -252,6 +253,21 @@ func (b *BeaconState) NumValidators() int {
return b.validatorsLen()
}
// NumActiveValidators returns the size of the active validators in the
// registry
func (b *BeaconState) NumActiveValidators() int {
epoch := slots.ToEpoch(b.Slot())
total := 0
err := b.ReadFromEveryValidator(func(_ int, val state.ReadOnlyValidator) error {
if helpers.IsActiveValidatorUsingTrie(val, epoch) {
total++
}
return nil
})
_ = err
return total
}
// ReadFromEveryValidator reads values from every validator and applies it to the provided function.
//
// WARNING: This method is potentially unsafe, as it exposes the actual validator registry.

View File

@@ -10,9 +10,11 @@ import (
testtmpl "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/testing"
"github.com/prysmaticlabs/prysm/v5/config/params"
consensus_types "github.com/prysmaticlabs/prysm/v5/consensus-types"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/util"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)
func TestBeaconState_ValidatorAtIndexReadOnly_HandlesNilSlice_Phase0(t *testing.T) {
@@ -70,6 +72,41 @@ func TestValidatorIndexes(t *testing.T) {
})
}
func TestNumActiveValidators(t *testing.T) {
currentSlot := primitives.Slot(5 * 32)
currentEpoch := slots.ToEpoch(currentSlot)
vals := []*ethpb.Validator{
{
ActivationEpoch: 0,
ExitEpoch: currentEpoch + 1,
},
{
ActivationEpoch: currentEpoch + 1,
ExitEpoch: currentEpoch + 2,
},
{
ActivationEpoch: 0,
ExitEpoch: currentEpoch - 1,
},
}
pb := &ethpb.BeaconStateElectra{
Slot: currentSlot,
Validators: vals,
}
s, err := statenative.InitializeFromProtoElectra(pb)
require.NoError(t, err)
require.Equal(t, 1, s.NumActiveValidators())
pb.Validators = append(pb.Validators, &ethpb.Validator{
ActivationEpoch: currentEpoch - 1,
ExitEpoch: currentEpoch + 1,
})
s, err = statenative.InitializeFromProtoElectra(pb)
require.NoError(t, err)
require.Equal(t, 2, s.NumActiveValidators())
}
func TestActiveBalanceAtIndex(t *testing.T) {
// Test setup with a state with 4 validators.
// Validators 0 & 1 have compounding withdrawal credentials while validators 2 & 3 have BLS withdrawal credentials.