Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Custom Type ValidatorIndex Across Prysm #8478

Merged
merged 21 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6c0010d
Use ValidtorIndex across Prysm. Build ok
terencechain Feb 19, 2021
86413fa
First take at fixing tests
terencechain Feb 19, 2021
388197e
Clean up e2e, fuzz... etc
terencechain Feb 19, 2021
9e64e05
Fix new lines
terencechain Feb 19, 2021
4a52220
Update beacon-chain/cache/proposer_indices_test.go
terencechain Feb 19, 2021
9a1df2c
Update beacon-chain/core/helpers/rewards_penalties.go
terencechain Feb 19, 2021
2904021
Update beacon-chain/core/helpers/shuffle.go
terencechain Feb 19, 2021
42dd515
Update validator/graffiti/parse_graffiti_test.go
terencechain Feb 19, 2021
f8c115a
Raul's feedback
terencechain Feb 19, 2021
e767094
Merge branch 'develop' into validator-index
rauljordan Feb 22, 2021
158fb5b
Radek's feedback
terencechain Feb 22, 2021
48c88a7
Merge branch 'validator-index' of github.com:prysmaticlabs/prysm into…
terencechain Feb 22, 2021
0ad9a7e
Merge branch 'develop' into validator-index
terencechain Feb 22, 2021
8ede772
Fix downcast int -> uint64
terencechain Feb 22, 2021
5b2655b
Merge branch 'validator-index' of github.com:prysmaticlabs/prysm into…
terencechain Feb 22, 2021
42360a3
Merge branch 'develop' into validator-index
rauljordan Feb 22, 2021
50023e6
Victor's feedback
terencechain Feb 22, 2021
c44c1d3
Merge branch 'validator-index' of github.com:prysmaticlabs/prysm into…
terencechain Feb 22, 2021
2392a53
Merge branch 'develop' into validator-index
terencechain Feb 22, 2021
cddba91
Merge refs/heads/develop into validator-index
prylabs-bulldozer[bot] Feb 22, 2021
86fa5f7
Merge refs/heads/develop into validator-index
prylabs-bulldozer[bot] Feb 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions beacon-chain/blockchain/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type HeadFetcher interface {
HeadRoot(ctx context.Context) ([]byte, error)
HeadBlock(ctx context.Context) (*ethpb.SignedBeaconBlock, error)
HeadState(ctx context.Context) (*state.BeaconState, error)
HeadValidatorsIndices(ctx context.Context, epoch types.Epoch) ([]uint64, error)
HeadValidatorsIndices(ctx context.Context, epoch types.Epoch) ([]types.ValidatorIndex, error)
HeadSeed(ctx context.Context, epoch types.Epoch) ([32]byte, error)
HeadGenesisValidatorRoot() [32]byte
HeadETH1Data() *ethpb.Eth1Data
Expand Down Expand Up @@ -167,12 +167,12 @@ func (s *Service) HeadState(ctx context.Context) (*state.BeaconState, error) {
}

// HeadValidatorsIndices returns a list of active validator indices from the head view of a given epoch.
func (s *Service) HeadValidatorsIndices(ctx context.Context, epoch types.Epoch) ([]uint64, error) {
func (s *Service) HeadValidatorsIndices(ctx context.Context, epoch types.Epoch) ([]types.ValidatorIndex, error) {
s.headLock.RLock()
defer s.headLock.RUnlock()

if !s.hasHeadState() {
return []uint64{}, nil
return []types.ValidatorIndex{}, nil
}
return helpers.ActiveValidatorIndices(s.headState(ctx), epoch)
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func reportEpochMetrics(ctx context.Context, postState, headState *stateTrie.Bea
slashingEffectiveBalance := uint64(0)

for i, validator := range postState.Validators() {
bal, err := postState.BalanceAtIndex(uint64(i))
bal, err := postState.BalanceAtIndex(types.ValidatorIndex(i))
if err != nil {
log.Errorf("Could not load validator balance: %v", err)
continue
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/blockchain/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ func (s *ChainService) AttestationPreState(_ context.Context, _ *ethpb.Attestati
}

// HeadValidatorsIndices mocks the same method in the chain service.
func (s *ChainService) HeadValidatorsIndices(_ context.Context, epoch types.Epoch) ([]uint64, error) {
func (s *ChainService) HeadValidatorsIndices(_ context.Context, epoch types.Epoch) ([]types.ValidatorIndex, error) {
if s.State == nil {
return []uint64{}, nil
return []types.ValidatorIndex{}, nil
}
return helpers.ActiveValidatorIndices(s.State, epoch)
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/cache/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCommitteesCache() *CommitteeCache {

// Committee fetches the shuffled indices by slot and committee index. Every list of indices
// represent one committee. Returns true if the list exists with slot and committee index. Otherwise returns false, nil.
func (c *CommitteeCache) Committee(slot types.Slot, seed [32]byte, index types.CommitteeIndex) ([]uint64, error) {
func (c *CommitteeCache) Committee(slot types.Slot, seed [32]byte, index types.CommitteeIndex) ([]types.ValidatorIndex, error) {
c.lock.RLock()
defer c.lock.RUnlock()

Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *CommitteeCache) AddCommitteeShuffledList(committees *Committees) error
}

// ActiveIndices returns the active indices of a given seed stored in cache.
func (c *CommitteeCache) ActiveIndices(seed [32]byte) ([]uint64, error) {
func (c *CommitteeCache) ActiveIndices(seed [32]byte) ([]types.ValidatorIndex, error) {
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.CommitteeCache.GetByKey(key(seed))
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/cache/committee_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewCommitteesCache() *FakeCommitteeCache {

// Committee fetches the shuffled indices by slot and committee index. Every list of indices
// represent one committee. Returns true if the list exists with slot and committee index. Otherwise returns false, nil.
func (c *FakeCommitteeCache) Committee(slot types.Slot, seed [32]byte, index types.CommitteeIndex) ([]uint64, error) {
func (c *FakeCommitteeCache) Committee(slot types.Slot, seed [32]byte, index types.CommitteeIndex) ([]types.ValidatorIndex, error) {
return nil, nil
}

Expand All @@ -27,12 +27,12 @@ func (c *FakeCommitteeCache) AddCommitteeShuffledList(committees *Committees) er
}

// AddProposerIndicesList updates the committee shuffled list with proposer indices.
func (c *FakeCommitteeCache) AddProposerIndicesList(seed [32]byte, indices []uint64) error {
func (c *FakeCommitteeCache) AddProposerIndicesList(seed [32]byte, indices []types.ValidatorIndex) error {
return nil
}

// ActiveIndices returns the active indices of a given seed stored in cache.
func (c *FakeCommitteeCache) ActiveIndices(seed [32]byte) ([]uint64, error) {
func (c *FakeCommitteeCache) ActiveIndices(seed [32]byte) ([]types.ValidatorIndex, error) {
return nil, nil
}

Expand All @@ -42,7 +42,7 @@ func (c *FakeCommitteeCache) ActiveIndicesCount(seed [32]byte) (int, error) {
}

// ProposerIndices returns the proposer indices of a given seed.
func (c *FakeCommitteeCache) ProposerIndices(seed [32]byte) ([]uint64, error) {
func (c *FakeCommitteeCache) ProposerIndices(seed [32]byte) ([]types.ValidatorIndex, error) {
return nil, nil
}

Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/cache/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCommitteeKeyFn_OK(t *testing.T) {
item := &Committees{
CommitteeCount: 1,
Seed: [32]byte{'A'},
ShuffledIndices: []uint64{1, 2, 3, 4, 5},
ShuffledIndices: []types.ValidatorIndex{1, 2, 3, 4, 5},
}

k, err := committeeKeyFn(item)
Expand All @@ -34,7 +34,7 @@ func TestCommitteeCache_CommitteesByEpoch(t *testing.T) {
cache := NewCommitteesCache()

item := &Committees{
ShuffledIndices: []uint64{1, 2, 3, 4, 5, 6},
ShuffledIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6},
Seed: [32]byte{'A'},
CommitteeCount: 3,
}
Expand All @@ -59,7 +59,7 @@ func TestCommitteeCache_CommitteesByEpoch(t *testing.T) {
func TestCommitteeCache_ActiveIndices(t *testing.T) {
cache := NewCommitteesCache()

item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []uint64{1, 2, 3, 4, 5, 6}}
item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}}
indices, err := cache.ActiveIndices(item.Seed)
require.NoError(t, err)
if indices != nil {
Expand All @@ -76,7 +76,7 @@ func TestCommitteeCache_ActiveIndices(t *testing.T) {
func TestCommitteeCache_ActiveCount(t *testing.T) {
cache := NewCommitteesCache()

item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []uint64{1, 2, 3, 4, 5, 6}}
item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}}
count, err := cache.ActiveIndicesCount(item.Seed)
require.NoError(t, err)
assert.Equal(t, 0, count, "Expected active count not to exist in empty cache")
Expand Down Expand Up @@ -120,8 +120,8 @@ func TestCommitteeCacheOutOfRange(t *testing.T) {
err := cache.CommitteeCache.Add(&Committees{
CommitteeCount: 1,
Seed: seed,
ShuffledIndices: []uint64{0},
SortedIndices: []uint64{},
ShuffledIndices: []types.ValidatorIndex{0},
SortedIndices: []types.ValidatorIndex{},
})
require.NoError(t, err)

Expand Down
10 changes: 7 additions & 3 deletions beacon-chain/cache/committees.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cache

import "errors"
import (
"errors"

types "github.com/prysmaticlabs/eth2-types"
)

// ErrNotCommittee will be returned when a cache object is not a pointer to
// a Committee struct.
Expand All @@ -10,6 +14,6 @@ var ErrNotCommittee = errors.New("object is not a committee struct")
type Committees struct {
CommitteeCount uint64
Seed [32]byte
ShuffledIndices []uint64
SortedIndices []uint64
ShuffledIndices []types.ValidatorIndex
SortedIndices []types.ValidatorIndex
}
3 changes: 2 additions & 1 deletion beacon-chain/cache/proposer_indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
types "github.com/prysmaticlabs/eth2-types"
"k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *ProposerIndicesCache) HasProposerIndices(r [32]byte) (bool, error) {
}

// ProposerIndices returns the proposer indices of a block root seed.
func (c *ProposerIndicesCache) ProposerIndices(r [32]byte) ([]uint64, error) {
func (c *ProposerIndicesCache) ProposerIndices(r [32]byte) ([]types.ValidatorIndex, error) {
c.lock.RLock()
defer c.lock.RUnlock()
obj, exists, err := c.ProposerIndicesCache.GetByKey(key(r))
Expand Down
4 changes: 3 additions & 1 deletion beacon-chain/cache/proposer_indices_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This file is used in fuzzer builds to bypass proposer indices caches.
package cache

import types "github.com/prysmaticlabs/eth2-types"

// FakeProposerIndicesCache is a struct with 1 queue for looking up proposer indices by root.
type FakeProposerIndicesCache struct {
}
Expand All @@ -19,7 +21,7 @@ func (c *FakeProposerIndicesCache) AddProposerIndices(p *ProposerIndices) error
}

// ProposerIndices returns the proposer indices of a block root seed.
func (c *FakeProposerIndicesCache) ProposerIndices(r [32]byte) ([]uint64, error) {
func (c *FakeProposerIndicesCache) ProposerIndices(r [32]byte) ([]types.ValidatorIndex, error) {
return nil, nil
}

Expand Down
6 changes: 4 additions & 2 deletions beacon-chain/cache/proposer_indices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strconv"
"testing"

types "github.com/prysmaticlabs/eth2-types"

terencechain marked this conversation as resolved.
Show resolved Hide resolved
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
Expand All @@ -12,7 +14,7 @@ import (
func TestProposerKeyFn_OK(t *testing.T) {
item := &ProposerIndices{
BlockRoot: [32]byte{'A'},
ProposerIndices: []uint64{1, 2, 3, 4, 5},
ProposerIndices: []types.ValidatorIndex{1, 2, 3, 4, 5},
}

k, err := proposerIndicesKeyFn(item)
Expand Down Expand Up @@ -48,7 +50,7 @@ func TestProposerCache_AddProposerIndicesList(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, true, has)

item := &ProposerIndices{BlockRoot: [32]byte{'B'}, ProposerIndices: []uint64{1, 2, 3, 4, 5, 6}}
item := &ProposerIndices{BlockRoot: [32]byte{'B'}, ProposerIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}}
require.NoError(t, cache.AddProposerIndices(item))

received, err = cache.ProposerIndices(item.BlockRoot)
Expand Down
8 changes: 6 additions & 2 deletions beacon-chain/cache/proposer_indices_type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cache

import "errors"
import (
"errors"

types "github.com/prysmaticlabs/eth2-types"
)

// ErrNotProposerIndices will be returned when a cache object is not a pointer to
// a ProposerIndices struct.
Expand All @@ -9,5 +13,5 @@ var ErrNotProposerIndices = errors.New("object is not a proposer indices struct"
// ProposerIndices defines the cached struct for proposer indices.
type ProposerIndices struct {
BlockRoot [32]byte
ProposerIndices []uint64
ProposerIndices []types.ValidatorIndex
}
3 changes: 2 additions & 1 deletion beacon-chain/core/blocks/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -248,7 +249,7 @@ func VerifyIndexedAttestation(ctx context.Context, beaconState *stateTrie.Beacon
indices := indexedAtt.AttestingIndices
farazdagi marked this conversation as resolved.
Show resolved Hide resolved
var pubkeys []bls.PublicKey
for i := 0; i < len(indices); i++ {
pubkeyAtIdx := beaconState.PubkeyAtIndex(indices[i])
pubkeyAtIdx := beaconState.PubkeyAtIndex(types.ValidatorIndex(indices[i]))
pk, err := bls.PublicKeyFromBytes(pubkeyAtIdx[:])
if err != nil {
return errors.Wrap(err, "could not deserialize validator public key")
Expand Down
5 changes: 3 additions & 2 deletions beacon-chain/core/blocks/attester_slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"

"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
Expand Down Expand Up @@ -56,12 +57,12 @@ func ProcessAttesterSlashings(
var slashedAny bool
var val stateTrie.ReadOnlyValidator
for _, validatorIndex := range slashableIndices {
val, err = beaconState.ValidatorAtIndexReadOnly(validatorIndex)
val, err = beaconState.ValidatorAtIndexReadOnly(types.ValidatorIndex(validatorIndex))
if err != nil {
return nil, err
}
if helpers.IsSlashableValidator(val.ActivationEpoch(), val.WithdrawableEpoch(), val.Slashed(), currentEpoch) {
beaconState, err = v.SlashValidator(beaconState, validatorIndex)
beaconState, err = v.SlashValidator(beaconState, types.ValidatorIndex(validatorIndex))
if err != nil {
return nil, errors.Wrapf(err, "could not slash validator index %d",
validatorIndex)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestPreGenesisDeposits_SkipInvalidDeposit(t *testing.T) {
require.Equal(t, false, ok, "bad pubkey should not exist in state")

for i := 1; i < newState.NumValidators(); i++ {
val, err := newState.ValidatorAtIndex(uint64(i))
val, err := newState.ValidatorAtIndex(types.ValidatorIndex(i))
require.NoError(t, err)
require.Equal(t, params.BeaconConfig().MaxEffectiveBalance, val.EffectiveBalance, "unequal effective balance")
require.Equal(t, types.Epoch(0), val.ActivationEpoch)
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/proposer_slashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) {
// We test the case when data is correct and verify the validator
// registry has been updated.
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
proposerIdx := uint64(1)
proposerIdx := types.ValidatorIndex(1)

header1 := &ethpb.SignedBeaconBlockHeader{
Header: testutil.HydrateBeaconHeader(&ethpb.BeaconBlockHeader{
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/core/blocks/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"

"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -148,7 +149,7 @@ func createAttestationSignatureSet(ctx context.Context, beaconState *stateTrie.B
indices := ia.AttestingIndices
pubkeys := make([][]byte, len(indices))
for i := 0; i < len(indices); i++ {
pubkeyAtIdx := beaconState.PubkeyAtIndex(indices[i])
pubkeyAtIdx := beaconState.PubkeyAtIndex(types.ValidatorIndex(indices[i]))
pubkeys[i] = pubkeyAtIdx[:]
}
aggP, err := bls.AggregatePublicKeys(pubkeys)
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/core/epoch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//shared/mathutil:go_default_library",
"//shared/params:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
],
)
Expand Down
Loading