Skip to content

Commit

Permalink
support query and tx commands for servers
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Dec 6, 2023
1 parent cfa334b commit b788bbb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 deletions.
11 changes: 7 additions & 4 deletions serverv2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
"github.com/spf13/cobra"
)

func Commands(homePath string, modules ...Module) ([]*cobra.Command, error) {
func Commands(homePath string, modules ...Module) (CLIConfig, error) {
if len(modules) == 0 {
// TODO figure if we should define default modules
// and if so it should be done here to avoid uncessary dependencies
return nil, errors.New("no modules provided")
return CLIConfig{}, errors.New("no modules provided")
}

server := NewServer(log.NewLogger(os.Stdout), modules...)
v, err := server.Config(filepath.Join(homePath, "config"))
if err != nil {
return nil, fmt.Errorf("failed to build server config: %w", err)
return CLIConfig{}, fmt.Errorf("failed to build server config: %w", err)
}

startCmd := &cobra.Command{
Expand Down Expand Up @@ -58,5 +58,8 @@ func Commands(homePath string, modules ...Module) ([]*cobra.Command, error) {
},
}

return append(server.CLICommands(), startCmd), nil
cmds := server.CLICommands()
cmds.Command = append(cmds.Command, startCmd)

return cmds, nil
}
24 changes: 24 additions & 0 deletions serverv2/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package serverv2

import "github.com/spf13/cobra"

var ServerContextKey = struct{}{}

// Config is the config of the main server.
type Config struct {
// StartBlock indicates if the server should block or not.
// If true, the server will block until the context is canceled.
StartBlock bool
}

// CLIConfig defines the CLI configuration for a module server.
type CLIConfig struct {
// Command defines the main command of a module server.
Command []*cobra.Command
// Query defines the query commands of a module server.
// Those commands are meant to be added in the root query command.
Query []*cobra.Command
// Tx defines the tx commands of a module server.
// Those commands are meant to be added in the root tx command.
Tx []*cobra.Command
}
10 changes: 0 additions & 10 deletions serverv2/context.go

This file was deleted.

13 changes: 13 additions & 0 deletions serverv2/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"

gogogrpc "github.com/cosmos/gogoproto/grpc"
"github.com/spf13/viper"
"google.golang.org/grpc"

"cosmossdk.io/log"
Expand Down Expand Up @@ -120,3 +121,15 @@ func (g Server) Stop(context.Context) error {

return nil
}

func (g Server) Config() (any, *viper.Viper) {
v := viper.New()

// TODO
v.Set("enable", true)
v.Set("address", g.config.Address)
v.Set("max_send_msg_size", g.config.MaxSendMsgSize)
v.Set("max_recv_msg_size", g.config.MaxRecvMsgSize)

return g.config, v
}
12 changes: 7 additions & 5 deletions serverv2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"

Expand All @@ -25,7 +24,7 @@ type Module interface {

// HasCLICommands is a server module that has CLI commands.
type HasCLICommands interface {
CLICommands() []*cobra.Command
CLICommands() CLIConfig
}

// HasConfig is a server module that has a config.
Expand Down Expand Up @@ -114,11 +113,14 @@ func (s *Server) Reload(ctx context.Context, moduleName string) error {
}

// CLICommands returns all CLI commands of all modules.
func (s *Server) CLICommands() []*cobra.Command {
var commands []*cobra.Command
func (s *Server) CLICommands() CLIConfig {
commands := CLIConfig{}

for _, mod := range s.modules {
if climod, ok := mod.(HasCLICommands); ok {
commands = append(commands, climod.CLICommands()...)
commands.Command = append(commands.Command, climod.CLICommands().Command...)
commands.Query = append(commands.Query, climod.CLICommands().Query...)
commands.Tx = append(commands.Tx, climod.CLICommands().Tx...)
}
}

Expand Down
30 changes: 16 additions & 14 deletions simapp/simd/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,7 @@ func initRootCmd(
)

// server.AddCommands(rootCmd, newApp, func(startCmd *cobra.Command) {})

// add keybase, auxiliary RPC, query, genesis, and tx child commands
rootCmd.AddCommand(
server.StatusCommand(),
genesisCommand(txConfig, basicManager, appExport),
queryCommand(),
txCommand(),
keys.Commands(),
)

// experimental commands
// server v2 commands
home, _ := rootCmd.Flags().GetString(flags.FlagHome)
if home == "" {
home = simapp.DefaultNodeHome
Expand All @@ -76,8 +66,16 @@ func initRootCmd(
if err != nil {
panic(err)
}

rootCmd.AddCommand(serverv2Cmds.Command...)

// add keybase, auxiliary RPC, query, genesis, and tx child commands
rootCmd.AddCommand(
serverv2Cmds...,
server.StatusCommand(),
genesisCommand(txConfig, basicManager, appExport),
queryCommand(serverv2Cmds.Query...),
txCommand(serverv2Cmds.Tx...),
keys.Commands(),
)
}

Expand All @@ -91,7 +89,7 @@ func genesisCommand(txConfig client.TxConfig, basicManager module.BasicManager,
return cmd
}

func queryCommand() *cobra.Command {
func queryCommand(extraCmds ...*cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Expand All @@ -110,10 +108,12 @@ func queryCommand() *cobra.Command {
server.QueryBlockResultsCmd(),
)

cmd.AddCommand(extraCmds...)

return cmd
}

func txCommand() *cobra.Command {
func txCommand(extraCmds ...*cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
Expand All @@ -134,6 +134,8 @@ func txCommand() *cobra.Command {
authcmd.GetSimulateCmd(),
)

cmd.AddCommand(extraCmds...)

return cmd
}

Expand Down

0 comments on commit b788bbb

Please sign in to comment.