2020-08-12 01:38:01 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-10-28 14:24:39 +00:00
|
|
|
"path/filepath"
|
2020-12-17 16:26:32 +00:00
|
|
|
"time"
|
2020-08-12 01:38:01 +00:00
|
|
|
|
2020-08-13 20:27:42 +00:00
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
2022-06-27 13:34:38 +00:00
|
|
|
grpcopentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
|
|
|
grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/async/event"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/io/logs"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/monitoring/tracing"
|
|
|
|
ethpbservice "github.com/prysmaticlabs/prysm/v4/proto/eth/service"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
validatorpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1/validator-client"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/validator/accounts/wallet"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/validator/client"
|
|
|
|
iface "github.com/prysmaticlabs/prysm/v4/validator/client/iface"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/validator/db"
|
2020-08-12 01:38:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-08-13 20:27:42 +00:00
|
|
|
"go.opencensus.io/plugin/ocgrpc"
|
2020-08-12 01:38:01 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config options for the gRPC server.
|
|
|
|
type Config struct {
|
2020-12-17 16:26:32 +00:00
|
|
|
ValidatorGatewayHost string
|
|
|
|
ValidatorGatewayPort int
|
|
|
|
ValidatorMonitoringHost string
|
|
|
|
ValidatorMonitoringPort int
|
|
|
|
BeaconClientEndpoint string
|
|
|
|
ClientMaxCallRecvMsgSize int
|
|
|
|
ClientGrpcRetries uint
|
|
|
|
ClientGrpcRetryDelay time.Duration
|
|
|
|
ClientGrpcHeaders []string
|
|
|
|
ClientWithCert string
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
CertFlag string
|
|
|
|
KeyFlag string
|
|
|
|
ValDB db.Database
|
|
|
|
WalletDir string
|
|
|
|
ValidatorService *client.ValidatorService
|
|
|
|
SyncChecker client.SyncChecker
|
|
|
|
GenesisFetcher client.GenesisFetcher
|
|
|
|
WalletInitializedFeed *event.Feed
|
|
|
|
NodeGatewayEndpoint string
|
|
|
|
Wallet *wallet.Wallet
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server defining a gRPC server for the remote signer API.
|
|
|
|
type Server struct {
|
2021-09-17 21:55:24 +00:00
|
|
|
logsStreamer logs.Streamer
|
2021-04-16 07:46:38 +00:00
|
|
|
streamLogsBufferSize int
|
2023-03-20 16:32:32 +00:00
|
|
|
beaconChainClient iface.BeaconChainClient
|
|
|
|
beaconNodeClient iface.NodeClient
|
2022-11-07 10:29:27 +00:00
|
|
|
beaconNodeValidatorClient iface.ValidatorClient
|
2021-12-07 17:52:39 +00:00
|
|
|
beaconNodeHealthClient ethpb.HealthClient
|
2021-04-16 07:46:38 +00:00
|
|
|
valDB db.Database
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
beaconClientEndpoint string
|
|
|
|
clientMaxCallRecvMsgSize int
|
|
|
|
clientGrpcRetries uint
|
|
|
|
clientGrpcRetryDelay time.Duration
|
|
|
|
clientGrpcHeaders []string
|
|
|
|
clientWithCert string
|
|
|
|
host string
|
|
|
|
port string
|
|
|
|
listener net.Listener
|
|
|
|
withCert string
|
|
|
|
withKey string
|
|
|
|
credentialError error
|
|
|
|
grpcServer *grpc.Server
|
2021-10-28 14:24:39 +00:00
|
|
|
jwtSecret []byte
|
2021-04-16 07:46:38 +00:00
|
|
|
validatorService *client.ValidatorService
|
|
|
|
syncChecker client.SyncChecker
|
|
|
|
genesisFetcher client.GenesisFetcher
|
|
|
|
walletDir string
|
|
|
|
wallet *wallet.Wallet
|
|
|
|
walletInitializedFeed *event.Feed
|
|
|
|
walletInitialized bool
|
|
|
|
nodeGatewayEndpoint string
|
|
|
|
validatorMonitoringHost string
|
|
|
|
validatorMonitoringPort int
|
|
|
|
validatorGatewayHost string
|
|
|
|
validatorGatewayPort int
|
2022-11-11 17:33:48 +00:00
|
|
|
beaconApiEndpoint string
|
|
|
|
beaconApiTimeout time.Duration
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer instantiates a new gRPC server.
|
|
|
|
func NewServer(ctx context.Context, cfg *Config) *Server {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return &Server{
|
2020-12-17 16:26:32 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2021-09-17 21:55:24 +00:00
|
|
|
logsStreamer: logs.NewStreamServer(),
|
2020-12-18 18:03:24 +00:00
|
|
|
streamLogsBufferSize: 1000, // Enough to handle most bursts of logs in the validator client.
|
2020-12-17 16:26:32 +00:00
|
|
|
host: cfg.Host,
|
|
|
|
port: cfg.Port,
|
|
|
|
withCert: cfg.CertFlag,
|
|
|
|
withKey: cfg.KeyFlag,
|
|
|
|
beaconClientEndpoint: cfg.BeaconClientEndpoint,
|
|
|
|
clientMaxCallRecvMsgSize: cfg.ClientMaxCallRecvMsgSize,
|
|
|
|
clientGrpcRetries: cfg.ClientGrpcRetries,
|
|
|
|
clientGrpcRetryDelay: cfg.ClientGrpcRetryDelay,
|
|
|
|
clientGrpcHeaders: cfg.ClientGrpcHeaders,
|
|
|
|
clientWithCert: cfg.ClientWithCert,
|
|
|
|
valDB: cfg.ValDB,
|
|
|
|
validatorService: cfg.ValidatorService,
|
|
|
|
syncChecker: cfg.SyncChecker,
|
|
|
|
genesisFetcher: cfg.GenesisFetcher,
|
|
|
|
walletDir: cfg.WalletDir,
|
|
|
|
walletInitializedFeed: cfg.WalletInitializedFeed,
|
|
|
|
walletInitialized: cfg.Wallet != nil,
|
|
|
|
wallet: cfg.Wallet,
|
|
|
|
nodeGatewayEndpoint: cfg.NodeGatewayEndpoint,
|
|
|
|
validatorMonitoringHost: cfg.ValidatorMonitoringHost,
|
|
|
|
validatorMonitoringPort: cfg.ValidatorMonitoringPort,
|
|
|
|
validatorGatewayHost: cfg.ValidatorGatewayHost,
|
|
|
|
validatorGatewayPort: cfg.ValidatorGatewayPort,
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the gRPC server.
|
|
|
|
func (s *Server) Start() {
|
|
|
|
// Setup the gRPC server options and TLS configuration.
|
|
|
|
address := fmt.Sprintf("%s:%s", s.host, s.port)
|
|
|
|
lis, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Errorf("Could not listen to port in Start() %s", address)
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
s.listener = lis
|
|
|
|
|
2020-08-13 20:27:42 +00:00
|
|
|
// Register interceptors for metrics gathering as well as our
|
|
|
|
// own, custom JWT unary interceptor.
|
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
|
|
|
|
grpc.UnaryInterceptor(middleware.ChainUnaryServer(
|
|
|
|
recovery.UnaryServerInterceptor(
|
2021-09-14 20:59:51 +00:00
|
|
|
recovery.WithRecoveryHandlerContext(tracing.RecoveryHandlerFunc),
|
2020-08-13 20:27:42 +00:00
|
|
|
),
|
2022-06-27 13:34:38 +00:00
|
|
|
grpcprometheus.UnaryServerInterceptor,
|
|
|
|
grpcopentracing.UnaryServerInterceptor(),
|
2020-08-13 20:27:42 +00:00
|
|
|
s.JWTInterceptor(),
|
|
|
|
)),
|
|
|
|
}
|
2022-06-27 13:34:38 +00:00
|
|
|
grpcprometheus.EnableHandlingTimeHistogram()
|
2020-08-13 20:27:42 +00:00
|
|
|
|
2020-08-12 01:38:01 +00:00
|
|
|
if s.withCert != "" && s.withKey != "" {
|
|
|
|
creds, err := credentials.NewServerTLSFromFile(s.withCert, s.withKey)
|
|
|
|
if err != nil {
|
2020-10-22 23:58:53 +00:00
|
|
|
log.WithError(err).Fatal("Could not load TLS keys")
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
opts = append(opts, grpc.Creds(creds))
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"crt-path": s.withCert,
|
|
|
|
"key-path": s.withKey,
|
|
|
|
}).Info("Loaded TLS certificates")
|
|
|
|
}
|
|
|
|
s.grpcServer = grpc.NewServer(opts...)
|
|
|
|
|
2020-12-17 16:26:32 +00:00
|
|
|
// Register a gRPC client to the beacon node.
|
|
|
|
if err := s.registerBeaconClient(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Could not register beacon chain gRPC client")
|
|
|
|
}
|
|
|
|
|
2020-08-12 01:38:01 +00:00
|
|
|
// Register services available for the gRPC server.
|
|
|
|
reflection.Register(s.grpcServer)
|
2021-08-18 21:24:01 +00:00
|
|
|
validatorpb.RegisterAuthServer(s.grpcServer, s)
|
|
|
|
validatorpb.RegisterWalletServer(s.grpcServer, s)
|
|
|
|
validatorpb.RegisterHealthServer(s.grpcServer, s)
|
|
|
|
validatorpb.RegisterBeaconServer(s.grpcServer, s)
|
|
|
|
validatorpb.RegisterAccountsServer(s.grpcServer, s)
|
2021-11-24 15:40:49 +00:00
|
|
|
ethpbservice.RegisterKeyManagementServer(s.grpcServer, s)
|
2021-08-18 21:24:01 +00:00
|
|
|
validatorpb.RegisterSlashingProtectionServer(s.grpcServer, s)
|
2020-08-12 01:38:01 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if s.listener != nil {
|
|
|
|
if err := s.grpcServer.Serve(s.listener); err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Error("Could not serve")
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.WithField("address", address).Info("gRPC server listening on address")
|
2021-10-28 14:24:39 +00:00
|
|
|
if s.walletDir != "" {
|
|
|
|
token, err := s.initializeAuthToken(s.walletDir)
|
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Error("Could not initialize web auth token")
|
2021-10-28 14:24:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
validatorWebAddr := fmt.Sprintf("%s:%d", s.validatorGatewayHost, s.validatorGatewayPort)
|
|
|
|
authTokenPath := filepath.Join(s.walletDir, authTokenFileName)
|
2022-08-04 15:59:36 +00:00
|
|
|
logValidatorWebAuth(validatorWebAddr, token, authTokenPath)
|
2021-10-28 14:24:39 +00:00
|
|
|
go s.refreshAuthTokenFromFileChanges(s.ctx, authTokenPath)
|
2021-10-20 16:37:05 +00:00
|
|
|
}
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the gRPC server.
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
s.cancel()
|
|
|
|
if s.listener != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
log.Debug("Initiated graceful stop of server")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns nil or credentialError.
|
|
|
|
func (s *Server) Status() error {
|
|
|
|
return s.credentialError
|
|
|
|
}
|