Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solve parsing autorest voluntary exit response #10

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Loading