From 5c7f2593e19bd05f6fe2a1a867457c52af869a15 Mon Sep 17 00:00:00 2001 From: javip97 Date: Thu, 1 Aug 2024 09:45:27 +0200 Subject: [PATCH 1/4] fix: solve parsing autorest voluntary exit response --- ethereum/consensus/client/client.go | 3 +- ...ist.go => submit_signed_voluntary_exit.go} | 23 ++++++---- .../http/submit_signed_voluntary_exit_test.go | 42 +++++++++++++++++++ ethereum/consensus/client/mock/client.go | 9 ++-- 4 files changed, 63 insertions(+), 14 deletions(-) rename ethereum/consensus/client/http/{submit_signed_voluntary_exist.go => submit_signed_voluntary_exit.go} (72%) create mode 100644 ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go diff --git a/ethereum/consensus/client/client.go b/ethereum/consensus/client/client.go index e90d31a..a3b30bf 100644 --- a/ethereum/consensus/client/client.go +++ b/ethereum/consensus/client/client.go @@ -3,6 +3,7 @@ package client import ( "context" + eth2http "github.com/kilnfi/go-utils/ethereum/consensus/client/http" "github.com/kilnfi/go-utils/ethereum/consensus/types" "github.com/protolambda/zrnt/eth2/beacon/bellatrix" @@ -102,7 +103,7 @@ type BeaconClient interface { GetVoluntaryExits(ctx context.Context) (beaconphase0.VoluntaryExits, error) // SubmitSignedVoluntaryExit submits a signed voluntary exit to the beacon node. - SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (string, error) + SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (*eth2http.SubmitSignedVoluntaryExitResponse, error) } type NodeClient interface { 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..72b68f2 100644 --- a/ethereum/consensus/client/http/submit_signed_voluntary_exist.go +++ b/ethereum/consensus/client/http/submit_signed_voluntary_exit.go @@ -20,7 +20,7 @@ type message struct { } // SubmitSignedVoluntaryExit submits a signed voluntary exit to the beacon node. -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) { resp, err := c.submitSignedVoluntaryExit(ctx, epoch, validatorIdx, signature) if err != nil { c.logger.WithError(err).Errorf("SubmitSignedVoluntaryExit failed") @@ -29,21 +29,21 @@ func (c *Client) SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcomm return resp, err } -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..fc3767b --- /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 + errBody = []byte(fmt.Sprintf(`{"code": %d,"message": "%s"}`, errStatusCode, errMsg)) + respError = &http.Response{StatusCode: errStatusCode, Body: io.NopCloser(bytes.NewReader(errBody)), 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 + okBody = []byte(fmt.Sprintf(`{"code": %d,"message": "%s"}`, okStatusCode, okMsg)) + ) + + respOK := &http.Response{StatusCode: okStatusCode, Body: io.NopCloser(bytes.NewReader(okBody)), 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) +} diff --git a/ethereum/consensus/client/mock/client.go b/ethereum/consensus/client/mock/client.go index d1e722b..e50ea6a 100644 --- a/ethereum/consensus/client/mock/client.go +++ b/ethereum/consensus/client/mock/client.go @@ -9,6 +9,7 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" + eth2http "github.com/kilnfi/go-utils/ethereum/consensus/client/http" types "github.com/kilnfi/go-utils/ethereum/consensus/types" bellatrix "github.com/protolambda/zrnt/eth2/beacon/bellatrix" common "github.com/protolambda/zrnt/eth2/beacon/common" @@ -339,10 +340,10 @@ func (mr *MockClientMockRecorder) GetVoluntaryExits(ctx interface{}) *gomock.Cal } // SubmitSignedVoluntaryExit mocks base method. -func (m *MockClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (string, error) { +func (m *MockClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (*eth2http.SubmitSignedVoluntaryExitResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSignedVoluntaryExit", ctx, epoch, validatorIdx, signature) - ret0, _ := ret[0].(string) + ret0, _ := ret[0].(*eth2http.SubmitSignedVoluntaryExitResponse) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -647,10 +648,10 @@ func (mr *MockBeaconClientMockRecorder) GetVoluntaryExits(ctx interface{}) *gomo } // SubmitSignedVoluntaryExit mocks base method. -func (m *MockBeaconClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (string, error) { +func (m *MockBeaconClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (*eth2http.SubmitSignedVoluntaryExitResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSignedVoluntaryExit", ctx, epoch, validatorIdx, signature) - ret0, _ := ret[0].(string) + ret0, _ := ret[0].(*eth2http.SubmitSignedVoluntaryExitResponse) ret1, _ := ret[1].(error) return ret0, ret1 } From 20d32e1fb1fe6fdf38030570e5c4590e58a7ebae Mon Sep 17 00:00:00 2001 From: javip97 Date: Thu, 1 Aug 2024 09:54:47 +0200 Subject: [PATCH 2/4] fix: solve tests --- ethereum/consensus/client/client.go | 3 +-- .../client/http/submit_signed_voluntary_exit.go | 6 +++--- ethereum/consensus/client/mock/client.go | 9 ++++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/ethereum/consensus/client/client.go b/ethereum/consensus/client/client.go index a3b30bf..e90d31a 100644 --- a/ethereum/consensus/client/client.go +++ b/ethereum/consensus/client/client.go @@ -3,7 +3,6 @@ package client import ( "context" - eth2http "github.com/kilnfi/go-utils/ethereum/consensus/client/http" "github.com/kilnfi/go-utils/ethereum/consensus/types" "github.com/protolambda/zrnt/eth2/beacon/bellatrix" @@ -103,7 +102,7 @@ type BeaconClient interface { GetVoluntaryExits(ctx context.Context) (beaconphase0.VoluntaryExits, error) // SubmitSignedVoluntaryExit submits a signed voluntary exit to the beacon node. - SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (*eth2http.SubmitSignedVoluntaryExitResponse, error) + SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (string, error) } type NodeClient interface { diff --git a/ethereum/consensus/client/http/submit_signed_voluntary_exit.go b/ethereum/consensus/client/http/submit_signed_voluntary_exit.go index 72b68f2..adb0ebc 100644 --- a/ethereum/consensus/client/http/submit_signed_voluntary_exit.go +++ b/ethereum/consensus/client/http/submit_signed_voluntary_exit.go @@ -20,13 +20,13 @@ type message struct { } // SubmitSignedVoluntaryExit submits a signed voluntary exit to the beacon node. -func (c *Client) SubmitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (*SubmitSignedVoluntaryExitResponse, error) { +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) (*SubmitSignedVoluntaryExitResponse, error) { diff --git a/ethereum/consensus/client/mock/client.go b/ethereum/consensus/client/mock/client.go index e50ea6a..d1e722b 100644 --- a/ethereum/consensus/client/mock/client.go +++ b/ethereum/consensus/client/mock/client.go @@ -9,7 +9,6 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" - eth2http "github.com/kilnfi/go-utils/ethereum/consensus/client/http" types "github.com/kilnfi/go-utils/ethereum/consensus/types" bellatrix "github.com/protolambda/zrnt/eth2/beacon/bellatrix" common "github.com/protolambda/zrnt/eth2/beacon/common" @@ -340,10 +339,10 @@ func (mr *MockClientMockRecorder) GetVoluntaryExits(ctx interface{}) *gomock.Cal } // SubmitSignedVoluntaryExit mocks base method. -func (m *MockClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (*eth2http.SubmitSignedVoluntaryExitResponse, error) { +func (m *MockClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSignedVoluntaryExit", ctx, epoch, validatorIdx, signature) - ret0, _ := ret[0].(*eth2http.SubmitSignedVoluntaryExitResponse) + ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -648,10 +647,10 @@ func (mr *MockBeaconClientMockRecorder) GetVoluntaryExits(ctx interface{}) *gomo } // SubmitSignedVoluntaryExit mocks base method. -func (m *MockBeaconClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (*eth2http.SubmitSignedVoluntaryExitResponse, error) { +func (m *MockBeaconClient) SubmitSignedVoluntaryExit(ctx context.Context, epoch common.Epoch, validatorIdx uint64, signature string) (string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SubmitSignedVoluntaryExit", ctx, epoch, validatorIdx, signature) - ret0, _ := ret[0].(*eth2http.SubmitSignedVoluntaryExitResponse) + ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } From df5cf311795c60aa9a233ac1428db91c87237d88 Mon Sep 17 00:00:00 2001 From: javip97 Date: Thu, 1 Aug 2024 09:59:02 +0200 Subject: [PATCH 3/4] fix: solve linter --- .../client/http/submit_signed_voluntary_exit_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go b/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go index fc3767b..0585b81 100644 --- a/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go +++ b/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go @@ -18,8 +18,8 @@ func TestSubmitSignedVoluntaryExit(t *testing.T) { var ( errMsg = "Invalid voluntary exit, it will never pass validation so it's rejected" errStatusCode = http.StatusBadRequest - errBody = []byte(fmt.Sprintf(`{"code": %d,"message": "%s"}`, errStatusCode, errMsg)) - respError = &http.Response{StatusCode: errStatusCode, Body: io.NopCloser(bytes.NewReader(errBody)), Request: &http.Request{Method: http.MethodPost, URL: u}} + errBodyMsg = fmt.Sprintf(`{"code": %d,"message": "%s"}`, 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) @@ -30,10 +30,10 @@ func TestSubmitSignedVoluntaryExit(t *testing.T) { var ( okMsg = "all right" okStatusCode = http.StatusOK - okBody = []byte(fmt.Sprintf(`{"code": %d,"message": "%s"}`, okStatusCode, okMsg)) + okBodyMsg = fmt.Sprintf(`{"code": %d,"message": "%s"}`, okStatusCode, okMsg) ) - respOK := &http.Response{StatusCode: okStatusCode, Body: io.NopCloser(bytes.NewReader(okBody)), Request: &http.Request{Method: http.MethodPost, URL: u}} + 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) From f5148506c4e7ab55e0a450d2c08cfa30578bcae8 Mon Sep 17 00:00:00 2001 From: javip97 Date: Thu, 1 Aug 2024 10:01:58 +0200 Subject: [PATCH 4/4] fix: solve linter --- .../client/http/submit_signed_voluntary_exit_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go b/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go index 0585b81..9d2a98b 100644 --- a/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go +++ b/ethereum/consensus/client/http/submit_signed_voluntary_exit_test.go @@ -18,7 +18,7 @@ func TestSubmitSignedVoluntaryExit(t *testing.T) { var ( errMsg = "Invalid voluntary exit, it will never pass validation so it's rejected" errStatusCode = http.StatusBadRequest - errBodyMsg = fmt.Sprintf(`{"code": %d,"message": "%s"}`, errStatusCode, errMsg) + 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}} ) @@ -30,7 +30,7 @@ func TestSubmitSignedVoluntaryExit(t *testing.T) { var ( okMsg = "all right" okStatusCode = http.StatusOK - okBodyMsg = fmt.Sprintf(`{"code": %d,"message": "%s"}`, okStatusCode, okMsg) + 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}}