From e7fe651b4900bdf768accab87f354c6272f9357c Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 2 Dec 2024 18:32:49 +0800 Subject: [PATCH 1/2] fix(simapp/v2): failed to start HTTP server on port 8080 conflict (#22687) --- simapp/v2/simdv2/cmd/testnet.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/simapp/v2/simdv2/cmd/testnet.go b/simapp/v2/simdv2/cmd/testnet.go index ed5786fb668c..dab0f327d037 100644 --- a/simapp/v2/simdv2/cmd/testnet.go +++ b/simapp/v2/simdv2/cmd/testnet.go @@ -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/rest" "cosmossdk.io/server/v2/cometbft" "cosmossdk.io/server/v2/store" banktypes "cosmossdk.io/x/bank/types" @@ -184,6 +185,7 @@ func initTestnetFiles[T transaction.Tx]( rpcPort = 26657 apiPort = 1317 grpcPort = 9090 + restPort = 8080 ) p2pPortStart := 26656 @@ -192,6 +194,7 @@ func initTestnetFiles[T transaction.Tx]( for i := 0; i < args.numValidators; i++ { var portOffset int grpcConfig := grpc.DefaultConfig() + restConfig := rest.DefaultConfig() if args.singleMachine { portOffset = i p2pPortStart = 16656 // use different start point to not conflict with rpc port @@ -205,6 +208,11 @@ func initTestnetFiles[T transaction.Tx]( MaxRecvMsgSize: grpc.DefaultConfig().MaxRecvMsgSize, MaxSendMsgSize: grpc.DefaultConfig().MaxSendMsgSize, } + + restConfig = &rest.Config{ + Enable: true, + Address: fmt.Sprintf("127.0.0.1:%d", restPort+portOffset), + } } nodeDirName := fmt.Sprintf("%s%d", args.nodeDirPrefix, i) @@ -338,7 +346,8 @@ 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) + restServer := rest.NewWithConfigOptions[T](rest.OverwriteDefaultConfig(restConfig)) + server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer, restServer) err = server.WriteConfig(filepath.Join(nodeDir, "config")) if err != nil { return err From f350775d0ed21ccfa36643698215dc612281bb16 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 2 Dec 2024 19:51:47 +0800 Subject: [PATCH 2/2] feat(server/v2/grpcgateway): register grpcgateway server and module endpoints (#22701) Co-authored-by: Julien Robert --- server/v2/api/grpcgateway/server.go | 17 ++++++++++------- simapp/v2/simdv2/cmd/commands.go | 20 ++++++++++++++++++++ simapp/v2/simdv2/cmd/testnet.go | 11 ++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/server/v2/api/grpcgateway/server.go b/server/v2/api/grpcgateway/server.go index 7fba8ce1be20..16a622a94961 100644 --- a/server/v2/api/grpcgateway/server.go +++ b/server/v2/api/grpcgateway/server.go @@ -9,7 +9,6 @@ import ( gateway "github.com/cosmos/gogogateway" "github.com/cosmos/gogoproto/jsonpb" "github.com/grpc-ecosystem/grpc-gateway/runtime" - "google.golang.org/grpc" "cosmossdk.io/core/server" "cosmossdk.io/core/transaction" @@ -30,15 +29,13 @@ type Server[T transaction.Tx] struct { cfgOptions []CfgOption server *http.Server - gRPCSrv *grpc.Server - gRPCGatewayRouter *runtime.ServeMux + GRPCGatewayRouter *runtime.ServeMux } // New creates a new gRPC-gateway server. func New[T transaction.Tx]( logger log.Logger, config server.ConfigMap, - grpcSrv *grpc.Server, ir jsonpb.AnyResolver, cfgOptions ...CfgOption, ) (*Server[T], error) { @@ -52,8 +49,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), @@ -83,6 +79,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 } @@ -108,7 +111,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, diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index 9b587e6dc477..a2eea8b49fbc 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -12,6 +12,7 @@ import ( 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" @@ -26,6 +27,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/rpc" sdktelemetry "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/version" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/genutil" @@ -85,6 +87,7 @@ func InitRootCmd[T transaction.Tx]( &serverstore.Server[T]{}, &telemetry.Server[T]{}, &rest.Server[T]{}, + &grpcgateway.Server[T]{}, ) } @@ -142,6 +145,22 @@ func InitRootCmd[T transaction.Tx]( return nil, err } + grpcgatewayServer, err := grpcgateway.New[T]( + logger, + deps.GlobalConfig, + simApp.InterfaceRegistry(), + ) + if err != nil { + return nil, err + } + + for _, mod := range deps.ModuleManager.Modules() { + if gmod, ok := mod.(module.HasGRPCGateway); ok { + // TODO(@julienrbrt) https://github.com/cosmos/cosmos-sdk/pull/22701#pullrequestreview-2470651390 + gmod.RegisterGRPCGatewayRoutes(deps.ClientContext, grpcgatewayServer.GRPCGatewayRouter) + } + } + // wire server commands return serverv2.AddCommands[T]( rootCmd, @@ -154,6 +173,7 @@ func InitRootCmd[T transaction.Tx]( storeComponent, telemetryServer, restServer, + grpcgatewayServer, ) } diff --git a/simapp/v2/simdv2/cmd/testnet.go b/simapp/v2/simdv2/cmd/testnet.go index dab0f327d037..657d37f0f532 100644 --- a/simapp/v2/simdv2/cmd/testnet.go +++ b/simapp/v2/simdv2/cmd/testnet.go @@ -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/api/rest" "cosmossdk.io/server/v2/cometbft" "cosmossdk.io/server/v2/store" @@ -194,7 +195,9 @@ func initTestnetFiles[T transaction.Tx]( for i := 0; i < args.numValidators; i++ { var portOffset int grpcConfig := grpc.DefaultConfig() + grpcgatewayConfig := grpcgateway.DefaultConfig() restConfig := rest.DefaultConfig() + if args.singleMachine { portOffset = i p2pPortStart = 16656 // use different start point to not conflict with rpc port @@ -209,6 +212,11 @@ func initTestnetFiles[T transaction.Tx]( MaxSendMsgSize: grpc.DefaultConfig().MaxSendMsgSize, } + grpcgatewayConfig = &grpcgateway.Config{ + Enable: true, + Address: fmt.Sprintf("127.0.0.1:%d", apiPort+portOffset), + } + restConfig = &rest.Config{ Enable: true, Address: fmt.Sprintf("127.0.0.1:%d", restPort+portOffset), @@ -346,8 +354,9 @@ func initTestnetFiles[T transaction.Tx]( cometServer := cometbft.NewWithConfigOptions[T](cometbft.OverwriteDefaultConfigTomlConfig(nodeConfig)) storeServer := &store.Server[T]{} grpcServer := grpc.NewWithConfigOptions[T](grpc.OverwriteDefaultConfig(grpcConfig)) + grpcgatewayServer := grpcgateway.NewWithConfigOptions[T](grpcgateway.OverwriteDefaultConfig(grpcgatewayConfig)) restServer := rest.NewWithConfigOptions[T](rest.OverwriteDefaultConfig(restConfig)) - server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer, restServer) + server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer, grpcgatewayServer, restServer) err = server.WriteConfig(filepath.Join(nodeDir, "config")) if err != nil { return err