Add REST implementation for SubmitValidatorRegistrations (#11816)

* Add REST implementation for `SubmitValidatorRegistrations`

* Remove unused context

* Use context

Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
This commit is contained in:
Manu NALEPA 2023-01-06 20:46:09 +01:00 committed by GitHub
parent 1c27b21b5a
commit 4c9a0bf772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 210 additions and 6 deletions

View File

@ -17,6 +17,7 @@ go_library(
"prepare_beacon_proposer.go",
"propose_beacon_block.go",
"propose_exit.go",
"registration.go",
"state_validators.go",
"status.go",
],
@ -66,6 +67,7 @@ go_test(
"propose_beacon_block_phase0_test.go",
"propose_beacon_block_test.go",
"propose_exit_test.go",
"registration_test.go",
"state_validators_test.go",
"status_test.go",
"wait_for_chain_start_test.go",
@ -86,6 +88,7 @@ go_test(
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@org_golang_google_protobuf//types/known/emptypb:go_default_library",
],
)

View File

@ -193,12 +193,7 @@ func (c *beaconApiValidatorClient) SubmitSyncMessage(ctx context.Context, in *et
}
func (c *beaconApiValidatorClient) SubmitValidatorRegistrations(ctx context.Context, in *ethpb.SignedValidatorRegistrationsV1) (*empty.Empty, error) {
if c.fallbackClient != nil {
return c.fallbackClient.SubmitValidatorRegistrations(ctx, in)
}
// TODO: Implement me
panic("beaconApiValidatorClient.SubmitValidatorRegistrations is not implemented. To use a fallback client, create this validator with NewBeaconApiValidatorClientWithFallback instead.")
return new(empty.Empty), c.submitValidatorRegistrations(ctx, in.Messages)
}
func (c *beaconApiValidatorClient) SubscribeCommitteeSubnets(ctx context.Context, in *ethpb.CommitteeSubnetsSubscribeRequest) (*empty.Empty, error) {

View File

@ -0,0 +1,45 @@
package beacon_api
import (
"bytes"
"context"
"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) submitValidatorRegistrations(ctx context.Context, registrations []*ethpb.SignedValidatorRegistrationV1) error {
const endpoint = "/eth/v1/validator/register_validator"
jsonRegistration := make([]*apimiddleware.SignedValidatorRegistrationJson, len(registrations))
for index, registration := range registrations {
inMessage := registration.Message
outMessage := &apimiddleware.ValidatorRegistrationJson{
FeeRecipient: hexutil.Encode(inMessage.FeeRecipient),
GasLimit: strconv.FormatUint(inMessage.GasLimit, 10),
Timestamp: strconv.FormatUint(inMessage.Timestamp, 10),
Pubkey: hexutil.Encode(inMessage.Pubkey),
}
jsonRegistration[index] = &apimiddleware.SignedValidatorRegistrationJson{
Message: outMessage,
Signature: hexutil.Encode(registration.Signature),
}
}
marshalledJsonRegistration, err := json.Marshal(jsonRegistration)
if err != nil {
return errors.Wrap(err, "failed to marshal registration")
}
if _, err := c.jsonRestHandler.PostRestJson(ctx, endpoint, nil, bytes.NewBuffer(marshalledJsonRegistration), nil); err != nil {
return errors.Wrapf(err, "failed to send POST data to `%s` REST endpoint", endpoint)
}
return nil
}

View File

@ -0,0 +1,161 @@
package beacon_api
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes/empty"
"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"
)
func TestRegistration_Valid(t *testing.T) {
const feeRecipient1 = "0xca008b199c03a2a2f6bc2ed52d6404c4d8510b35"
const pubKey1 = "0x8000091c2ae64ee414a54c1cc1fc67dec663408bc636cb86756e0200e41a75c8f86603f104f02c856983d2783116be13"
const signature1 = "0xb459ef852bd4e0cb96e6723d67cacc8215406dd9ba663f8874a083167ebf428b28b746431bdbc1820a25289377b2610881e52b3a05c3548c5e99c08c8a36342573be5962d7510c03dcba8ddfb8ae419e59d222ddcf31cc512e704ef2cc3cf8"
const feeRecipient2 = "0x8145d80111309e4621ed7632319664ac440b0198"
const pubKey2 = "0x800015473bdc3a7f45ef8eb8abc598bc20021e55ad6e6ad1d745aaef9730dd2c28ec08bf42df18451de94dd4a6d24ec5"
const signature2 = "0xc459ef852bd4e0cb96e6723d67cacc8215406dd9ba663f8874a083167ebf428b28b746431bdbc1820a25289377b2610881e52b3a05c3548c5e99c08c8a36342573be5962d7510c03dcba8ddfb8ae419e59d222ddcf31cc512e704ef2cc3cf8"
const feeRecipient3 = "0x085f2adb1295821838910be402b3c8cdc118bd86"
const pubKey3 = "0x80006dbd87090ce8d611ffb8d2c700901d7a07a73607e18c6dc5ff39e44dae317816387b61fa9008de3cbe07583c0358"
const signature3 = "0xd459ef852bd4e0cb96e6723d67cacc8215406dd9ba663f8874a083167ebf428b28b746431bdbc1820a25289377b2610881e52b3a05c3548c5e99c08c8a36342573be5962d7510c03dcba8ddfb8ae419e59d222ddcf31cc512e704ef2cc3cf8"
ctrl := gomock.NewController(t)
defer ctrl.Finish()
jsonRegistrations := []*apimiddleware.SignedValidatorRegistrationJson{
{
Message: &apimiddleware.ValidatorRegistrationJson{
FeeRecipient: feeRecipient1,
GasLimit: "100",
Timestamp: "1000",
Pubkey: pubKey1,
},
Signature: signature1,
},
{
Message: &apimiddleware.ValidatorRegistrationJson{
FeeRecipient: feeRecipient2,
GasLimit: "200",
Timestamp: "2000",
Pubkey: pubKey2,
},
Signature: signature2,
},
{
Message: &apimiddleware.ValidatorRegistrationJson{
FeeRecipient: feeRecipient3,
GasLimit: "300",
Timestamp: "3000",
Pubkey: pubKey3,
},
Signature: signature3,
},
}
marshalledJsonRegistrations, err := json.Marshal(jsonRegistrations)
require.NoError(t, err)
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
jsonRestHandler.EXPECT().PostRestJson(
context.Background(),
"/eth/v1/validator/register_validator",
nil,
bytes.NewBuffer(marshalledJsonRegistrations),
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)
decodedPubkey1, err := hexutil.Decode(pubKey1)
require.NoError(t, err)
decodedPubkey2, err := hexutil.Decode(pubKey2)
require.NoError(t, err)
decodedPubkey3, err := hexutil.Decode(pubKey3)
require.NoError(t, err)
decodedSignature1, err := hexutil.Decode(signature1)
require.NoError(t, err)
decodedSignature2, err := hexutil.Decode(signature2)
require.NoError(t, err)
decodedSignature3, err := hexutil.Decode(signature3)
require.NoError(t, err)
protoRegistrations := ethpb.SignedValidatorRegistrationsV1{
Messages: []*ethpb.SignedValidatorRegistrationV1{
{
Message: &ethpb.ValidatorRegistrationV1{
FeeRecipient: decodedFeeRecipient1,
GasLimit: 100,
Timestamp: 1000,
Pubkey: decodedPubkey1,
},
Signature: decodedSignature1,
},
{
Message: &ethpb.ValidatorRegistrationV1{
FeeRecipient: decodedFeeRecipient2,
GasLimit: 200,
Timestamp: 2000,
Pubkey: decodedPubkey2,
},
Signature: decodedSignature2,
},
{
Message: &ethpb.ValidatorRegistrationV1{
FeeRecipient: decodedFeeRecipient3,
GasLimit: 300,
Timestamp: 3000,
Pubkey: decodedPubkey3,
},
Signature: decodedSignature3,
},
},
}
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
res, err := validatorClient.SubmitValidatorRegistrations(context.Background(), &protoRegistrations)
assert.DeepEqual(t, new(empty.Empty), res)
require.NoError(t, err)
}
func TestRegistration_BadRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
jsonRestHandler.EXPECT().PostRestJson(
context.Background(),
"/eth/v1/validator/register_validator",
nil,
gomock.Any(),
nil,
).Return(
nil,
errors.New("foo error"),
).Times(1)
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
_, err := validatorClient.SubmitValidatorRegistrations(context.Background(), &ethpb.SignedValidatorRegistrationsV1{})
assert.ErrorContains(t, "failed to send POST data to `/eth/v1/validator/register_validator` REST endpoint", err)
assert.ErrorContains(t, "foo error", err)
}