2018-08-09 18:25:48 +00:00
|
|
|
// Package rpc defines the services that the beacon-chain uses to communicate via gRPC.
|
2018-08-01 22:08:44 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-02-06 18:06:31 +00:00
|
|
|
"math/big"
|
2018-08-01 22:08:44 +00:00
|
|
|
"net"
|
2018-09-27 02:34:35 +00:00
|
|
|
"time"
|
2018-08-01 22:08:44 +00:00
|
|
|
|
2018-11-16 17:01:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2018-09-05 03:35:32 +00:00
|
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-08-01 22:08:44 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2018-11-18 16:39:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2018-08-01 22:08:44 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/grpc"
|
2018-08-08 22:43:25 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
2019-01-13 16:58:52 +00:00
|
|
|
"google.golang.org/grpc/reflection"
|
2018-08-01 22:08:44 +00:00
|
|
|
)
|
|
|
|
|
2019-02-04 21:54:30 +00:00
|
|
|
var log logrus.FieldLogger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log = logrus.WithField("prefix", "rpc")
|
|
|
|
}
|
2018-08-01 22:08:44 +00:00
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
type chainService interface {
|
|
|
|
IncomingBlockFeed() *event.Feed
|
2018-09-21 14:32:20 +00:00
|
|
|
// These methods are not called on-demand by a validator
|
|
|
|
// but instead streamed to connected validators every
|
|
|
|
// time the canonical head changes in the chain service.
|
|
|
|
CanonicalBlockFeed() *event.Feed
|
2018-12-01 22:09:12 +00:00
|
|
|
CanonicalStateFeed() *event.Feed
|
2019-02-13 17:51:57 +00:00
|
|
|
StateInitializedFeed() *event.Feed
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
type operationService interface {
|
|
|
|
IncomingExitFeed() *event.Feed
|
|
|
|
IncomingAttFeed() *event.Feed
|
2018-09-05 03:35:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
type powChainService interface {
|
2019-02-03 22:44:48 +00:00
|
|
|
HasChainStartLogOccurred() (bool, uint64, error)
|
2019-01-30 12:28:53 +00:00
|
|
|
ChainStartFeed() *event.Feed
|
2019-02-06 18:06:31 +00:00
|
|
|
LatestBlockNumber() *big.Int
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
// Service defining an RPC server for a beacon node.
|
|
|
|
type Service struct {
|
2018-09-27 02:34:35 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2018-11-16 17:01:41 +00:00
|
|
|
beaconDB *db.BeaconDB
|
2018-09-27 02:34:35 +00:00
|
|
|
chainService chainService
|
|
|
|
powChainService powChainService
|
2019-01-31 18:54:24 +00:00
|
|
|
operationService operationService
|
2018-09-27 02:34:35 +00:00
|
|
|
port string
|
|
|
|
listener net.Listener
|
|
|
|
withCert string
|
|
|
|
withKey string
|
|
|
|
grpcServer *grpc.Server
|
2018-12-20 22:00:38 +00:00
|
|
|
canonicalBlockChan chan *pbp2p.BeaconBlock
|
2018-12-23 22:51:04 +00:00
|
|
|
canonicalStateChan chan *pbp2p.BeaconState
|
2018-12-22 20:30:59 +00:00
|
|
|
incomingAttestation chan *pbp2p.Attestation
|
2018-09-27 02:34:35 +00:00
|
|
|
slotAlignmentDuration time.Duration
|
2019-02-04 21:54:30 +00:00
|
|
|
credentialError error
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config options for the beacon node RPC server.
|
|
|
|
type Config struct {
|
2019-01-31 18:54:24 +00:00
|
|
|
Port string
|
|
|
|
CertFlag string
|
|
|
|
KeyFlag string
|
|
|
|
SubscriptionBuf int
|
|
|
|
BeaconDB *db.BeaconDB
|
|
|
|
ChainService chainService
|
|
|
|
POWChainService powChainService
|
|
|
|
OperationService operationService
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRPCService creates a new instance of a struct implementing the BeaconServiceServer
|
|
|
|
// interface.
|
2018-09-05 03:35:32 +00:00
|
|
|
func NewRPCService(ctx context.Context, cfg *Config) *Service {
|
2018-08-01 22:08:44 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return &Service{
|
2018-09-27 02:34:35 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2018-10-05 17:14:50 +00:00
|
|
|
beaconDB: cfg.BeaconDB,
|
2018-09-27 02:34:35 +00:00
|
|
|
chainService: cfg.ChainService,
|
|
|
|
powChainService: cfg.POWChainService,
|
2019-01-31 18:54:24 +00:00
|
|
|
operationService: cfg.OperationService,
|
2018-09-27 02:34:35 +00:00
|
|
|
port: cfg.Port,
|
|
|
|
withCert: cfg.CertFlag,
|
|
|
|
withKey: cfg.KeyFlag,
|
2018-11-18 16:39:35 +00:00
|
|
|
slotAlignmentDuration: time.Duration(params.BeaconConfig().SlotDuration) * time.Second,
|
2018-12-20 22:00:38 +00:00
|
|
|
canonicalBlockChan: make(chan *pbp2p.BeaconBlock, cfg.SubscriptionBuf),
|
2018-12-23 22:51:04 +00:00
|
|
|
canonicalStateChan: make(chan *pbp2p.BeaconState, cfg.SubscriptionBuf),
|
2018-12-22 20:30:59 +00:00
|
|
|
incomingAttestation: make(chan *pbp2p.Attestation, cfg.SubscriptionBuf),
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the gRPC server.
|
|
|
|
func (s *Service) Start() {
|
|
|
|
log.Info("Starting service")
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", s.port))
|
|
|
|
if err != nil {
|
2019-02-04 21:54:30 +00:00
|
|
|
log.Errorf("Could not listen to port in Start() :%s: %v", s.port, err)
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
s.listener = lis
|
|
|
|
log.Infof("RPC server listening on port :%s", s.port)
|
|
|
|
|
2018-11-16 17:01:41 +00:00
|
|
|
// TODO(#791): Utilize a certificate for secure connections
|
|
|
|
// between beacon nodes and validator clients.
|
2018-08-08 22:43:25 +00:00
|
|
|
if s.withCert != "" && s.withKey != "" {
|
|
|
|
creds, err := credentials.NewServerTLSFromFile(s.withCert, s.withKey)
|
|
|
|
if err != nil {
|
2018-10-31 22:05:55 +00:00
|
|
|
log.Errorf("Could not load TLS keys: %s", err)
|
2019-02-04 21:54:30 +00:00
|
|
|
s.credentialError = err
|
2018-08-08 22:43:25 +00:00
|
|
|
}
|
2018-08-18 03:34:56 +00:00
|
|
|
s.grpcServer = grpc.NewServer(grpc.Creds(creds))
|
2018-08-08 22:43:25 +00:00
|
|
|
} else {
|
2018-11-16 17:01:41 +00:00
|
|
|
log.Warn("You are using an insecure gRPC connection! Provide a certificate and key to connect securely")
|
2018-08-18 03:34:56 +00:00
|
|
|
s.grpcServer = grpc.NewServer()
|
2018-08-08 22:43:25 +00:00
|
|
|
}
|
2019-01-13 20:52:31 +00:00
|
|
|
|
2019-01-28 15:40:40 +00:00
|
|
|
beaconServer := &BeaconServer{
|
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
ctx: s.ctx,
|
2019-01-30 12:28:53 +00:00
|
|
|
powChainService: s.powChainService,
|
2019-02-13 17:51:57 +00:00
|
|
|
chainService: s.chainService,
|
2019-01-31 18:54:24 +00:00
|
|
|
operationService: s.operationService,
|
2019-01-28 15:40:40 +00:00
|
|
|
incomingAttestation: s.incomingAttestation,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
2019-01-30 12:28:53 +00:00
|
|
|
chainStartChan: make(chan time.Time, 1),
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
proposerServer := &ProposerServer{
|
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
chainService: s.chainService,
|
|
|
|
powChainService: s.powChainService,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
|
|
|
}
|
|
|
|
attesterServer := &AttesterServer{
|
2019-02-11 16:15:25 +00:00
|
|
|
beaconDB: s.beaconDB,
|
2019-01-31 18:54:24 +00:00
|
|
|
operationService: s.operationService,
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
validatorServer := &ValidatorServer{
|
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
}
|
|
|
|
pb.RegisterBeaconServiceServer(s.grpcServer, beaconServer)
|
|
|
|
pb.RegisterProposerServiceServer(s.grpcServer, proposerServer)
|
|
|
|
pb.RegisterAttesterServiceServer(s.grpcServer, attesterServer)
|
|
|
|
pb.RegisterValidatorServiceServer(s.grpcServer, validatorServer)
|
2019-01-13 20:52:31 +00:00
|
|
|
|
2019-01-13 16:58:52 +00:00
|
|
|
// Register reflection service on gRPC server.
|
|
|
|
reflection.Register(s.grpcServer)
|
2019-01-13 20:52:31 +00:00
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
go func() {
|
2019-02-04 21:54:30 +00:00
|
|
|
if s.listener != nil {
|
|
|
|
if err := s.grpcServer.Serve(s.listener); err != nil {
|
|
|
|
log.Errorf("Could not serve gRPC: %v", err)
|
|
|
|
}
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the service.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
log.Info("Stopping service")
|
2018-08-18 03:34:56 +00:00
|
|
|
s.cancel()
|
2018-08-01 22:08:44 +00:00
|
|
|
if s.listener != nil {
|
2018-08-18 03:34:56 +00:00
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
log.Debug("Initiated graceful stop of gRPC server")
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:54:30 +00:00
|
|
|
// Status returns nil or credentialError
|
2018-12-30 21:20:43 +00:00
|
|
|
func (s *Service) Status() error {
|
2019-02-04 21:54:30 +00:00
|
|
|
if s.credentialError != nil {
|
|
|
|
return s.credentialError
|
|
|
|
}
|
2018-12-30 21:20:43 +00:00
|
|
|
return nil
|
|
|
|
}
|