mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 19:40:37 +00:00
44973b0bb3
* in progress... * in progress... * remove log * log root * Revert "Auxiliary commit to revert individual files from f12a609ea2a2f1e87e97321f3a717cd324b5ae97" This reverts commit 5ae35edb6477d8d0ea4e94b273efc6590484da85. * cleanup * remove log * remove whitespace * remove logs * more stuff * copy * always rebuild trie * revert * add state * init state * fix all * uintptr * move slice to new package * lock in `Detach` * remove constraint * reorder * blockroots and stateroots * fill roots in empty() * fix hasher * implement slice for balances and inactivity scores * detach in setters * Revert "implement slice for balances and inactivity scores" This reverts commit 59eb9df8d766cb1c44a7eb5b3f5e3c042249943d. # Conflicts: # beacon-chain/state/state-native/setters_validator.go * use counter to track states * typos * rename interface * balances * gauge * some improvements * first try with map * fix * inactivity scores in progress * fix test # Conflicts: # beacon-chain/state/state-native/helpers_test.go * test fixes * ToProto fix * copy roots * validators * build fixes * fix bug in `ToProto` * fix fuzz test * fix bug in slice getters * fix state equality checks * make tests pass * make tests pass * more test updates * Revert "Auxiliary commit to revert individual files from 34e7344bff08a589e6341bb1829e3cb74159e878" This reverts commit ecd64efa8917f37ca41460e0356ff007fe55dd9d. * Revert "make tests pass" This reverts commit 0cf00f19eecf4678cd2b866dd107f3179d0426ef. * Revert "make tests pass" This reverts commit 521b65e1d2e13be3d720f333008b6838a8e78878. * pass tests * deepequal identifiable types * Deflake `cloners_test.go` * feature flag for block roots * feature flag * remove recursive locks * reduce complexity of rootSelector * fix randao mixes root * some fixes * revisit tests * revert change to field trie helpers * initialize field map for tests * remove whitespace * initialize roots with proper length * more fixes * out of bounds message fix * optimize length calculation * remove call to Len in PubkeyAtIndex * don't log deposits * unit tests * unit tests * fix * comments * test fixes * id * remove Enumerator interface * review feedback * simplify field trie * bring back fieldtrie package * fix bazel file * use handle32ByteArrays for root computation * fix locks * metrics * bzl * simplify some things * use htr in state test * remove code from require package * gzl * more htr * Fuzzing of the multi-value slice * assert values * getter optimizations * use At when reading from validators * Nishant's review * restore safe copy * remove empty line * build fix * restore how we get root at index for deafult mode * more review comments * optimize default behavior * simplify Slice calls * test fix * remove unnecessary package * remove unused setter * make fieldMap unexported * some improvements in state package * call `Slice` instead of manually copying * unlock in ReadFromEveryValidator * Potuz's comments * lock the state when reading from all validators # Conflicts: # beacon-chain/state/state-native/getters_validator.go * add back preston's changes * add index --------- Co-authored-by: Potuz <potuz@prysmaticlabs.com> Co-authored-by: nisdas <nishdas93@gmail.com> Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
353 lines
8.8 KiB
Go
353 lines
8.8 KiB
Go
package state_native
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/v4/config/features"
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
|
consensus_types "github.com/prysmaticlabs/prysm/v4/consensus-types"
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/v4/runtime/version"
|
|
)
|
|
|
|
// Validators participating in consensus on the beacon chain.
|
|
func (b *BeaconState) Validators() []*ethpb.Validator {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.validatorsVal()
|
|
}
|
|
|
|
func (b *BeaconState) validatorsVal() []*ethpb.Validator {
|
|
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([]*ethpb.Validator, len(v))
|
|
for i := 0; i < len(res); i++ {
|
|
val := v[i]
|
|
if val == nil {
|
|
continue
|
|
}
|
|
res[i] = ethpb.CopyValidator(val)
|
|
}
|
|
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.validators == nil {
|
|
return nil
|
|
}
|
|
|
|
res := make([]*ethpb.Validator, len(b.validators))
|
|
for i := 0; i < len(res); i++ {
|
|
validator := b.validators[i]
|
|
if validator == nil {
|
|
continue
|
|
}
|
|
// copy validator reference instead.
|
|
res[i] = validator
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (b *BeaconState) validatorsLen() int {
|
|
if features.Get().EnableExperimentalState {
|
|
if b.validatorsMultiValue == nil {
|
|
return 0
|
|
}
|
|
return b.validatorsMultiValue.Len(b)
|
|
}
|
|
return len(b.validators)
|
|
}
|
|
|
|
// ValidatorAtIndex is the validator at the provided index.
|
|
func (b *BeaconState) ValidatorAtIndex(idx primitives.ValidatorIndex) (*ethpb.Validator, error) {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.validatorAtIndex(idx)
|
|
}
|
|
|
|
func (b *BeaconState) validatorAtIndex(idx primitives.ValidatorIndex) (*ethpb.Validator, error) {
|
|
if features.Get().EnableExperimentalState {
|
|
if b.validatorsMultiValue == nil {
|
|
return ðpb.Validator{}, nil
|
|
}
|
|
v, err := b.validatorsMultiValue.At(b, uint64(idx))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ethpb.CopyValidator(v), nil
|
|
}
|
|
|
|
if b.validators == nil {
|
|
return ðpb.Validator{}, nil
|
|
}
|
|
if uint64(len(b.validators)) <= uint64(idx) {
|
|
return nil, errors.Wrapf(consensus_types.ErrOutOfBounds, "validator index %d does not exist", idx)
|
|
}
|
|
val := b.validators[idx]
|
|
return ethpb.CopyValidator(val), nil
|
|
}
|
|
|
|
// ValidatorAtIndexReadOnly is the validator at the provided index. This method
|
|
// doesn't clone the validator.
|
|
func (b *BeaconState) ValidatorAtIndexReadOnly(idx primitives.ValidatorIndex) (state.ReadOnlyValidator, error) {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
if features.Get().EnableExperimentalState {
|
|
if b.validatorsMultiValue == nil {
|
|
return nil, state.ErrNilValidatorsInState
|
|
}
|
|
v, err := b.validatorsMultiValue.At(b, uint64(idx))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return NewValidator(v)
|
|
}
|
|
|
|
if b.validators == nil {
|
|
return nil, state.ErrNilValidatorsInState
|
|
}
|
|
if uint64(len(b.validators)) <= uint64(idx) {
|
|
return nil, errors.Wrapf(consensus_types.ErrOutOfBounds, "validator index %d does not exist", idx)
|
|
}
|
|
val := b.validators[idx]
|
|
return NewValidator(val)
|
|
}
|
|
|
|
// ValidatorIndexByPubkey returns a given validator by its 48-byte public key.
|
|
func (b *BeaconState) ValidatorIndexByPubkey(key [fieldparams.BLSPubkeyLength]byte) (primitives.ValidatorIndex, bool) {
|
|
if b == nil || b.valMapHandler == nil || b.valMapHandler.IsNil() {
|
|
return 0, false
|
|
}
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
var numOfVals int
|
|
if features.Get().EnableExperimentalState {
|
|
numOfVals = b.validatorsMultiValue.Len(b)
|
|
} else {
|
|
numOfVals = len(b.validators)
|
|
}
|
|
|
|
idx, ok := b.valMapHandler.Get(key)
|
|
if ok && primitives.ValidatorIndex(numOfVals) <= idx {
|
|
return primitives.ValidatorIndex(0), false
|
|
}
|
|
return idx, ok
|
|
}
|
|
|
|
// PubkeyAtIndex returns the pubkey at the given
|
|
// validator index.
|
|
func (b *BeaconState) PubkeyAtIndex(idx primitives.ValidatorIndex) [fieldparams.BLSPubkeyLength]byte {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
var v *ethpb.Validator
|
|
if features.Get().EnableExperimentalState {
|
|
var err error
|
|
v, err = b.validatorsMultiValue.At(b, uint64(idx))
|
|
if err != nil {
|
|
return [fieldparams.BLSPubkeyLength]byte{}
|
|
}
|
|
} else {
|
|
if uint64(idx) >= uint64(len(b.validators)) {
|
|
return [fieldparams.BLSPubkeyLength]byte{}
|
|
}
|
|
v = b.validators[idx]
|
|
}
|
|
|
|
if v == nil {
|
|
return [fieldparams.BLSPubkeyLength]byte{}
|
|
}
|
|
return bytesutil.ToBytes48(v.PublicKey)
|
|
}
|
|
|
|
// NumValidators returns the size of the validator registry.
|
|
func (b *BeaconState) NumValidators() int {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.validatorsLen()
|
|
}
|
|
|
|
// 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.
|
|
func (b *BeaconState) ReadFromEveryValidator(f func(idx int, val state.ReadOnlyValidator) error) error {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
if features.Get().EnableExperimentalState {
|
|
return b.readFromEveryValidatorMVSlice(f)
|
|
}
|
|
|
|
if b.validators == nil {
|
|
return state.ErrNilValidatorsInState
|
|
}
|
|
|
|
validators := b.validators
|
|
|
|
for i, v := range validators {
|
|
v, err := NewValidator(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = f(i, v); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// WARNING: This function works only for the multi-value slice feature.
|
|
func (b *BeaconState) readFromEveryValidatorMVSlice(f func(idx int, val state.ReadOnlyValidator) error) error {
|
|
if b.validatorsMultiValue == nil {
|
|
return state.ErrNilValidatorsInState
|
|
}
|
|
l := b.validatorsMultiValue.Len(b)
|
|
for i := 0; i < l; i++ {
|
|
v, err := b.validatorsMultiValue.At(b, uint64(i))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rov, err := NewValidator(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = f(i, rov); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Balances of validators participating in consensus on the beacon chain.
|
|
func (b *BeaconState) Balances() []uint64 {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.balancesVal()
|
|
}
|
|
|
|
func (b *BeaconState) balancesVal() []uint64 {
|
|
if features.Get().EnableExperimentalState {
|
|
if b.balancesMultiValue == nil {
|
|
return nil
|
|
}
|
|
return b.balancesMultiValue.Value(b)
|
|
}
|
|
if b.balances == nil {
|
|
return nil
|
|
}
|
|
res := make([]uint64, len(b.balances))
|
|
copy(res, b.balances)
|
|
return res
|
|
}
|
|
|
|
// BalanceAtIndex of validator with the provided index.
|
|
func (b *BeaconState) BalanceAtIndex(idx primitives.ValidatorIndex) (uint64, error) {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.balanceAtIndex(idx)
|
|
}
|
|
|
|
func (b *BeaconState) balanceAtIndex(idx primitives.ValidatorIndex) (uint64, error) {
|
|
if features.Get().EnableExperimentalState {
|
|
if b.balancesMultiValue == nil {
|
|
return 0, nil
|
|
}
|
|
return b.balancesMultiValue.At(b, uint64(idx))
|
|
}
|
|
if b.balances == nil {
|
|
return 0, nil
|
|
}
|
|
if uint64(len(b.balances)) <= uint64(idx) {
|
|
return 0, errors.Wrapf(consensus_types.ErrOutOfBounds, "balance index %d does not exist", idx)
|
|
}
|
|
return b.balances[idx], nil
|
|
}
|
|
|
|
// BalancesLength returns the length of the balances slice.
|
|
func (b *BeaconState) BalancesLength() int {
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
if features.Get().EnableExperimentalState {
|
|
if b.balancesMultiValue == nil {
|
|
return 0
|
|
}
|
|
return b.balancesMultiValue.Len(b)
|
|
}
|
|
return len(b.balances)
|
|
}
|
|
|
|
// Slashings of validators on the beacon chain.
|
|
func (b *BeaconState) Slashings() []uint64 {
|
|
if b.slashings == nil {
|
|
return nil
|
|
}
|
|
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.slashingsVal()
|
|
}
|
|
|
|
// slashingsVal of validators on the beacon chain.
|
|
// This assumes that a lock is already held on BeaconState.
|
|
func (b *BeaconState) slashingsVal() []uint64 {
|
|
if b.slashings == nil {
|
|
return nil
|
|
}
|
|
|
|
res := make([]uint64, len(b.slashings))
|
|
copy(res, b.slashings)
|
|
return res
|
|
}
|
|
|
|
// InactivityScores of validators participating in consensus on the beacon chain.
|
|
func (b *BeaconState) InactivityScores() ([]uint64, error) {
|
|
if b.version == version.Phase0 {
|
|
return nil, errNotSupported("InactivityScores", b.version)
|
|
}
|
|
|
|
b.lock.RLock()
|
|
defer b.lock.RUnlock()
|
|
|
|
return b.inactivityScoresVal(), nil
|
|
}
|
|
|
|
func (b *BeaconState) inactivityScoresVal() []uint64 {
|
|
if features.Get().EnableExperimentalState {
|
|
if b.inactivityScoresMultiValue == nil {
|
|
return nil
|
|
}
|
|
return b.inactivityScoresMultiValue.Value(b)
|
|
}
|
|
if b.inactivityScores == nil {
|
|
return nil
|
|
}
|
|
res := make([]uint64, len(b.inactivityScores))
|
|
copy(res, b.inactivityScores)
|
|
return res
|
|
}
|