diff --git a/validator/client/beacon-api/get_beacon_block_test.go b/validator/client/beacon-api/get_beacon_block_test.go index 83abbb9e02cc..f05db18f83ae 100644 --- a/validator/client/beacon-api/get_beacon_block_test.go +++ b/validator/client/beacon-api/get_beacon_block_test.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/golang/mock/gomock" "github.com/prysmaticlabs/prysm/v4/api/gateway/apimiddleware" - apimiddleware2 "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/apimiddleware" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/validator" "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" @@ -518,6 +517,8 @@ func TestGetBeaconBlock_FallbackToBlindedBlock(t *testing.T) { proto := test_helpers.GenerateProtoBlindedDenebBeaconBlock() block := test_helpers.GenerateJsonBlindedDenebBeaconBlock() + blockBytes, err := json.Marshal(block) + require.NoError(t, err) const slot = primitives.Slot(1) randaoReveal := []byte{2} @@ -537,12 +538,12 @@ func TestGetBeaconBlock_FallbackToBlindedBlock(t *testing.T) { jsonRestHandler.EXPECT().GetRestJsonResponse( ctx, fmt.Sprintf("/eth/v1/validator/blinded_blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)), - &apimiddleware2.ProduceBlindedBlockResponseJson{}, + &abstractProduceBlockResponseJson{}, ).SetArg( 2, - apimiddleware2.ProduceBlindedBlockResponseJson{ + abstractProduceBlockResponseJson{ Version: "deneb", - Data: &apimiddleware2.BlindedBeaconBlockContainerJson{DenebContents: block}, + Data: blockBytes, }, ).Return( nil, @@ -569,6 +570,8 @@ func TestGetBeaconBlock_FallbackToFullBlock(t *testing.T) { proto := test_helpers.GenerateProtoDenebBeaconBlock() block := test_helpers.GenerateJsonDenebBeaconBlock() + blockBytes, err := json.Marshal(block) + require.NoError(t, err) const slot = primitives.Slot(1) randaoReveal := []byte{2} @@ -588,7 +591,7 @@ func TestGetBeaconBlock_FallbackToFullBlock(t *testing.T) { jsonRestHandler.EXPECT().GetRestJsonResponse( ctx, fmt.Sprintf("/eth/v1/validator/blinded_blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)), - &apimiddleware2.ProduceBlindedBlockResponseJson{}, + &abstractProduceBlockResponseJson{}, ).Return( &apimiddleware.DefaultErrorJson{Code: http2.StatusInternalServerError}, errors.New("foo"), @@ -596,12 +599,12 @@ func TestGetBeaconBlock_FallbackToFullBlock(t *testing.T) { jsonRestHandler.EXPECT().GetRestJsonResponse( ctx, fmt.Sprintf("/eth/v2/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)), - &apimiddleware2.ProduceBlockResponseV2Json{}, + &abstractProduceBlockResponseJson{}, ).SetArg( 2, - apimiddleware2.ProduceBlockResponseV2Json{ + abstractProduceBlockResponseJson{ Version: "deneb", - Data: &apimiddleware2.BeaconBlockContainerV2Json{DenebContents: block}, + Data: blockBytes, }, ).Return( nil, diff --git a/validator/client/beacon-api/json_rest_handler.go b/validator/client/beacon-api/json_rest_handler.go index 4a032b7aee92..88597b4e7e0a 100644 --- a/validator/client/beacon-api/json_rest_handler.go +++ b/validator/client/beacon-api/json_rest_handler.go @@ -92,8 +92,8 @@ func decodeJsonResp(resp *http.Response, responseJson interface{}) (*apimiddlewa if resp.StatusCode == http.StatusNotFound { errorJson = &apimiddleware.DefaultErrorJson{Code: http.StatusNotFound, Message: "Resource not found"} } else { - remaining, err := io.ReadAll(decoder.Buffered()) - if err == nil { + remaining, readErr := io.ReadAll(decoder.Buffered()) + if readErr == nil { log.Debugf("Undecoded value: %s", string(remaining)) } return nil, errors.Wrapf(err, "failed to decode error json for %s", resp.Request.URL) @@ -104,8 +104,8 @@ func decodeJsonResp(resp *http.Response, responseJson interface{}) (*apimiddlewa if responseJson != nil { if err := decoder.Decode(responseJson); err != nil { - remaining, err := io.ReadAll(decoder.Buffered()) - if err == nil { + remaining, readErr := io.ReadAll(decoder.Buffered()) + if readErr == nil { log.Debugf("Undecoded value: %s", string(remaining)) } return nil, errors.Wrapf(err, "failed to decode response json for %s", resp.Request.URL) diff --git a/validator/client/beacon-api/json_rest_handler_test.go b/validator/client/beacon-api/json_rest_handler_test.go index 68548b322b36..109e91ec3916 100644 --- a/validator/client/beacon-api/json_rest_handler_test.go +++ b/validator/client/beacon-api/json_rest_handler_test.go @@ -138,6 +138,17 @@ func TestGetRestJsonResponse_Error(t *testing.T) { timeout: 1, responseJson: &beacon.GetGenesisResponse{}, }, + { + name: "resource not found", + funcHandler: resourceNotFoundHandler, + expectedErrorMessage: "error 404: Resource not found", + expectedErrorJson: &apimiddleware.DefaultErrorJson{ + Code: 404, + Message: "Resource not found", + }, + timeout: time.Second * 5, + responseJson: &beacon.GetGenesisResponse{}, + }, } for _, testCase := range testCases { @@ -420,8 +431,12 @@ func httpErrorJsonHandler(statusCode int, errorMessage string) func(w http.Respo } } -func invalidJsonErrHandler(w http.ResponseWriter, _ *http.Request) { +func resourceNotFoundHandler(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNotFound) +} + +func invalidJsonErrHandler(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) _, err := w.Write([]byte("foo")) if err != nil { panic(err)