prysm-pulse/beacon-chain/state/v1/readonly_validator.go
Preston Van Loon 412ddbb29e
beacon-chain/state/v1: ReadOnlyValidator wrapper constructor method (#9221)
* Introduce changes from Altair hf1 branch

* go pkg viz changes

* Fix test

* goimports for some reason

* Use a more safe method of wrapping validators with regards to nil validators. Add basic tests for this wrapped validator

* Use a more safe method of wrapping validators with regards to nil validators. Add basic tests for this wrapped validator

* Panic fixes

* Fix tests

* remove nil validator test as it is no longer possible

* goimports for some reason

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-07-19 18:26:39 +00:00

89 lines
2.5 KiB
Go

package v1
import (
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
)
var (
// ErrNilWrappedValidator returns when caller attempts to wrap a nil pointer validator.
ErrNilWrappedValidator = errors.New("nil validator cannot be wrapped as readonly")
)
// readOnlyValidator returns a wrapper that only allows fields from a validator
// to be read, and prevents any modification of internal validator fields.
type readOnlyValidator struct {
validator *ethpb.Validator
}
var _ = iface.ReadOnlyValidator(&readOnlyValidator{})
// NewValidator initializes the read only wrapper for validator.
func NewValidator(v *ethpb.Validator) (iface.ReadOnlyValidator, error) {
rov := &readOnlyValidator{
validator: v,
}
if rov.IsNil() {
return nil, ErrNilWrappedValidator
}
return rov, nil
}
// EffectiveBalance returns the effective balance of the
// read only validator.
func (v readOnlyValidator) EffectiveBalance() uint64 {
return v.validator.EffectiveBalance
}
// ActivationEligibilityEpoch returns the activation eligibility epoch of the
// read only validator.
func (v readOnlyValidator) ActivationEligibilityEpoch() types.Epoch {
return v.validator.ActivationEligibilityEpoch
}
// ActivationEpoch returns the activation epoch of the
// read only validator.
func (v readOnlyValidator) ActivationEpoch() types.Epoch {
return v.validator.ActivationEpoch
}
// WithdrawableEpoch returns the withdrawable epoch of the
// read only validator.
func (v readOnlyValidator) WithdrawableEpoch() types.Epoch {
return v.validator.WithdrawableEpoch
}
// ExitEpoch returns the exit epoch of the
// read only validator.
func (v readOnlyValidator) ExitEpoch() types.Epoch {
return v.validator.ExitEpoch
}
// PublicKey returns the public key of the
// read only validator.
func (v readOnlyValidator) PublicKey() [48]byte {
var pubkey [48]byte
copy(pubkey[:], v.validator.PublicKey)
return pubkey
}
// WithdrawalCredentials returns the withdrawal credentials of the
// read only validator.
func (v readOnlyValidator) WithdrawalCredentials() []byte {
creds := make([]byte, len(v.validator.WithdrawalCredentials))
copy(creds, v.validator.WithdrawalCredentials)
return creds
}
// Slashed returns the read only validator is slashed.
func (v readOnlyValidator) Slashed() bool {
return v.validator.Slashed
}
// IsNil returns true if the validator is nil.
func (v readOnlyValidator) IsNil() bool {
return v.validator == nil
}