prysm-pulse/validator/client/validator_propose_test.go
Raul Jordan 053038446c
Allow 8 Validator Multinode Cluster to Run Indefinitely (#2050)
* plug forkchoice to blockchain service's block processing

* fixed tests

* more fixes...

* clean ups

* fixed test

* Update beacon-chain/blockchain/block_processing.go

* merged with 2006 and started fixing tests

* remove prints

* fixed tests

* lint

* include ops service

* if there's a skip slot, slot--

* fixed typo

* started working on test

* no fork choice in propose

* bleh, need to fix state generator first

* state gen takes input slot

* feedback

* fixed tests

* preston's feedback

* fmt

* removed extra logging

* add more logs

* fixed validator attest

* builds

* fixed save block

* children fix

* removed verbose logs

* fix fork choice

* right logs

* Add Prometheus Counter for Reorg (#2051)

* fetch every slot (#2052)

* test Fixes

* lint

* only regenerate state if there was a reorg

* better logging

* fixed seed

* better logging

* process skip slots in assignment requests

* fix lint

* disable state root computation

* filter attestations in regular sync

* log important items

* better info logs

* added spans to stategen

* span in stategen

* set validator deadline

* randao stuff

* disable sig verify

* lint

* lint

* save only using historical states

* use new goroutine for handling sync messages

* change default buffer sizes

* better p2p

* rem some useless logs

* lint

* sync tests complete

* complete tests

* tests fixed

* lint

* fix flakey att service

* PR feedback

* undo k8s changes

* Update beacon-chain/blockchain/block_processing.go

* Update beacon-chain/sync/regular_sync.go

* Add feature flag to enable compute state root

* add comment

* gazelle lint fix
2019-03-25 10:21:21 -05:00

351 lines
10 KiB
Go

package client
import (
"bytes"
"context"
"errors"
"testing"
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/params"
"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,
key: validatorKey,
}
return validator, m, ctrl.Finish
}
func TestProposeBlock_DoesNotProposeGenesisBlock(t *testing.T) {
hook := logTest.NewGlobal()
validator, _, finish := setup(t)
defer finish()
validator.ProposeBlock(context.Background(), params.BeaconConfig().GenesisSlot)
testutil.AssertLogsContain(t, hook, "Assigned to genesis slot, skipping proposal")
}
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.beaconClient.EXPECT().ForkData(
gomock.Any(), // ctx
gomock.Eq(&ptypes.Empty{}),
).Return(&pbp2p.Fork{
Epoch: params.BeaconConfig().GenesisEpoch,
CurrentVersion: 0,
PreviousVersion: 0,
}, nil /*err*/)
m.proposerClient.EXPECT().PendingAttestations(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&pb.PendingAttestationsRequest{}),
).Return(&pb.PendingAttestationsResponse{PendingAttestations: []*pbp2p.Attestation{}}, nil)
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.beaconClient.EXPECT().ForkData(
gomock.Any(), // ctx
gomock.Eq(&ptypes.Empty{}),
).Return(&pbp2p.Fork{
Epoch: params.BeaconConfig().GenesisEpoch,
CurrentVersion: 0,
PreviousVersion: 0,
}, nil /*err*/)
m.proposerClient.EXPECT().PendingAttestations(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&pb.PendingAttestationsRequest{}),
).Return(&pb.PendingAttestationsResponse{PendingAttestations: []*pbp2p.Attestation{}}, nil)
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_PendingAttestations_UsesCurrentSlot(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.beaconClient.EXPECT().ForkData(
gomock.Any(), // ctx
gomock.Eq(&ptypes.Empty{}),
).Return(&pbp2p.Fork{
Epoch: params.BeaconConfig().GenesisEpoch,
CurrentVersion: 0,
PreviousVersion: 0,
}, nil /*err*/)
var req *pb.PendingAttestationsRequest
m.proposerClient.EXPECT().PendingAttestations(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&pb.PendingAttestationsRequest{}),
).DoAndReturn(func(_ context.Context, r *pb.PendingAttestationsRequest) (*pb.PendingAttestationsResponse, error) {
req = r
return &pb.PendingAttestationsResponse{PendingAttestations: []*pbp2p.Attestation{}}, nil
})
m.proposerClient.EXPECT().ProposeBlock(
gomock.Any(), // context
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
).Return(&pb.ProposeResponse{}, nil /*error*/)
validator.ProposeBlock(context.Background(), 55)
if req.ProposalBlockSlot != 55 {
t.Errorf(
"expected request to use the current proposal slot %d, but got %d",
55,
req.ProposalBlockSlot,
)
}
}
func TestProposeBlock_PendingAttestationsFailure(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{
Eth1Data: &pbp2p.Eth1Data{BlockHash32: []byte{'B', 'L', 'O', 'C', 'K'}},
}, nil /*err*/)
m.beaconClient.EXPECT().ForkData(
gomock.Any(), // ctx
gomock.Eq(&ptypes.Empty{}),
).Return(&pbp2p.Fork{
Epoch: params.BeaconConfig().GenesisEpoch,
CurrentVersion: 0,
PreviousVersion: 0,
}, nil /*err*/)
m.proposerClient.EXPECT().PendingAttestations(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&pb.PendingAttestationsRequest{}),
).Return(nil, errors.New("failed"))
validator.ProposeBlock(context.Background(), 55)
testutil.AssertLogsContain(t, hook, "Failed to fetch pending attestations")
}
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.beaconClient.EXPECT().ForkData(
gomock.Any(), // ctx
gomock.Eq(&ptypes.Empty{}),
).Return(&pbp2p.Fork{
Epoch: params.BeaconConfig().GenesisEpoch,
CurrentVersion: 0,
PreviousVersion: 0,
}, nil /*err*/)
m.proposerClient.EXPECT().PendingAttestations(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&pb.PendingAttestationsRequest{}),
).Return(&pb.PendingAttestationsResponse{PendingAttestations: []*pbp2p.Attestation{}}, nil)
m.proposerClient.EXPECT().ProposeBlock(
gomock.Any(), // ctx
gomock.AssignableToTypeOf(&pbp2p.BeaconBlock{}),
).Return(&pb.ProposeResponse{}, nil /*error*/)
validator.ProposeBlock(context.Background(), 55)
}