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-11-12 15:57:27 +00:00
|
|
|
"math/rand"
|
2018-08-01 22:08:44 +00:00
|
|
|
"net"
|
2019-11-12 15:57:27 +00:00
|
|
|
"os"
|
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-09-29 18:48:55 +00:00
|
|
|
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
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-11-12 17:01:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/attester"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/beacon"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/node"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/proposer"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc/validator"
|
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"
|
2019-10-07 23:59:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
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")
|
2019-11-12 15:57:27 +00:00
|
|
|
rand.Seed(int64(os.Getpid()))
|
2019-02-04 21:54:30 +00:00
|
|
|
}
|
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-10-01 14:36:36 +00:00
|
|
|
forkFetcher blockchain.ForkFetcher
|
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-10-01 14:36:36 +00:00
|
|
|
ForkFetcher blockchain.ForkFetcher
|
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-10-01 14:36:36 +00:00
|
|
|
forkFetcher: cfg.ForkFetcher,
|
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() {
|
|
|
|
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-10-01 20:05:17 +00:00
|
|
|
log.WithField("port", fmt.Sprintf(":%s", s.port)).Info("RPC-API 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(
|
2019-10-07 23:59:08 +00:00
|
|
|
recovery.StreamServerInterceptor(
|
|
|
|
recovery.WithRecoveryHandlerContext(traceutil.RecoveryHandlerFunc),
|
|
|
|
),
|
2019-03-26 16:26:47 +00:00
|
|
|
grpc_prometheus.StreamServerInterceptor,
|
2019-09-29 18:48:55 +00:00
|
|
|
grpc_opentracing.StreamServerInterceptor(),
|
2019-03-11 01:19:52 +00:00
|
|
|
)),
|
|
|
|
grpc.UnaryInterceptor(middleware.ChainUnaryServer(
|
2019-10-07 23:59:08 +00:00
|
|
|
recovery.UnaryServerInterceptor(
|
|
|
|
recovery.WithRecoveryHandlerContext(traceutil.RecoveryHandlerFunc),
|
|
|
|
),
|
2019-03-26 16:26:47 +00:00
|
|
|
grpc_prometheus.UnaryServerInterceptor,
|
2019-09-29 18:48:55 +00:00
|
|
|
grpc_opentracing.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-11-12 17:01:27 +00:00
|
|
|
proposerServer := &proposer.Server{
|
|
|
|
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,
|
|
|
|
SyncChecker: s.syncService,
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
attesterServer := &attester.Server{
|
|
|
|
P2p: s.p2p,
|
|
|
|
BeaconDB: s.beaconDB,
|
|
|
|
OperationsHandler: s.operationsHandler,
|
|
|
|
AttReceiver: s.attestationReceiver,
|
|
|
|
HeadFetcher: s.headFetcher,
|
|
|
|
AttestationCache: cache.NewAttestationCache(),
|
|
|
|
SyncChecker: s.syncService,
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
validatorServer := &validator.Server{
|
|
|
|
Ctx: s.ctx,
|
|
|
|
BeaconDB: s.beaconDB,
|
|
|
|
HeadFetcher: s.headFetcher,
|
|
|
|
ForkFetcher: s.forkFetcher,
|
|
|
|
CanonicalStateChan: s.canonicalStateChan,
|
|
|
|
BlockFetcher: s.powChainService,
|
|
|
|
ChainStartFetcher: s.chainStartFetcher,
|
|
|
|
Eth1InfoFetcher: s.powChainService,
|
|
|
|
DepositFetcher: s.depositFetcher,
|
|
|
|
SyncChecker: s.syncService,
|
|
|
|
StateFeedListener: s.stateFeedListener,
|
|
|
|
ChainStartChan: make(chan time.Time),
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
nodeServer := &node.Server{
|
|
|
|
BeaconDB: s.beaconDB,
|
|
|
|
Server: s.grpcServer,
|
|
|
|
SyncChecker: s.syncService,
|
|
|
|
GenesisTimeFetcher: s.genesisTimeFetcher,
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
beaconChainServer := &beacon.Server{
|
|
|
|
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-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 {
|
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
|
|
|
|
}
|