prysm-pulse/validator/client/validator_propose_test.go
Raul Jordan e19920aec1
Miscellaneous Runtime Fixes & Improvements - Raul (#1674)
* fatal if impossible to receive chainstart

* fix tests

* fix

* custom delay

* completed custom delay

* errors

* better logs, nothing at genesis

* use demo in val

* add gazelle

* log

* starting to log stuff

* pass in ops

* avoid printing the large #s for debug, still working on tests..

* all around better logging

* fixed build error in epoch process

* fixed state transiton tests

* fixed block tests

* lint

* verify sigs in randao

* ready for inclusion falg

* only print waiting when slot is not valid

* fix build

* mod config

* fixed last justified slot issue

* fix inclusion

* fixed attestation issue

* using zero hash from params instead

* fix tests

* update balance

* removed swp

* more `- genesis_slot` for logs

* rem unused log

* fix broken tests

* account for skip slots in state root computation

* fixes done

* validator guide bug fixes - 671

* epoch boundary at the last slot of the epoch

* fix epoch issue

* more balance cal logs for debugging

* greater balance

* attestaton fixes

* fixes

* addressed testrun

* fixed ejection balance

* fix tests with far future epoch

* revert sync change

* revert initial sync change

* fix changes

* off by one att fix

* revert the att fix

* address comments

* format

* fix build

* rem file
2019-02-24 20:09:45 -06:00

417 lines
12 KiB
Go

package client
import (
"bytes"
"context"
"errors"
"testing"
"github.com/prysmaticlabs/prysm/shared/params"
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),
}
validator := &validator{
proposerClient: m.proposerClient,
beaconClient: m.beaconClient,
attesterClient: m.attesterClient,
validatorClient: m.validatorClient,
key: validatorKey,
}
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.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().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.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().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_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_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.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*/)
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*/)
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*/)
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.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().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)
}