ETH2 API: Implement v1/state/validator endpoints (#8896)

* Implement validator endpoints

* Import

* Fix errors

* Add comments and fix naming :wq

* Fix functions to access state once

* Optimization for slots

* Test more

* Fix

* test listing committees for specific epoch

* Fix error retrurn

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: rkapka <rkapka@wp.pl>
This commit is contained in:
Ivan Martinez
2021-05-20 03:41:39 -04:00
committed by GitHub
parent 36e9edd654
commit 8aa909de2d
5 changed files with 618 additions and 5 deletions

View File

@@ -62,6 +62,7 @@ go_test(
"pool_test.go",
"server_test.go",
"state_test.go",
"validator_test.go",
],
embed = [":go_default_library"],
deps = [
@@ -75,6 +76,7 @@ go_test(
"//beacon-chain/p2p/testing:go_default_library",
"//beacon-chain/powchain/testing:go_default_library",
"//beacon-chain/rpc/statefetcher:go_default_library",
"//beacon-chain/state/interface:go_default_library",
"//beacon-chain/state/stategen:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/migration:go_default_library",

View File

@@ -2,27 +2,162 @@ package beaconv1
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/proto/migration"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// GetValidator returns a validator specified by state and id or public key along with status and balance.
func (bs *Server) GetValidator(ctx context.Context, req *ethpb.StateValidatorRequest) (*ethpb.StateValidatorResponse, error) {
return nil, errors.New("unimplemented")
state, err := bs.StateFetcher.State(ctx, req.StateId)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
}
if len(req.ValidatorId) == 0 {
return nil, status.Error(codes.Internal, "Must request a validator id")
}
valContainer, err := valContainersByRequestIds(state, [][]byte{req.ValidatorId})
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get validator container: %v", err)
}
return &ethpb.StateValidatorResponse{Data: valContainer[0]}, nil
}
// ListValidators returns filterable list of validators with their balance, status and index.
// TODO(#8901): missing status support.
func (bs *Server) ListValidators(ctx context.Context, req *ethpb.StateValidatorsRequest) (*ethpb.StateValidatorsResponse, error) {
return nil, errors.New("unimplemented")
state, err := bs.StateFetcher.State(ctx, req.StateId)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
}
valContainers, err := valContainersByRequestIds(state, req.Id)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get validator container: %v", err)
}
return &ethpb.StateValidatorsResponse{Data: valContainers}, nil
}
// ListValidatorBalances returns a filterable list of validator balances.
func (bs *Server) ListValidatorBalances(ctx context.Context, req *ethpb.ValidatorBalancesRequest) (*ethpb.ValidatorBalancesResponse, error) {
return nil, errors.New("unimplemented")
state, err := bs.StateFetcher.State(ctx, req.StateId)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
}
valContainers, err := valContainersByRequestIds(state, req.Id)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get validator: %v", err)
}
valBalances := make([]*ethpb.ValidatorBalance, len(valContainers))
for i := 0; i < len(valContainers); i++ {
valBalances[i] = &ethpb.ValidatorBalance{
Index: valContainers[i].Index,
Balance: valContainers[i].Balance,
}
}
return &ethpb.ValidatorBalancesResponse{Data: valBalances}, nil
}
// ListCommittees retrieves the committees for the given state at the given epoch.
// If the requested slot and index are defined, only those committees are returned.
func (bs *Server) ListCommittees(ctx context.Context, req *ethpb.StateCommitteesRequest) (*ethpb.StateCommitteesResponse, error) {
return nil, errors.New("unimplemented")
state, err := bs.StateFetcher.State(ctx, req.StateId)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get state: %v", err)
}
epoch := helpers.SlotToEpoch(state.Slot())
if req.Epoch != nil {
epoch = *req.Epoch
}
activeCount, err := helpers.ActiveValidatorCount(state, epoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get active validator count: %v", err)
}
startSlot, err := helpers.StartSlot(epoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get epoch start slot: %v", err)
}
endSlot, err := helpers.EndSlot(epoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get epoch end slot: %v", err)
}
committeesPerSlot := helpers.SlotCommitteeCount(activeCount)
committees := make([]*ethpb.Committee, 0)
for slot := startSlot; slot <= endSlot; slot++ {
if req.Slot != nil && slot != *req.Slot {
continue
}
for index := types.CommitteeIndex(0); index < types.CommitteeIndex(committeesPerSlot); index++ {
if req.Index != nil && index != *req.Index {
continue
}
committee, err := helpers.BeaconCommitteeFromState(state, slot, index)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get committee: %v", err)
}
committeeContainer := &ethpb.Committee{
Index: index,
Slot: slot,
Validators: committee,
}
committees = append(committees, committeeContainer)
}
}
return &ethpb.StateCommitteesResponse{Data: committees}, nil
}
// This function returns the validator object based on the passed in ID. The validator ID could be its public key,
// or its index.
func valContainersByRequestIds(state iface.BeaconState, validatorIds [][]byte) ([]*ethpb.ValidatorContainer, error) {
allValidators := state.Validators()
allBalances := state.Balances()
var valContainers []*ethpb.ValidatorContainer
if len(validatorIds) == 0 {
valContainers = make([]*ethpb.ValidatorContainer, len(allValidators))
for i, validator := range allValidators {
valContainers[i] = &ethpb.ValidatorContainer{
Index: types.ValidatorIndex(i),
Balance: allBalances[i],
Validator: migration.V1Alpha1ValidatorToV1(validator),
}
}
} else {
valContainers = make([]*ethpb.ValidatorContainer, len(validatorIds))
for i, validatorId := range validatorIds {
var valIndex types.ValidatorIndex
if len(validatorId) == params.BeaconConfig().BLSPubkeyLength {
var ok bool
valIndex, ok = state.ValidatorIndexByPubkey(bytesutil.ToBytes48(validatorId))
if !ok {
return nil, fmt.Errorf("could not find validator with public key: %#x", validatorId)
}
} else {
index, err := strconv.ParseUint(string(validatorId), 10, 64)
if err != nil {
return nil, errors.Wrap(err, "could not decode validator id")
}
valIndex = types.ValidatorIndex(index)
}
valContainers[i] = &ethpb.ValidatorContainer{
Index: valIndex,
Balance: allBalances[valIndex],
Validator: migration.V1Alpha1ValidatorToV1(allValidators[valIndex]),
}
}
}
return valContainers, nil
}

View File

@@ -0,0 +1,433 @@
package beaconv1
import (
"bytes"
"context"
"strings"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/statefetcher"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestGetValidator(t *testing.T) {
ctx := context.Background()
var state iface.BeaconState
state, _ = testutil.DeterministicGenesisState(t, 8192)
t.Run("Head Get Validator by index", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
resp, err := s.GetValidator(ctx, &ethpb.StateValidatorRequest{
StateId: []byte("head"),
ValidatorId: []byte("15"),
})
require.NoError(t, err)
assert.Equal(t, types.ValidatorIndex(15), resp.Data.Index)
})
t.Run("Head Get Validator by pubkey", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
pubKey := state.PubkeyAtIndex(types.ValidatorIndex(20))
resp, err := s.GetValidator(ctx, &ethpb.StateValidatorRequest{
StateId: []byte("head"),
ValidatorId: pubKey[:],
})
require.NoError(t, err)
assert.Equal(t, types.ValidatorIndex(20), resp.Data.Index)
assert.Equal(t, true, bytes.Equal(pubKey[:], resp.Data.Validator.PublicKey))
})
t.Run("Hex root not found", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
require.NoError(t, err)
_, err = s.GetValidator(ctx, &ethpb.StateValidatorRequest{
StateId: stateId,
})
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
})
t.Run("Invalid state ID", func(t *testing.T) {
s := Server{}
pubKey := state.PubkeyAtIndex(types.ValidatorIndex(20))
_, err := s.GetValidator(ctx, &ethpb.StateValidatorRequest{
StateId: []byte("foo"),
ValidatorId: pubKey[:],
})
require.ErrorContains(t, "invalid state ID: foo", err)
})
t.Run("Validator ID required", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
_, err := s.GetValidator(ctx, &ethpb.StateValidatorRequest{
StateId: []byte("head"),
})
require.ErrorContains(t, "Must request a validator id", err)
})
}
func TestListValidators(t *testing.T) {
ctx := context.Background()
var state iface.BeaconState
state, _ = testutil.DeterministicGenesisState(t, 8192)
t.Run("Head List All Validators", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
resp, err := s.ListValidators(ctx, &ethpb.StateValidatorsRequest{
StateId: []byte("head"),
})
require.NoError(t, err)
assert.Equal(t, len(resp.Data), 8192)
})
t.Run("Head List Validators by index", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
ids := [][]byte{[]byte("15"), []byte("26"), []byte("400")}
idNums := []types.ValidatorIndex{15, 26, 400}
resp, err := s.ListValidators(ctx, &ethpb.StateValidatorsRequest{
StateId: []byte("head"),
Id: ids,
})
require.NoError(t, err)
for i, val := range resp.Data {
assert.Equal(t, idNums[i], val.Index)
}
})
t.Run("Head List Validators by pubkey", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
idNums := []types.ValidatorIndex{20, 66, 90, 100}
pubkey1 := state.PubkeyAtIndex(types.ValidatorIndex(20))
pubkey2 := state.PubkeyAtIndex(types.ValidatorIndex(66))
pubkey3 := state.PubkeyAtIndex(types.ValidatorIndex(90))
pubkey4 := state.PubkeyAtIndex(types.ValidatorIndex(100))
pubKeys := [][]byte{pubkey1[:], pubkey2[:], pubkey3[:], pubkey4[:]}
resp, err := s.ListValidators(ctx, &ethpb.StateValidatorsRequest{
StateId: []byte("head"),
Id: pubKeys,
})
require.NoError(t, err)
for i, val := range resp.Data {
assert.Equal(t, idNums[i], val.Index)
assert.Equal(t, true, bytes.Equal(pubKeys[i], val.Validator.PublicKey))
}
})
t.Run("Head List Validators by both index and pubkey", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
idNums := []types.ValidatorIndex{20, 90, 170, 129}
pubkey1 := state.PubkeyAtIndex(types.ValidatorIndex(20))
pubkey2 := state.PubkeyAtIndex(types.ValidatorIndex(90))
pubkey3 := state.PubkeyAtIndex(types.ValidatorIndex(170))
pubkey4 := state.PubkeyAtIndex(types.ValidatorIndex(129))
pubkeys := [][]byte{pubkey1[:], pubkey2[:], pubkey3[:], pubkey4[:]}
ids := [][]byte{pubkey1[:], []byte("90"), pubkey3[:], []byte("129")}
resp, err := s.ListValidators(ctx, &ethpb.StateValidatorsRequest{
StateId: []byte("head"),
Id: ids,
})
require.NoError(t, err)
for i, val := range resp.Data {
assert.Equal(t, idNums[i], val.Index)
assert.Equal(t, true, bytes.Equal(pubkeys[i], val.Validator.PublicKey))
}
})
t.Run("Hex root not found", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
require.NoError(t, err)
_, err = s.ListValidators(ctx, &ethpb.StateValidatorsRequest{
StateId: stateId,
})
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
})
t.Run("Invalid state ID", func(t *testing.T) {
s := Server{}
_, err := s.ListValidators(ctx, &ethpb.StateValidatorsRequest{
StateId: []byte("foo"),
})
require.ErrorContains(t, "invalid state ID: foo", err)
})
}
func TestListValidatorBalances(t *testing.T) {
ctx := context.Background()
var state iface.BeaconState
state, _ = testutil.DeterministicGenesisState(t, 8192)
t.Run("Head List Validators Balance by index", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
ids := [][]byte{[]byte("15"), []byte("26"), []byte("400")}
idNums := []types.ValidatorIndex{15, 26, 400}
resp, err := s.ListValidatorBalances(ctx, &ethpb.ValidatorBalancesRequest{
StateId: []byte("head"),
Id: ids,
})
require.NoError(t, err)
for i, val := range resp.Data {
assert.Equal(t, idNums[i], val.Index)
assert.Equal(t, params.BeaconConfig().MaxEffectiveBalance, val.Balance)
}
})
t.Run("Head List Validators Balance by pubkey", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
idNums := []types.ValidatorIndex{20, 66, 90, 100}
pubkey1 := state.PubkeyAtIndex(types.ValidatorIndex(20))
pubkey2 := state.PubkeyAtIndex(types.ValidatorIndex(66))
pubkey3 := state.PubkeyAtIndex(types.ValidatorIndex(90))
pubkey4 := state.PubkeyAtIndex(types.ValidatorIndex(100))
pubKeys := [][]byte{pubkey1[:], pubkey2[:], pubkey3[:], pubkey4[:]}
resp, err := s.ListValidatorBalances(ctx, &ethpb.ValidatorBalancesRequest{
StateId: []byte("head"),
Id: pubKeys,
})
require.NoError(t, err)
for i, val := range resp.Data {
assert.Equal(t, idNums[i], val.Index)
assert.Equal(t, params.BeaconConfig().MaxEffectiveBalance, val.Balance)
}
})
t.Run("Head List Validators Balance by both index and pubkey", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
idNums := []types.ValidatorIndex{20, 90, 170, 129}
pubkey1 := state.PubkeyAtIndex(types.ValidatorIndex(20))
pubkey3 := state.PubkeyAtIndex(types.ValidatorIndex(170))
ids := [][]byte{pubkey1[:], []byte("90"), pubkey3[:], []byte("129")}
resp, err := s.ListValidatorBalances(ctx, &ethpb.ValidatorBalancesRequest{
StateId: []byte("head"),
Id: ids,
})
require.NoError(t, err)
for i, val := range resp.Data {
assert.Equal(t, idNums[i], val.Index)
assert.Equal(t, params.BeaconConfig().MaxEffectiveBalance, val.Balance)
}
})
t.Run("Hex root not found", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
require.NoError(t, err)
_, err = s.ListValidatorBalances(ctx, &ethpb.ValidatorBalancesRequest{
StateId: stateId,
})
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
})
t.Run("Invalid state ID", func(t *testing.T) {
s := Server{}
_, err := s.ListValidatorBalances(ctx, &ethpb.ValidatorBalancesRequest{
StateId: []byte("foo"),
})
require.ErrorContains(t, "invalid state ID: foo", err)
})
}
func TestListCommittees(t *testing.T) {
ctx := context.Background()
var state iface.BeaconState
state, _ = testutil.DeterministicGenesisState(t, 8192)
epoch := helpers.SlotToEpoch(state.Slot())
t.Run("Head All Committees", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
resp, err := s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: []byte("head"),
})
require.NoError(t, err)
assert.Equal(t, int(params.BeaconConfig().SlotsPerEpoch)*2, len(resp.Data))
for _, datum := range resp.Data {
assert.Equal(t, true, datum.Index == types.CommitteeIndex(0) || datum.Index == types.CommitteeIndex(1))
assert.Equal(t, epoch, helpers.SlotToEpoch(datum.Slot))
}
})
t.Run("Head All Committees of Epoch 10", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
epoch := types.Epoch(10)
resp, err := s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: []byte("head"),
Epoch: &epoch,
})
require.NoError(t, err)
for _, datum := range resp.Data {
assert.Equal(t, true, datum.Slot >= 320 && datum.Slot <= 351)
}
})
t.Run("Head All Committees of Slot 4", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
slot := types.Slot(4)
resp, err := s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: []byte("head"),
Slot: &slot,
})
require.NoError(t, err)
assert.Equal(t, 2, len(resp.Data))
index := types.CommitteeIndex(0)
for _, datum := range resp.Data {
assert.Equal(t, epoch, helpers.SlotToEpoch(datum.Slot))
assert.Equal(t, slot, datum.Slot)
assert.Equal(t, index, datum.Index)
index++
}
})
t.Run("Head All Committees of Index 1", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
index := types.CommitteeIndex(1)
resp, err := s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: []byte("head"),
Index: &index,
})
require.NoError(t, err)
assert.Equal(t, int(params.BeaconConfig().SlotsPerEpoch), len(resp.Data))
slot := types.Slot(0)
for _, datum := range resp.Data {
assert.Equal(t, epoch, helpers.SlotToEpoch(datum.Slot))
assert.Equal(t, slot, datum.Slot)
assert.Equal(t, index, datum.Index)
slot++
}
})
t.Run("Head All Committees of Slot 2, Index 1", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
index := types.CommitteeIndex(1)
slot := types.Slot(2)
resp, err := s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: []byte("head"),
Slot: &slot,
Index: &index,
})
require.NoError(t, err)
assert.Equal(t, 1, len(resp.Data))
for _, datum := range resp.Data {
assert.Equal(t, epoch, helpers.SlotToEpoch(datum.Slot))
assert.Equal(t, slot, datum.Slot)
assert.Equal(t, index, datum.Index)
}
})
t.Run("Hex root not found", func(t *testing.T) {
s := Server{
StateFetcher: statefetcher.StateProvider{
ChainInfoFetcher: &chainMock.ChainService{State: state},
},
}
stateId, err := hexutil.Decode("0x" + strings.Repeat("f", 64))
require.NoError(t, err)
_, err = s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: stateId,
})
require.ErrorContains(t, "state not found in the last 8192 state roots in head state", err)
})
t.Run("Invalid state ID", func(t *testing.T) {
s := Server{}
_, err := s.ListCommittees(ctx, &ethpb.StateCommitteesRequest{
StateId: []byte("foo"),
})
require.ErrorContains(t, "invalid state ID: foo", err)
})
}