Use read only validators in Beacon API (#13873)

This commit is contained in:
Radosław Kapka
2024-04-12 16:19:40 +09:00
committed by GitHub
parent 425f5387fa
commit 8cd05f098b
3 changed files with 39 additions and 10 deletions

View File

@@ -118,6 +118,7 @@ type ReadOnlyValidator interface {
// ReadOnlyValidators defines a struct which only has read access to validators methods.
type ReadOnlyValidators interface {
Validators() []*ethpb.Validator
ValidatorsReadOnly() []ReadOnlyValidator
ValidatorAtIndex(idx primitives.ValidatorIndex) (*ethpb.Validator, error)
ValidatorAtIndexReadOnly(idx primitives.ValidatorIndex) (ReadOnlyValidator, error)
ValidatorIndexByPubkey(key [fieldparams.BLSPubkeyLength]byte) (primitives.ValidatorIndex, bool)

View File

@@ -21,6 +21,14 @@ func (b *BeaconState) Validators() []*ethpb.Validator {
return b.validatorsVal()
}
// ValidatorsReadOnly participating in consensus on the beacon chain.
func (b *BeaconState) ValidatorsReadOnly() []state.ReadOnlyValidator {
b.lock.RLock()
defer b.lock.RUnlock()
return b.validatorsReadOnlyVal()
}
func (b *BeaconState) validatorsVal() []*ethpb.Validator {
var v []*ethpb.Validator
if features.Get().EnableExperimentalState {
@@ -46,6 +54,35 @@ func (b *BeaconState) validatorsVal() []*ethpb.Validator {
return res
}
func (b *BeaconState) validatorsReadOnlyVal() []state.ReadOnlyValidator {
var v []*ethpb.Validator
if features.Get().EnableExperimentalState {
if b.validatorsMultiValue == nil {
return nil
}
v = b.validatorsMultiValue.Value(b)
} else {
if b.validators == nil {
return nil
}
v = b.validators
}
res := make([]state.ReadOnlyValidator, len(v))
var err error
for i := 0; i < len(res); i++ {
val := v[i]
if val == nil {
continue
}
res[i], err = NewValidator(val)
if err != nil {
continue
}
}
return res
}
// references of validators participating in consensus on the beacon chain.
// This assumes that a lock is already held on BeaconState. This does not
// copy fully and instead just copies the reference.