github.com/prysmaticlabs/prysm@v1.4.4/validator/rpc/beacon_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/golang/protobuf/ptypes/empty"
    10  	"github.com/pkg/errors"
    11  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    12  	pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
    13  	"github.com/prysmaticlabs/prysm/shared/mock"
    14  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    15  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    16  	"google.golang.org/grpc/metadata"
    17  	"google.golang.org/protobuf/types/known/timestamppb"
    18  )
    19  
    20  func TestGetBeaconStatus_NotConnected(t *testing.T) {
    21  	ctrl := gomock.NewController(t)
    22  	nodeClient := mock.NewMockNodeClient(ctrl)
    23  	nodeClient.EXPECT().GetSyncStatus(
    24  		gomock.Any(), // ctx
    25  		gomock.Any(),
    26  	).Return(nil /*response*/, errors.New("uh oh"))
    27  	srv := &Server{
    28  		beaconNodeClient: nodeClient,
    29  	}
    30  	ctx := context.Background()
    31  	resp, err := srv.GetBeaconStatus(ctx, &empty.Empty{})
    32  	require.NoError(t, err)
    33  	want := &pb.BeaconStatusResponse{
    34  		BeaconNodeEndpoint: "",
    35  		Connected:          false,
    36  		Syncing:            false,
    37  	}
    38  	assert.DeepEqual(t, want, resp)
    39  }
    40  
    41  func TestGetBeaconStatus_OK(t *testing.T) {
    42  	ctrl := gomock.NewController(t)
    43  	nodeClient := mock.NewMockNodeClient(ctrl)
    44  	beaconChainClient := mock.NewMockBeaconChainClient(ctrl)
    45  	nodeClient.EXPECT().GetSyncStatus(
    46  		gomock.Any(), // ctx
    47  		gomock.Any(),
    48  	).Return(&ethpb.SyncStatus{Syncing: true}, nil)
    49  	timeStamp := timestamppb.New(time.Unix(0, 0))
    50  	nodeClient.EXPECT().GetGenesis(
    51  		gomock.Any(), // ctx
    52  		gomock.Any(),
    53  	).Return(&ethpb.Genesis{
    54  		GenesisTime:            timeStamp,
    55  		DepositContractAddress: []byte("hello"),
    56  	}, nil)
    57  	beaconChainClient.EXPECT().GetChainHead(
    58  		gomock.Any(), // ctx
    59  		gomock.Any(),
    60  	).Return(&ethpb.ChainHead{
    61  		HeadEpoch: 1,
    62  	}, nil)
    63  	srv := &Server{
    64  		beaconNodeClient:  nodeClient,
    65  		beaconChainClient: beaconChainClient,
    66  	}
    67  	ctx := context.Background()
    68  	resp, err := srv.GetBeaconStatus(ctx, &empty.Empty{})
    69  	require.NoError(t, err)
    70  	want := &pb.BeaconStatusResponse{
    71  		BeaconNodeEndpoint:     "",
    72  		Connected:              true,
    73  		Syncing:                true,
    74  		GenesisTime:            uint64(time.Unix(0, 0).Unix()),
    75  		DepositContractAddress: []byte("hello"),
    76  		ChainHead: &ethpb.ChainHead{
    77  			HeadEpoch: 1,
    78  		},
    79  	}
    80  	assert.DeepEqual(t, want, resp)
    81  }
    82  
    83  func TestGrpcHeaders(t *testing.T) {
    84  	s := &Server{
    85  		ctx:               context.Background(),
    86  		clientGrpcHeaders: []string{"first=value1", "second=value2"},
    87  	}
    88  	err := s.registerBeaconClient()
    89  	require.NoError(t, err)
    90  	md, _ := metadata.FromOutgoingContext(s.ctx)
    91  	require.Equal(t, 2, md.Len(), "MetadataV0 contains wrong number of values")
    92  	assert.Equal(t, "value1", md.Get("first")[0])
    93  	assert.Equal(t, "value2", md.Get("second")[0])
    94  }