Validator REST api: adding in check for empty keys changed (#14637)

* adding in check for empty keys changed

* changelog

* kasey feedback

* fixing unit tests

* Update CHANGELOG.md

Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com>

---------

Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com>
This commit is contained in:
james-prysm
2024-11-13 10:09:11 -06:00
committed by GitHub
parent 1857496159
commit be60504512
6 changed files with 22 additions and 14 deletions

View File

@@ -50,6 +50,10 @@ func (c *beaconApiValidatorClient) validatorsStatusResponse(ctx context.Context,
[]*ethpb.ValidatorStatusResponse,
error,
) {
// if no parameters are provided we should just return an empty response
if len(inPubKeys) == 0 && len(inIndexes) == 0 {
return [][]byte{}, []primitives.ValidatorIndex{}, []*ethpb.ValidatorStatusResponse{}, nil
}
// Represents the target set of keys
stringTargetPubKeysToPubKeys := make(map[string][]byte, len(inPubKeys))
stringTargetPubKeys := make([]string, len(inPubKeys))
@@ -78,6 +82,7 @@ func (c *beaconApiValidatorClient) validatorsStatusResponse(ctx context.Context,
return nil, nil, nil, errors.Wrap(err, "failed to get state validators")
}
// TODO: we should remove this API call
validatorsCountResponse, err := c.prysmChainClient.ValidatorCount(ctx, "head", nil)
if err != nil && !errors.Is(err, iface.ErrNotSupported) {
return nil, nil, nil, errors.Wrap(err, "failed to get total validator count")

View File

@@ -215,32 +215,23 @@ func TestMultipleValidatorStatus_Nominal(t *testing.T) {
assert.DeepEqual(t, &expectedValidatorStatusResponse, actualValidatorStatusResponse)
}
func TestMultipleValidatorStatus_Error(t *testing.T) {
func TestMultipleValidatorStatus_No_Keys(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().StateValidators(
gomock.Any(),
gomock.Any(),
[]primitives.ValidatorIndex{},
nil,
).Return(
&structs.GetValidatorsResponse{},
errors.New("a specific error"),
).Times(1)
validatorClient := beaconApiValidatorClient{stateValidatorsProvider: stateValidatorsProvider}
_, err := validatorClient.MultipleValidatorStatus(
resp, err := validatorClient.MultipleValidatorStatus(
ctx,
&ethpb.MultipleValidatorStatusRequest{
PublicKeys: [][]byte{},
},
)
require.ErrorContains(t, "failed to get validators status response", err)
require.NoError(t, err)
require.DeepEqual(t, &ethpb.MultipleValidatorStatusResponse{}, resp)
}
func TestGetValidatorsStatusResponse_Nominal_SomeActiveValidators(t *testing.T) {

View File

@@ -1224,6 +1224,10 @@ func (v *validator) filterAndCacheActiveKeys(ctx context.Context, pubkeys [][fie
// updateValidatorStatusCache updates the validator statuses cache, a map of keys currently used by the validator client
func (v *validator) updateValidatorStatusCache(ctx context.Context, pubkeys [][fieldparams.BLSPubkeyLength]byte) error {
if len(pubkeys) == 0 {
v.pubkeyToStatus = make(map[[fieldparams.BLSPubkeyLength]byte]*validatorStatus, 0)
return nil
}
statusRequestKeys := make([][]byte, 0)
for _, k := range pubkeys {
statusRequestKeys = append(statusRequestKeys, k[:])

View File

@@ -2899,4 +2899,9 @@ func TestUpdateValidatorStatusCache(t *testing.T) {
require.Equal(t, mockResponse.Statuses[i], status.status)
require.Equal(t, mockResponse.Indices[i], status.index)
}
err = v.updateValidatorStatusCache(ctx, nil)
assert.NoError(t, err)
// make sure the value is 0
assert.Equal(t, 0, len(v.pubkeyToStatus))
}