Skip to content

Commit

Permalink
GetValidatorPerformance empty body bug (#14240)
Browse files Browse the repository at this point in the history
* fix possible empty body panic bug

* gaz

* fix test
  • Loading branch information
saolyn authored Jul 19, 2024
1 parent 0e8f98b commit d066480
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions beacon-chain/rpc/prysm/validator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//beacon-chain/rpc/core:go_default_library",
"//network/httputil:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@io_opencensus_go//trace:go_default_library",
],
)
Expand Down
23 changes: 15 additions & 8 deletions beacon-chain/rpc/prysm/validator/validator_performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package validator

import (
"encoding/json"
"io"
"net/http"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/rpc/core"
"github.com/prysmaticlabs/prysm/v5/network/httputil"
Expand All @@ -17,21 +19,26 @@ func (s *Server) GetValidatorPerformance(w http.ResponseWriter, r *http.Request)
defer span.End()

var req structs.GetValidatorPerformanceRequest
if r.Body != http.NoBody {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
handleHTTPError(w, "Could not decode request body: "+err.Error(), http.StatusBadRequest)
return
}

err := json.NewDecoder(r.Body).Decode(&req)
switch {
case errors.Is(err, io.EOF):
httputil.HandleError(w, "No data submitted", http.StatusBadRequest)
return
case err != nil:
httputil.HandleError(w, "Could not decode request body: "+err.Error(), http.StatusBadRequest)
return
}
computed, err := s.CoreService.ComputeValidatorPerformance(

computed, rpcError := s.CoreService.ComputeValidatorPerformance(
ctx,
&ethpb.ValidatorPerformanceRequest{
PublicKeys: req.PublicKeys,
Indices: req.Indices,
},
)
if err != nil {
handleHTTPError(w, "Could not compute validator performance: "+err.Err.Error(), core.ErrorReasonToHTTP(err.Reason))
if rpcError != nil {
handleHTTPError(w, "Could not compute validator performance: "+rpcError.Err.Error(), core.ErrorReasonToHTTP(rpcError.Reason))
return
}
response := &structs.GetValidatorPerformanceResponse{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestServer_GetValidatorPerformance(t *testing.T) {
client := &http.Client{}
rawResp, err := client.Post(srv.URL, "application/json", req.Body)
require.NoError(t, err)
require.Equal(t, http.StatusServiceUnavailable, rawResp.StatusCode)
require.Equal(t, http.StatusBadRequest, rawResp.StatusCode)
})
t.Run("OK", func(t *testing.T) {
helpers.ClearCache()
Expand Down

0 comments on commit d066480

Please sign in to comment.