Skip to content

Commit

Permalink
feat: adding submit signed voluntary exit method (#6)
Browse files Browse the repository at this point in the history
* feat: adding submit signed voluntary exit method

* chore: renaming function

* chore: update mocks
  • Loading branch information
javip97 committed Jun 27, 2024
1 parent f9d72dd commit 62a9440
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ethereum/consensus/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type BeaconClient interface {

// GetVoluntaryExits returns voluntary exits known by the node but not necessarily incorporated into any block.
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)
}

type NodeClient interface {
Expand Down
79 changes: 79 additions & 0 deletions ethereum/consensus/client/http/submit_signed_voluntary_exist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package eth2http

import (
"context"
"net/http"
"strconv"

"github.com/Azure/go-autorest/autorest"
beaconcommon "github.com/protolambda/zrnt/eth2/beacon/common"
)

type signedVoluntaryExit struct {
Message message `json:"message"`
Signature string `json:"signature"`
}

type message struct {
Epoch beaconcommon.Epoch `json:"epoch"`
ValidatorIndex string `json:"validator_index"`
}

// 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) {
resp, err := c.submitSignedVoluntaryExit(ctx, epoch, validatorIdx, signature)
if err != nil {
c.logger.WithError(err).Errorf("SubmitSignedVoluntaryExit failed")
}

return resp, err
}

func (c *Client) submitSignedVoluntaryExit(ctx context.Context, epoch beaconcommon.Epoch, validatorIdx uint64, signature string) (string, 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")
}

resp, err := c.client.Do(req)
if err != nil {
return "", 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 ve, nil
}

func newSignedVoluntaryExitsRequest(ctx context.Context, signedVoluntaryExit *signedVoluntaryExit) (*http.Request, error) {
return autorest.CreatePreparer(
autorest.AsPost(),
autorest.AsJSON(),
autorest.WithJSON(signedVoluntaryExit),
autorest.WithPath("/eth/v1/beacon/pool/voluntary_exits"),
).Prepare(newRequest(ctx))
}

func newSignedVoluntaryExit(epoch beaconcommon.Epoch, validatorIdx uint64, signature string) *signedVoluntaryExit {
return &signedVoluntaryExit{
Message: message{
Epoch: epoch,
ValidatorIndex: strconv.Itoa(int(validatorIdx)),
},
Signature: signature,
}
}

func inspectSubmitSignedVoluntaryExitResponse(resp *http.Response) (string, error) {
var msg string
err := inspectResponse(resp, &msg)
if err != nil {
return "", err
}

return msg, nil
}
30 changes: 30 additions & 0 deletions ethereum/consensus/client/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62a9440

Please sign in to comment.