2020-04-29 17:40:33 +00:00
|
|
|
// Package gateway defines a gRPC gateway to serve HTTP-JSON
|
|
|
|
// traffic as a proxy and forward it to a beacon node's gRPC service.
|
2019-06-02 15:33:44 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1_gateway"
|
2020-05-01 01:47:10 +00:00
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1_gateway"
|
2019-06-02 15:33:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = shared.Service(&Gateway{})
|
|
|
|
|
|
|
|
// Gateway is the gRPC gateway to serve HTTP JSON traffic as a proxy and forward
|
|
|
|
// it to the beacon-chain gRPC server.
|
|
|
|
type Gateway struct {
|
2020-05-01 01:47:10 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
gatewayAddr string
|
|
|
|
remoteAddr string
|
|
|
|
server *http.Server
|
|
|
|
mux *http.ServeMux
|
|
|
|
allowedOrigins []string
|
|
|
|
startFailure error
|
|
|
|
enableDebugRPCEndpoints bool
|
2020-05-06 21:01:23 +00:00
|
|
|
maxCallRecvMsgSize uint64
|
2019-06-02 15:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the gateway service. This serves the HTTP JSON traffic on the specified
|
|
|
|
// port.
|
|
|
|
func (g *Gateway) Start() {
|
|
|
|
ctx, cancel := context.WithCancel(g.ctx)
|
|
|
|
g.cancel = cancel
|
|
|
|
|
2020-06-03 18:11:43 +00:00
|
|
|
log.WithField("address", g.gatewayAddr).Info("Starting JSON-HTTP API")
|
2019-06-02 15:33:44 +00:00
|
|
|
|
2020-05-06 21:01:23 +00:00
|
|
|
conn, err := g.dial(ctx, "tcp", g.remoteAddr)
|
2019-06-02 15:33:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to connect to gRPC server")
|
|
|
|
g.startFailure = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g.conn = conn
|
2019-11-11 22:03:44 +00:00
|
|
|
|
2020-05-01 01:47:10 +00:00
|
|
|
gwmux := gwruntime.NewServeMux(
|
|
|
|
gwruntime.WithMarshalerOption(
|
|
|
|
gwruntime.MIMEWildcard,
|
|
|
|
&gwruntime.JSONPb{OrigName: false, EmitDefaults: true},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
handlers := []func(context.Context, *gwruntime.ServeMux, *grpc.ClientConn) error{
|
2019-10-22 00:15:57 +00:00
|
|
|
ethpb.RegisterNodeHandler,
|
|
|
|
ethpb.RegisterBeaconChainHandler,
|
|
|
|
ethpb.RegisterBeaconNodeValidatorHandler,
|
2020-05-01 01:47:10 +00:00
|
|
|
}
|
|
|
|
if g.enableDebugRPCEndpoints {
|
|
|
|
handlers = append(handlers, pbrpc.RegisterDebugHandler)
|
|
|
|
}
|
|
|
|
for _, f := range handlers {
|
2019-06-02 15:33:44 +00:00
|
|
|
if err := f(ctx, gwmux, conn); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to start gateway")
|
|
|
|
g.startFailure = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g.mux.Handle("/", gwmux)
|
|
|
|
|
|
|
|
g.server = &http.Server{
|
|
|
|
Addr: g.gatewayAddr,
|
2020-03-23 18:17:17 +00:00
|
|
|
Handler: newCorsHandler(g.mux, g.allowedOrigins),
|
2019-06-02 15:33:44 +00:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
if err := g.server.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
log.WithError(err).Error("Failed to listen and serve")
|
|
|
|
g.startFailure = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status of grpc gateway. Returns an error if this service is unhealthy.
|
|
|
|
func (g *Gateway) Status() error {
|
|
|
|
if g.startFailure != nil {
|
|
|
|
return g.startFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := g.conn.GetState(); s != connectivity.Ready {
|
|
|
|
return fmt.Errorf("grpc server is %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the gateway with a graceful shutdown.
|
|
|
|
func (g *Gateway) Stop() error {
|
2020-06-03 18:11:43 +00:00
|
|
|
if g.server != nil {
|
|
|
|
if err := g.server.Shutdown(g.ctx); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to shut down server")
|
|
|
|
}
|
2019-06-02 15:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if g.cancel != nil {
|
|
|
|
g.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new gateway server which translates HTTP into gRPC.
|
|
|
|
// Accepts a context and optional http.ServeMux.
|
2020-05-01 01:47:10 +00:00
|
|
|
func New(
|
|
|
|
ctx context.Context,
|
|
|
|
remoteAddress,
|
|
|
|
gatewayAddress string,
|
|
|
|
mux *http.ServeMux,
|
|
|
|
allowedOrigins []string,
|
|
|
|
enableDebugRPCEndpoints bool,
|
2020-05-06 21:01:23 +00:00
|
|
|
maxCallRecvMsgSize uint64,
|
2020-05-01 01:47:10 +00:00
|
|
|
) *Gateway {
|
2019-06-02 15:33:44 +00:00
|
|
|
if mux == nil {
|
|
|
|
mux = http.NewServeMux()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Gateway{
|
2020-05-01 01:47:10 +00:00
|
|
|
remoteAddr: remoteAddress,
|
|
|
|
gatewayAddr: gatewayAddress,
|
|
|
|
ctx: ctx,
|
|
|
|
mux: mux,
|
|
|
|
allowedOrigins: allowedOrigins,
|
|
|
|
enableDebugRPCEndpoints: enableDebugRPCEndpoints,
|
2020-05-06 21:01:23 +00:00
|
|
|
maxCallRecvMsgSize: maxCallRecvMsgSize,
|
2019-06-02 15:33:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial the gRPC server.
|
2020-05-06 21:01:23 +00:00
|
|
|
func (g *Gateway) dial(ctx context.Context, network, addr string) (*grpc.ClientConn, error) {
|
2019-06-02 15:33:44 +00:00
|
|
|
switch network {
|
|
|
|
case "tcp":
|
2020-05-06 21:01:23 +00:00
|
|
|
return g.dialTCP(ctx, addr)
|
2019-06-02 15:33:44 +00:00
|
|
|
case "unix":
|
2020-06-12 20:02:20 +00:00
|
|
|
return g.dialUnix(ctx, addr)
|
2019-06-02 15:33:44 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported network type %q", network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dialTCP creates a client connection via TCP.
|
|
|
|
// "addr" must be a valid TCP address with a port number.
|
2020-05-06 21:01:23 +00:00
|
|
|
func (g *Gateway) dialTCP(ctx context.Context, addr string) (*grpc.ClientConn, error) {
|
2020-06-12 20:02:20 +00:00
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(g.maxCallRecvMsgSize))),
|
2020-05-06 21:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return grpc.DialContext(
|
|
|
|
ctx,
|
|
|
|
addr,
|
|
|
|
opts...,
|
|
|
|
)
|
2019-06-02 15:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dialUnix creates a client connection via a unix domain socket.
|
|
|
|
// "addr" must be a valid path to the socket.
|
2020-06-12 20:02:20 +00:00
|
|
|
func (g *Gateway) dialUnix(ctx context.Context, addr string) (*grpc.ClientConn, error) {
|
2019-06-02 15:33:44 +00:00
|
|
|
d := func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", addr, timeout)
|
|
|
|
}
|
2020-06-12 20:02:20 +00:00
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithDialer(d),
|
|
|
|
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(g.maxCallRecvMsgSize))),
|
|
|
|
}
|
|
|
|
return grpc.DialContext(ctx, addr, opts...)
|
2019-06-02 15:33:44 +00:00
|
|
|
}
|