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"
|
|
|
|
"net"
|
2018-09-27 02:34:35 +00:00
|
|
|
"time"
|
2018-08-01 22:08:44 +00:00
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "rpc")
|
|
|
|
|
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
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 01:22:09 +00:00
|
|
|
type attestationService interface {
|
|
|
|
IncomingAttestationFeed() *event.Feed
|
2018-09-05 03:35:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
type powChainService interface {
|
|
|
|
LatestBlockHash() common.Hash
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
attestationService attestationService
|
|
|
|
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-10-02 02:04:37 +00:00
|
|
|
enablePOWChain bool
|
2018-09-27 02:34:35 +00:00
|
|
|
slotAlignmentDuration time.Duration
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config options for the beacon node RPC server.
|
|
|
|
type Config struct {
|
2018-09-24 01:22:09 +00:00
|
|
|
Port string
|
|
|
|
CertFlag string
|
|
|
|
KeyFlag string
|
|
|
|
SubscriptionBuf int
|
2018-11-16 17:01:41 +00:00
|
|
|
BeaconDB *db.BeaconDB
|
2018-09-24 01:22:09 +00:00
|
|
|
ChainService chainService
|
|
|
|
POWChainService powChainService
|
|
|
|
AttestationService attestationService
|
2018-10-02 02:04:37 +00:00
|
|
|
EnablePOWChain bool
|
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,
|
|
|
|
attestationService: cfg.AttestationService,
|
|
|
|
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-10-02 02:04:37 +00:00
|
|
|
enablePOWChain: cfg.EnablePOWChain,
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the gRPC server.
|
|
|
|
func (s *Service) Start() {
|
|
|
|
log.Info("Starting service")
|
2018-08-09 22:54:59 +00:00
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", s.port))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not listen to port :%s: %v", s.port, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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)
|
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,
|
|
|
|
attestationService: s.attestationService,
|
|
|
|
incomingAttestation: s.incomingAttestation,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
|
|
|
}
|
|
|
|
proposerServer := &ProposerServer{
|
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
chainService: s.chainService,
|
|
|
|
powChainService: s.powChainService,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
|
|
|
enablePOWChain: s.enablePOWChain,
|
|
|
|
}
|
|
|
|
attesterServer := &AttesterServer{
|
|
|
|
attestationService: s.attestationService,
|
|
|
|
}
|
|
|
|
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() {
|
2018-08-18 03:34:56 +00:00
|
|
|
err = s.grpcServer.Serve(lis)
|
2018-08-01 22:08:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not serve gRPC: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
// Status always returns nil.
|
|
|
|
// TODO(1205): Add service health checks.
|
|
|
|
func (s *Service) Status() error {
|
|
|
|
return nil
|
|
|
|
}
|