mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
1b5b8a57e0
* Update io_kubernetes_build commit hash to 1246899 * Update dependency build_bazel_rules_nodejs to v0.33.1 * Update dependency com_github_hashicorp_golang_lru to v0.5.1 * Update libp2p * Update io_bazel_rules_k8s commit hash to e68d5d7 * Starting to remove old protos * Bazel build proto passes * Fixing pb version * Cleaned up core package * Fixing tests * 6 tests failing * Update proto bugs * Fixed incorrect validator ordering proto * Sync with master * Update go-ssz commit * Removed bad copies from v1alpha1 folder * add json spec json to pb handler * add nested proto example * proto/testing test works * fix refactoring build failures * use merged ssz * push latest changes * used forked json encoding * used forked json encoding * fix warning * fix build issues * fix test and lint * fix build * lint
140 lines
4.2 KiB
Go
140 lines
4.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/validator/internal"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
type mocks struct {
|
|
proposerClient *internal.MockProposerServiceClient
|
|
beaconClient *internal.MockBeaconServiceClient
|
|
validatorClient *internal.MockValidatorServiceClient
|
|
attesterClient *internal.MockAttesterServiceClient
|
|
}
|
|
|
|
func setup(t *testing.T) (*validator, *mocks, func()) {
|
|
ctrl := gomock.NewController(t)
|
|
m := &mocks{
|
|
proposerClient: internal.NewMockProposerServiceClient(ctrl),
|
|
beaconClient: internal.NewMockBeaconServiceClient(ctrl),
|
|
validatorClient: internal.NewMockValidatorServiceClient(ctrl),
|
|
attesterClient: internal.NewMockAttesterServiceClient(ctrl),
|
|
}
|
|
validator := &validator{
|
|
proposerClient: m.proposerClient,
|
|
beaconClient: m.beaconClient,
|
|
attesterClient: m.attesterClient,
|
|
validatorClient: m.validatorClient,
|
|
keys: keyMap,
|
|
}
|
|
|
|
return validator, m, ctrl.Finish
|
|
}
|
|
|
|
func TestProposeBlock_DoesNotProposeGenesisBlock(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, _, finish := setup(t)
|
|
defer finish()
|
|
validator.ProposeBlock(context.Background(), 0, hex.EncodeToString(validatorKey.PublicKey.Marshal()))
|
|
|
|
testutil.AssertLogsContain(t, hook, "Assigned to genesis slot, skipping proposal")
|
|
}
|
|
|
|
func TestProposeBlock_DomainDataFailed(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // epoch
|
|
).Return(nil /*response*/, errors.New("uh oh"))
|
|
|
|
validator.ProposeBlock(context.Background(), 1, hex.EncodeToString(validatorKey.PublicKey.Marshal()))
|
|
testutil.AssertLogsContain(t, hook, "Failed to get domain data from beacon node")
|
|
}
|
|
|
|
func TestProposeBlock_RequestBlockFailed(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // epoch
|
|
).Return(&pb.DomainResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().RequestBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), // block request
|
|
).Return(nil /*response*/, errors.New("uh oh"))
|
|
|
|
validator.ProposeBlock(context.Background(), 1, hex.EncodeToString(validatorKey.PublicKey.Marshal()))
|
|
testutil.AssertLogsContain(t, hook, "Failed to request block from beacon node")
|
|
}
|
|
|
|
func TestProposeBlock_ProposeBlockFailed(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), //epoch
|
|
).Return(&pb.DomainResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().RequestBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(),
|
|
).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}, nil /*err*/)
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), //epoch
|
|
).Return(&pb.DomainResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(ðpb.BeaconBlock{}),
|
|
).Return(nil /*response*/, errors.New("uh oh"))
|
|
|
|
validator.ProposeBlock(context.Background(), 1, hex.EncodeToString(validatorKey.PublicKey.Marshal()))
|
|
testutil.AssertLogsContain(t, hook, "Failed to propose block")
|
|
}
|
|
|
|
func TestProposeBlock_BroadcastsBlock(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), //epoch
|
|
).Return(&pb.DomainResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().RequestBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(),
|
|
).Return(ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}, nil /*err*/)
|
|
|
|
m.validatorClient.EXPECT().DomainData(
|
|
gomock.Any(), // ctx
|
|
gomock.Any(), //epoch
|
|
).Return(&pb.DomainResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(ðpb.BeaconBlock{}),
|
|
).Return(&pb.ProposeResponse{}, nil /*error*/)
|
|
|
|
validator.ProposeBlock(context.Background(), 1, hex.EncodeToString(validatorKey.PublicKey.Marshal()))
|
|
}
|