fix validaotr pending attestations call (#1941)

This commit is contained in:
Preston Van Loon 2019-03-08 19:29:47 -05:00 committed by Nishant Das
parent 90221c32cf
commit 16be050f4b
3 changed files with 71 additions and 3 deletions

View File

@ -19,6 +19,7 @@ go_library(
"//shared/keystore:go_default_library",
"//shared/params:go_default_library",
"//shared/slotutil:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@io_opencensus_go//plugin/ocgrpc:go_default_library",

View File

@ -7,6 +7,7 @@ import (
"encoding/binary"
"fmt"
"github.com/gogo/protobuf/proto"
ptypes "github.com/gogo/protobuf/types"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
@ -82,6 +83,7 @@ func (v *validator) ProposeBlock(ctx context.Context, slot uint64) {
// Fetch pending attestations seen by the beacon node.
attResp, err := v.proposerClient.PendingAttestations(ctx, &pb.PendingAttestationsRequest{
FilterReadyForInclusion: true,
ProposalBlockSlot: slot,
})
if err != nil {
log.Errorf("Failed to fetch pending attestations from the beacon node: %v", err)
@ -106,7 +108,9 @@ func (v *validator) ProposeBlock(ctx context.Context, slot uint64) {
// 3. Compute state root transition from parent block to the new block.
resp, err := v.proposerClient.ComputeStateRoot(ctx, block)
if err != nil {
log.Errorf("Unable to compute state root: %v", err)
log.WithField(
"block", proto.MarshalTextString(block),
).Errorf("Unable to compute state root: %v", err)
}
block.StateRootHash32 = resp.GetStateRoot()

View File

@ -6,12 +6,11 @@ import (
"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/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/validator/internal"
logTest "github.com/sirupsen/logrus/hooks/test"
@ -220,6 +219,70 @@ func TestProposeBlock_UsesEth1Data(t *testing.T) {
}
}
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().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 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)