Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use same port for grpc-web and api server #14652

Merged
merged 22 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,13 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf
* (baseapp) [#14050](https://github.com/cosmos/cosmos-sdk/pull/14050) refactor `ABCIListener` interface to accept go contexts
* (store/streaming)[#14603](https://github.com/cosmos/cosmos-sdk/pull/14603) `StoreDecoderRegistry` moved from store to `types/simulations` this breaks the `AppModuleSimulation` interface.

### Client Breaking Changes

* (grpc-web) [#14652](https://github.com/cosmos/cosmos-sdk/pull/14652) Use same port for gRPC-Web and the API server.

### CLI Breaking Changes

* (grpc-web) [#14652](https://github.com/cosmos/cosmos-sdk/pull/14652) Remove `grpc-web.address` flag.
* (client) [#14342](https://github.com/cosmos/cosmos-sdk/pull/14342) `simd config` command is now a sub-command. Use `simd config --help` to learn more.
* (x/genutil) [#13535](https://github.com/cosmos/cosmos-sdk/pull/13535) Replace in `simd init`, the `--staking-bond-denom` flag with `--default-denom` which is used for all default denomination in the genesis, instead of only staking.
* (tx) [#12659](https://github.com/cosmos/cosmos-sdk/pull/12659) Remove broadcast mode `block`.
Expand Down
18 changes: 17 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ This guide provides instructions for upgrading to specific versions of Cosmos SD

## [Unreleased]

### Database configuration
### Configuration

A new tool have been created for migrating configuration of the SDK. Use the following command to migrate your configuration:

```bash
simd config migrate v0.48
```

More information about [confix](https://docs.cosmos.network/main/tooling/confix).

#### gRPC-Web

gRPC-Web is now listening to the same address as the API server (default: `localhost:1317`).
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
The possibility to listen to a different address has been removed, as well as its settings.
Use `confix` to clean-up your `app.toml`. A nginx (or alike) reverse-proxy can be set to keep the previous behavior.

#### Database

ClevelDB, BoltDB and BadgerDB are not supported anymore. To migrate from a unsupported database to a supported database please use the database migration tool.

Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Example:
f.String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")

// support old flags name for backwards compatibility
cmd.PersistentFlags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
f.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated but see #14655 (comment)

if name == "algo" {
name = flags.FlagKeyType
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/rosetta/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3"
services:
cosmos:
image: rosetta-ci:latest
command: ["simd", "start", "--pruning", "nothing", "--grpc.enable", "true", "--grpc.address", "0.0.0.0:9090", "--grpc-web.enable", "true", "--grpc-web.address", "0.0.0.0:9091"]
command: ["simd", "start", "--pruning", "nothing", "--grpc.enable", "true", "--grpc.address", "0.0.0.0:9090", "--grpc-web.enable", "true"]
ports:
- 9090:9090
- 26657:26657
Expand Down
46 changes: 35 additions & 11 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/tendermint/tendermint/libs/log"
tmrpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec/legacy"
Expand All @@ -28,6 +30,8 @@ type Server struct {
GRPCGatewayRouter *runtime.ServeMux
ClientCtx client.Context

GRPCSrv *grpc.Server

logger log.Logger
metrics *telemetry.Metrics
// Start() is blocking and generally called from a separate goroutine.
Expand All @@ -52,7 +56,7 @@ func CustomGRPCHeaderMatcher(key string) (string, bool) {
}
}

func New(clientCtx client.Context, logger log.Logger) *Server {
func New(clientCtx client.Context, logger log.Logger, grpcSrv *grpc.Server) *Server {
// The default JSON marshaller used by the gRPC-Gateway is unable to marshal non-nullable non-scalar fields.
// Using the gogo/gateway package with the gRPC-Gateway WithMarshaler option fixes the scalar field marshalling issue.
marshalerOption := &gateway.JSONPb{
Expand All @@ -63,9 +67,9 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
}

return &Server{
logger: logger,
Router: mux.NewRouter(),
ClientCtx: clientCtx,
logger: logger,
GRPCGatewayRouter: runtime.NewServeMux(
// Custom marshaler option is required for gogo proto
runtime.WithMarshalerOption(runtime.MIMEWildcard, marshalerOption),
Expand All @@ -78,6 +82,7 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
// GRPC metadata
runtime.WithIncomingHeaderMatcher(CustomGRPCHeaderMatcher),
),
GRPCSrv: grpcSrv,
}
}

Expand All @@ -100,18 +105,41 @@ func (s *Server) Start(cfg config.Config) error {
return err
}

s.registerGRPCGatewayRoutes()
s.listener = listener
var h http.Handler = s.Router

s.mtx.Unlock()

// configure grpc-web server
if cfg.GRPC.Enable && cfg.GRPCWeb.Enable {
var options []grpcweb.Option
if cfg.API.EnableUnsafeCORS {
options = append(options,
grpcweb.WithOriginFunc(func(origin string) bool {
return true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be hardcoded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have kept the same behavior as before. The API constructor will look a bit weird if we add grpcweb options.

}),
)
}

wrappedGrpc := grpcweb.WrapServer(s.GRPCSrv, options...)
s.Router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if wrappedGrpc.IsGrpcWebRequest(req) {
wrappedGrpc.ServeHTTP(w, req)
return
}

// Fall back to grpc gateway server.
s.GRPCGatewayRouter.ServeHTTP(w, req)
}))
}

// register grpc-gateway routes (after grpc-web server as the first match is used)
s.Router.PathPrefix("/").Handler(s.GRPCGatewayRouter)

s.logger.Info("starting API server...")
if cfg.API.EnableUnsafeCORS {
allowAllCORS := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))
return tmrpcserver.Serve(s.listener, allowAllCORS(h), s.logger, tmCfg)
return tmrpcserver.Serve(s.listener, allowAllCORS(s.Router), s.logger, tmCfg)
}

s.logger.Info("starting API server...")
return tmrpcserver.Serve(s.listener, s.Router, s.logger, tmCfg)
}

Expand All @@ -122,10 +150,6 @@ func (s *Server) Close() error {
return s.listener.Close()
}

func (s *Server) registerGRPCGatewayRoutes() {
s.Router.PathPrefix("/").Handler(s.GRPCGatewayRouter)
}

func (s *Server) SetTelemetry(m *telemetry.Metrics) {
s.mtx.Lock()
s.metrics = m
Expand Down
4 changes: 2 additions & 2 deletions server/grpc/grpc_web_test.go → server/api/server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpc_test
package api_test

import (
"bufio"
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *GRPCWebTestSuite) makeRequest(
contentType = "application/grpc-web-text"
}

url := fmt.Sprintf("http://%s%s", val.AppConfig.GRPCWeb.Address, method)
url := fmt.Sprintf("http://%s%s", strings.TrimPrefix(val.AppConfig.API.Address, "tcp://"), method)
req, err := http.NewRequest(verb, url, body)
s.Require().NoError(err, "failed creating a request")
req.Header = headers
Expand Down
12 changes: 1 addition & 11 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const (
// DefaultGRPCAddress defines the default address to bind the gRPC server to.
DefaultGRPCAddress = "localhost:9090"

// DefaultGRPCWebAddress defines the default address to bind the gRPC-web server to.
DefaultGRPCWebAddress = "localhost:9091"

// DefaultGRPCMaxRecvMsgSize defines the default gRPC max message size in
// bytes the server can receive.
DefaultGRPCMaxRecvMsgSize = 1024 * 1024 * 10
Expand Down Expand Up @@ -147,12 +144,6 @@ type GRPCConfig struct {
type GRPCWebConfig struct {
// Enable defines if the gRPC-web should be enabled.
Enable bool `mapstructure:"enable"`

// Address defines the gRPC-web server to listen on
Address string `mapstructure:"address"`

// EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk)
EnableUnsafeCORS bool `mapstructure:"enable-unsafe-cors"`
}

// StateSyncConfig defines the state sync snapshot configuration.
Expand Down Expand Up @@ -283,8 +274,7 @@ func DefaultConfig() *Config {
MaxSendMsgSize: DefaultGRPCMaxSendMsgSize,
},
GRPCWeb: GRPCWebConfig{
Enable: true,
Address: DefaultGRPCWebAddress,
Enable: true,
},
StateSync: StateSyncConfig{
SnapshotInterval: 0,
Expand Down
7 changes: 1 addition & 6 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,9 @@ max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}"

# GRPCWebEnable defines if the gRPC-web should be enabled.
# NOTE: gRPC must also be enabled, otherwise, this configuration is a no-op.
# NOTE: gRPC-Web uses the same address as the API server.
enable = {{ .GRPCWeb.Enable }}

# Address defines the gRPC-web server address to bind to.
address = "{{ .GRPCWeb.Address }}"

# EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk).
enable-unsafe-cors = {{ .GRPCWeb.EnableUnsafeCORS }}

###############################################################################
### State Sync Configuration ###
###############################################################################
Expand Down
46 changes: 0 additions & 46 deletions server/grpc/grpc_web.go

This file was deleted.

52 changes: 18 additions & 34 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"fmt"
"net"
"net/http"
"os"
"runtime/pprof"
"time"
Expand Down Expand Up @@ -69,11 +68,10 @@ const (
FlagAPIEnableUnsafeCORS = "api.enabled-unsafe-cors"

// gRPC-related flags
flagGRPCOnly = "grpc-only"
flagGRPCEnable = "grpc.enable"
flagGRPCAddress = "grpc.address"
flagGRPCWebEnable = "grpc-web.enable"
flagGRPCWebAddress = "grpc-web.address"
flagGRPCOnly = "grpc-only"
flagGRPCEnable = "grpc.enable"
flagGRPCAddress = "grpc.address"
flagGRPCWebEnable = "grpc-web.enable"

// mempool flags
FlagMempoolMaxTxs = "mempool.max-txs"
Expand Down Expand Up @@ -181,7 +179,6 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
cmd.Flags().String(flagGRPCAddress, serverconfig.DefaultGRPCAddress, "the gRPC server address to listen on")

cmd.Flags().Bool(flagGRPCWebEnable, true, "Define if the gRPC-Web server should be enabled. (Note: gRPC must also be enabled)")
cmd.Flags().String(flagGRPCWebAddress, serverconfig.DefaultGRPCWebAddress, "The gRPC-Web server address to listen on")

cmd.Flags().Uint64(FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval")
cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep")
Expand Down Expand Up @@ -364,7 +361,11 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
return err
}

var apiSrv *api.Server
var (
apiSrv *api.Server
grpcSrv *grpc.Server
)

if config.API.Enable {
genDoc, err := genDocProvider()
if err != nil {
Expand Down Expand Up @@ -407,9 +408,17 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App

clientCtx = clientCtx.WithGRPCClient(grpcClient)
ctx.Logger.Debug("grpc client assigned to client context", "target", grpcAddress)

// start grpc server
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
if err != nil {
return err
}
defer grpcSrv.Stop()
}

apiSrv = api.New(clientCtx, ctx.Logger.With("module", "api-server"))
// configure api server
apiSrv = api.New(clientCtx, ctx.Logger.With("module", "api-server"), grpcSrv)
app.RegisterAPIRoutes(apiSrv, config.API)
if config.Telemetry.Enabled {
apiSrv.SetTelemetry(metrics)
Expand All @@ -430,31 +439,6 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
}
}

var (
grpcSrv *grpc.Server
grpcWebSrv *http.Server
)

if config.GRPC.Enable {
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
if err != nil {
return err
}
defer grpcSrv.Stop()
if config.GRPCWeb.Enable {
grpcWebSrv, err = servergrpc.StartGRPCWeb(grpcSrv, config)
if err != nil {
ctx.Logger.Error("failed to start grpc-web http server: ", err)
return err
}
defer func() {
if err := grpcWebSrv.Close(); err != nil {
ctx.Logger.Error("failed to close grpc-web http server: ", err)
}
}()
}
}

// At this point it is safe to block the process if we're in gRPC only mode as
// we do not need to handle any Tendermint related processes.
if gRPCOnly {
Expand Down
2 changes: 1 addition & 1 deletion server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigDefault(t *testing.T) {
t.Fatalf("function failed with [%T] %v", err, err)
}

if "tcp://127.0.0.1:26657" != serverCtx.Config.RPC.ListenAddress { //nolint:stylecheck
if serverCtx.Config.RPC.ListenAddress != "tcp://127.0.0.1:26657" {
t.Error("RPCListenAddress is not using default")
}
}
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")

// support old flags name for backwards compatibility
cmd.PersistentFlags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated but see #14655 (comment)

if name == "algo" {
name = flags.FlagKeyType
}
Expand Down
Loading