2020-12-17 16:26:32 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-03-12 00:03:19 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
2020-12-17 16:26:32 +00:00
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
|
|
|
|
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
|
|
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
|
|
|
"github.com/pkg/errors"
|
2021-09-16 19:55:51 +00:00
|
|
|
grpcutil "github.com/prysmaticlabs/prysm/api/grpc"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-07-28 21:23:44 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-08-18 21:24:01 +00:00
|
|
|
validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client"
|
2020-12-17 16:26:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/client"
|
|
|
|
"google.golang.org/grpc"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2020-12-17 16:26:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Initialize a client connect to a beacon node gRPC endpoint.
|
|
|
|
func (s *Server) registerBeaconClient() error {
|
|
|
|
streamInterceptor := grpc.WithStreamInterceptor(middleware.ChainStreamClient(
|
|
|
|
grpc_opentracing.StreamClientInterceptor(),
|
|
|
|
grpc_prometheus.StreamClientInterceptor,
|
|
|
|
grpc_retry.StreamClientInterceptor(),
|
|
|
|
))
|
|
|
|
dialOpts := client.ConstructDialOptions(
|
|
|
|
s.clientMaxCallRecvMsgSize,
|
|
|
|
s.clientWithCert,
|
|
|
|
s.clientGrpcRetries,
|
|
|
|
s.clientGrpcRetryDelay,
|
|
|
|
streamInterceptor,
|
|
|
|
)
|
|
|
|
if dialOpts == nil {
|
|
|
|
return errors.New("no dial options for beacon chain gRPC client")
|
|
|
|
}
|
2021-01-28 14:58:32 +00:00
|
|
|
|
2021-09-16 19:55:51 +00:00
|
|
|
s.ctx = grpcutil.AppendHeaders(s.ctx, s.clientGrpcHeaders)
|
2021-01-28 14:58:32 +00:00
|
|
|
|
2020-12-17 16:26:32 +00:00
|
|
|
conn, err := grpc.DialContext(s.ctx, s.beaconClientEndpoint, dialOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not dial endpoint: %s", s.beaconClientEndpoint)
|
|
|
|
}
|
|
|
|
if s.clientWithCert != "" {
|
|
|
|
log.Info("Established secure gRPC connection")
|
|
|
|
}
|
|
|
|
s.beaconChainClient = ethpb.NewBeaconChainClient(conn)
|
|
|
|
s.beaconNodeClient = ethpb.NewNodeClient(conn)
|
2021-07-22 21:00:28 +00:00
|
|
|
s.beaconNodeHealthClient = pb.NewHealthClient(conn)
|
2021-04-16 07:46:38 +00:00
|
|
|
s.beaconNodeValidatorClient = ethpb.NewBeaconNodeValidatorClient(conn)
|
2020-12-17 16:26:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBeaconStatus retrieves information about the beacon node gRPC connection
|
|
|
|
// and certain chain metadata, such as the genesis time, the chain head, and the
|
|
|
|
// deposit contract address.
|
2021-08-18 21:24:01 +00:00
|
|
|
func (s *Server) GetBeaconStatus(ctx context.Context, _ *empty.Empty) (*validatorpb.BeaconStatusResponse, error) {
|
2021-05-17 18:32:04 +00:00
|
|
|
syncStatus, err := s.beaconNodeClient.GetSyncStatus(ctx, &emptypb.Empty{})
|
2020-12-17 16:26:32 +00:00
|
|
|
if err != nil {
|
2021-08-18 21:24:01 +00:00
|
|
|
return &validatorpb.BeaconStatusResponse{
|
2020-12-17 16:26:32 +00:00
|
|
|
BeaconNodeEndpoint: s.nodeGatewayEndpoint,
|
|
|
|
Connected: false,
|
|
|
|
Syncing: false,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-17 18:32:04 +00:00
|
|
|
genesis, err := s.beaconNodeClient.GetGenesis(ctx, &emptypb.Empty{})
|
2020-12-17 16:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
genesisTime := uint64(time.Unix(genesis.GenesisTime.Seconds, 0).Unix())
|
|
|
|
address := genesis.DepositContractAddress
|
2021-05-17 18:32:04 +00:00
|
|
|
chainHead, err := s.beaconChainClient.GetChainHead(ctx, &emptypb.Empty{})
|
2020-12-17 16:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-18 21:24:01 +00:00
|
|
|
return &validatorpb.BeaconStatusResponse{
|
2020-12-17 16:26:32 +00:00
|
|
|
BeaconNodeEndpoint: s.beaconClientEndpoint,
|
|
|
|
Connected: true,
|
|
|
|
Syncing: syncStatus.Syncing,
|
|
|
|
GenesisTime: genesisTime,
|
|
|
|
DepositContractAddress: address,
|
|
|
|
ChainHead: chainHead,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidatorParticipation is a wrapper around the /eth/v1alpha1 endpoint of the same name.
|
|
|
|
func (s *Server) GetValidatorParticipation(
|
|
|
|
ctx context.Context, req *ethpb.GetValidatorParticipationRequest,
|
|
|
|
) (*ethpb.ValidatorParticipationResponse, error) {
|
|
|
|
return s.beaconChainClient.GetValidatorParticipation(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidatorPerformance is a wrapper around the /eth/v1alpha1 endpoint of the same name.
|
|
|
|
func (s *Server) GetValidatorPerformance(
|
|
|
|
ctx context.Context, req *ethpb.ValidatorPerformanceRequest,
|
|
|
|
) (*ethpb.ValidatorPerformanceResponse, error) {
|
|
|
|
return s.beaconChainClient.GetValidatorPerformance(ctx, req)
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// GetValidatorBalances is a wrapper around the /eth/v1alpha1 endpoint of the same name.
|
2020-12-17 16:26:32 +00:00
|
|
|
func (s *Server) GetValidatorBalances(
|
|
|
|
ctx context.Context, req *ethpb.ListValidatorBalancesRequest,
|
|
|
|
) (*ethpb.ValidatorBalances, error) {
|
|
|
|
return s.beaconChainClient.ListValidatorBalances(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidators is a wrapper around the /eth/v1alpha1 endpoint of the same name.
|
|
|
|
func (s *Server) GetValidators(
|
|
|
|
ctx context.Context, req *ethpb.ListValidatorsRequest,
|
|
|
|
) (*ethpb.Validators, error) {
|
|
|
|
return s.beaconChainClient.ListValidators(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidatorQueue is a wrapper around the /eth/v1alpha1 endpoint of the same name.
|
|
|
|
func (s *Server) GetValidatorQueue(
|
2021-03-12 00:03:19 +00:00
|
|
|
ctx context.Context, _ *empty.Empty,
|
2020-12-17 16:26:32 +00:00
|
|
|
) (*ethpb.ValidatorQueue, error) {
|
2021-05-17 18:32:04 +00:00
|
|
|
return s.beaconChainClient.GetValidatorQueue(ctx, &emptypb.Empty{})
|
2020-12-17 16:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeers is a wrapper around the /eth/v1alpha1 endpoint of the same name.
|
|
|
|
func (s *Server) GetPeers(
|
2021-03-12 00:03:19 +00:00
|
|
|
ctx context.Context, _ *empty.Empty,
|
2020-12-17 16:26:32 +00:00
|
|
|
) (*ethpb.Peers, error) {
|
2021-05-17 18:32:04 +00:00
|
|
|
return s.beaconNodeClient.ListPeers(ctx, &emptypb.Empty{})
|
2020-12-17 16:26:32 +00:00
|
|
|
}
|