2021-06-30 15:06:19 +00:00
|
|
|
package v1
|
2021-04-12 14:23:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-04 06:55:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2022-01-06 17:33:08 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-04-12 14:23:55 +00:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:38:48 +00:00
|
|
|
// ValidatorIndexOutOfRangeError represents an error scenario where a validator does not exist
|
|
|
|
// at a given index in the validator's array.
|
|
|
|
type ValidatorIndexOutOfRangeError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:26:39 +00:00
|
|
|
// NewValidatorIndexOutOfRangeError creates a new error instance.
|
2021-06-14 16:38:48 +00:00
|
|
|
func NewValidatorIndexOutOfRangeError(index types.ValidatorIndex) ValidatorIndexOutOfRangeError {
|
|
|
|
return ValidatorIndexOutOfRangeError{
|
|
|
|
message: fmt.Sprintf("index %d out of range", index),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the underlying error message.
|
|
|
|
func (e *ValidatorIndexOutOfRangeError) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2021-04-12 14:23:55 +00:00
|
|
|
// Validators participating in consensus on the beacon chain.
|
|
|
|
func (b *BeaconState) Validators() []*ethpb.Validator {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Validators == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.validators()
|
|
|
|
}
|
|
|
|
|
|
|
|
// validators participating in consensus on the beacon chain.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) validators() []*ethpb.Validator {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Validators == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*ethpb.Validator, len(b.state.Validators))
|
|
|
|
for i := 0; i < len(res); i++ {
|
|
|
|
val := b.state.Validators[i]
|
|
|
|
if val == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-04 06:55:57 +00:00
|
|
|
res[i] = ethpb.CopyValidator(val)
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
func (b *BeaconState) validatorsReferences() []*ethpb.Validator {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Validators == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*ethpb.Validator, len(b.state.Validators))
|
|
|
|
for i := 0; i < len(res); i++ {
|
|
|
|
validator := b.state.Validators[i]
|
|
|
|
if validator == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// copy validator reference instead.
|
|
|
|
res[i] = validator
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatorAtIndex is the validator at the provided index.
|
|
|
|
func (b *BeaconState) ValidatorAtIndex(idx types.ValidatorIndex) (*ethpb.Validator, error) {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil, ErrNilInnerState
|
|
|
|
}
|
|
|
|
if b.state.Validators == nil {
|
|
|
|
return ðpb.Validator{}, nil
|
|
|
|
}
|
|
|
|
if uint64(len(b.state.Validators)) <= uint64(idx) {
|
2021-06-14 16:38:48 +00:00
|
|
|
e := NewValidatorIndexOutOfRangeError(idx)
|
|
|
|
return nil, &e
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
val := b.state.Validators[idx]
|
2021-09-04 06:55:57 +00:00
|
|
|
return ethpb.CopyValidator(val), nil
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatorAtIndexReadOnly is the validator at the provided index. This method
|
|
|
|
// doesn't clone the validator.
|
2021-07-23 16:11:21 +00:00
|
|
|
func (b *BeaconState) ValidatorAtIndexReadOnly(idx types.ValidatorIndex) (state.ReadOnlyValidator, error) {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
2021-07-19 18:26:39 +00:00
|
|
|
return nil, ErrNilInnerState
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
if b.state.Validators == nil {
|
2022-01-18 08:19:20 +00:00
|
|
|
return nil, state.ErrNilValidatorsInState
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
if uint64(len(b.state.Validators)) <= uint64(idx) {
|
2021-06-14 16:38:48 +00:00
|
|
|
e := NewValidatorIndexOutOfRangeError(idx)
|
2021-07-19 18:26:39 +00:00
|
|
|
return nil, &e
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
2021-07-19 18:26:39 +00:00
|
|
|
return NewValidator(b.state.Validators[idx])
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatorIndexByPubkey returns a given validator by its 48-byte public key.
|
2022-01-06 17:33:08 +00:00
|
|
|
func (b *BeaconState) ValidatorIndexByPubkey(key [fieldparams.BLSPubkeyLength]byte) (types.ValidatorIndex, bool) {
|
2021-04-12 14:23:55 +00:00
|
|
|
if b == nil || b.valMapHandler == nil || b.valMapHandler.IsNil() {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
2021-05-05 08:28:35 +00:00
|
|
|
numOfVals := len(b.state.Validators)
|
|
|
|
|
2021-04-12 14:23:55 +00:00
|
|
|
idx, ok := b.valMapHandler.Get(key)
|
2021-05-05 08:28:35 +00:00
|
|
|
if ok && numOfVals <= int(idx) {
|
|
|
|
return types.ValidatorIndex(0), false
|
|
|
|
}
|
2021-04-12 14:23:55 +00:00
|
|
|
return idx, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// PubkeyAtIndex returns the pubkey at the given
|
|
|
|
// validator index.
|
2022-01-06 17:33:08 +00:00
|
|
|
func (b *BeaconState) PubkeyAtIndex(idx types.ValidatorIndex) [fieldparams.BLSPubkeyLength]byte {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
2022-01-06 17:33:08 +00:00
|
|
|
return [fieldparams.BLSPubkeyLength]byte{}
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
if uint64(idx) >= uint64(len(b.state.Validators)) {
|
2022-01-06 17:33:08 +00:00
|
|
|
return [fieldparams.BLSPubkeyLength]byte{}
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
if b.state.Validators[idx] == nil {
|
2022-01-06 17:33:08 +00:00
|
|
|
return [fieldparams.BLSPubkeyLength]byte{}
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
return bytesutil.ToBytes48(b.state.Validators[idx].PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumValidators returns the size of the validator registry.
|
|
|
|
func (b *BeaconState) NumValidators() int {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return len(b.state.Validators)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-07-23 16:11:21 +00:00
|
|
|
func (b *BeaconState) ReadFromEveryValidator(f func(idx int, val state.ReadOnlyValidator) error) error {
|
2021-04-12 14:23:55 +00:00
|
|
|
if !b.hasInnerState() {
|
|
|
|
return ErrNilInnerState
|
|
|
|
}
|
|
|
|
if b.state.Validators == nil {
|
|
|
|
return errors.New("nil validators in state")
|
|
|
|
}
|
|
|
|
b.lock.RLock()
|
|
|
|
validators := b.state.Validators
|
|
|
|
b.lock.RUnlock()
|
|
|
|
|
|
|
|
for i, v := range validators {
|
2021-07-19 18:26:39 +00:00
|
|
|
v, err := NewValidator(v)
|
2021-04-12 14:23:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-19 18:26:39 +00:00
|
|
|
if err := f(i, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-12 14:23:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Balances of validators participating in consensus on the beacon chain.
|
|
|
|
func (b *BeaconState) Balances() []uint64 {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Balances == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.balances()
|
|
|
|
}
|
|
|
|
|
|
|
|
// balances of validators participating in consensus on the beacon chain.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) balances() []uint64 {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Balances == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]uint64, len(b.state.Balances))
|
|
|
|
copy(res, b.state.Balances)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// BalanceAtIndex of validator with the provided index.
|
|
|
|
func (b *BeaconState) BalanceAtIndex(idx types.ValidatorIndex) (uint64, error) {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0, ErrNilInnerState
|
|
|
|
}
|
|
|
|
if b.state.Balances == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
if uint64(len(b.state.Balances)) <= uint64(idx) {
|
|
|
|
return 0, fmt.Errorf("index of %d does not exist", idx)
|
|
|
|
}
|
|
|
|
return b.state.Balances[idx], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BalancesLength returns the length of the balances slice.
|
|
|
|
func (b *BeaconState) BalancesLength() int {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if b.state.Balances == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.balancesLength()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slashings of validators on the beacon chain.
|
|
|
|
func (b *BeaconState) Slashings() []uint64 {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Slashings == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.RLock()
|
|
|
|
defer b.lock.RUnlock()
|
|
|
|
|
|
|
|
return b.slashings()
|
|
|
|
}
|
|
|
|
|
|
|
|
// slashings of validators on the beacon chain.
|
|
|
|
// This assumes that a lock is already held on BeaconState.
|
|
|
|
func (b *BeaconState) slashings() []uint64 {
|
|
|
|
if !b.hasInnerState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.state.Slashings == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]uint64, len(b.state.Slashings))
|
|
|
|
copy(res, b.state.Slashings)
|
|
|
|
return res
|
|
|
|
}
|