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

     1  package rpc
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/golang/protobuf/ptypes/empty"
     8  	middleware "github.com/grpc-ecosystem/go-grpc-middleware"
     9  	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
    10  	grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
    11  	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
    12  	"github.com/pkg/errors"
    13  	healthpb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
    14  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    15  	pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
    16  	"github.com/prysmaticlabs/prysm/shared/grpcutils"
    17  	"github.com/prysmaticlabs/prysm/validator/client"
    18  	"google.golang.org/grpc"
    19  	"google.golang.org/protobuf/types/known/emptypb"
    20  )
    21  
    22  // Initialize a client connect to a beacon node gRPC endpoint.
    23  func (s *Server) registerBeaconClient() error {
    24  	streamInterceptor := grpc.WithStreamInterceptor(middleware.ChainStreamClient(
    25  		grpc_opentracing.StreamClientInterceptor(),
    26  		grpc_prometheus.StreamClientInterceptor,
    27  		grpc_retry.StreamClientInterceptor(),
    28  	))
    29  	dialOpts := client.ConstructDialOptions(
    30  		s.clientMaxCallRecvMsgSize,
    31  		s.clientWithCert,
    32  		s.clientGrpcRetries,
    33  		s.clientGrpcRetryDelay,
    34  		streamInterceptor,
    35  	)
    36  	if dialOpts == nil {
    37  		return errors.New("no dial options for beacon chain gRPC client")
    38  	}
    39  
    40  	s.ctx = grpcutils.AppendHeaders(s.ctx, s.clientGrpcHeaders)
    41  
    42  	conn, err := grpc.DialContext(s.ctx, s.beaconClientEndpoint, dialOpts...)
    43  	if err != nil {
    44  		return errors.Wrapf(err, "could not dial endpoint: %s", s.beaconClientEndpoint)
    45  	}
    46  	if s.clientWithCert != "" {
    47  		log.Info("Established secure gRPC connection")
    48  	}
    49  	s.beaconChainClient = ethpb.NewBeaconChainClient(conn)
    50  	s.beaconNodeClient = ethpb.NewNodeClient(conn)
    51  	s.beaconNodeHealthClient = healthpb.NewHealthClient(conn)
    52  	s.beaconNodeValidatorClient = ethpb.NewBeaconNodeValidatorClient(conn)
    53  	return nil
    54  }
    55  
    56  // GetBeaconStatus retrieves information about the beacon node gRPC connection
    57  // and certain chain metadata, such as the genesis time, the chain head, and the
    58  // deposit contract address.
    59  func (s *Server) GetBeaconStatus(ctx context.Context, _ *empty.Empty) (*pb.BeaconStatusResponse, error) {
    60  	syncStatus, err := s.beaconNodeClient.GetSyncStatus(ctx, &emptypb.Empty{})
    61  	if err != nil {
    62  		return &pb.BeaconStatusResponse{
    63  			BeaconNodeEndpoint: s.nodeGatewayEndpoint,
    64  			Connected:          false,
    65  			Syncing:            false,
    66  		}, nil
    67  	}
    68  	genesis, err := s.beaconNodeClient.GetGenesis(ctx, &emptypb.Empty{})
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	genesisTime := uint64(time.Unix(genesis.GenesisTime.Seconds, 0).Unix())
    73  	address := genesis.DepositContractAddress
    74  	chainHead, err := s.beaconChainClient.GetChainHead(ctx, &emptypb.Empty{})
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	return &pb.BeaconStatusResponse{
    79  		BeaconNodeEndpoint:     s.beaconClientEndpoint,
    80  		Connected:              true,
    81  		Syncing:                syncStatus.Syncing,
    82  		GenesisTime:            genesisTime,
    83  		DepositContractAddress: address,
    84  		ChainHead:              chainHead,
    85  	}, nil
    86  }
    87  
    88  // GetValidatorParticipation is a wrapper around the /eth/v1alpha1 endpoint of the same name.
    89  func (s *Server) GetValidatorParticipation(
    90  	ctx context.Context, req *ethpb.GetValidatorParticipationRequest,
    91  ) (*ethpb.ValidatorParticipationResponse, error) {
    92  	return s.beaconChainClient.GetValidatorParticipation(ctx, req)
    93  }
    94  
    95  // GetValidatorPerformance is a wrapper around the /eth/v1alpha1 endpoint of the same name.
    96  func (s *Server) GetValidatorPerformance(
    97  	ctx context.Context, req *ethpb.ValidatorPerformanceRequest,
    98  ) (*ethpb.ValidatorPerformanceResponse, error) {
    99  	return s.beaconChainClient.GetValidatorPerformance(ctx, req)
   100  }
   101  
   102  // GetValidatorBalances is a wrapper around the /eth/v1alpha1 endpoint of the same name.
   103  func (s *Server) GetValidatorBalances(
   104  	ctx context.Context, req *ethpb.ListValidatorBalancesRequest,
   105  ) (*ethpb.ValidatorBalances, error) {
   106  	return s.beaconChainClient.ListValidatorBalances(ctx, req)
   107  }
   108  
   109  // GetValidators is a wrapper around the /eth/v1alpha1 endpoint of the same name.
   110  func (s *Server) GetValidators(
   111  	ctx context.Context, req *ethpb.ListValidatorsRequest,
   112  ) (*ethpb.Validators, error) {
   113  	return s.beaconChainClient.ListValidators(ctx, req)
   114  }
   115  
   116  // GetValidatorQueue is a wrapper around the /eth/v1alpha1 endpoint of the same name.
   117  func (s *Server) GetValidatorQueue(
   118  	ctx context.Context, _ *empty.Empty,
   119  ) (*ethpb.ValidatorQueue, error) {
   120  	return s.beaconChainClient.GetValidatorQueue(ctx, &emptypb.Empty{})
   121  }
   122  
   123  // GetPeers is a wrapper around the /eth/v1alpha1 endpoint of the same name.
   124  func (s *Server) GetPeers(
   125  	ctx context.Context, _ *empty.Empty,
   126  ) (*ethpb.Peers, error) {
   127  	return s.beaconNodeClient.ListPeers(ctx, &emptypb.Empty{})
   128  }