From d066480a514908bb62a7d1129b5e185ef73cab70 Mon Sep 17 00:00:00 2001 From: Sammy Rosso <15244892+saolyn@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:56:13 +0200 Subject: [PATCH] GetValidatorPerformance empty body bug (#14240) * fix possible empty body panic bug * gaz * fix test --- beacon-chain/rpc/prysm/validator/BUILD.bazel | 1 + .../prysm/validator/validator_performance.go | 23 ++++++++++++------- .../validator/validator_performance_test.go | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/beacon-chain/rpc/prysm/validator/BUILD.bazel b/beacon-chain/rpc/prysm/validator/BUILD.bazel index 1f8767bc55b8..1c0e4f9368cb 100644 --- a/beacon-chain/rpc/prysm/validator/BUILD.bazel +++ b/beacon-chain/rpc/prysm/validator/BUILD.bazel @@ -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", ], ) diff --git a/beacon-chain/rpc/prysm/validator/validator_performance.go b/beacon-chain/rpc/prysm/validator/validator_performance.go index fbd7547ad273..2af8d00414e1 100644 --- a/beacon-chain/rpc/prysm/validator/validator_performance.go +++ b/beacon-chain/rpc/prysm/validator/validator_performance.go @@ -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" @@ -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, ðpb.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{ diff --git a/beacon-chain/rpc/prysm/validator/validator_performance_test.go b/beacon-chain/rpc/prysm/validator/validator_performance_test.go index 98b3aa9390b9..379f5669a9d0 100644 --- a/beacon-chain/rpc/prysm/validator/validator_performance_test.go +++ b/beacon-chain/rpc/prysm/validator/validator_performance_test.go @@ -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()