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(server/v2/grpcgateway): register grpcgateway server and module endpoints #22701

Merged
merged 15 commits into from
Dec 2, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* RocksDB libraries have been upgraded to support RockDB v9 instead of v8.
* (testutil/integration) [#22616](https://github.com/cosmos/cosmos-sdk/pull/22616) Remove double context in integration tests v1.
* Use integrationApp.Context() instead of creating a context prior.
* (server) [#22701](https://github.com/cosmos/cosmos-sdk/pull/22701) Register grpcgateway server and module endpoints for server/v2.
Copy link
Member

Choose a reason for hiding this comment

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

no changelog needed, thanks!


### Bug Fixes

Expand Down
4 changes: 4 additions & 0 deletions server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet {
return flags
}

func (s *Server[T]) GrpcServer() *grpc.Server {
Copy link
Member

Choose a reason for hiding this comment

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

See other comment, if we don't need the grpc server, let's not expose this

return s.grpcSrv
}

func makeUnknownServiceHandler(
handlers map[string]appmodulev2.Handler,
queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error),
Expand Down
13 changes: 10 additions & 3 deletions server/v2/api/grpcgateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Server[T transaction.Tx] struct {

server *http.Server
gRPCSrv *grpc.Server
Copy link
Member

Choose a reason for hiding this comment

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

Looks like grpc.Server isn't used here. Let's remove it from here

gRPCGatewayRouter *runtime.ServeMux
GRPCGatewayRouter *runtime.ServeMux
}

// New creates a new gRPC-gateway server.
Expand All @@ -53,7 +53,7 @@ func New[T transaction.Tx](

s := &Server[T]{
gRPCSrv: grpcSrv,
gRPCGatewayRouter: runtime.NewServeMux(
GRPCGatewayRouter: runtime.NewServeMux(
// Custom marshaler option is required for gogo proto
runtime.WithMarshalerOption(runtime.MIMEWildcard, marshalerOption),

Expand Down Expand Up @@ -83,6 +83,13 @@ func New[T transaction.Tx](
return s, nil
}

// NewWithConfigOptions creates a new gRPC-gateway server with the provided config options.
func NewWithConfigOptions[T transaction.Tx](opts ...CfgOption) *Server[T] {
return &Server[T]{
cfgOptions: opts,
}
}

func (s *Server[T]) Name() string {
return ServerName
}
Expand All @@ -108,7 +115,7 @@ func (s *Server[T]) Start(ctx context.Context) error {
}

mux := http.NewServeMux()
mux.Handle("/", s.gRPCGatewayRouter)
mux.Handle("/", s.GRPCGatewayRouter)

s.server = &http.Server{
Addr: s.config.Address,
Expand Down
27 changes: 27 additions & 0 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
runtimev2 "cosmossdk.io/runtime/v2"
serverv2 "cosmossdk.io/server/v2"
grpcserver "cosmossdk.io/server/v2/api/grpc"
"cosmossdk.io/server/v2/api/grpcgateway"
"cosmossdk.io/server/v2/api/rest"
"cosmossdk.io/server/v2/api/telemetry"
"cosmossdk.io/server/v2/cometbft"
Expand All @@ -20,6 +21,7 @@
confixcmd "cosmossdk.io/tools/confix/cmd"

"github.com/cosmos/cosmos-sdk/client"
sdkclient "github.com/cosmos/cosmos-sdk/client"
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid duplicate imports of the same package

The package "github.com/cosmos/cosmos-sdk/client" is imported twice: once without an alias and once with the alias sdkclient. This can cause confusion and potential conflicts.

Apply the following diff to remove the duplicate import:

-	"github.com/cosmos/cosmos-sdk/client"

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

24-24: ST1019(related information): other import of "github.com/cosmos/cosmos-sdk/client"

(stylecheck)

"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/keys"
Expand All @@ -31,8 +33,13 @@
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2/cli"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
)

type ModuleWithGRPCGateway interface {
Copy link
Member

Choose a reason for hiding this comment

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

to delete

RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *gwruntime.ServeMux)
}

// CommandDependencies is a struct that contains all the dependencies needed to initialize the root command.
// an alternative design could fetch these even later from the command context
type CommandDependencies[T transaction.Tx] struct {
Expand Down Expand Up @@ -83,6 +90,7 @@
&serverstore.Server[T]{},
&telemetry.Server[T]{},
&rest.Server[T]{},
&grpcgateway.Server[T]{},
)
}

Expand Down Expand Up @@ -140,6 +148,24 @@
return nil, err
}

grpcSrv := grpcServer.GrpcServer()

grpcgatewayServer, err := grpcgateway.New[T](
logger,
deps.GlobalConfig,
grpcSrv,
simApp.InterfaceRegistry(),
)
if err != nil {
return nil, err
}

for _, mod := range deps.ModuleManager.Modules() {
if gmod, ok := mod.(ModuleWithGRPCGateway); ok {
Copy link
Member

Choose a reason for hiding this comment

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

let's use module.HasGRPCGateway (from types/modules), and can you add a /TODO(@julienrbrt) and link #22701 (review)

gmod.RegisterGRPCGatewayRoutes(deps.ClientContext, grpcgatewayServer.GRPCGatewayRouter)
}
}
Fixed Show fixed Hide fixed

// wire server commands
return serverv2.AddCommands[T](
rootCmd,
Expand All @@ -152,6 +178,7 @@
storeComponent,
telemetryServer,
restServer,
grpcgatewayServer,
)
}

Expand Down
11 changes: 10 additions & 1 deletion simapp/v2/simdv2/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
runtimev2 "cosmossdk.io/runtime/v2"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/api/grpc"
"cosmossdk.io/server/v2/api/grpcgateway"
"cosmossdk.io/server/v2/cometbft"
"cosmossdk.io/server/v2/store"
banktypes "cosmossdk.io/x/bank/types"
Expand Down Expand Up @@ -192,6 +193,7 @@ func initTestnetFiles[T transaction.Tx](
for i := 0; i < args.numValidators; i++ {
var portOffset int
grpcConfig := grpc.DefaultConfig()
grpcgatewayConfig := grpcgateway.DefaultConfig()
if args.singleMachine {
portOffset = i
p2pPortStart = 16656 // use different start point to not conflict with rpc port
Expand All @@ -205,6 +207,11 @@ func initTestnetFiles[T transaction.Tx](
MaxRecvMsgSize: grpc.DefaultConfig().MaxRecvMsgSize,
MaxSendMsgSize: grpc.DefaultConfig().MaxSendMsgSize,
}

grpcgatewayConfig = &grpcgateway.Config{
Enable: true,
Address: fmt.Sprintf("127.0.0.1:%d", apiPort+portOffset),
}
}

nodeDirName := fmt.Sprintf("%s%d", args.nodeDirPrefix, i)
Expand Down Expand Up @@ -338,7 +345,9 @@ func initTestnetFiles[T transaction.Tx](
cometServer := cometbft.NewWithConfigOptions[T](cometbft.OverwriteDefaultConfigTomlConfig(nodeConfig))
storeServer := &store.Server[T]{}
grpcServer := grpc.NewWithConfigOptions[T](grpc.OverwriteDefaultConfig(grpcConfig))
server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer)

grpcgatewayServer := grpcgateway.NewWithConfigOptions[T](grpcgateway.OverwriteDefaultConfig(grpcgatewayConfig))
server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer, grpcgatewayServer)
err = server.WriteConfig(filepath.Join(nodeDir, "config"))
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions tests/systemtests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ require (
pgregory.net/rapid v1.1.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace cosmossdk.io/systemtests => ../../systemtests
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove local replace directive before merging

The replace cosmossdk.io/systemtests => ../../systemtests directive is intended for local development and testing. Committing it can cause build issues for others who do not have the same directory structure.

Please remove the replace directive before merging to the main branch.

Copy link
Member

Choose a reason for hiding this comment

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

Could you remove that replace? We have rc.2 tagged

2 changes: 0 additions & 2 deletions tests/systemtests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ cosmossdk.io/math v1.4.0 h1:XbgExXFnXmF/CccPPEto40gOO7FpWu9yWNAZPN3nkNQ=
cosmossdk.io/math v1.4.0/go.mod h1:O5PkD4apz2jZs4zqFdTr16e1dcaQCc5z6lkEnrrppuk=
cosmossdk.io/store v1.1.0 h1:LnKwgYMc9BInn9PhpTFEQVbL9UK475G2H911CGGnWHk=
cosmossdk.io/store v1.1.0/go.mod h1:oZfW/4Fc/zYqu3JmQcQdUJ3fqu5vnYTn3LZFFy8P8ng=
cosmossdk.io/systemtests v1.0.0-rc.1.0.20241128092904-215d5c16f64d h1:zZUt9DD+3qvwnpQHPwYJ5+NEUU4wMFeNpPBegWMfbn8=
cosmossdk.io/systemtests v1.0.0-rc.1.0.20241128092904-215d5c16f64d/go.mod h1:ME2eFY/+2CjaFrPOMW8paPXupMzYaDMTKQ1sRvi71hU=
cosmossdk.io/x/tx v0.13.3-0.20240419091757-db5906b1e894 h1:kHEvzVqpNv/9pnaEPBsgE/FMc+cVmWjSsInRufkZkpQ=
cosmossdk.io/x/tx v0.13.3-0.20240419091757-db5906b1e894/go.mod h1:Tb6/tpONmtL5qFdOMdv1pdvrtJNxcazZBoz04HB71ss=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
Loading