Skip to content

Commit

Permalink
fix: solve parsing autorest voluntary exit response (#10)
Browse files Browse the repository at this point in the history
* fix: solve parsing autorest voluntary exit response

* fix: solve tests

* fix: solve linter

* fix: solve linter
  • Loading branch information
javip97 authored Aug 1, 2024
1 parent d4d7e34 commit 3e505c9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
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)
}

0 comments on commit 3e505c9

Please sign in to comment.