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
|
|
|
|
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-09-05 16:04:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2019-05-09 00:27:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2019-08-23 02:49:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache"
|
2018-11-16 17:01:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-07-26 17:07:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
|
2019-08-19 21:20:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-09-09 21:13:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2019-08-21 20:58:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync"
|
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"
|
2019-07-22 14:03:57 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
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"
|
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
|
|
|
|
|
|
|
// Service defining an RPC server for a beacon node.
|
|
|
|
type Service struct {
|
2019-09-12 04:30:04 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
beaconDB db.Database
|
|
|
|
stateFeedListener blockchain.ChainFeeds
|
|
|
|
headFetcher blockchain.HeadFetcher
|
2019-09-23 15:47:34 +00:00
|
|
|
finalizationFetcher blockchain.FinalizationFetcher
|
2019-09-16 20:45:03 +00:00
|
|
|
genesisTimeFetcher blockchain.GenesisTimeFetcher
|
2019-09-12 04:30:04 +00:00
|
|
|
attestationReceiver blockchain.AttestationReceiver
|
|
|
|
blockReceiver blockchain.BlockReceiver
|
|
|
|
powChainService powchain.Chain
|
|
|
|
chainStartFetcher powchain.ChainStartFetcher
|
|
|
|
mockEth1Votes bool
|
|
|
|
attestationsPool operations.Pool
|
|
|
|
operationsHandler operations.Handler
|
|
|
|
syncService sync.Checker
|
|
|
|
port string
|
|
|
|
listener net.Listener
|
|
|
|
withCert string
|
|
|
|
withKey string
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
canonicalStateChan chan *pbp2p.BeaconState
|
|
|
|
incomingAttestation chan *ethpb.Attestation
|
|
|
|
credentialError error
|
|
|
|
p2p p2p.Broadcaster
|
|
|
|
depositFetcher depositcache.DepositFetcher
|
|
|
|
pendingDepositFetcher depositcache.PendingDepositsFetcher
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config options for the beacon node RPC server.
|
|
|
|
type Config struct {
|
2019-09-12 04:30:04 +00:00
|
|
|
Port string
|
|
|
|
CertFlag string
|
|
|
|
KeyFlag string
|
|
|
|
BeaconDB db.Database
|
|
|
|
StateFeedListener blockchain.ChainFeeds
|
|
|
|
HeadFetcher blockchain.HeadFetcher
|
2019-09-23 15:47:34 +00:00
|
|
|
FinalizationFetcher blockchain.FinalizationFetcher
|
2019-09-12 04:30:04 +00:00
|
|
|
AttestationReceiver blockchain.AttestationReceiver
|
|
|
|
BlockReceiver blockchain.BlockReceiver
|
|
|
|
POWChainService powchain.Chain
|
|
|
|
ChainStartFetcher powchain.ChainStartFetcher
|
2019-09-16 20:45:03 +00:00
|
|
|
GenesisTimeFetcher blockchain.GenesisTimeFetcher
|
2019-09-12 04:30:04 +00:00
|
|
|
MockEth1Votes bool
|
|
|
|
OperationsHandler operations.Handler
|
|
|
|
AttestationsPool operations.Pool
|
|
|
|
SyncService sync.Checker
|
|
|
|
Broadcaster p2p.Broadcaster
|
|
|
|
DepositFetcher depositcache.DepositFetcher
|
|
|
|
PendingDepositFetcher depositcache.PendingDepositsFetcher
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 02:39:14 +00:00
|
|
|
// NewService instantiates a new RPC service instance that will
|
|
|
|
// be registered into a running beacon node.
|
|
|
|
func NewService(ctx context.Context, cfg *Config) *Service {
|
2018-08-01 22:08:44 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return &Service{
|
2019-09-12 04:30:04 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
beaconDB: cfg.BeaconDB,
|
|
|
|
stateFeedListener: cfg.StateFeedListener,
|
|
|
|
headFetcher: cfg.HeadFetcher,
|
2019-09-23 15:47:34 +00:00
|
|
|
finalizationFetcher: cfg.FinalizationFetcher,
|
2019-09-16 20:45:03 +00:00
|
|
|
genesisTimeFetcher: cfg.GenesisTimeFetcher,
|
2019-09-12 04:30:04 +00:00
|
|
|
attestationReceiver: cfg.AttestationReceiver,
|
|
|
|
blockReceiver: cfg.BlockReceiver,
|
|
|
|
p2p: cfg.Broadcaster,
|
|
|
|
powChainService: cfg.POWChainService,
|
|
|
|
chainStartFetcher: cfg.ChainStartFetcher,
|
|
|
|
mockEth1Votes: cfg.MockEth1Votes,
|
|
|
|
attestationsPool: cfg.AttestationsPool,
|
|
|
|
operationsHandler: cfg.OperationsHandler,
|
|
|
|
syncService: cfg.SyncService,
|
|
|
|
port: cfg.Port,
|
|
|
|
withCert: cfg.CertFlag,
|
|
|
|
withKey: cfg.KeyFlag,
|
|
|
|
depositFetcher: cfg.DepositFetcher,
|
|
|
|
pendingDepositFetcher: cfg.PendingDepositFetcher,
|
|
|
|
canonicalStateChan: make(chan *pbp2p.BeaconState, params.BeaconConfig().DefaultBufferSize),
|
|
|
|
incomingAttestation: make(chan *ethpb.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
|
|
|
proposerServer := &ProposerServer{
|
2019-09-12 04:30:04 +00:00
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
headFetcher: s.headFetcher,
|
|
|
|
blockReceiver: s.blockReceiver,
|
|
|
|
chainStartFetcher: s.chainStartFetcher,
|
|
|
|
eth1InfoFetcher: s.powChainService,
|
|
|
|
eth1BlockFetcher: s.powChainService,
|
|
|
|
mockEth1Votes: s.mockEth1Votes,
|
|
|
|
pool: s.attestationsPool,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
|
|
|
depositFetcher: s.depositFetcher,
|
|
|
|
pendingDepositsFetcher: s.pendingDepositFetcher,
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
attesterServer := &AttesterServer{
|
2019-09-09 21:13:50 +00:00
|
|
|
p2p: s.p2p,
|
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
operationsHandler: s.operationsHandler,
|
|
|
|
attReceiver: s.attestationReceiver,
|
|
|
|
headFetcher: s.headFetcher,
|
2019-09-12 04:30:04 +00:00
|
|
|
attestationCache: cache.NewAttestationCache(),
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
validatorServer := &ValidatorServer{
|
2019-03-05 23:06:50 +00:00
|
|
|
ctx: s.ctx,
|
|
|
|
beaconDB: s.beaconDB,
|
2019-09-09 21:13:50 +00:00
|
|
|
headFetcher: s.headFetcher,
|
2019-03-05 23:06:50 +00:00
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
2019-09-09 21:13:50 +00:00
|
|
|
blockFetcher: s.powChainService,
|
2019-09-12 04:30:04 +00:00
|
|
|
chainStartFetcher: s.chainStartFetcher,
|
|
|
|
depositFetcher: s.depositFetcher,
|
2019-09-19 08:00:47 +00:00
|
|
|
stateFeedListener: s.stateFeedListener,
|
|
|
|
chainStartChan: make(chan time.Time),
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-07-23 02:19:55 +00:00
|
|
|
nodeServer := &NodeServer{
|
2019-09-16 20:45:03 +00:00
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
server: s.grpcServer,
|
|
|
|
syncChecker: s.syncService,
|
|
|
|
genesisTimeFetcher: s.genesisTimeFetcher,
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
2019-07-23 20:29:13 +00:00
|
|
|
beaconChainServer := &BeaconChainServer{
|
2019-09-23 15:47:34 +00:00
|
|
|
beaconDB: s.beaconDB,
|
|
|
|
pool: s.attestationsPool,
|
|
|
|
headFetcher: s.headFetcher,
|
|
|
|
finalizationFetcher: s.finalizationFetcher,
|
|
|
|
chainStartFetcher: s.chainStartFetcher,
|
|
|
|
canonicalStateChan: s.canonicalStateChan,
|
2019-07-23 20:29:13 +00:00
|
|
|
}
|
2019-01-28 15:40:40 +00:00
|
|
|
pb.RegisterProposerServiceServer(s.grpcServer, proposerServer)
|
|
|
|
pb.RegisterAttesterServiceServer(s.grpcServer, attesterServer)
|
|
|
|
pb.RegisterValidatorServiceServer(s.grpcServer, validatorServer)
|
2019-07-23 02:19:55 +00:00
|
|
|
ethpb.RegisterNodeServer(s.grpcServer, nodeServer)
|
2019-07-23 20:29:13 +00:00
|
|
|
ethpb.RegisterBeaconChainServer(s.grpcServer, beaconChainServer)
|
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
|
|
|
|
}
|