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 ed5786fb668c..657d37f0f532 100644 --- a/simapp/v2/simdv2/cmd/testnet.go +++ b/simapp/v2/simdv2/cmd/testnet.go @@ -20,6 +20,8 @@ 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" banktypes "cosmossdk.io/x/bank/types" @@ -184,6 +186,7 @@ func initTestnetFiles[T transaction.Tx]( rpcPort = 26657 apiPort = 1317 grpcPort = 9090 + restPort = 8080 ) p2pPortStart := 26656 @@ -192,6 +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 @@ -205,6 +211,16 @@ 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), + } + + 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 +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)) - server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer) + grpcgatewayServer := grpcgateway.NewWithConfigOptions[T](grpcgateway.OverwriteDefaultConfig(grpcgatewayConfig)) + restServer := rest.NewWithConfigOptions[T](rest.OverwriteDefaultConfig(restConfig)) + server := serverv2.NewServer[T](serverCfg, cometServer, storeServer, grpcServer, grpcgatewayServer, restServer) err = server.WriteConfig(filepath.Join(nodeDir, "config")) if err != nil { return err