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
|
|
|
|
2019-02-20 17:57:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-03-17 02:56:05 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-03-11 01:19:52 +00:00
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
2019-03-26 16:26:47 +00:00
|
|
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
2019-04-01 01:44:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
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"
|
2019-03-12 04:05:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/trieutil"
|
2018-08-01 22:08:44 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-02-28 03:55:47 +00:00
|
|
|
"go.opencensus.io/plugin/ocgrpc"
|
2018-08-01 22:08:44 +00:00
|
|
|
"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 {
|
2019-02-13 17:51:57 +00:00
|
|
|
StateInitializedFeed() *event.Feed
|
2019-04-01 01:44:16 +00:00
|
|
|
blockchain.BlockReceiver
|
2019-05-06 20:06:41 +00:00
|
|
|
blockchain.ForkChoice
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
type operationService interface {
|
2019-04-29 20:03:28 +00:00
|
|
|
PendingAttestations(ctx context.Context) ([]*pbp2p.Attestation, error)
|
2019-03-17 02:56:05 +00:00
|
|
|
HandleAttestations(context.Context, proto.Message) error
|
|
|
|
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-20 17:57:02 +00:00
|
|
|
LatestBlockHeight() *big.Int
|
2019-02-28 03:55:47 +00:00
|
|
|
BlockExists(ctx context.Context, hash common.Hash) (bool, *big.Int, error)
|
2019-03-01 20:31:38 +00:00
|
|
|
BlockHashByHeight(ctx context.Context, height *big.Int) (common.Hash, error)
|
2019-05-02 23:13:51 +00:00
|
|
|
BlockTimeByHeight(ctx context.Context, height *big.Int) (uint64, error)
|
2019-02-20 17:57:02 +00:00
|
|
|
DepositRoot() [32]byte
|
2019-03-12 04:05:55 +00:00
|
|
|
DepositTrie() *trieutil.MerkleTrie
|
|
|
|
ChainStartDeposits() [][]byte
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:17:45 +00:00
|
|
|
type syncService interface {
|
|
|
|
Status() error
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
// Service defining an RPC server for a beacon node.
|
|
|
|
type Service struct {
|
2019-04-03 14:59:18 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
beaconDB *db.BeaconDB
|
|
|
|
chainService chainService
|
|
|
|
powChainService powChainService
|
|
|
|
operationService operationService
|
2019-05-02 13:17:45 +00:00
|
|
|
syncService syncService
|
2019-04-03 14:59:18 +00:00
|
|
|
port string
|
|
|
|
listener net.Listener
|
|
|
|
withCert string
|
|
|
|
withKey string
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
canonicalStateChan chan *pbp2p.BeaconState
|
|
|
|
incomingAttestation chan *pbp2p.Attestation
|
|
|
|
credentialError error
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config options for the beacon node RPC server.
|
|
|
|
type Config struct {
|
2019-03-18 15:45:28 +00:00
|
|
|
Port string
|
|
|
|
CertFlag string
|
|
|
|
KeyFlag string
|
|
|
|
BeaconDB *db.BeaconDB
|
|
|
|
ChainService chainService
|
|
|
|
POWChainService powChainService
|
|
|
|
OperationService operationService
|
2019-05-02 13:17:45 +00:00
|
|
|
SyncService syncService
|
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{
|
2019-04-03 14:59:18 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
beaconDB: cfg.BeaconDB,
|
|
|
|
chainService: cfg.ChainService,
|
|
|
|
powChainService: cfg.POWChainService,
|
|
|
|
operationService: cfg.OperationService,
|
2019-05-02 13:17:45 +00:00
|
|
|
syncService: cfg.SyncService,
|
2019-04-03 14:59:18 +00:00
|
|
|
port: cfg.Port,
|
|
|
|
withCert: cfg.CertFlag,
|
|
|
|
withKey: cfg.KeyFlag,
|
|
|
|
canonicalStateChan: make(chan *pbp2p.BeaconState, params.BeaconConfig().DefaultBufferSize),
|
|
|
|
incomingAttestation: make(chan *pbp2p.Attestation, params.BeaconConfig().DefaultBufferSize),
|
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
|
2019-04-20 04:09:01 +00:00
|
|
|
log.WithField("port", s.port).Info("Listening on port")
|
2018-08-01 22:08:44 +00:00
|
|
|
|
2019-03-11 01:19:52 +00:00
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
|
|
|
|
grpc.StreamInterceptor(middleware.ChainStreamServer(
|
|
|
|
recovery.StreamServerInterceptor(),
|
2019-03-26 16:26:47 +00:00
|
|
|
grpc_prometheus.StreamServerInterceptor,
|
2019-03-11 01:19:52 +00:00
|
|
|
)),
|
|
|
|
grpc.UnaryInterceptor(middleware.ChainUnaryServer(
|
|
|
|
recovery.UnaryServerInterceptor(),
|
2019-03-26 16:26:47 +00:00
|
|
|
grpc_prometheus.UnaryServerInterceptor,
|
2019-03-11 01:19:52 +00:00
|
|
|
)),
|
|
|
|
}
|
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
|
|
|
}
|
2019-03-11 01:19:52 +00:00
|
|
|
opts = append(opts, 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-08 22:43:25 +00:00
|
|
|
}
|
2019-03-11 01:19:52 +00:00
|
|
|
s.grpcServer = grpc.NewServer(opts...)
|
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,
|
2019-02-16 00:36:40 +00:00
|
|
|
operationService: s.operationService,
|
2019-01-28 15:40:40 +00:00
|
|
|
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{
|
2019-03-05 23:06:50 +00:00
|
|
|
ctx: s.ctx,
|
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
chainService: s.chainService,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
2019-05-02 23:13:51 +00:00
|
|
|
powChainService: s.powChainService,
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
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-05-02 13:17:45 +00:00
|
|
|
for s.syncService.Status() != nil {
|
|
|
|
time.Sleep(time.Second * params.BeaconConfig().RPCSyncCheck)
|
|
|
|
}
|
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
|
|
|
|
}
|