From d0bf03e863640a8a7e01d78a8852581029e3e0b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Fri, 22 Dec 2023 23:39:20 +0100 Subject: [PATCH] Simplify error handling for JsonRestHandler (#13369) * Simplify error handling for `JsonRestHandler` * POST * reduce complexity * review feedback * uncomment route * fix rest of tests --- testing/assertions/assertions.go | 3 +- validator/client/beacon-api/BUILD.bazel | 1 - .../client/beacon-api/activation_test.go | 3 - .../client/beacon-api/attestation_data.go | 8 +- .../beacon-api/attestation_data_test.go | 3 - .../beacon_api_beacon_chain_client.go | 23 ++--- .../beacon_api_beacon_chain_client_test.go | 12 +-- .../client/beacon-api/beacon_api_helpers.go | 32 ++----- .../beacon-api/beacon_api_helpers_test.go | 8 -- .../beacon-api/beacon_api_node_client.go | 29 ++---- .../beacon-api/beacon_api_node_client_test.go | 4 - .../beacon_api_validator_client_test.go | 5 - validator/client/beacon-api/domain_data.go | 5 +- .../client/beacon-api/domain_data_test.go | 4 +- .../client/beacon-api/doppelganger_test.go | 8 -- validator/client/beacon-api/duties.go | 36 ++----- validator/client/beacon-api/duties_test.go | 15 --- validator/client/beacon-api/errors.go | 5 - validator/client/beacon-api/genesis.go | 21 +++-- validator/client/beacon-api/genesis_test.go | 38 +------- .../client/beacon-api/get_beacon_block.go | 78 ++++++++++------ .../beacon-api/get_beacon_block_test.go | 16 ---- validator/client/beacon-api/index_test.go | 4 - .../client/beacon-api/json_rest_handler.go | 36 +++---- .../beacon-api/json_rest_handler_test.go | 93 +++++++++++-------- validator/client/beacon-api/mock/BUILD.bazel | 1 - .../client/beacon-api/mock/genesis_mock.go | 16 ++-- .../beacon-api/mock/json_rest_handler_mock.go | 29 +++--- .../beacon-api/prepare_beacon_proposer.go | 16 +--- .../prepare_beacon_proposer_test.go | 2 - .../client/beacon-api/propose_attestation.go | 14 +-- .../beacon-api/propose_attestation_test.go | 1 - .../client/beacon-api/propose_beacon_block.go | 17 ++-- .../beacon-api/propose_beacon_block_test.go | 24 +++-- validator/client/beacon-api/propose_exit.go | 10 +- .../client/beacon-api/propose_exit_test.go | 2 - .../beacon-api/prysm_beacon_chain_client.go | 8 +- validator/client/beacon-api/registration.go | 10 +- .../client/beacon-api/registration_test.go | 2 - .../client/beacon-api/state_validators.go | 8 +- .../beacon-api/state_validators_test.go | 3 - validator/client/beacon-api/status_test.go | 6 -- validator/client/beacon-api/stream_blocks.go | 8 +- .../client/beacon-api/stream_blocks_test.go | 17 ---- .../submit_aggregate_selection_proof.go | 8 +- .../submit_aggregate_selection_proof_test.go | 5 - .../submit_signed_aggregate_proof.go | 8 +- .../submit_signed_aggregate_proof_test.go | 2 - .../submit_signed_contribution_and_proof.go | 10 +- ...bmit_signed_contribution_and_proof_test.go | 2 - .../beacon-api/subscribe_committee_subnets.go | 10 +- .../subscribe_committee_subnets_test.go | 2 - validator/client/beacon-api/sync_committee.go | 26 +----- .../client/beacon-api/sync_committee_test.go | 7 -- .../client/beacon-api/validator_count_test.go | 2 - .../beacon-api/wait_for_chain_start_test.go | 5 - 56 files changed, 250 insertions(+), 521 deletions(-) delete mode 100644 validator/client/beacon-api/errors.go diff --git a/testing/assertions/assertions.go b/testing/assertions/assertions.go index aed2fa9f0222..f88d89031b80 100644 --- a/testing/assertions/assertions.go +++ b/testing/assertions/assertions.go @@ -137,7 +137,8 @@ func StringContains(loggerFn assertionLoggerFn, expected, actual string, flag bo // NoError asserts that error is nil. func NoError(loggerFn assertionLoggerFn, err error, msg ...interface{}) { - if err != nil { + // reflect.ValueOf is needed for nil instances of custom types implementing Error + if err != nil && !reflect.ValueOf(err).IsNil() { errMsg := parseMsg("Unexpected error", msg...) _, file, line, _ := runtime.Caller(2) loggerFn("%s:%d %s: %v", filepath.Base(file), line, errMsg, err) diff --git a/validator/client/beacon-api/BUILD.bazel b/validator/client/beacon-api/BUILD.bazel index fd487340884e..07746d098eff 100644 --- a/validator/client/beacon-api/BUILD.bazel +++ b/validator/client/beacon-api/BUILD.bazel @@ -15,7 +15,6 @@ go_library( "domain_data.go", "doppelganger.go", "duties.go", - "errors.go", "genesis.go", "get_beacon_block.go", "index.go", diff --git a/validator/client/beacon-api/activation_test.go b/validator/client/beacon-api/activation_test.go index fdb642fd8853..dd1b8fb637bd 100644 --- a/validator/client/beacon-api/activation_test.go +++ b/validator/client/beacon-api/activation_test.go @@ -123,7 +123,6 @@ func TestActivation_Nominal(t *testing.T) { &stateValidatorsResponseJson, ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ @@ -248,7 +247,6 @@ func TestActivation_InvalidData(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ @@ -289,7 +287,6 @@ func TestActivation_JsonResponseError(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("some specific json error"), ).Times(1) diff --git a/validator/client/beacon-api/attestation_data.go b/validator/client/beacon-api/attestation_data.go index e556bab234d1..3af3b0f3fe5b 100644 --- a/validator/client/beacon-api/attestation_data.go +++ b/validator/client/beacon-api/attestation_data.go @@ -24,12 +24,8 @@ func (c beaconApiValidatorClient) getAttestationData( query := buildURL("/eth/v1/validator/attestation_data", params) produceAttestationDataResponseJson := validator.GetAttestationDataResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, query, &produceAttestationDataResponseJson) - if err != nil { - return nil, errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, query, &produceAttestationDataResponseJson); err != nil { + return nil, err } if produceAttestationDataResponseJson.Data == nil { diff --git a/validator/client/beacon-api/attestation_data_test.go b/validator/client/beacon-api/attestation_data_test.go index f4197cac751b..0249595659af 100644 --- a/validator/client/beacon-api/attestation_data_test.go +++ b/validator/client/beacon-api/attestation_data_test.go @@ -39,7 +39,6 @@ func TestGetAttestationData_ValidAttestation(t *testing.T) { &produceAttestationDataResponseJson, ).Return( nil, - nil, ).SetArg( 2, validator.GetAttestationDataResponse{ @@ -190,7 +189,6 @@ func TestGetAttestationData_InvalidData(t *testing.T) { &produceAttestationDataResponseJson, ).Return( nil, - nil, ).SetArg( 2, testCase.generateData(), @@ -219,7 +217,6 @@ func TestGetAttestationData_JsonResponseError(t *testing.T) { fmt.Sprintf("/eth/v1/validator/attestation_data?committee_index=%d&slot=%d", committeeIndex, slot), &produceAttestationDataResponseJson, ).Return( - nil, errors.New("some specific json response error"), ).Times(1) diff --git a/validator/client/beacon-api/beacon_api_beacon_chain_client.go b/validator/client/beacon-api/beacon_api_beacon_chain_client.go index b6685f42b41a..08805224deba 100644 --- a/validator/client/beacon-api/beacon_api_beacon_chain_client.go +++ b/validator/client/beacon-api/beacon_api_beacon_chain_client.go @@ -30,12 +30,9 @@ const getValidatorPerformanceEndpoint = "/prysm/validators/performance" func (c beaconApiBeaconChainClient) getHeadBlockHeaders(ctx context.Context) (*beacon.GetBlockHeaderResponse, error) { blockHeader := beacon.GetBlockHeaderResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/headers/head", &blockHeader) + err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/headers/head", &blockHeader) if err != nil { - return nil, errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + return nil, err } if blockHeader.Data == nil || blockHeader.Data.Header == nil { @@ -53,12 +50,8 @@ func (c beaconApiBeaconChainClient) GetChainHead(ctx context.Context, _ *empty.E const endpoint = "/eth/v1/beacon/states/head/finality_checkpoints" finalityCheckpoints := beacon.GetFinalityCheckpointsResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, endpoint, &finalityCheckpoints) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, endpoint, &finalityCheckpoints); err != nil { + return nil, err } if finalityCheckpoints.Data == nil { @@ -338,12 +331,8 @@ func (c beaconApiBeaconChainClient) GetValidatorPerformance(ctx context.Context, return nil, errors.Wrap(err, "failed to marshal request") } resp := &validator.PerformanceResponse{} - errJson, err := c.jsonRestHandler.Post(ctx, getValidatorPerformanceEndpoint, nil, bytes.NewBuffer(request), resp) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Post(ctx, getValidatorPerformanceEndpoint, nil, bytes.NewBuffer(request), resp); err != nil { + return nil, err } return ðpb.ValidatorPerformanceResponse{ diff --git a/validator/client/beacon-api/beacon_api_beacon_chain_client_test.go b/validator/client/beacon-api/beacon_api_beacon_chain_client_test.go index aa7ab0d3573d..b3d0dbbd67f4 100644 --- a/validator/client/beacon-api/beacon_api_beacon_chain_client_test.go +++ b/validator/client/beacon-api/beacon_api_beacon_chain_client_test.go @@ -122,10 +122,7 @@ func TestListValidators(t *testing.T) { ) jsonRestHandler := mock.NewMockJsonRestHandler(ctrl) - jsonRestHandler.EXPECT().Get(ctx, blockHeaderEndpoint, gomock.Any()).Return( - nil, - errors.New("bar error"), - ) + jsonRestHandler.EXPECT().Get(ctx, blockHeaderEndpoint, gomock.Any()).Return(errors.New("bar error")) beaconChainClient := beaconApiBeaconChainClient{ stateValidatorsProvider: stateValidatorsProvider, @@ -200,7 +197,6 @@ func TestListValidators(t *testing.T) { jsonRestHandler := mock.NewMockJsonRestHandler(ctrl) jsonRestHandler.EXPECT().Get(ctx, blockHeaderEndpoint, gomock.Any()).Return( nil, - nil, ).SetArg( 2, testCase.blockHeaderResponse, @@ -752,7 +748,6 @@ func TestGetChainHead(t *testing.T) { finalityCheckpointsResponse := beacon.GetFinalityCheckpointsResponse{} jsonRestHandler := mock.NewMockJsonRestHandler(ctrl) jsonRestHandler.EXPECT().Get(ctx, finalityCheckpointsEndpoint, &finalityCheckpointsResponse).Return( - nil, testCase.finalityCheckpointsError, ).SetArg( 2, @@ -853,7 +848,6 @@ func TestGetChainHead(t *testing.T) { finalityCheckpointsResponse := beacon.GetFinalityCheckpointsResponse{} jsonRestHandler.EXPECT().Get(ctx, finalityCheckpointsEndpoint, &finalityCheckpointsResponse).Return( nil, - nil, ).SetArg( 2, generateValidFinalityCheckpointsResponse(), @@ -861,7 +855,6 @@ func TestGetChainHead(t *testing.T) { headBlockHeadersResponse := beacon.GetBlockHeaderResponse{} jsonRestHandler.EXPECT().Get(ctx, headBlockHeadersEndpoint, &headBlockHeadersResponse).Return( - nil, testCase.headBlockHeadersError, ).SetArg( 2, @@ -885,7 +878,6 @@ func TestGetChainHead(t *testing.T) { finalityCheckpointsResponse := beacon.GetFinalityCheckpointsResponse{} jsonRestHandler.EXPECT().Get(ctx, finalityCheckpointsEndpoint, &finalityCheckpointsResponse).Return( nil, - nil, ).SetArg( 2, generateValidFinalityCheckpointsResponse(), @@ -894,7 +886,6 @@ func TestGetChainHead(t *testing.T) { headBlockHeadersResponse := beacon.GetBlockHeaderResponse{} jsonRestHandler.EXPECT().Get(ctx, headBlockHeadersEndpoint, &headBlockHeadersResponse).Return( nil, - nil, ).SetArg( 2, generateValidBlockHeadersResponse(), @@ -958,7 +949,6 @@ func Test_beaconApiBeaconChainClient_GetValidatorPerformance(t *testing.T) { wantResponse, ).Return( nil, - nil, ) c := beaconApiBeaconChainClient{ diff --git a/validator/client/beacon-api/beacon_api_helpers.go b/validator/client/beacon-api/beacon_api_helpers.go index 1eb0b0ddd402..aa78a18eb7d7 100644 --- a/validator/client/beacon-api/beacon_api_helpers.go +++ b/validator/client/beacon-api/beacon_api_helpers.go @@ -55,12 +55,8 @@ func (c *beaconApiValidatorClient) getFork(ctx context.Context) (*beacon.GetStat stateForkResponseJson := &beacon.GetStateForkResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, endpoint, stateForkResponseJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, endpoint, stateForkResponseJson); err != nil { + return nil, err } return stateForkResponseJson, nil @@ -71,12 +67,8 @@ func (c *beaconApiValidatorClient) getHeaders(ctx context.Context) (*beacon.GetB blockHeadersResponseJson := &beacon.GetBlockHeadersResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, endpoint, blockHeadersResponseJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, endpoint, blockHeadersResponseJson); err != nil { + return nil, err } return blockHeadersResponseJson, nil @@ -93,12 +85,8 @@ func (c *beaconApiValidatorClient) getLiveness(ctx context.Context, epoch primit return nil, errors.Wrapf(err, "failed to marshal validator indexes") } - errJson, err := c.jsonRestHandler.Post(ctx, url, nil, bytes.NewBuffer(marshalledJsonValidatorIndexes), livenessResponseJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Post(ctx, url, nil, bytes.NewBuffer(marshalledJsonValidatorIndexes), livenessResponseJson); err != nil { + return nil, err } return livenessResponseJson, nil @@ -109,12 +97,8 @@ func (c *beaconApiValidatorClient) getSyncing(ctx context.Context) (*node.SyncSt syncingResponseJson := &node.SyncStatusResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, endpoint, syncingResponseJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, endpoint, syncingResponseJson); err != nil { + return nil, err } return syncingResponseJson, nil diff --git a/validator/client/beacon-api/beacon_api_helpers_test.go b/validator/client/beacon-api/beacon_api_helpers_test.go index fa1d36d70c97..dd0ec636b561 100644 --- a/validator/client/beacon-api/beacon_api_helpers_test.go +++ b/validator/client/beacon-api/beacon_api_helpers_test.go @@ -117,7 +117,6 @@ func TestGetFork_Nominal(t *testing.T) { &stateForkResponseJson, ).Return( nil, - nil, ).SetArg( 2, expected, @@ -145,7 +144,6 @@ func TestGetFork_Invalid(t *testing.T) { forkEndpoint, gomock.Any(), ).Return( - nil, errors.New("custom error"), ).Times(1) @@ -186,7 +184,6 @@ func TestGetHeaders_Nominal(t *testing.T) { &blockHeadersResponseJson, ).Return( nil, - nil, ).SetArg( 2, expected, @@ -214,7 +211,6 @@ func TestGetHeaders_Invalid(t *testing.T) { headersEndpoint, gomock.Any(), ).Return( - nil, errors.New("custom error"), ).Times(1) @@ -265,7 +261,6 @@ func TestGetLiveness_Nominal(t *testing.T) { expected, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -289,7 +284,6 @@ func TestGetLiveness_Invalid(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("custom error"), ).Times(1) @@ -338,7 +332,6 @@ func TestGetIsSyncing_Nominal(t *testing.T) { &syncingResponseJson, ).Return( nil, - nil, ).SetArg( 2, expected, @@ -369,7 +362,6 @@ func TestGetIsSyncing_Invalid(t *testing.T) { syncingEnpoint, &syncingResponseJson, ).Return( - nil, errors.New("custom error"), ).Times(1) diff --git a/validator/client/beacon-api/beacon_api_node_client.go b/validator/client/beacon-api/beacon_api_node_client.go index 01912dfddcd2..adc3ff6bf766 100644 --- a/validator/client/beacon-api/beacon_api_node_client.go +++ b/validator/client/beacon-api/beacon_api_node_client.go @@ -24,12 +24,8 @@ type beaconApiNodeClient struct { func (c *beaconApiNodeClient) GetSyncStatus(ctx context.Context, _ *empty.Empty) (*ethpb.SyncStatus, error) { syncingResponse := node.SyncStatusResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, "/eth/v1/node/syncing", &syncingResponse) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, "/eth/v1/node/syncing", &syncingResponse); err != nil { + return nil, err } if syncingResponse.Data == nil { @@ -42,13 +38,10 @@ func (c *beaconApiNodeClient) GetSyncStatus(ctx context.Context, _ *empty.Empty) } func (c *beaconApiNodeClient) GetGenesis(ctx context.Context, _ *empty.Empty) (*ethpb.Genesis, error) { - genesisJson, errJson, err := c.genesisProvider.GetGenesis(ctx) + genesisJson, err := c.genesisProvider.GetGenesis(ctx) if err != nil { return nil, errors.Wrap(err, "failed to get genesis") } - if errJson != nil { - return nil, errJson - } genesisValidatorRoot, err := hexutil.Decode(genesisJson.GenesisValidatorsRoot) if err != nil { @@ -61,12 +54,8 @@ func (c *beaconApiNodeClient) GetGenesis(ctx context.Context, _ *empty.Empty) (* } depositContractJson := config.GetDepositContractResponse{} - errJson, err = c.jsonRestHandler.Get(ctx, "/eth/v1/config/deposit_contract", &depositContractJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Get(ctx, "/eth/v1/config/deposit_contract", &depositContractJson); err != nil { + return nil, err } if depositContractJson.Data == nil { @@ -89,12 +78,8 @@ func (c *beaconApiNodeClient) GetGenesis(ctx context.Context, _ *empty.Empty) (* func (c *beaconApiNodeClient) GetVersion(ctx context.Context, _ *empty.Empty) (*ethpb.Version, error) { var versionResponse node.GetVersionResponse - errJson, err := c.jsonRestHandler.Get(ctx, "/eth/v1/node/version", &versionResponse) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, "/eth/v1/node/version", &versionResponse); err != nil { + return nil, err } if versionResponse.Data == nil || versionResponse.Data.Version == "" { diff --git a/validator/client/beacon-api/beacon_api_node_client_test.go b/validator/client/beacon-api/beacon_api_node_client_test.go index f530ce4161a0..b5c69fe21bf0 100644 --- a/validator/client/beacon-api/beacon_api_node_client_test.go +++ b/validator/client/beacon-api/beacon_api_node_client_test.go @@ -118,7 +118,6 @@ func TestGetGenesis(t *testing.T) { ctx, ).Return( testCase.genesisResponse, - nil, testCase.genesisError, ) @@ -131,7 +130,6 @@ func TestGetGenesis(t *testing.T) { "/eth/v1/config/deposit_contract", &depositContractJson, ).Return( - nil, testCase.depositContractError, ).SetArg( 2, @@ -211,7 +209,6 @@ func TestGetSyncStatus(t *testing.T) { syncingEndpoint, &syncingResponse, ).Return( - nil, testCase.restEndpointError, ).SetArg( 2, @@ -276,7 +273,6 @@ func TestGetVersion(t *testing.T) { versionEndpoint, &versionResponse, ).Return( - nil, testCase.restEndpointError, ).SetArg( 2, diff --git a/validator/client/beacon-api/beacon_api_validator_client_test.go b/validator/client/beacon-api/beacon_api_validator_client_test.go index e6762ab923f0..a57647fa58bf 100644 --- a/validator/client/beacon-api/beacon_api_validator_client_test.go +++ b/validator/client/beacon-api/beacon_api_validator_client_test.go @@ -38,7 +38,6 @@ func TestBeaconApiValidatorClient_GetAttestationDataValid(t *testing.T) { &produceAttestationDataResponseJson, ).Return( nil, - nil, ).SetArg( 2, generateValidAttestation(uint64(slot), uint64(committeeIndex)), @@ -78,7 +77,6 @@ func TestBeaconApiValidatorClient_GetAttestationDataError(t *testing.T) { fmt.Sprintf("/eth/v1/validator/attestation_data?committee_index=%d&slot=%d", committeeIndex, slot), &produceAttestationDataResponseJson, ).Return( - nil, errors.New("some specific json error"), ).SetArg( 2, @@ -121,7 +119,6 @@ func TestBeaconApiValidatorClient_DomainDataValid(t *testing.T) { genesisProvider.EXPECT().GetGenesis(ctx).Return( &beacon.Genesis{GenesisValidatorsRoot: genesisValidatorRoot}, nil, - nil, ).Times(2) validatorClient := beaconApiValidatorClient{genesisProvider: genesisProvider} @@ -156,7 +153,6 @@ func TestBeaconApiValidatorClient_ProposeBeaconBlockValid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(2) validatorClient := beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -192,7 +188,6 @@ func TestBeaconApiValidatorClient_ProposeBeaconBlockError(t *testing.T) { gomock.Any(), nil, ).Return( - nil, errors.New("foo error"), ).Times(2) diff --git a/validator/client/beacon-api/domain_data.go b/validator/client/beacon-api/domain_data.go index c65438876072..fdfdf4a3fc04 100644 --- a/validator/client/beacon-api/domain_data.go +++ b/validator/client/beacon-api/domain_data.go @@ -19,13 +19,10 @@ func (c beaconApiValidatorClient) getDomainData(ctx context.Context, epoch primi } // Get the genesis validator root - genesis, errJson, err := c.genesisProvider.GetGenesis(ctx) + genesis, err := c.genesisProvider.GetGenesis(ctx) if err != nil { return nil, errors.Wrapf(err, "failed to get genesis info") } - if errJson != nil { - return nil, errJson - } if !validRoot(genesis.GenesisValidatorsRoot) { return nil, errors.Errorf("invalid genesis validators root: %s", genesis.GenesisValidatorsRoot) diff --git a/validator/client/beacon-api/domain_data_test.go b/validator/client/beacon-api/domain_data_test.go index 8f961ff740e5..dc4332cda435 100644 --- a/validator/client/beacon-api/domain_data_test.go +++ b/validator/client/beacon-api/domain_data_test.go @@ -40,7 +40,6 @@ func TestGetDomainData_ValidDomainData(t *testing.T) { genesisProvider.EXPECT().GetGenesis(ctx).Return( &beacon.Genesis{GenesisValidatorsRoot: genesisValidatorRoot}, nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider} @@ -67,7 +66,7 @@ func TestGetDomainData_GenesisError(t *testing.T) { // Make sure that GetGenesis() is called exactly once genesisProvider := mock.NewMockGenesisProvider(ctrl) - genesisProvider.EXPECT().GetGenesis(ctx).Return(nil, nil, errors.New("foo error")).Times(1) + genesisProvider.EXPECT().GetGenesis(ctx).Return(nil, errors.New("foo error")).Times(1) validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider} _, err := validatorClient.getDomainData(ctx, epoch, domainType) @@ -89,7 +88,6 @@ func TestGetDomainData_InvalidGenesisRoot(t *testing.T) { genesisProvider.EXPECT().GetGenesis(ctx).Return( &beacon.Genesis{GenesisValidatorsRoot: "foo"}, nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{genesisProvider: genesisProvider} diff --git a/validator/client/beacon-api/doppelganger_test.go b/validator/client/beacon-api/doppelganger_test.go index 281baf988f1b..08d2dc64a17d 100644 --- a/validator/client/beacon-api/doppelganger_test.go +++ b/validator/client/beacon-api/doppelganger_test.go @@ -305,7 +305,6 @@ func TestCheckDoppelGanger_Nominal(t *testing.T) { &syncingResponseJson, ).Return( nil, - nil, ).SetArg( 2, *testCase.getSyncingOutput, @@ -321,7 +320,6 @@ func TestCheckDoppelGanger_Nominal(t *testing.T) { &stateForkResponseJson, ).Return( nil, - nil, ).SetArg( 2, *testCase.getForkOutput, @@ -337,7 +335,6 @@ func TestCheckDoppelGanger_Nominal(t *testing.T) { &blockHeadersResponseJson, ).Return( nil, - nil, ).SetArg( 2, *testCase.getHeadersOutput, @@ -362,7 +359,6 @@ func TestCheckDoppelGanger_Nominal(t *testing.T) { *iface.output, ).Return( nil, - nil, ).Times(1) } } @@ -744,7 +740,6 @@ func TestCheckDoppelGanger_Errors(t *testing.T) { syncingEnpoint, &syncingResponseJson, ).Return( - nil, testCase.getSyncingError, ).SetArg( 2, @@ -760,7 +755,6 @@ func TestCheckDoppelGanger_Errors(t *testing.T) { forkEndpoint, &stateForkResponseJson, ).Return( - nil, testCase.getForkError, ).SetArg( 2, @@ -776,7 +770,6 @@ func TestCheckDoppelGanger_Errors(t *testing.T) { headersEndpoint, &blockHeadersResponseJson, ).Return( - nil, testCase.getHeadersError, ).SetArg( 2, @@ -815,7 +808,6 @@ func TestCheckDoppelGanger_Errors(t *testing.T) { 4, *iface.output, ).Return( - nil, iface.err, ).Times(1) } diff --git a/validator/client/beacon-api/duties.go b/validator/client/beacon-api/duties.go index cea6434fb7c6..c4078bd4616d 100644 --- a/validator/client/beacon-api/duties.go +++ b/validator/client/beacon-api/duties.go @@ -219,12 +219,8 @@ func (c beaconApiDutiesProvider) GetCommittees(ctx context.Context, epoch primit committeesRequest := buildURL("/eth/v1/beacon/states/head/committees", committeeParams) var stateCommittees beacon.GetCommitteesResponse - errJson, err := c.jsonRestHandler.Get(ctx, committeesRequest, &stateCommittees) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, committeesRequest, &stateCommittees); err != nil { + return nil, err } if stateCommittees.Data == nil { @@ -253,18 +249,14 @@ func (c beaconApiDutiesProvider) GetAttesterDuties(ctx context.Context, epoch pr } attesterDuties := &validator.GetAttesterDutiesResponse{} - errJson, err := c.jsonRestHandler.Post( + if err = c.jsonRestHandler.Post( ctx, fmt.Sprintf("/eth/v1/validator/duties/attester/%d", epoch), nil, bytes.NewBuffer(validatorIndicesBytes), attesterDuties, - ) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + ); err != nil { + return nil, err } for index, attesterDuty := range attesterDuties.Data { @@ -279,12 +271,8 @@ func (c beaconApiDutiesProvider) GetAttesterDuties(ctx context.Context, epoch pr // GetProposerDuties retrieves the proposer duties for the given epoch func (c beaconApiDutiesProvider) GetProposerDuties(ctx context.Context, epoch primitives.Epoch) ([]*validator.ProposerDuty, error) { proposerDuties := validator.GetProposerDutiesResponse{} - errJson, err := c.jsonRestHandler.Get(ctx, fmt.Sprintf("/eth/v1/validator/duties/proposer/%d", epoch), &proposerDuties) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, fmt.Sprintf("/eth/v1/validator/duties/proposer/%d", epoch), &proposerDuties); err != nil { + return nil, err } if proposerDuties.Data == nil { @@ -313,18 +301,14 @@ func (c beaconApiDutiesProvider) GetSyncDuties(ctx context.Context, epoch primit } syncDuties := validator.GetSyncCommitteeDutiesResponse{} - errJson, err := c.jsonRestHandler.Post( + if err = c.jsonRestHandler.Post( ctx, fmt.Sprintf("/eth/v1/validator/duties/sync/%d", epoch), nil, bytes.NewBuffer(validatorIndicesBytes), &syncDuties, - ) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + ); err != nil { + return nil, err } if syncDuties.Data == nil { diff --git a/validator/client/beacon-api/duties_test.go b/validator/client/beacon-api/duties_test.go index e39734bb3dfd..3aae06cdde69 100644 --- a/validator/client/beacon-api/duties_test.go +++ b/validator/client/beacon-api/duties_test.go @@ -75,7 +75,6 @@ func TestGetAttesterDuties_Valid(t *testing.T) { &validator.GetAttesterDutiesResponse{}, ).Return( nil, - nil, ).SetArg( 4, expectedAttesterDuties, @@ -103,7 +102,6 @@ func TestGetAttesterDuties_HttpError(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) @@ -129,7 +127,6 @@ func TestGetAttesterDuties_NilAttesterDuty(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 4, validator.GetAttesterDutiesResponse{ @@ -172,7 +169,6 @@ func TestGetProposerDuties_Valid(t *testing.T) { &validator.GetProposerDutiesResponse{}, ).Return( nil, - nil, ).SetArg( 2, expectedProposerDuties, @@ -198,7 +194,6 @@ func TestGetProposerDuties_HttpError(t *testing.T) { fmt.Sprintf("%s/%d", getProposerDutiesTestEndpoint, epoch), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) @@ -222,7 +217,6 @@ func TestGetProposerDuties_NilData(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 2, validator.GetProposerDutiesResponse{ @@ -250,7 +244,6 @@ func TestGetProposerDuties_NilProposerDuty(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 2, validator.GetProposerDutiesResponse{ @@ -306,7 +299,6 @@ func TestGetSyncDuties_Valid(t *testing.T) { &validator.GetSyncCommitteeDutiesResponse{}, ).Return( nil, - nil, ).SetArg( 4, expectedSyncDuties, @@ -334,7 +326,6 @@ func TestGetSyncDuties_HttpError(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) @@ -360,7 +351,6 @@ func TestGetSyncDuties_NilData(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 4, validator.GetSyncCommitteeDutiesResponse{ @@ -390,7 +380,6 @@ func TestGetSyncDuties_NilSyncDuty(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 4, validator.GetSyncCommitteeDutiesResponse{ @@ -439,7 +428,6 @@ func TestGetCommittees_Valid(t *testing.T) { &beacon.GetCommitteesResponse{}, ).Return( nil, - nil, ).SetArg( 2, expectedCommittees, @@ -465,7 +453,6 @@ func TestGetCommittees_HttpError(t *testing.T) { fmt.Sprintf("%s?epoch=%d", getCommitteesTestEndpoint, epoch), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) @@ -489,7 +476,6 @@ func TestGetCommittees_NilData(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 2, beacon.GetCommitteesResponse{ @@ -517,7 +503,6 @@ func TestGetCommittees_NilCommittee(t *testing.T) { gomock.Any(), ).Return( nil, - nil, ).SetArg( 2, beacon.GetCommitteesResponse{ diff --git a/validator/client/beacon-api/errors.go b/validator/client/beacon-api/errors.go deleted file mode 100644 index e99623a763de..000000000000 --- a/validator/client/beacon-api/errors.go +++ /dev/null @@ -1,5 +0,0 @@ -package beacon_api - -const ( - msgUnexpectedError = "unexpected error when making request" -) diff --git a/validator/client/beacon-api/genesis.go b/validator/client/beacon-api/genesis.go index 2ec15552fd67..5059da4ca74f 100644 --- a/validator/client/beacon-api/genesis.go +++ b/validator/client/beacon-api/genesis.go @@ -14,7 +14,7 @@ import ( ) type GenesisProvider interface { - GetGenesis(ctx context.Context) (*beacon.Genesis, *httputil.DefaultJsonError, error) + GetGenesis(ctx context.Context) (*beacon.Genesis, error) } type beaconApiGenesisProvider struct { @@ -22,17 +22,19 @@ type beaconApiGenesisProvider struct { } func (c beaconApiValidatorClient) waitForChainStart(ctx context.Context) (*ethpb.ChainStartResponse, error) { - genesis, httpError, err := c.genesisProvider.GetGenesis(ctx) + genesis, err := c.genesisProvider.GetGenesis(ctx) for err != nil { - if httpError == nil || httpError.Code != http.StatusNotFound { + jsonErr := &httputil.DefaultJsonError{} + httpNotFound := errors.As(err, &jsonErr) && jsonErr.Code == http.StatusNotFound + if !httpNotFound { return nil, errors.Wrap(err, "failed to get genesis data") } // Error 404 means that the chain genesis info is not yet known, so we query it every second until it's ready select { case <-time.After(time.Second): - genesis, httpError, err = c.genesisProvider.GetGenesis(ctx) + genesis, err = c.genesisProvider.GetGenesis(ctx) case <-ctx.Done(): return nil, errors.New("context canceled") } @@ -61,16 +63,15 @@ func (c beaconApiValidatorClient) waitForChainStart(ctx context.Context) (*ethpb } // GetGenesis gets the genesis information from the beacon node via the /eth/v1/beacon/genesis endpoint -func (c beaconApiGenesisProvider) GetGenesis(ctx context.Context) (*beacon.Genesis, *httputil.DefaultJsonError, error) { +func (c beaconApiGenesisProvider) GetGenesis(ctx context.Context) (*beacon.Genesis, error) { genesisJson := &beacon.GetGenesisResponse{} - errorJson, err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/genesis", genesisJson) - if err != nil { - return nil, errorJson, errors.Wrap(err, "failed to get json response") + if err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/genesis", genesisJson); err != nil { + return nil, err } if genesisJson.Data == nil { - return nil, nil, errors.New("genesis data is nil") + return nil, errors.New("genesis data is nil") } - return genesisJson.Data, nil, nil + return genesisJson.Data, nil } diff --git a/validator/client/beacon-api/genesis_test.go b/validator/client/beacon-api/genesis_test.go index 42721462465b..da7122d2f0ed 100644 --- a/validator/client/beacon-api/genesis_test.go +++ b/validator/client/beacon-api/genesis_test.go @@ -5,9 +5,7 @@ import ( "testing" "github.com/golang/mock/gomock" - "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/beacon" - "github.com/prysmaticlabs/prysm/v4/network/httputil" "github.com/prysmaticlabs/prysm/v4/testing/assert" "github.com/prysmaticlabs/prysm/v4/testing/require" "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api/mock" @@ -27,7 +25,6 @@ func TestGetGenesis_ValidGenesis(t *testing.T) { &genesisResponseJson, ).Return( nil, - nil, ).SetArg( 2, beacon.GetGenesisResponse{ @@ -39,9 +36,8 @@ func TestGetGenesis_ValidGenesis(t *testing.T) { ).Times(1) genesisProvider := &beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler} - resp, httpError, err := genesisProvider.GetGenesis(ctx) + resp, err := genesisProvider.GetGenesis(ctx) assert.NoError(t, err) - assert.Equal(t, (*httputil.DefaultJsonError)(nil), httpError) require.NotNil(t, resp) assert.Equal(t, "1234", resp.GenesisTime) assert.Equal(t, "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2", resp.GenesisValidatorsRoot) @@ -61,42 +57,12 @@ func TestGetGenesis_NilData(t *testing.T) { &genesisResponseJson, ).Return( nil, - nil, ).SetArg( 2, beacon.GetGenesisResponse{Data: nil}, ).Times(1) genesisProvider := &beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler} - _, httpError, err := genesisProvider.GetGenesis(ctx) - assert.Equal(t, (*httputil.DefaultJsonError)(nil), httpError) + _, err := genesisProvider.GetGenesis(ctx) assert.ErrorContains(t, "genesis data is nil", err) } - -func TestGetGenesis_JsonResponseError(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - ctx := context.Background() - - expectedHttpErrorJson := &httputil.DefaultJsonError{ - Message: "http error message", - Code: 999, - } - - genesisResponseJson := beacon.GetGenesisResponse{} - jsonRestHandler := mock.NewMockJsonRestHandler(ctrl) - jsonRestHandler.EXPECT().Get( - ctx, - "/eth/v1/beacon/genesis", - &genesisResponseJson, - ).Return( - expectedHttpErrorJson, - errors.New("some specific json response error"), - ).Times(1) - - genesisProvider := &beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler} - _, httpError, err := genesisProvider.GetGenesis(ctx) - assert.ErrorContains(t, "some specific json response error", err) - assert.DeepEqual(t, expectedHttpErrorJson, httpError) -} diff --git a/validator/client/beacon-api/get_beacon_block.go b/validator/client/beacon-api/get_beacon_block.go index bcbd2c327f7a..5726894fbd3d 100644 --- a/validator/client/beacon-api/get_beacon_block.go +++ b/validator/client/beacon-api/get_beacon_block.go @@ -13,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/validator" "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" + "github.com/prysmaticlabs/prysm/v4/network/httputil" ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v4/runtime/version" ) @@ -37,40 +38,37 @@ func (c beaconApiValidatorClient) getBeaconBlock(ctx context.Context, slot primi // We try the blinded block endpoint first. If it fails, we assume that we got a full block and try the full block endpoint. queryUrl := buildURL(fmt.Sprintf("/eth/v3/validator/blocks/%d", slot), queryParams) produceBlockV3ResponseJson := validator.ProduceBlockV3Response{} - errJson, err := c.jsonRestHandler.Get(ctx, queryUrl, &produceBlockV3ResponseJson) - if err == nil { - ver = produceBlockV3ResponseJson.Version - blinded = produceBlockV3ResponseJson.ExecutionPayloadBlinded - decoder = json.NewDecoder(bytes.NewReader(produceBlockV3ResponseJson.Data)) - } else if errJson == nil { - return nil, errors.Wrap(err, "failed to query GET REST endpoint") - } else if errJson.Code == http.StatusNotFound { + err := c.jsonRestHandler.Get(ctx, queryUrl, &produceBlockV3ResponseJson) + errJson := &httputil.DefaultJsonError{} + if err != nil { + if !errors.As(err, &errJson) { + return nil, err + } + if errJson.Code != http.StatusNotFound { + return nil, errJson + } log.Debug("Endpoint /eth/v3/validator/blocks is not supported, falling back to older endpoints for block proposal.") - produceBlindedBlockResponseJson := abstractProduceBlockResponseJson{} - queryUrl = buildURL(fmt.Sprintf("/eth/v1/validator/blinded_blocks/%d", slot), queryParams) - errJson, err = c.jsonRestHandler.Get(ctx, queryUrl, &produceBlindedBlockResponseJson) - if err == nil { - ver = produceBlindedBlockResponseJson.Version - blinded = true - decoder = json.NewDecoder(bytes.NewReader(produceBlindedBlockResponseJson.Data)) - } else if errJson == nil { - return nil, errors.Wrap(err, "failed to query GET REST endpoint: nil result") - } else { + fallbackResp, err := c.fallBackToBlinded(ctx, slot, queryParams) + errJson = &httputil.DefaultJsonError{} + if err != nil { + if !errors.As(err, &errJson) { + return nil, err + } log.Debug("Endpoint /eth/v1/validator/blinded_blocks failed to produce a blinded block, trying /eth/v2/validator/blocks.") - produceBlockResponseJson := abstractProduceBlockResponseJson{} - queryUrl = buildURL(fmt.Sprintf("/eth/v2/validator/blocks/%d", slot), queryParams) - errJson, err = c.jsonRestHandler.Get(ctx, queryUrl, &produceBlockResponseJson) + fallbackResp, err = c.fallBackToFull(ctx, slot, queryParams) if err != nil { - return nil, errors.Wrap(err, "failed to query GET REST endpoint") - } else if errJson != nil { - return nil, errors.Wrap(errJson, "failed to query GET REST endpoint") + return nil, err } - ver = produceBlockResponseJson.Version blinded = false - decoder = json.NewDecoder(bytes.NewReader(produceBlockResponseJson.Data)) + } else { + blinded = true } + ver = fallbackResp.Version + decoder = json.NewDecoder(bytes.NewReader(fallbackResp.Data)) } else { - return nil, fmt.Errorf("failed to query GET REST endpoint: %s (status code %d)", errJson.Message, errJson.Code) + ver = produceBlockV3ResponseJson.Version + blinded = produceBlockV3ResponseJson.ExecutionPayloadBlinded + decoder = json.NewDecoder(bytes.NewReader(produceBlockV3ResponseJson.Data)) } var response *ethpb.GenericBeaconBlock @@ -166,3 +164,29 @@ func (c beaconApiValidatorClient) getBeaconBlock(ctx context.Context, slot primi } return response, nil } + +func (c beaconApiValidatorClient) fallBackToBlinded( + ctx context.Context, + slot primitives.Slot, + queryParams neturl.Values, +) (*abstractProduceBlockResponseJson, error) { + resp := &abstractProduceBlockResponseJson{} + url := buildURL(fmt.Sprintf("/eth/v1/validator/blinded_blocks/%d", slot), queryParams) + if err := c.jsonRestHandler.Get(ctx, url, resp); err != nil { + return nil, err + } + return resp, nil +} + +func (c beaconApiValidatorClient) fallBackToFull( + ctx context.Context, + slot primitives.Slot, + queryParams neturl.Values, +) (*abstractProduceBlockResponseJson, error) { + resp := &abstractProduceBlockResponseJson{} + url := buildURL(fmt.Sprintf("/eth/v2/validator/blocks/%d", slot), queryParams) + if err := c.jsonRestHandler.Get(ctx, url, resp); err != nil { + return nil, err + } + return resp, nil +} diff --git a/validator/client/beacon-api/get_beacon_block_test.go b/validator/client/beacon-api/get_beacon_block_test.go index f930ee456add..79e2a1e1e3f3 100644 --- a/validator/client/beacon-api/get_beacon_block_test.go +++ b/validator/client/beacon-api/get_beacon_block_test.go @@ -32,13 +32,11 @@ func TestGetBeaconBlock_RequestFailed(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} _, err := validatorClient.getBeaconBlock(ctx, 1, []byte{1}, []byte{2}) - assert.ErrorContains(t, "failed to query GET REST endpoint", err) assert.ErrorContains(t, "foo error", err) } @@ -138,7 +136,6 @@ func TestGetBeaconBlock_Error(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -175,7 +172,6 @@ func TestGetBeaconBlock_Phase0Valid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -219,7 +215,6 @@ func TestGetBeaconBlock_AltairValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -264,7 +259,6 @@ func TestGetBeaconBlock_BellatrixValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -310,7 +304,6 @@ func TestGetBeaconBlock_BlindedBellatrixValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -356,7 +349,6 @@ func TestGetBeaconBlock_CapellaValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -402,7 +394,6 @@ func TestGetBeaconBlock_BlindedCapellaValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -448,7 +439,6 @@ func TestGetBeaconBlock_DenebValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -494,7 +484,6 @@ func TestGetBeaconBlock_BlindedDenebValid(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -533,7 +522,6 @@ func TestGetBeaconBlock_FallbackToBlindedBlock(t *testing.T) { &validator.ProduceBlockV3Response{}, ).Return( &httputil.DefaultJsonError{Code: http.StatusNotFound}, - errors.New("foo"), ).Times(1) jsonRestHandler.EXPECT().Get( ctx, @@ -547,7 +535,6 @@ func TestGetBeaconBlock_FallbackToBlindedBlock(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -586,7 +573,6 @@ func TestGetBeaconBlock_FallbackToFullBlock(t *testing.T) { &validator.ProduceBlockV3Response{}, ).Return( &httputil.DefaultJsonError{Code: http.StatusNotFound}, - errors.New("foo"), ).Times(1) jsonRestHandler.EXPECT().Get( ctx, @@ -594,7 +580,6 @@ func TestGetBeaconBlock_FallbackToFullBlock(t *testing.T) { &abstractProduceBlockResponseJson{}, ).Return( &httputil.DefaultJsonError{Code: http.StatusInternalServerError}, - errors.New("foo"), ).Times(1) jsonRestHandler.EXPECT().Get( ctx, @@ -608,7 +593,6 @@ func TestGetBeaconBlock_FallbackToFullBlock(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} diff --git a/validator/client/beacon-api/index_test.go b/validator/client/beacon-api/index_test.go index ef525f7d39d1..8ec1f0923077 100644 --- a/validator/client/beacon-api/index_test.go +++ b/validator/client/beacon-api/index_test.go @@ -49,7 +49,6 @@ func TestIndex_Nominal(t *testing.T) { &stateValidatorsResponseJson, ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ @@ -100,7 +99,6 @@ func TestIndex_UnexistingValidator(t *testing.T) { &stateValidatorsResponseJson, ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ @@ -143,7 +141,6 @@ func TestIndex_BadIndexError(t *testing.T) { &stateValidatorsResponseJson, ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ @@ -192,7 +189,6 @@ func TestIndex_JsonResponseError(t *testing.T) { reqBuffer, &stateValidatorsResponseJson, ).Return( - nil, errors.New("some specific json error"), ).Times(1) diff --git a/validator/client/beacon-api/json_rest_handler.go b/validator/client/beacon-api/json_rest_handler.go index fa3962dfcbe3..9a7c30e3e312 100644 --- a/validator/client/beacon-api/json_rest_handler.go +++ b/validator/client/beacon-api/json_rest_handler.go @@ -13,8 +13,8 @@ import ( ) type JsonRestHandler interface { - Get(ctx context.Context, query string, resp interface{}) (*httputil.DefaultJsonError, error) - Post(ctx context.Context, endpoint string, headers map[string]string, data *bytes.Buffer, resp interface{}) (*httputil.DefaultJsonError, error) + Get(ctx context.Context, query string, resp interface{}) error + Post(ctx context.Context, endpoint string, headers map[string]string, data *bytes.Buffer, resp interface{}) error } type beaconApiJsonRestHandler struct { @@ -24,20 +24,20 @@ type beaconApiJsonRestHandler struct { // Get sends a GET request and decodes the response body as a JSON object into the passed in object. // If an HTTP error is returned, the body is decoded as a DefaultJsonError JSON object and returned as the first return value. -func (c beaconApiJsonRestHandler) Get(ctx context.Context, endpoint string, resp interface{}) (*httputil.DefaultJsonError, error) { +func (c beaconApiJsonRestHandler) Get(ctx context.Context, endpoint string, resp interface{}) error { if resp == nil { - return nil, errors.New("resp is nil") + return errors.New("resp is nil") } url := c.host + endpoint req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { - return nil, errors.Wrap(err, "failed to create request with context") + return errors.Wrapf(err, "failed to create request for endpoint %s", url) } httpResp, err := c.httpClient.Do(req) if err != nil { - return nil, errors.Wrap(err, "failed to perform request with HTTP client") + return errors.Wrapf(err, "failed to perform request for endpoint %s", url) } defer func() { if err := httpResp.Body.Close(); err != nil { @@ -56,15 +56,15 @@ func (c beaconApiJsonRestHandler) Post( headers map[string]string, data *bytes.Buffer, resp interface{}, -) (*httputil.DefaultJsonError, error) { +) error { if data == nil { - return nil, errors.New("data is nil") + return errors.New("data is nil") } url := c.host + apiEndpoint req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, data) if err != nil { - return nil, errors.Wrap(err, "failed to create request with context") + return errors.Wrapf(err, "failed to create request for endpoint %s", url) } for headerKey, headerValue := range headers { @@ -74,7 +74,7 @@ func (c beaconApiJsonRestHandler) Post( httpResp, err := c.httpClient.Do(req) if err != nil { - return nil, errors.Wrap(err, "failed to perform request with HTTP client") + return errors.Wrapf(err, "failed to perform request for endpoint %s", url) } defer func() { if err = httpResp.Body.Close(); err != nil { @@ -85,33 +85,33 @@ func (c beaconApiJsonRestHandler) Post( return decodeResp(httpResp, resp) } -func decodeResp(httpResp *http.Response, resp interface{}) (*httputil.DefaultJsonError, error) { +func decodeResp(httpResp *http.Response, resp interface{}) error { body, err := io.ReadAll(httpResp.Body) if err != nil { - return nil, errors.Wrapf(err, "failed to read response body for %s", httpResp.Request.URL) + return errors.Wrapf(err, "failed to read response body for %s", httpResp.Request.URL) } if httpResp.Header.Get("Content-Type") != api.JsonMediaType { if httpResp.StatusCode == http.StatusOK { - return nil, nil + return nil } - return &httputil.DefaultJsonError{Code: httpResp.StatusCode, Message: string(body)}, nil + return &httputil.DefaultJsonError{Code: httpResp.StatusCode, Message: string(body)} } decoder := json.NewDecoder(bytes.NewBuffer(body)) if httpResp.StatusCode != http.StatusOK { errorJson := &httputil.DefaultJsonError{} if err = decoder.Decode(errorJson); err != nil { - return nil, errors.Wrapf(err, "failed to decode response body into error json for %s", httpResp.Request.URL) + return errors.Wrapf(err, "failed to decode response body into error json for %s", httpResp.Request.URL) } - return errorJson, nil + return errorJson } // resp is nil for requests that do not return anything. if resp != nil { if err = decoder.Decode(resp); err != nil { - return nil, errors.Wrapf(err, "failed to decode response body into json for %s", httpResp.Request.URL) + return errors.Wrapf(err, "failed to decode response body into json for %s", httpResp.Request.URL) } } - return nil, nil + return nil } diff --git a/validator/client/beacon-api/json_rest_handler_test.go b/validator/client/beacon-api/json_rest_handler_test.go index a26565116183..b9652a6d6f90 100644 --- a/validator/client/beacon-api/json_rest_handler_test.go +++ b/validator/client/beacon-api/json_rest_handler_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v4/api" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/beacon" "github.com/prysmaticlabs/prysm/v4/network/httputil" @@ -44,9 +45,7 @@ func TestGet(t *testing.T) { host: server.URL, } resp := &beacon.GetGenesisResponse{} - errJson, err := jsonRestHandler.Get(ctx, endpoint+"?arg1=abc&arg2=def", resp) - assert.Equal(t, true, errJson == nil) - assert.NoError(t, err) + require.NoError(t, jsonRestHandler.Get(ctx, endpoint+"?arg1=abc&arg2=def", resp)) assert.DeepEqual(t, genesisJson, resp) } @@ -92,87 +91,103 @@ func TestPost(t *testing.T) { host: server.URL, } resp := &beacon.GetGenesisResponse{} - errJson, err := jsonRestHandler.Post( - ctx, - endpoint, - headers, - bytes.NewBuffer(dataBytes), - resp, - ) - assert.Equal(t, true, errJson == nil) - assert.NoError(t, err) + require.NoError(t, jsonRestHandler.Post(ctx, endpoint, headers, bytes.NewBuffer(dataBytes), resp)) assert.DeepEqual(t, genesisJson, resp) } -func decodeRespTest(t *testing.T) { +func Test_decodeResp(t *testing.T) { type j struct { Foo string `json:"foo"` } t.Run("200 non-JSON", func(t *testing.T) { - r := &http.Response{StatusCode: http.StatusOK, Header: map[string][]string{"Content-Type": {api.OctetStreamMediaType}}} - errJson, err := decodeResp(r, nil) - require.Equal(t, true, errJson == nil) - require.NoError(t, err) + body := bytes.Buffer{} + r := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.OctetStreamMediaType}}, + } + require.NoError(t, decodeResp(r, nil)) }) t.Run("non-200 non-JSON", func(t *testing.T) { body := bytes.Buffer{} _, err := body.WriteString("foo") require.NoError(t, err) - r := &http.Response{StatusCode: http.StatusInternalServerError, Header: map[string][]string{"Content-Type": {api.OctetStreamMediaType}}} - errJson, err := decodeResp(r, nil) - require.NotNil(t, errJson) + r := &http.Response{ + StatusCode: http.StatusInternalServerError, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.OctetStreamMediaType}}, + } + err = decodeResp(r, nil) + errJson := &httputil.DefaultJsonError{} + require.Equal(t, true, errors.As(err, &errJson)) assert.Equal(t, http.StatusInternalServerError, errJson.Code) assert.Equal(t, "foo", errJson.Message) - require.NoError(t, err) }) t.Run("200 JSON with resp", func(t *testing.T) { body := bytes.Buffer{} b, err := json.Marshal(&j{Foo: "foo"}) require.NoError(t, err) body.Write(b) - r := &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(&body), Header: map[string][]string{"Content-Type": {api.JsonMediaType}}} + r := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.JsonMediaType}}, + } resp := &j{} - errJson, err := decodeResp(r, resp) - require.Equal(t, true, errJson == nil) - require.NoError(t, err) + require.NoError(t, decodeResp(r, resp)) assert.Equal(t, "foo", resp.Foo) }) t.Run("200 JSON without resp", func(t *testing.T) { - r := &http.Response{StatusCode: http.StatusOK, Header: map[string][]string{"Content-Type": {api.JsonMediaType}}} - errJson, err := decodeResp(r, nil) - require.Equal(t, true, errJson == nil) - require.NoError(t, err) + body := bytes.Buffer{} + r := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.JsonMediaType}}, + } + require.NoError(t, decodeResp(r, nil)) }) t.Run("non-200 JSON", func(t *testing.T) { body := bytes.Buffer{} b, err := json.Marshal(&httputil.DefaultJsonError{Code: http.StatusInternalServerError, Message: "error"}) require.NoError(t, err) body.Write(b) - r := &http.Response{StatusCode: http.StatusInternalServerError, Body: io.NopCloser(&body), Header: map[string][]string{"Content-Type": {api.JsonMediaType}}} - errJson, err := decodeResp(r, nil) - require.NotNil(t, errJson) + r := &http.Response{ + StatusCode: http.StatusInternalServerError, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.JsonMediaType}}, + } + err = decodeResp(r, nil) + errJson := &httputil.DefaultJsonError{} + require.Equal(t, true, errors.As(err, &errJson)) assert.Equal(t, http.StatusInternalServerError, errJson.Code) assert.Equal(t, "error", errJson.Message) - require.NoError(t, err) }) t.Run("200 JSON cannot decode", func(t *testing.T) { body := bytes.Buffer{} _, err := body.WriteString("foo") require.NoError(t, err) - r := &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(&body), Header: map[string][]string{"Content-Type": {api.JsonMediaType}}} + r := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.JsonMediaType}}, + Request: &http.Request{}, + } resp := &j{} - errJson, err := decodeResp(r, resp) - require.Equal(t, true, errJson == nil) + err = decodeResp(r, resp) assert.ErrorContains(t, "failed to decode response body into json", err) }) t.Run("non-200 JSON cannot decode", func(t *testing.T) { body := bytes.Buffer{} _, err := body.WriteString("foo") require.NoError(t, err) - r := &http.Response{StatusCode: http.StatusInternalServerError, Body: io.NopCloser(&body), Header: map[string][]string{"Content-Type": {api.JsonMediaType}}} - errJson, err := decodeResp(r, nil) - require.Equal(t, true, errJson == nil) + r := &http.Response{ + StatusCode: http.StatusInternalServerError, + Body: io.NopCloser(&body), + Header: map[string][]string{"Content-Type": {api.JsonMediaType}}, + Request: &http.Request{}, + } + err = decodeResp(r, nil) assert.ErrorContains(t, "failed to decode response body into error json", err) }) } diff --git a/validator/client/beacon-api/mock/BUILD.bazel b/validator/client/beacon-api/mock/BUILD.bazel index 445f3e61e6b6..696ad18a1f86 100644 --- a/validator/client/beacon-api/mock/BUILD.bazel +++ b/validator/client/beacon-api/mock/BUILD.bazel @@ -16,7 +16,6 @@ go_library( "//beacon-chain/rpc/eth/shared:go_default_library", "//beacon-chain/rpc/eth/validator:go_default_library", "//consensus-types/primitives:go_default_library", - "//network/httputil:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "@com_github_golang_mock//gomock:go_default_library", ], diff --git a/validator/client/beacon-api/mock/genesis_mock.go b/validator/client/beacon-api/mock/genesis_mock.go index 3dbad4be2e8d..caa7aa74d391 100644 --- a/validator/client/beacon-api/mock/genesis_mock.go +++ b/validator/client/beacon-api/mock/genesis_mock.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: validator/client/beacon-api/genesis.go +// Source: github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api (interfaces: GenesisProvider) // Package mock is a generated GoMock package. package mock @@ -10,7 +10,6 @@ import ( gomock "github.com/golang/mock/gomock" beacon "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/beacon" - httputil "github.com/prysmaticlabs/prysm/v4/network/httputil" ) // MockGenesisProvider is a mock of GenesisProvider interface. @@ -37,17 +36,16 @@ func (m *MockGenesisProvider) EXPECT() *MockGenesisProviderMockRecorder { } // GetGenesis mocks base method. -func (m *MockGenesisProvider) GetGenesis(ctx context.Context) (*beacon.Genesis, *httputil.DefaultJsonError, error) { +func (m *MockGenesisProvider) GetGenesis(arg0 context.Context) (*beacon.Genesis, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetGenesis", ctx) + ret := m.ctrl.Call(m, "GetGenesis", arg0) ret0, _ := ret[0].(*beacon.Genesis) - ret1, _ := ret[1].(*httputil.DefaultJsonError) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 + ret1, _ := ret[1].(error) + return ret0, ret1 } // GetGenesis indicates an expected call of GetGenesis. -func (mr *MockGenesisProviderMockRecorder) GetGenesis(ctx interface{}) *gomock.Call { +func (mr *MockGenesisProviderMockRecorder) GetGenesis(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGenesis", reflect.TypeOf((*MockGenesisProvider)(nil).GetGenesis), ctx) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGenesis", reflect.TypeOf((*MockGenesisProvider)(nil).GetGenesis), arg0) } diff --git a/validator/client/beacon-api/mock/json_rest_handler_mock.go b/validator/client/beacon-api/mock/json_rest_handler_mock.go index 1551fa712e9c..ae95ecd74ffd 100644 --- a/validator/client/beacon-api/mock/json_rest_handler_mock.go +++ b/validator/client/beacon-api/mock/json_rest_handler_mock.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: validator/client/beacon-api/json_rest_handler.go +// Source: github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api (interfaces: JsonRestHandler) // Package mock is a generated GoMock package. package mock @@ -10,7 +10,6 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" - httputil "github.com/prysmaticlabs/prysm/v4/network/httputil" ) // MockJsonRestHandler is a mock of JsonRestHandler interface. @@ -37,31 +36,29 @@ func (m *MockJsonRestHandler) EXPECT() *MockJsonRestHandlerMockRecorder { } // Get mocks base method. -func (m *MockJsonRestHandler) Get(ctx context.Context, query string, resp interface{}) (*httputil.DefaultJsonError, error) { +func (m *MockJsonRestHandler) Get(arg0 context.Context, arg1 string, arg2 interface{}) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Get", ctx, query, resp) - ret0, _ := ret[0].(*httputil.DefaultJsonError) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "Get", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 } // Get indicates an expected call of Get. -func (mr *MockJsonRestHandlerMockRecorder) Get(ctx, query, resp interface{}) *gomock.Call { +func (mr *MockJsonRestHandlerMockRecorder) Get(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockJsonRestHandler)(nil).Get), ctx, query, resp) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockJsonRestHandler)(nil).Get), arg0, arg1, arg2) } // Post mocks base method. -func (m *MockJsonRestHandler) Post(ctx context.Context, endpoint string, headers map[string]string, data *bytes.Buffer, resp interface{}) (*httputil.DefaultJsonError, error) { +func (m *MockJsonRestHandler) Post(arg0 context.Context, arg1 string, arg2 map[string]string, arg3 *bytes.Buffer, arg4 interface{}) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Post", ctx, endpoint, headers, data, resp) - ret0, _ := ret[0].(*httputil.DefaultJsonError) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "Post", arg0, arg1, arg2, arg3, arg4) + ret0, _ := ret[0].(error) + return ret0 } // Post indicates an expected call of Post. -func (mr *MockJsonRestHandlerMockRecorder) Post(ctx, endpoint, headers, data, resp interface{}) *gomock.Call { +func (mr *MockJsonRestHandlerMockRecorder) Post(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Post", reflect.TypeOf((*MockJsonRestHandler)(nil).Post), ctx, endpoint, headers, data, resp) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Post", reflect.TypeOf((*MockJsonRestHandler)(nil).Post), arg0, arg1, arg2, arg3, arg4) } diff --git a/validator/client/beacon-api/prepare_beacon_proposer.go b/validator/client/beacon-api/prepare_beacon_proposer.go index aeb891e3f630..f068b4111609 100644 --- a/validator/client/beacon-api/prepare_beacon_proposer.go +++ b/validator/client/beacon-api/prepare_beacon_proposer.go @@ -26,19 +26,5 @@ func (c *beaconApiValidatorClient) prepareBeaconProposer(ctx context.Context, re return errors.Wrap(err, "failed to marshal recipients") } - errJson, err := c.jsonRestHandler.Post( - ctx, - "/eth/v1/validator/prepare_beacon_proposer", - nil, - bytes.NewBuffer(marshalledJsonRecipients), - nil, - ) - if err != nil { - return errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return errJson - } - - return nil + return c.jsonRestHandler.Post(ctx, "/eth/v1/validator/prepare_beacon_proposer", nil, bytes.NewBuffer(marshalledJsonRecipients), nil) } diff --git a/validator/client/beacon-api/prepare_beacon_proposer_test.go b/validator/client/beacon-api/prepare_beacon_proposer_test.go index 1d4756ffcb21..9a5da617a6ed 100644 --- a/validator/client/beacon-api/prepare_beacon_proposer_test.go +++ b/validator/client/beacon-api/prepare_beacon_proposer_test.go @@ -55,7 +55,6 @@ func TestPrepareBeaconProposer_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) decodedFeeRecipient1, err := hexutil.Decode(feeRecipient1) @@ -99,7 +98,6 @@ func TestPrepareBeaconProposer_BadRequest(t *testing.T) { gomock.Any(), nil, ).Return( - nil, errors.New("foo error"), ).Times(1) diff --git a/validator/client/beacon-api/propose_attestation.go b/validator/client/beacon-api/propose_attestation.go index 879bcf5d8c48..333b71636cfe 100644 --- a/validator/client/beacon-api/propose_attestation.go +++ b/validator/client/beacon-api/propose_attestation.go @@ -19,12 +19,14 @@ func (c beaconApiValidatorClient) proposeAttestation(ctx context.Context, attest return nil, err } - errJson, err := c.jsonRestHandler.Post(ctx, "/eth/v1/beacon/pool/attestations", nil, bytes.NewBuffer(marshalledAttestation), nil) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Post( + ctx, + "/eth/v1/beacon/pool/attestations", + nil, + bytes.NewBuffer(marshalledAttestation), + nil, + ); err != nil { + return nil, err } attestationDataRoot, err := attestation.Data.HashTreeRoot() diff --git a/validator/client/beacon-api/propose_attestation_test.go b/validator/client/beacon-api/propose_attestation_test.go index 60ea8c14be28..a9a18c61ed1d 100644 --- a/validator/client/beacon-api/propose_attestation_test.go +++ b/validator/client/beacon-api/propose_attestation_test.go @@ -132,7 +132,6 @@ func TestProposeAttestation(t *testing.T) { bytes.NewBuffer(marshalledAttestations), nil, ).Return( - nil, test.endpointError, ).Times(test.endpointCall) diff --git a/validator/client/beacon-api/propose_beacon_block.go b/validator/client/beacon-api/propose_beacon_block.go index 20db5101471c..536adb510061 100644 --- a/validator/client/beacon-api/propose_beacon_block.go +++ b/validator/client/beacon-api/propose_beacon_block.go @@ -10,6 +10,7 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/shared" "github.com/prysmaticlabs/prysm/v4/encoding/bytesutil" + "github.com/prysmaticlabs/prysm/v4/network/httputil" ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" ) @@ -132,13 +133,17 @@ func (c beaconApiValidatorClient) proposeBeaconBlock(ctx context.Context, in *et } headers := map[string]string{"Eth-Consensus-Version": consensusVersion} - if httpError, err := c.jsonRestHandler.Post(ctx, endpoint, headers, bytes.NewBuffer(marshalledSignedBeaconBlockJson), nil); err != nil { - if httpError != nil && httpError.Code == http.StatusAccepted { - // Error 202 means that the block was successfully broadcasted, but validation failed - return nil, errors.Wrap(err, "block was successfully broadcasted but failed validation") + err = c.jsonRestHandler.Post(ctx, endpoint, headers, bytes.NewBuffer(marshalledSignedBeaconBlockJson), nil) + errJson := &httputil.DefaultJsonError{} + if err != nil { + if !errors.As(err, &errJson) { + return nil, err } - - return nil, errors.Wrap(err, "failed to send POST data to REST endpoint") + // Error 202 means that the block was successfully broadcast, but validation failed + if errJson.Code == http.StatusAccepted { + return nil, errors.New("block was successfully broadcast but failed validation") + } + return nil, errJson } return ðpb.ProposeResponse{BlockRoot: beaconBlockRoot[:]}, nil diff --git a/validator/client/beacon-api/propose_beacon_block_test.go b/validator/client/beacon-api/propose_beacon_block_test.go index c3a2a9abedf8..116ba073aa82 100644 --- a/validator/client/beacon-api/propose_beacon_block_test.go +++ b/validator/client/beacon-api/propose_beacon_block_test.go @@ -16,21 +16,29 @@ import ( func TestProposeBeaconBlock_Error(t *testing.T) { testSuites := []struct { name string + returnedError error expectedErrorMessage string - expectedHttpError *httputil.DefaultJsonError }{ { name: "error 202", - expectedErrorMessage: "block was successfully broadcasted but failed validation", - expectedHttpError: &httputil.DefaultJsonError{ + expectedErrorMessage: "block was successfully broadcast but failed validation", + returnedError: &httputil.DefaultJsonError{ Code: http.StatusAccepted, Message: "202 error", }, }, { - name: "request failed", - expectedErrorMessage: "failed to send POST data to REST endpoint", - expectedHttpError: nil, + name: "error 500", + expectedErrorMessage: "HTTP request unsuccessful (500: foo error)", + returnedError: &httputil.DefaultJsonError{ + Code: http.StatusInternalServerError, + Message: "foo error", + }, + }, + { + name: "other error", + expectedErrorMessage: "foo error", + returnedError: errors.New("foo error"), }, } @@ -99,14 +107,12 @@ func TestProposeBeaconBlock_Error(t *testing.T) { gomock.Any(), nil, ).Return( - testSuite.expectedHttpError, - errors.New("foo error"), + testSuite.returnedError, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} _, err := validatorClient.proposeBeaconBlock(ctx, testCase.block) assert.ErrorContains(t, testSuite.expectedErrorMessage, err) - assert.ErrorContains(t, "foo error", err) }) } } diff --git a/validator/client/beacon-api/propose_exit.go b/validator/client/beacon-api/propose_exit.go index 20ef4d6fb248..e1c247ae1441 100644 --- a/validator/client/beacon-api/propose_exit.go +++ b/validator/client/beacon-api/propose_exit.go @@ -34,18 +34,14 @@ func (c beaconApiValidatorClient) proposeExit(ctx context.Context, signedVolunta return nil, errors.Wrap(err, "failed to marshal signed voluntary exit") } - errJson, err := c.jsonRestHandler.Post( + if err = c.jsonRestHandler.Post( ctx, "/eth/v1/beacon/pool/voluntary_exits", nil, bytes.NewBuffer(marshalledSignedVoluntaryExit), nil, - ) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + ); err != nil { + return nil, err } exitRoot, err := signedVoluntaryExit.Exit.HashTreeRoot() diff --git a/validator/client/beacon-api/propose_exit_test.go b/validator/client/beacon-api/propose_exit_test.go index 501afb7efd47..96626b9934d1 100644 --- a/validator/client/beacon-api/propose_exit_test.go +++ b/validator/client/beacon-api/propose_exit_test.go @@ -46,7 +46,6 @@ func TestProposeExit_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) decodedSignature, err := hexutil.Decode(signature) @@ -95,7 +94,6 @@ func TestProposeExit_BadRequest(t *testing.T) { gomock.Any(), nil, ).Return( - nil, errors.New("foo error"), ).Times(1) diff --git a/validator/client/beacon-api/prysm_beacon_chain_client.go b/validator/client/beacon-api/prysm_beacon_chain_client.go index 34431c239966..6a9876ac7042 100644 --- a/validator/client/beacon-api/prysm_beacon_chain_client.go +++ b/validator/client/beacon-api/prysm_beacon_chain_client.go @@ -52,12 +52,8 @@ func (c prysmBeaconChainClient) GetValidatorCount(ctx context.Context, stateID s queryUrl := buildURL(fmt.Sprintf("/eth/v1/beacon/states/%s/validator_count", stateID), queryParams) var validatorCountResponse validator.CountResponse - errJson, err := c.jsonRestHandler.Get(ctx, queryUrl, &validatorCountResponse) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Get(ctx, queryUrl, &validatorCountResponse); err != nil { + return nil, err } if validatorCountResponse.Data == nil { diff --git a/validator/client/beacon-api/registration.go b/validator/client/beacon-api/registration.go index d5cb7f2bd7cc..bb1d3717fd21 100644 --- a/validator/client/beacon-api/registration.go +++ b/validator/client/beacon-api/registration.go @@ -24,13 +24,5 @@ func (c *beaconApiValidatorClient) submitValidatorRegistrations(ctx context.Cont return errors.Wrap(err, "failed to marshal registration") } - errJson, err := c.jsonRestHandler.Post(ctx, endpoint, nil, bytes.NewBuffer(marshalledJsonRegistration), nil) - if err != nil { - return errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return errJson - } - - return nil + return c.jsonRestHandler.Post(ctx, endpoint, nil, bytes.NewBuffer(marshalledJsonRegistration), nil) } diff --git a/validator/client/beacon-api/registration_test.go b/validator/client/beacon-api/registration_test.go index e675389029b0..28cbb7ac3fdf 100644 --- a/validator/client/beacon-api/registration_test.go +++ b/validator/client/beacon-api/registration_test.go @@ -75,7 +75,6 @@ func TestRegistration_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) decodedFeeRecipient1, err := hexutil.Decode(feeRecipient1) @@ -150,7 +149,6 @@ func TestRegistration_BadRequest(t *testing.T) { gomock.Any(), nil, ).Return( - nil, errors.New("foo error"), ).Times(1) diff --git a/validator/client/beacon-api/state_validators.go b/validator/client/beacon-api/state_validators.go index 3ad287fb2480..165ca0e40c01 100644 --- a/validator/client/beacon-api/state_validators.go +++ b/validator/client/beacon-api/state_validators.go @@ -91,12 +91,8 @@ func (c beaconApiStateValidatorsProvider) getStateValidatorsHelper( return nil, errors.Wrapf(err, "failed to marshal request into JSON") } stateValidatorsJson := &beacon.GetValidatorsResponse{} - errJson, err := c.jsonRestHandler.Post(ctx, endpoint, nil, bytes.NewBuffer(reqBytes), stateValidatorsJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Post(ctx, endpoint, nil, bytes.NewBuffer(reqBytes), stateValidatorsJson); err != nil { + return nil, err } if stateValidatorsJson.Data == nil { diff --git a/validator/client/beacon-api/state_validators_test.go b/validator/client/beacon-api/state_validators_test.go index fd368eca922c..066ecd03a47a 100644 --- a/validator/client/beacon-api/state_validators_test.go +++ b/validator/client/beacon-api/state_validators_test.go @@ -76,7 +76,6 @@ func TestGetStateValidators_Nominal(t *testing.T) { &stateValidatorsResponseJson, ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ @@ -125,7 +124,6 @@ func TestGetStateValidators_GetRestJsonResponseOnError(t *testing.T) { bytes.NewBuffer(reqBytes), &stateValidatorsResponseJson, ).Return( - nil, errors.New("an error"), ).Times(1) @@ -161,7 +159,6 @@ func TestGetStateValidators_DataIsNil(t *testing.T) { &stateValidatorsResponseJson, ).Return( nil, - nil, ).SetArg( 4, beacon.GetValidatorsResponse{ diff --git a/validator/client/beacon-api/status_test.go b/validator/client/beacon-api/status_test.go index 5af1f1619b9a..3ed593bc882b 100644 --- a/validator/client/beacon-api/status_test.go +++ b/validator/client/beacon-api/status_test.go @@ -71,7 +71,6 @@ func TestValidatorStatus_Nominal(t *testing.T) { "/eth/v1/node/version", &nodeVersionResponse, ).Return( - nil, iface.ErrNotSupported, ).Times(1) @@ -179,7 +178,6 @@ func TestMultipleValidatorStatus_Nominal(t *testing.T) { "/eth/v1/node/version", &nodeVersionResponse, ).Return( - nil, iface.ErrNotSupported, ).Times(1) @@ -342,7 +340,6 @@ func TestGetValidatorsStatusResponse_Nominal_SomeActiveValidators(t *testing.T) &nodeVersionResponse, ).Return( nil, - nil, ).SetArg( 2, node.GetVersionResponse{Data: &node.Version{Version: "prysm/v0.0.1"}}, @@ -355,7 +352,6 @@ func TestGetValidatorsStatusResponse_Nominal_SomeActiveValidators(t *testing.T) &validatorCountResponse, ).Return( nil, - nil, ).SetArg( 2, validator2.CountResponse{ @@ -491,7 +487,6 @@ func TestGetValidatorsStatusResponse_Nominal_NoActiveValidators(t *testing.T) { "/eth/v1/node/version", &nodeVersionResponse, ).Return( - nil, iface.ErrNotSupported, ).Times(1) @@ -731,7 +726,6 @@ func TestValidatorStatusResponse_InvalidData(t *testing.T) { "/eth/v1/node/version", &nodeVersionResponse, ).Return( - nil, iface.ErrNotSupported, ).Times(testCase.validatorCountCalled) diff --git a/validator/client/beacon-api/stream_blocks.go b/validator/client/beacon-api/stream_blocks.go index 7be045b250cb..d68b7e030af2 100644 --- a/validator/client/beacon-api/stream_blocks.go +++ b/validator/client/beacon-api/stream_blocks.go @@ -115,12 +115,8 @@ func (c beaconApiValidatorClient) getHeadSignedBeaconBlock(ctx context.Context) // Since we don't know yet what the json looks like, we unmarshal into an abstract structure that has only a version // and a blob of data signedBlockResponseJson := abstractSignedBlockResponseJson{} - errJson, err := c.jsonRestHandler.Get(ctx, "/eth/v2/beacon/blocks/head", &signedBlockResponseJson) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, "/eth/v2/beacon/blocks/head", &signedBlockResponseJson); err != nil { + return nil, err } // Once we know what the consensus version is, we can go ahead and unmarshal into the specific structs unique to each version diff --git a/validator/client/beacon-api/stream_blocks_test.go b/validator/client/beacon-api/stream_blocks_test.go index 3afcf787a84d..a8e6dec6abf8 100644 --- a/validator/client/beacon-api/stream_blocks_test.go +++ b/validator/client/beacon-api/stream_blocks_test.go @@ -35,7 +35,6 @@ func TestStreamBlocks_UnsupportedConsensusVersion(t *testing.T) { abstractSignedBlockResponseJson{Version: "foo"}, ).Return( nil, - nil, ).Times(1) validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler} @@ -161,7 +160,6 @@ func TestStreamBlocks_Error(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) beaconBlockConverter := testSuite.generateBeaconBlockConverter(ctrl, testCase.conversionError) @@ -221,7 +219,6 @@ func TestStreamBlocks_Phase0Valid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -259,7 +256,6 @@ func TestStreamBlocks_Phase0Valid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -287,7 +283,6 @@ func TestStreamBlocks_Phase0Valid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -385,7 +380,6 @@ func TestStreamBlocks_AltairValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -423,7 +417,6 @@ func TestStreamBlocks_AltairValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -451,7 +444,6 @@ func TestStreamBlocks_AltairValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -549,7 +541,6 @@ func TestStreamBlocks_BellatrixValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -587,7 +578,6 @@ func TestStreamBlocks_BellatrixValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -615,7 +605,6 @@ func TestStreamBlocks_BellatrixValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -713,7 +702,6 @@ func TestStreamBlocks_CapellaValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -751,7 +739,6 @@ func TestStreamBlocks_CapellaValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -779,7 +766,6 @@ func TestStreamBlocks_CapellaValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -877,7 +863,6 @@ func TestStreamBlocks_DenebValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -907,7 +892,6 @@ func TestStreamBlocks_DenebValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ @@ -925,7 +909,6 @@ func TestStreamBlocks_DenebValid(t *testing.T) { &signedBlockResponseJson, ).Return( nil, - nil, ).SetArg( 2, abstractSignedBlockResponseJson{ diff --git a/validator/client/beacon-api/submit_aggregate_selection_proof.go b/validator/client/beacon-api/submit_aggregate_selection_proof.go index 731823814d1d..41ce86d4bf0e 100644 --- a/validator/client/beacon-api/submit_aggregate_selection_proof.go +++ b/validator/client/beacon-api/submit_aggregate_selection_proof.go @@ -95,12 +95,8 @@ func (c *beaconApiValidatorClient) getAggregateAttestation( endpoint := buildURL("/eth/v1/validator/aggregate_attestation", params) var aggregateAttestationResponse validator.AggregateAttestationResponse - errJson, err := c.jsonRestHandler.Get(ctx, endpoint, &aggregateAttestationResponse) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, endpoint, &aggregateAttestationResponse); err != nil { + return nil, err } return &aggregateAttestationResponse, nil diff --git a/validator/client/beacon-api/submit_aggregate_selection_proof_test.go b/validator/client/beacon-api/submit_aggregate_selection_proof_test.go index 493f2347524c..010709fe323f 100644 --- a/validator/client/beacon-api/submit_aggregate_selection_proof_test.go +++ b/validator/client/beacon-api/submit_aggregate_selection_proof_test.go @@ -169,7 +169,6 @@ func TestSubmitAggregateSelectionProof(t *testing.T) { }, }, ).Return( - nil, test.syncingErr, ).Times(1) @@ -201,7 +200,6 @@ func TestSubmitAggregateSelectionProof(t *testing.T) { }, }, ).Return( - nil, test.validatorsErr, ).Times(test.validatorsCalled) @@ -220,7 +218,6 @@ func TestSubmitAggregateSelectionProof(t *testing.T) { Data: test.duties, }, ).Return( - nil, test.dutiesErr, ).Times(test.attesterDutiesCalled) @@ -233,7 +230,6 @@ func TestSubmitAggregateSelectionProof(t *testing.T) { 2, attestationDataResponse, ).Return( - nil, test.attestationDataErr, ).Times(test.attestationDataCalled) @@ -248,7 +244,6 @@ func TestSubmitAggregateSelectionProof(t *testing.T) { Data: jsonifyAttestation(aggregateAttestation), }, ).Return( - nil, test.aggregateAttestationErr, ).Times(test.aggregateAttestationCalled) diff --git a/validator/client/beacon-api/submit_signed_aggregate_proof.go b/validator/client/beacon-api/submit_signed_aggregate_proof.go index 81a435309f68..85a8d6177467 100644 --- a/validator/client/beacon-api/submit_signed_aggregate_proof.go +++ b/validator/client/beacon-api/submit_signed_aggregate_proof.go @@ -16,12 +16,8 @@ func (c *beaconApiValidatorClient) submitSignedAggregateSelectionProof(ctx conte return nil, errors.Wrap(err, "failed to marshal SignedAggregateAttestationAndProof") } - errJson, err := c.jsonRestHandler.Post(ctx, "/eth/v1/validator/aggregate_and_proofs", nil, bytes.NewBuffer(body), nil) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Post(ctx, "/eth/v1/validator/aggregate_and_proofs", nil, bytes.NewBuffer(body), nil); err != nil { + return nil, err } attestationDataRoot, err := in.SignedAggregateAndProof.Message.Aggregate.Data.HashTreeRoot() diff --git a/validator/client/beacon-api/submit_signed_aggregate_proof_test.go b/validator/client/beacon-api/submit_signed_aggregate_proof_test.go index 4c6a60c29c0d..3049c1a79fca 100644 --- a/validator/client/beacon-api/submit_signed_aggregate_proof_test.go +++ b/validator/client/beacon-api/submit_signed_aggregate_proof_test.go @@ -35,7 +35,6 @@ func TestSubmitSignedAggregateSelectionProof_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) attestationDataRoot, err := signedAggregateAndProof.Message.Aggregate.Data.HashTreeRoot() @@ -66,7 +65,6 @@ func TestSubmitSignedAggregateSelectionProof_BadRequest(t *testing.T) { bytes.NewBuffer(marshalledSignedAggregateSignedAndProof), nil, ).Return( - nil, errors.New("bad request"), ).Times(1) diff --git a/validator/client/beacon-api/submit_signed_contribution_and_proof.go b/validator/client/beacon-api/submit_signed_contribution_and_proof.go index e718e1a1f9be..061a09a423e1 100644 --- a/validator/client/beacon-api/submit_signed_contribution_and_proof.go +++ b/validator/client/beacon-api/submit_signed_contribution_and_proof.go @@ -47,19 +47,11 @@ func (c beaconApiValidatorClient) submitSignedContributionAndProof(ctx context.C return errors.Wrap(err, "failed to marshall signed contribution and proof") } - errJson, err := c.jsonRestHandler.Post( + return c.jsonRestHandler.Post( ctx, "/eth/v1/validator/contribution_and_proofs", nil, bytes.NewBuffer(jsonContributionAndProofsBytes), nil, ) - if err != nil { - return errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return errJson - } - - return nil } diff --git a/validator/client/beacon-api/submit_signed_contribution_and_proof_test.go b/validator/client/beacon-api/submit_signed_contribution_and_proof_test.go index 3c56ed3812ce..b47191d8aae7 100644 --- a/validator/client/beacon-api/submit_signed_contribution_and_proof_test.go +++ b/validator/client/beacon-api/submit_signed_contribution_and_proof_test.go @@ -53,7 +53,6 @@ func TestSubmitSignedContributionAndProof_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) contributionAndProof := ðpb.SignedContributionAndProof{ @@ -128,7 +127,6 @@ func TestSubmitSignedContributionAndProof_Error(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) } diff --git a/validator/client/beacon-api/subscribe_committee_subnets.go b/validator/client/beacon-api/subscribe_committee_subnets.go index 99be9f7e3f3d..0f8391cef515 100644 --- a/validator/client/beacon-api/subscribe_committee_subnets.go +++ b/validator/client/beacon-api/subscribe_committee_subnets.go @@ -73,19 +73,11 @@ func (c beaconApiValidatorClient) subscribeCommitteeSubnets(ctx context.Context, return errors.Wrap(err, "failed to marshal committees subscriptions") } - errJson, err := c.jsonRestHandler.Post( + return c.jsonRestHandler.Post( ctx, "/eth/v1/validator/beacon_committee_subscriptions", nil, bytes.NewBuffer(committeeSubscriptionsBytes), nil, ) - if err != nil { - return errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return errJson - } - - return nil } diff --git a/validator/client/beacon-api/subscribe_committee_subnets_test.go b/validator/client/beacon-api/subscribe_committee_subnets_test.go index 503c2241998d..ca50277cb26f 100644 --- a/validator/client/beacon-api/subscribe_committee_subnets_test.go +++ b/validator/client/beacon-api/subscribe_committee_subnets_test.go @@ -56,7 +56,6 @@ func TestSubscribeCommitteeSubnets_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) duties := make([]*validator.AttesterDuty, len(subscribeSlots)) @@ -280,7 +279,6 @@ func TestSubscribeCommitteeSubnets_Error(t *testing.T) { gomock.Any(), gomock.Any(), ).Return( - nil, errors.New("foo error"), ).Times(1) } diff --git a/validator/client/beacon-api/sync_committee.go b/validator/client/beacon-api/sync_committee.go index 5cb65dcfedbe..fac04a0d52ad 100644 --- a/validator/client/beacon-api/sync_committee.go +++ b/validator/client/beacon-api/sync_committee.go @@ -32,26 +32,14 @@ func (c *beaconApiValidatorClient) submitSyncMessage(ctx context.Context, syncMe return errors.Wrap(err, "failed to marshal sync committee message") } - errJson, err := c.jsonRestHandler.Post(ctx, endpoint, nil, bytes.NewBuffer(marshalledJsonSyncCommitteeMessage), nil) - if err != nil { - return errors.Wrap(err, msgUnexpectedError) - } - if errJson != nil { - return errJson - } - - return nil + return c.jsonRestHandler.Post(ctx, endpoint, nil, bytes.NewBuffer(marshalledJsonSyncCommitteeMessage), nil) } func (c *beaconApiValidatorClient) getSyncMessageBlockRoot(ctx context.Context) (*ethpb.SyncMessageBlockRootResponse, error) { // Get head beacon block root. var resp beacon.BlockRootResponse - errJson, err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/blocks/head/root", &resp) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/blocks/head/root", &resp); err != nil { + return nil, err } // An optimistic validator MUST NOT participate in sync committees @@ -97,12 +85,8 @@ func (c *beaconApiValidatorClient) getSyncCommitteeContribution( url := buildURL("/eth/v1/validator/sync_committee_contribution", params) var resp validator.ProduceSyncCommitteeContributionResponse - errJson, err := c.jsonRestHandler.Get(ctx, url, &resp) - if err != nil { - return nil, errors.Wrapf(err, msgUnexpectedError) - } - if errJson != nil { - return nil, errJson + if err = c.jsonRestHandler.Get(ctx, url, &resp); err != nil { + return nil, err } return convertSyncContributionJsonToProto(resp.Data) diff --git a/validator/client/beacon-api/sync_committee_test.go b/validator/client/beacon-api/sync_committee_test.go index 760759e2a1a2..bb8a9e33041b 100644 --- a/validator/client/beacon-api/sync_committee_test.go +++ b/validator/client/beacon-api/sync_committee_test.go @@ -54,7 +54,6 @@ func TestSubmitSyncMessage_Valid(t *testing.T) { nil, ).Return( nil, - nil, ).Times(1) protoSyncCommiteeMessage := ethpb.SyncCommitteeMessage{ @@ -83,7 +82,6 @@ func TestSubmitSyncMessage_BadRequest(t *testing.T) { gomock.Any(), nil, ).Return( - nil, errors.New("foo error"), ).Times(1) @@ -149,7 +147,6 @@ func TestGetSyncMessageBlockRoot(t *testing.T) { 2, test.expectedResponse, ).Return( - nil, test.endpointError, ).Times(1) @@ -225,7 +222,6 @@ func TestGetSyncCommitteeContribution(t *testing.T) { }, ).Return( nil, - nil, ).Times(1) jsonRestHandler.EXPECT().Get( @@ -237,7 +233,6 @@ func TestGetSyncCommitteeContribution(t *testing.T) { 2, test.contribution, ).Return( - nil, test.endpointErr, ).Times(1) @@ -341,7 +336,6 @@ func TestGetSyncSubCommitteeIndex(t *testing.T) { }, }, ).Return( - nil, test.validatorsErr, ).Times(1) @@ -365,7 +359,6 @@ func TestGetSyncSubCommitteeIndex(t *testing.T) { Data: test.duties, }, ).Return( - nil, test.dutiesErr, ).Times(syncDutiesCalled) diff --git a/validator/client/beacon-api/validator_count_test.go b/validator/client/beacon-api/validator_count_test.go index 1db7f503de88..821be792faa4 100644 --- a/validator/client/beacon-api/validator_count_test.go +++ b/validator/client/beacon-api/validator_count_test.go @@ -123,7 +123,6 @@ func TestGetValidatorCount(t *testing.T) { "/eth/v1/node/version", &nodeVersionResponse, ).Return( - nil, test.versionEndpointError, ).SetArg( 2, @@ -136,7 +135,6 @@ func TestGetValidatorCount(t *testing.T) { "/eth/v1/beacon/states/head/validator_count?status=active", &validatorCountResponse, ).Return( - nil, test.validatorCountEndpointError, ).SetArg( 2, diff --git a/validator/client/beacon-api/wait_for_chain_start_test.go b/validator/client/beacon-api/wait_for_chain_start_test.go index 9772e16d5339..31dc341b8fd9 100644 --- a/validator/client/beacon-api/wait_for_chain_start_test.go +++ b/validator/client/beacon-api/wait_for_chain_start_test.go @@ -29,7 +29,6 @@ func TestWaitForChainStart_ValidGenesis(t *testing.T) { &genesisResponseJson, ).Return( nil, - nil, ).SetArg( 2, beacon.GetGenesisResponse{ @@ -97,7 +96,6 @@ func TestWaitForChainStart_BadGenesis(t *testing.T) { &genesisResponseJson, ).Return( nil, - nil, ).SetArg( 2, beacon.GetGenesisResponse{ @@ -125,7 +123,6 @@ func TestWaitForChainStart_JsonResponseError(t *testing.T) { "/eth/v1/beacon/genesis", &genesisResponseJson, ).Return( - nil, errors.New("some specific json error"), ).Times(1) @@ -155,7 +152,6 @@ func TestWaitForChainStart_JsonResponseError404(t *testing.T) { Code: http.StatusNotFound, Message: "404 error", }, - errors.New("404 error"), ).Times(1) // After receiving a 404 error, mock a request that actually has genesis data available @@ -165,7 +161,6 @@ func TestWaitForChainStart_JsonResponseError404(t *testing.T) { &genesisResponseJson, ).Return( nil, - nil, ).SetArg( 2, beacon.GetGenesisResponse{