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
17 changes: 10 additions & 7 deletions server/v2/api/grpcgateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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),

Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
20 changes: 20 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 @@ -26,6 +27,7 @@
"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"
Expand Down Expand Up @@ -85,6 +87,7 @@
&serverstore.Server[T]{},
&telemetry.Server[T]{},
&rest.Server[T]{},
&grpcgateway.Server[T]{},
)
}

Expand Down Expand Up @@ -142,6 +145,22 @@
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)
}
}
Comment on lines +157 to +162

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism

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

Expand Down
10 changes: 9 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,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)
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
Loading