mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
Add REST implementation for Validator's PrepareBeaconProposer
(#11784)
* Add REST implementation for Validator's PrepareBeaconProposer * Change fee recipients * Handle error in a better way * Move endpoint into constant Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
parent
81b049ea02
commit
4eb4cd9756
@ -14,6 +14,7 @@ go_library(
|
||||
"get_beacon_block.go",
|
||||
"index.go",
|
||||
"json_rest_handler.go",
|
||||
"prepare_beacon_proposer.go",
|
||||
"propose_beacon_block.go",
|
||||
"state_validators.go",
|
||||
"status.go",
|
||||
@ -56,6 +57,7 @@ go_test(
|
||||
"get_beacon_block_test.go",
|
||||
"index_test.go",
|
||||
"json_rest_handler_test.go",
|
||||
"prepare_beacon_proposer_test.go",
|
||||
"propose_beacon_block_altair_test.go",
|
||||
"propose_beacon_block_bellatrix_test.go",
|
||||
"propose_beacon_block_blinded_bellatrix_test.go",
|
||||
|
@ -120,13 +120,8 @@ func (c *beaconApiValidatorClient) MultipleValidatorStatus(ctx context.Context,
|
||||
panic("beaconApiValidatorClient.MultipleValidatorStatus is not implemented. To use a fallback client, create this validator with NewBeaconApiValidatorClientWithFallback instead.")
|
||||
}
|
||||
|
||||
func (c *beaconApiValidatorClient) PrepareBeaconProposer(ctx context.Context, in *ethpb.PrepareBeaconProposerRequest) (*empty.Empty, error) {
|
||||
if c.fallbackClient != nil {
|
||||
return c.fallbackClient.PrepareBeaconProposer(ctx, in)
|
||||
}
|
||||
|
||||
// TODO: Implement me
|
||||
panic("beaconApiValidatorClient.PrepareBeaconProposer is not implemented. To use a fallback client, create this validator with NewBeaconApiValidatorClientWithFallback instead.")
|
||||
func (c *beaconApiValidatorClient) PrepareBeaconProposer(_ context.Context, in *ethpb.PrepareBeaconProposerRequest) (*empty.Empty, error) {
|
||||
return new(empty.Empty), c.prepareBeaconProposer(in.Recipients)
|
||||
}
|
||||
|
||||
func (c *beaconApiValidatorClient) ProposeAttestation(ctx context.Context, in *ethpb.Attestation) (*ethpb.AttestResponse, error) {
|
||||
|
33
validator/client/beacon-api/prepare_beacon_proposer.go
Normal file
33
validator/client/beacon-api/prepare_beacon_proposer.go
Normal file
@ -0,0 +1,33 @@
|
||||
package beacon_api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func (c *beaconApiValidatorClient) prepareBeaconProposer(recipients []*ethpb.PrepareBeaconProposerRequest_FeeRecipientContainer) error {
|
||||
jsonRecipients := make([]*apimiddleware.FeeRecipientJson, len(recipients))
|
||||
for index, recipient := range recipients {
|
||||
jsonRecipients[index] = &apimiddleware.FeeRecipientJson{
|
||||
FeeRecipient: hexutil.Encode(recipient.FeeRecipient),
|
||||
ValidatorIndex: strconv.FormatUint(uint64(recipient.ValidatorIndex), 10),
|
||||
}
|
||||
}
|
||||
|
||||
marshalledJsonRecipients, err := json.Marshal(jsonRecipients)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to marshal recipients")
|
||||
}
|
||||
|
||||
if _, err := c.jsonRestHandler.PostRestJson("/eth/v1/validator/prepare_beacon_proposer", nil, bytes.NewBuffer(marshalledJsonRecipients), nil); err != nil {
|
||||
return errors.Wrap(err, "failed to send POST data to REST endpoint")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
103
validator/client/beacon-api/prepare_beacon_proposer_test.go
Normal file
103
validator/client/beacon-api/prepare_beacon_proposer_test.go
Normal file
@ -0,0 +1,103 @@
|
||||
package beacon_api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
||||
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
||||
"github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api/mock"
|
||||
)
|
||||
|
||||
const prepareBeaconProposerTestEndpoint = "/eth/v1/validator/prepare_beacon_proposer"
|
||||
|
||||
func TestPrepareBeaconProposer_Valid(t *testing.T) {
|
||||
const feeRecipient1 = "0xca008b199c03a2a2f6bc2ed52d6404c4d8510b35"
|
||||
const feeRecipient2 = "0x8145d80111309e4621ed7632319664ac440b0198"
|
||||
const feeRecipient3 = "0x085f2adb1295821838910be402b3c8cdc118bd86"
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
jsonRecipients := []*apimiddleware.FeeRecipientJson{
|
||||
{
|
||||
ValidatorIndex: "1",
|
||||
FeeRecipient: feeRecipient1,
|
||||
},
|
||||
{
|
||||
ValidatorIndex: "2",
|
||||
FeeRecipient: feeRecipient2,
|
||||
},
|
||||
{
|
||||
ValidatorIndex: "3",
|
||||
FeeRecipient: feeRecipient3,
|
||||
},
|
||||
}
|
||||
|
||||
marshalledJsonRecipients, err := json.Marshal(jsonRecipients)
|
||||
require.NoError(t, err)
|
||||
|
||||
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
||||
jsonRestHandler.EXPECT().PostRestJson(
|
||||
prepareBeaconProposerTestEndpoint,
|
||||
nil,
|
||||
bytes.NewBuffer(marshalledJsonRecipients),
|
||||
nil,
|
||||
).Return(
|
||||
nil,
|
||||
nil,
|
||||
).Times(1)
|
||||
|
||||
decodedFeeRecipient1, err := hexutil.Decode(feeRecipient1)
|
||||
require.NoError(t, err)
|
||||
decodedFeeRecipient2, err := hexutil.Decode(feeRecipient2)
|
||||
require.NoError(t, err)
|
||||
decodedFeeRecipient3, err := hexutil.Decode(feeRecipient3)
|
||||
require.NoError(t, err)
|
||||
|
||||
protoRecipients := []*ethpb.PrepareBeaconProposerRequest_FeeRecipientContainer{
|
||||
{
|
||||
ValidatorIndex: 1,
|
||||
FeeRecipient: decodedFeeRecipient1,
|
||||
},
|
||||
{
|
||||
ValidatorIndex: 2,
|
||||
FeeRecipient: decodedFeeRecipient2,
|
||||
},
|
||||
{
|
||||
ValidatorIndex: 3,
|
||||
FeeRecipient: decodedFeeRecipient3,
|
||||
},
|
||||
}
|
||||
|
||||
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
|
||||
err = validatorClient.prepareBeaconProposer(protoRecipients)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestPrepareBeaconProposer_BadRequest(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
||||
jsonRestHandler.EXPECT().PostRestJson(
|
||||
prepareBeaconProposerTestEndpoint,
|
||||
nil,
|
||||
gomock.Any(),
|
||||
nil,
|
||||
).Return(
|
||||
nil,
|
||||
errors.New("foo error"),
|
||||
).Times(1)
|
||||
|
||||
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
|
||||
err := validatorClient.prepareBeaconProposer(nil)
|
||||
assert.ErrorContains(t, "failed to send POST data to REST endpoint", err)
|
||||
assert.ErrorContains(t, "foo error", err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user