diff --git a/ethereum/consensus/client/http/submit_signed_voluntary_exist.go b/ethereum/consensus/client/http/submit_signed_voluntary_exit.go similarity index 72% rename from ethereum/consensus/client/http/submit_signed_voluntary_exist.go rename to ethereum/consensus/client/http/submit_signed_voluntary_exit.go index ec9a6d0..adb0ebc 100644 --- a/ethereum/consensus/client/http/submit_signed_voluntary_exist.go +++ b/ethereum/consensus/client/http/submit_signed_voluntary_exit.go @@ -23,27 +23,27 @@ type message struct { func (c *Client) SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (string, error) { resp, err := c.submitSignedVoluntaryExit(ctx, epoch, validatorIdx, signature) if err != nil { - c.logger.WithError(err).Errorf("SubmitSignedVoluntaryExit failed") + return "", err } - return resp, err + return resp.Message, nil } -func (c *Client) submitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (string, error) { +func (c *Client) submitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (*SubmitSignedVoluntaryExitResponse, error) { reqBody := newSignedVoluntaryExit(epoch, validatorIdx, signature) req, err := newSignedVoluntaryExitsRequest(ctx, reqBody) if err != nil { - return "", autorest.NewErrorWithError(err, "eth2http.Client", "SubmitSignedVoluntaryExit", nil, "Failure preparing request") + return nil, autorest.NewErrorWithError(err, "eth2http.Client", "SubmitSignedVoluntaryExit", nil, "Failure preparing request") } resp, err := c.client.Do(req) if err != nil { - return "", autorest.NewErrorWithError(err, "eth2http.Client", "SubmitSignedVoluntaryExit", resp, "Failure sending request") + return nil, autorest.NewErrorWithError(err, "eth2http.Client", "SubmitSignedVoluntaryExit", resp, "Failure sending request") } ve, err := inspectSubmitSignedVoluntaryExitResponse(resp) if err != nil { - return "", autorest.NewErrorWithError(err, "eth2http.Client", "SubmitSignedVoluntaryExit", resp, "Invalid response") + return nil, autorest.NewErrorWithError(err, "eth2http.Client", "SubmitSignedVoluntaryExit", resp, "Invalid response") } return ve, nil @@ -68,11 +68,16 @@ func newSignedVoluntaryExit(epoch beaconcommon.Epoch, validatorIdx uint64, signa } } -func inspectSubmitSignedVoluntaryExitResponse(resp *http.Response) (string, error) { - var msg string - err := inspectResponse(resp, &msg) +type SubmitSignedVoluntaryExitResponse struct { + Code int `json:"code"` + Message string `json:"message"` +} + +func inspectSubmitSignedVoluntaryExitResponse(resp *http.Response) (*SubmitSignedVoluntaryExitResponse, error) { + msg := new(SubmitSignedVoluntaryExitResponse) + err := inspectResponse(resp, msg) if err != nil { - return "", err + return nil, err } return msg, nil diff --git a/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go b/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go new file mode 100644 index 0000000..9d2a98b --- /dev/null +++ b/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go @@ -0,0 +1,42 @@ +package eth2http + +import ( + "bytes" + "fmt" + "io" + "net/http" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSubmitSignedVoluntaryExit(t *testing.T) { + u, err := url.Parse("http://localhost") + require.NoError(t, err) + var ( + errMsg = "Invalid voluntary exit, it will never pass validation so it's rejected" + errStatusCode = http.StatusBadRequest + errBodyMsg = fmt.Sprintf(`{"code": %d,"message": %q}`, errStatusCode, errMsg) + respError = &http.Response{StatusCode: errStatusCode, Body: io.NopCloser(bytes.NewReader([]byte(errBodyMsg))), Request: &http.Request{Method: http.MethodPost, URL: u}} + ) + + respMsg, err := inspectSubmitSignedVoluntaryExitResponse(respError) + require.Error(t, err) + require.Contains(t, err.Error(), errMsg) + require.Nil(t, respMsg) + + var ( + okMsg = "all right" + okStatusCode = http.StatusOK + okBodyMsg = fmt.Sprintf(`{"code": %d,"message": %q}`, okStatusCode, okMsg) + ) + + respOK := &http.Response{StatusCode: okStatusCode, Body: io.NopCloser(bytes.NewReader([]byte(okBodyMsg))), Request: &http.Request{Method: http.MethodPost, URL: u}} + respMsg, err = inspectSubmitSignedVoluntaryExitResponse(respOK) + require.NoError(t, err) + require.NotNil(t, respMsg) + assert.Equal(t, okStatusCode, respMsg.Code) + assert.Equal(t, okMsg, respMsg.Message) +}