2020-08-12 01:38:01 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
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"
|
|
|
|
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
|
|
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
|
2020-09-03 15:11:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2020-08-13 20:27:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-08-12 23:33:15 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/client"
|
2020-08-13 20:27:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/db"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log logrus.FieldLogger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log = logrus.WithField("prefix", "rpc")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config options for the gRPC server.
|
|
|
|
type Config struct {
|
2020-09-03 15:11:17 +00:00
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
CertFlag string
|
|
|
|
KeyFlag string
|
|
|
|
ValDB db.Database
|
2020-10-10 02:07:28 +00:00
|
|
|
WalletDir string
|
2020-09-03 15:11:17 +00:00
|
|
|
ValidatorService *client.ValidatorService
|
2020-09-03 23:25:56 +00:00
|
|
|
SyncChecker client.SyncChecker
|
2020-09-08 20:54:56 +00:00
|
|
|
GenesisFetcher client.GenesisFetcher
|
2020-09-03 15:11:17 +00:00
|
|
|
WalletInitializedFeed *event.Feed
|
2020-09-04 19:03:18 +00:00
|
|
|
NodeGatewayEndpoint string
|
2020-08-12 01:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server defining a gRPC server for the remote signer API.
|
|
|
|
type Server struct {
|
2020-09-03 15:11:17 +00:00
|
|
|
valDB db.Database
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
host string
|
|
|
|
port string
|
|
|
|
listener net.Listener
|
2020-10-15 22:31:52 +00:00
|
|
|
keymanager keymanager.IKeymanager
|
2020-09-03 15:11:17 +00:00
|
|
|
withCert string
|
|
|
|
withKey string
|
|
|
|
credentialError error
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
jwtKey []byte
|
|
|
|
validatorService *client.ValidatorService
|
2020-09-03 23:25:56 +00:00
|
|
|
syncChecker client.SyncChecker
|
2020-09-08 20:54:56 +00:00
|
|
|
genesisFetcher client.GenesisFetcher
|
2020-10-10 02:07:28 +00:00
|
|
|
walletDir string
|
2020-10-15 22:31:52 +00:00
|
|
|
wallet *wallet.Wallet
|
2020-09-03 15:11:17 +00:00
|
|
|
walletInitializedFeed *event.Feed
|
|
|
|
walletInitialized bool
|
2020-09-04 19:03:18 +00:00
|
|
|
nodeGatewayEndpoint string
|
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-09-03 15:11:17 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
host: cfg.Host,
|
|
|
|
port: cfg.Port,
|
|
|
|
withCert: cfg.CertFlag,
|
|
|
|
withKey: cfg.KeyFlag,
|
|
|
|
valDB: cfg.ValDB,
|
|
|
|
validatorService: cfg.ValidatorService,
|
2020-09-03 23:25:56 +00:00
|
|
|
syncChecker: cfg.SyncChecker,
|
2020-09-08 20:54:56 +00:00
|
|
|
genesisFetcher: cfg.GenesisFetcher,
|
2020-10-10 02:07:28 +00:00
|
|
|
walletDir: cfg.WalletDir,
|
2020-09-03 15:11:17 +00:00
|
|
|
walletInitializedFeed: cfg.WalletInitializedFeed,
|
|
|
|
walletInitialized: false,
|
2020-09-04 19:03:18 +00:00
|
|
|
nodeGatewayEndpoint: cfg.NodeGatewayEndpoint,
|
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 {
|
|
|
|
log.Errorf("Could not listen to port in Start() %s: %v", address, err)
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
recovery.WithRecoveryHandlerContext(traceutil.RecoveryHandlerFunc),
|
|
|
|
),
|
|
|
|
grpc_prometheus.UnaryServerInterceptor,
|
|
|
|
grpc_opentracing.UnaryServerInterceptor(),
|
|
|
|
s.JWTInterceptor(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
grpc_prometheus.EnableHandlingTimeHistogram()
|
|
|
|
|
2020-08-12 01:38:01 +00:00
|
|
|
if s.withCert != "" && s.withKey != "" {
|
|
|
|
creds, err := credentials.NewServerTLSFromFile(s.withCert, s.withKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not load TLS keys: %s", err)
|
|
|
|
s.credentialError = err
|
|
|
|
}
|
|
|
|
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-08-13 20:27:42 +00:00
|
|
|
// We create a new, random JWT key upon validator startup.
|
|
|
|
r := rand.NewGenerator()
|
|
|
|
jwtKey := make([]byte, 32)
|
|
|
|
n, err := r.Read(jwtKey)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("Could not initialize validator jwt key")
|
|
|
|
}
|
|
|
|
if n != len(jwtKey) {
|
|
|
|
log.WithError(err).Fatal("Could not create random jwt key for validator")
|
|
|
|
}
|
|
|
|
s.jwtKey = jwtKey
|
|
|
|
|
2020-08-12 01:38:01 +00:00
|
|
|
// Register services available for the gRPC server.
|
|
|
|
reflection.Register(s.grpcServer)
|
2020-08-13 20:27:42 +00:00
|
|
|
pb.RegisterAuthServer(s.grpcServer, s)
|
2020-09-03 15:11:17 +00:00
|
|
|
pb.RegisterWalletServer(s.grpcServer, s)
|
2020-09-03 23:25:56 +00:00
|
|
|
pb.RegisterHealthServer(s.grpcServer, s)
|
2020-09-04 00:58:36 +00:00
|
|
|
pb.RegisterAccountsServer(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 {
|
|
|
|
log.Errorf("Could not serve: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.WithField("address", address).Info("gRPC server listening on address")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|