Skip to content

Commit

Permalink
feat: use same port for grpc-web and api server (#14652)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jan 20, 2023
1 parent a4cf8e8 commit 4f6f6c0
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 150 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,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
44 changes: 32 additions & 12 deletions 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 gRPC Gateway API server (default: `localhost:1317`).
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 Expand Up @@ -32,6 +48,21 @@ References to `types/store.go` which contained aliases for store types have been
Previously, all modules were required to be set in `OrderBeginBlockers`, `OrderEndBlockers` and `OrderInitGenesis / OrderExportGenesis` in `app.go` / `app_config.go`.
This is no longer the case, the assertion has been loosened to only require modules implementing, respectively, the `module.BeginBlockAppModule`, `module.EndBlockAppModule` and `module.HasGenesis` interfaces.

### Modules

#### `x/gov`

##### Cancelling Proposals

The `gov` module has been updated to support the ability to cancel governance proposals. When a proposal is canceled, all the deposits of the proposal are either burnt or sent to `ProposalCancelDest` address. The deposits burn rate will be determined by a new parameter called `ProposalCancelRatio` parameter.

```text
1. deposits * proposal_cancel_ratio will be burned or sent to `ProposalCancelDest` address , if `ProposalCancelDest` is empty then deposits will be burned.
2. deposits * (1 - proposal_cancel_ratio) will be sent to depositors.
```

By default, the new `ProposalCancelRatio` parameter is set to 0.5 during migration and `ProposalCancelDest` is set to empty string (i.e. burnt).

## [v0.47.x](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.0)

### Simulation
Expand Down Expand Up @@ -201,17 +232,6 @@ func (app SimApp) RegisterUpgradeHandlers() {

```

##### New Feature: Cancelling Proposals

The `gov` module has been updated to support the ability to cancel governance proposals. When a proposal is canceled, all the deposits of the proposal are either burnt or sent to `ProposalCancelDest` address. The deposits burn rate will be determined by a new parameter called `ProposalCancelRatio` parameter.

```
1. deposits * proposal_cancel_ratio will be burned or sent to `ProposalCancelDest` address , if `ProposalCancelDest` is empty then deposits will be burned.
2. deposits * (1 - proposal_cancel_ratio) will be sent to depositors.
```

By default, the new `ProposalCancelRatio` parameter is set to 0.5 during migration and `ProposalCancelDest` is set to empty string (i.e. burnt).

#### `x/consensus`

Introducing a new `x/consensus` module to handle managing Tendermint consensus
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 {
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
}),
)
}

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.

Loading

0 comments on commit 4f6f6c0

Please sign in to comment.