mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
281e5d1cfd
* provide validator with real keystore public key * fmt * build errors buildkite * readd imports removed by goimport * gazelle * address broken tests * make reference to filename a param * fmt * imports * gazelle * os remove all * Use better Temp storage for tests * testmain approach to avoid repeating expensive operation for each test * travis lint * fix test * nokeystore in status test * passing * fix goimports
313 lines
8.8 KiB
Go
313 lines
8.8 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/keystore"
|
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
|
"github.com/golang/mock/gomock"
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
|
"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),
|
|
}
|
|
|
|
k, err := keystore.NewKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
validator := &validator{
|
|
attestationPool: &fakeAttestationPool{},
|
|
proposerClient: m.proposerClient,
|
|
beaconClient: m.beaconClient,
|
|
attesterClient: m.attesterClient,
|
|
validatorClient: m.validatorClient,
|
|
key: k,
|
|
}
|
|
|
|
return validator, m, ctrl.Finish
|
|
}
|
|
|
|
func TestProposeBlock_LogsCanonicalHeadFailure(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(nil /*beaconBlock*/, errors.New("something bad happened"))
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
|
|
testutil.AssertLogsContain(t, hook, "something bad happened")
|
|
}
|
|
|
|
func TestProposeBlock_PendingDepositsFailure(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(nil /*response*/, errors.New("something bad happened"))
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
|
|
testutil.AssertLogsContain(t, hook, "something bad happened")
|
|
}
|
|
|
|
func TestProposeBlock_UsePendingDeposits(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.PendingDepositsResponse{
|
|
PendingDeposits: []*pbp2p.Deposit{
|
|
{DepositData: []byte{'D', 'A', 'T', 'A'}},
|
|
},
|
|
}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().Eth1Data(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.Eth1DataResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ComputeStateRoot(
|
|
gomock.Any(), // context
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(&pb.StateRootResponse{
|
|
StateRoot: []byte{'F'},
|
|
}, nil /*err*/)
|
|
|
|
var broadcastedBlock *pbp2p.BeaconBlock
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // context
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Do(func(_ context.Context, blk *pbp2p.BeaconBlock) {
|
|
broadcastedBlock = blk
|
|
}).Return(&pb.ProposeResponse{}, nil /*error*/)
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
|
|
if !bytes.Equal(broadcastedBlock.Body.Deposits[0].DepositData, []byte{'D', 'A', 'T', 'A'}) {
|
|
t.Errorf("Unexpected deposit data: %v", broadcastedBlock.Body.Deposits)
|
|
}
|
|
}
|
|
|
|
func TestProposeBlock_Eth1DataFailure(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.PendingDepositsResponse{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().Eth1Data(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(nil /*response*/, errors.New("something bad happened"))
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
|
|
testutil.AssertLogsContain(t, hook, "something bad happened")
|
|
}
|
|
|
|
func TestProposeBlock_UsesEth1Data(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.PendingDepositsResponse{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().Eth1Data(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.Eth1DataResponse{
|
|
Eth1Data: &pbp2p.Eth1Data{BlockHash32: []byte{'B', 'L', 'O', 'C', 'K'}},
|
|
}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ComputeStateRoot(
|
|
gomock.Any(), // context
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(&pb.StateRootResponse{
|
|
StateRoot: []byte{'F'},
|
|
}, nil /*err*/)
|
|
|
|
var broadcastedBlock *pbp2p.BeaconBlock
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Do(func(_ context.Context, blk *pbp2p.BeaconBlock) {
|
|
broadcastedBlock = blk
|
|
}).Return(&pb.ProposeResponse{}, nil /*error*/)
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
|
|
if !bytes.Equal(broadcastedBlock.Eth1Data.BlockHash32, []byte{'B', 'L', 'O', 'C', 'K'}) {
|
|
t.Errorf("Unexpected ETH1 data: %v", broadcastedBlock.Eth1Data)
|
|
}
|
|
}
|
|
|
|
func TestProposeBlock_ComputeStateFailure(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.PendingDepositsResponse{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().Eth1Data(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.Eth1DataResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(&pb.ProposeResponse{}, nil /*error*/)
|
|
|
|
m.proposerClient.EXPECT().ComputeStateRoot(
|
|
gomock.Any(), // context
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(nil /*response*/, errors.New("something bad happened"))
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
testutil.AssertLogsContain(t, hook, "something bad happened")
|
|
}
|
|
|
|
func TestProposeBlock_UsesComputedState(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.PendingDepositsResponse{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().Eth1Data(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.Eth1DataResponse{}, nil /*err*/)
|
|
|
|
var broadcastedBlock *pbp2p.BeaconBlock
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Do(func(_ context.Context, blk *pbp2p.BeaconBlock) {
|
|
broadcastedBlock = blk
|
|
}).Return(&pb.ProposeResponse{}, nil /*error*/)
|
|
|
|
computedStateRoot := []byte{'T', 'E', 'S', 'T'}
|
|
m.proposerClient.EXPECT().ComputeStateRoot(
|
|
gomock.Any(), // context
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(
|
|
&pb.StateRootResponse{
|
|
StateRoot: computedStateRoot,
|
|
},
|
|
nil, // err
|
|
)
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
|
|
if !bytes.Equal(broadcastedBlock.StateRootHash32, computedStateRoot) {
|
|
t.Errorf("Unexpected state root hash. want=%#x got=%#x", computedStateRoot, broadcastedBlock.StateRootHash32)
|
|
}
|
|
}
|
|
|
|
func TestProposeBlock_BroadcastsABlock(t *testing.T) {
|
|
validator, m, finish := setup(t)
|
|
defer finish()
|
|
|
|
m.beaconClient.EXPECT().CanonicalHead(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pbp2p.BeaconBlock{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().PendingDeposits(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.PendingDepositsResponse{}, nil /*err*/)
|
|
|
|
m.beaconClient.EXPECT().Eth1Data(
|
|
gomock.Any(), // ctx
|
|
gomock.Eq(&ptypes.Empty{}),
|
|
).Return(&pb.Eth1DataResponse{}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ComputeStateRoot(
|
|
gomock.Any(), // context
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(&pb.StateRootResponse{
|
|
StateRoot: []byte{'F'},
|
|
}, nil /*err*/)
|
|
|
|
m.proposerClient.EXPECT().ProposeBlock(
|
|
gomock.Any(), // ctx
|
|
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
|
|
).Return(&pb.ProposeResponse{}, nil /*error*/)
|
|
|
|
validator.ProposeBlock(context.Background(), 55)
|
|
}
|