Fix get validator endpoint for empty query parameters (#13780)

* fix handlers for get validators

* removing log
This commit is contained in:
james-prysm 2024-03-21 09:00:07 -05:00 committed by GitHub
parent 02abb3e3c0
commit f7f1d249f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 162 additions and 5 deletions

View File

@ -158,24 +158,29 @@ func (s *Server) GetValidators(w http.ResponseWriter, r *http.Request) {
} }
pageToken := r.URL.Query().Get("page_token") pageToken := r.URL.Query().Get("page_token")
publicKeys := r.URL.Query()["public_keys"] publicKeys := r.URL.Query()["public_keys"]
pubkeys := make([][]byte, len(publicKeys)) pubkeys := make([][]byte, 0)
for i, key := range publicKeys { for i, key := range publicKeys {
var pk []byte if key == "" {
continue
}
if strings.HasPrefix(key, "0x") { if strings.HasPrefix(key, "0x") {
k, ok := shared.ValidateHex(w, fmt.Sprintf("PublicKeys[%d]", i), key, fieldparams.BLSPubkeyLength) k, ok := shared.ValidateHex(w, fmt.Sprintf("PublicKeys[%d]", i), key, fieldparams.BLSPubkeyLength)
if !ok { if !ok {
return return
} }
pk = bytesutil.SafeCopyBytes(k) pubkeys = append(pubkeys, bytesutil.SafeCopyBytes(k))
} else { } else {
data, err := base64.StdEncoding.DecodeString(key) data, err := base64.StdEncoding.DecodeString(key)
if err != nil { if err != nil {
httputil.HandleError(w, errors.Wrap(err, "Failed to decode base64").Error(), http.StatusBadRequest) httputil.HandleError(w, errors.Wrap(err, "Failed to decode base64").Error(), http.StatusBadRequest)
return 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 := &ethpb.ListValidatorsRequest{ req := &ethpb.ListValidatorsRequest{
PublicKeys: pubkeys, PublicKeys: pubkeys,

View File

@ -9,6 +9,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors" "github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/assert"
@ -102,3 +103,154 @@ func TestGetBeaconStatus_OK(t *testing.T) {
} }
assert.DeepEqual(t, want, resp) 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 &ethpb.ListValidatorsRequest{
PublicKeys: pubkeys,
PageSize: int32(4),
PageToken: "0",
}
}(),
chainResp: func() *ethpb.Validators {
b, err := hexutil.Decode("0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4")
require.NoError(t, err)
return &ethpb.Validators{
Epoch: 0,
ValidatorList: []*ethpb.Validators_ValidatorContainer{
{
Index: 0,
Validator: &ethpb.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 &ethpb.ListValidatorsRequest{
PublicKeys: pubkeys,
PageSize: int32(4),
PageToken: "0",
}
}(),
chainResp: func() *ethpb.Validators {
b, err := hexutil.Decode("0x855ae9c6184d6edd46351b375f16f541b2d33b0ed0da9be4571b13938588aee840ba606a946f0e8023ae3a4b2a43b4d4")
require.NoError(t, err)
return &ethpb.Validators{
Epoch: 0,
ValidatorList: []*ethpb.Validators_ValidatorContainer{
{
Index: 0,
Validator: &ethpb.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)
}
})
}
}