diff --git a/validator/rpc/handlers_beacon.go b/validator/rpc/handlers_beacon.go index 3295f2bf7..107cd5373 100644 --- a/validator/rpc/handlers_beacon.go +++ b/validator/rpc/handlers_beacon.go @@ -158,24 +158,29 @@ func (s *Server) GetValidators(w http.ResponseWriter, r *http.Request) { } pageToken := r.URL.Query().Get("page_token") publicKeys := r.URL.Query()["public_keys"] - pubkeys := make([][]byte, len(publicKeys)) + pubkeys := make([][]byte, 0) for i, key := range publicKeys { - var pk []byte + if key == "" { + continue + } if strings.HasPrefix(key, "0x") { k, ok := shared.ValidateHex(w, fmt.Sprintf("PublicKeys[%d]", i), key, fieldparams.BLSPubkeyLength) if !ok { return } - pk = bytesutil.SafeCopyBytes(k) + pubkeys = append(pubkeys, bytesutil.SafeCopyBytes(k)) } else { data, err := base64.StdEncoding.DecodeString(key) if err != nil { httputil.HandleError(w, errors.Wrap(err, "Failed to decode base64").Error(), http.StatusBadRequest) return } - pk = bytesutil.SafeCopyBytes(data) + pubkeys = append(pubkeys, bytesutil.SafeCopyBytes(data)) } - pubkeys[i] = pk + } + if len(pubkeys) == 0 { + httputil.HandleError(w, "no pubkeys provided", http.StatusBadRequest) + return } req := ðpb.ListValidatorsRequest{ PublicKeys: pubkeys, diff --git a/validator/rpc/handlers_beacon_test.go b/validator/rpc/handlers_beacon_test.go index d9f17653b..5dd9bab45 100644 --- a/validator/rpc/handlers_beacon_test.go +++ b/validator/rpc/handlers_beacon_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/assert" @@ -102,3 +103,154 @@ func TestGetBeaconStatus_OK(t *testing.T) { } assert.DeepEqual(t, want, resp) } + +func TestServer_GetValidators(t *testing.T) { + tests := []struct { + name string + query string + expectedReq *ethpb.ListValidatorsRequest + chainResp *ethpb.Validators + want *ValidatorsResponse + wantCode int + wantErr string + }{ + { + name: "happypath on page_size, page_token, public_keys", + wantCode: http.StatusOK, + query: "page_size=4&page_token=0&public_keys=0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4", + expectedReq: func() *ethpb.ListValidatorsRequest { + b, err := hexutil.Decode("0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4") + require.NoError(t, err) + pubkeys := [][]byte{b} + return ðpb.ListValidatorsRequest{ + PublicKeys: pubkeys, + PageSize: int32(4), + PageToken: "0", + } + }(), + chainResp: func() *ethpb.Validators { + b, err := hexutil.Decode("0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4") + require.NoError(t, err) + return ðpb.Validators{ + Epoch: 0, + ValidatorList: []*ethpb.Validators_ValidatorContainer{ + { + Index: 0, + Validator: ðpb.Validator{ + PublicKey: b, + }, + }, + }, + NextPageToken: "0", + TotalSize: 0, + } + }(), + want: &ValidatorsResponse{ + Epoch: 0, + ValidatorList: []*ValidatorContainer{ + { + Index: 0, + Validator: &Validator{ + PublicKey: "0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4", + WithdrawalCredentials: "0x", + EffectiveBalance: 0, + Slashed: false, + ActivationEligibilityEpoch: 0, + ActivationEpoch: 0, + ExitEpoch: 0, + WithdrawableEpoch: 0, + }, + }, + }, + NextPageToken: "0", + TotalSize: 0, + }, + }, + { + name: "extra public key that's empty still returns correct response", + wantCode: http.StatusOK, + query: "page_size=4&page_token=0&public_keys=0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4&public_keys=", + expectedReq: func() *ethpb.ListValidatorsRequest { + b, err := hexutil.Decode("0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4") + require.NoError(t, err) + pubkeys := [][]byte{b} + return ðpb.ListValidatorsRequest{ + PublicKeys: pubkeys, + PageSize: int32(4), + PageToken: "0", + } + }(), + chainResp: func() *ethpb.Validators { + b, err := hexutil.Decode("0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4") + require.NoError(t, err) + return ðpb.Validators{ + Epoch: 0, + ValidatorList: []*ethpb.Validators_ValidatorContainer{ + { + Index: 0, + Validator: ðpb.Validator{ + PublicKey: b, + }, + }, + }, + NextPageToken: "0", + TotalSize: 0, + } + }(), + want: &ValidatorsResponse{ + Epoch: 0, + ValidatorList: []*ValidatorContainer{ + { + Index: 0, + Validator: &Validator{ + PublicKey: "0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4", + WithdrawalCredentials: "0x", + EffectiveBalance: 0, + Slashed: false, + ActivationEligibilityEpoch: 0, + ActivationEpoch: 0, + ExitEpoch: 0, + WithdrawableEpoch: 0, + }, + }, + }, + NextPageToken: "0", + TotalSize: 0, + }, + }, + { + name: "no public keys passed results in error", + wantCode: http.StatusBadRequest, + query: "page_size=4&page_token=0&public_keys=", + wantErr: "no pubkeys provided", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + beaconChainClient := validatormock.NewMockBeaconChainClient(ctrl) + if tt.wantErr == "" { + beaconChainClient.EXPECT().ListValidators( + gomock.Any(), // ctx + tt.expectedReq, + ).Return(tt.chainResp, nil) + } + s := &Server{ + beaconChainClient: beaconChainClient, + } + req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/v2/validator/beacon/validators?%s", tt.query), http.NoBody) + wr := httptest.NewRecorder() + wr.Body = &bytes.Buffer{} + s.GetValidators(wr, req) + require.Equal(t, tt.wantCode, wr.Code) + if tt.wantErr != "" { + require.StringContains(t, tt.wantErr, string(wr.Body.Bytes())) + } else { + resp := &ValidatorsResponse{} + require.NoError(t, json.Unmarshal(wr.Body.Bytes(), resp)) + + require.DeepEqual(t, resp, tt.want) + } + }) + } +}