package client import ( "context" "testing" "time" "github.com/golang/mock/gomock" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/config/params" "github.com/prysmaticlabs/prysm/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/testing/require" "google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/timestamppb" ) func TestSubmitValidatorRegistration(t *testing.T) { _, m, validatorKey, finish := setup(t) defer finish() ctx := context.Background() reg := ðpb.ValidatorRegistrationV1{ FeeRecipient: bytesutil.PadTo([]byte("fee"), 20), GasLimit: 123456, Timestamp: uint64(time.Now().Unix()), Pubkey: validatorKey.PublicKey().Marshal(), } ti := ×tamppb.Timestamp{} m.nodeClient.EXPECT(). GetGenesis(gomock.Any(), &emptypb.Empty{}). Return(ðpb.Genesis{GenesisTime: ti}, nil) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx ðpb.DomainRequest{Domain: params.BeaconConfig().DomainApplicationBuilder[:]}, ).Times(1).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT(). SubmitValidatorRegistration(gomock.Any(), ðpb.SignedValidatorRegistrationV1{ Message: reg, Signature: params.BeaconConfig().ZeroHash[:], }). Return(nil, nil) require.NoError(t, nil, SubmitValidatorRegistration(ctx, m.validatorClient, m.nodeClient, m.signfunc, reg)) } func TestSubmitValidatorRegistration_InvalidDomain(t *testing.T) { _, m, validatorKey, finish := setup(t) defer finish() ctx := context.Background() reg := ðpb.ValidatorRegistrationV1{ FeeRecipient: bytesutil.PadTo([]byte("fee"), 20), GasLimit: 123456, Timestamp: uint64(time.Now().Unix()), Pubkey: validatorKey.PublicKey().Marshal(), } genesisTime := ×tamppb.Timestamp{} m.nodeClient.EXPECT(). GetGenesis(gomock.Any(), &emptypb.Empty{}). Return(ðpb.Genesis{GenesisTime: genesisTime}, nil) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx ðpb.DomainRequest{Domain: params.BeaconConfig().DomainApplicationBuilder[:]}, ).Times(1).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, errors.New(domainDataErr)) require.ErrorContains(t, domainDataErr, SubmitValidatorRegistration(ctx, m.validatorClient, m.nodeClient, m.signfunc, reg)) } func TestSubmitValidatorRegistration_CantSign(t *testing.T) { _, m, validatorKey, finish := setup(t) defer finish() ctx := context.Background() reg := ðpb.ValidatorRegistrationV1{ FeeRecipient: bytesutil.PadTo([]byte("fee"), 20), GasLimit: 123456, Timestamp: uint64(time.Now().Unix()), Pubkey: validatorKey.PublicKey().Marshal(), } genesisTime := ×tamppb.Timestamp{} m.nodeClient.EXPECT(). GetGenesis(gomock.Any(), &emptypb.Empty{}). Return(ðpb.Genesis{GenesisTime: genesisTime}, nil) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx ðpb.DomainRequest{Domain: params.BeaconConfig().DomainApplicationBuilder[:]}, ).Times(1).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) m.validatorClient.EXPECT(). SubmitValidatorRegistration(gomock.Any(), ðpb.SignedValidatorRegistrationV1{ Message: reg, Signature: params.BeaconConfig().ZeroHash[:], }). Return(nil, errors.New("could not sign")) require.ErrorContains(t, "could not submit signed registration to beacon node", SubmitValidatorRegistration(ctx, m.validatorClient, m.nodeClient, m.signfunc, reg)) } func Test_signValidatorRegistration(t *testing.T) { _, m, validatorKey, finish := setup(t) defer finish() ctx := context.Background() reg := ðpb.ValidatorRegistrationV1{ FeeRecipient: bytesutil.PadTo([]byte("fee"), 20), GasLimit: 123456, Timestamp: uint64(time.Now().Unix()), Pubkey: validatorKey.PublicKey().Marshal(), } m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx ðpb.DomainRequest{Domain: params.BeaconConfig().DomainApplicationBuilder[:]}, ).Times(1).Return(ðpb.DomainResponse{SignatureDomain: make([]byte, 32)}, nil /*err*/) _, err := signValidatorRegistration( ctx, 1, m.validatorClient, m.signfunc, reg) require.NoError(t, err) m.validatorClient.EXPECT().DomainData( gomock.Any(), // ctx ðpb.DomainRequest{Domain: params.BeaconConfig().DomainApplicationBuilder[:]}, ).Times(1).Return(nil, errors.New(domainDataErr) /*err*/) _, err = signValidatorRegistration( ctx, 1, m.validatorClient, m.signfunc, reg) require.ErrorContains(t, domainDataErr, err) }