Skip to content

Commit

Permalink
Added "configure" command to cheqd-node CLI to set parameters (#187)
Browse files Browse the repository at this point in the history
* Added a new `cheqd-noded configure` command to change useful values in `app.toml` and `config.toml`. The following default parameters are set: min-gas-prices, fast-sync version, set `create-empty-blocks` to false. Additionally, the following optional parameters can be defined: seed mode, seeds, persistent peers, and external address.
* Set default keyring backend to `os` method
* Fixed typo in create-empty-blocks command

Co-authored-by: @askolesov @ankurdotb
  • Loading branch information
askolesov authored Nov 10, 2021
1 parent 572aa47 commit 43ebbeb
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 7 deletions.
271 changes: 271 additions & 0 deletions cmd/cheqd-noded/cmd/configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
package cmd

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cosmcfg "github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cobra"
tmcfg "github.com/tendermint/tendermint/config"
"strconv"
)

// configureCmd returns configure cobra Command.
func configureCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "configure",
Short: "Adjust node parameters",
}

cmd.AddCommand(
minGasPricesCmd(defaultNodeHome),
p2pCmd(defaultNodeHome),
rpcLaddrCmd(defaultNodeHome),
createEmptyBlocksCmd(defaultNodeHome))

return cmd
}

// p2pCmd returns configure cobra Command.
func p2pCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "p2p",
Short: "Adjust p2p parameters",
}

cmd.AddCommand(
seedModeCmd(defaultNodeHome),
seedsCmd(defaultNodeHome),
externalAddressCmd(defaultNodeHome),
persistentPeersCmd(defaultNodeHome),
sendRateCmd(defaultNodeHome),
recvRateCmd(defaultNodeHome),
maxPacketMsgPayloadSizeCmd(defaultNodeHome))

return cmd
}

// minGasPricesCmd returns configuration cobra Command.
func minGasPricesCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "min-gas-prices [value]",
Short: "Update min-gas-prices value in app.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

return updateCosmConfig(clientCtx.HomeDir, func(config *cosmcfg.Config) {
config.MinGasPrices = args[0]
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// seedModeCmd returns configuration cobra Command.
func seedModeCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "seed-mode [value]",
Short: "Update seed-mode value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

value, err := strconv.ParseBool(args[0])
if err != nil {
return errors.Wrap(err, "can't parse seed mode")
}

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.SeedMode = value
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// seedsCmd returns configuration cobra Command.
func seedsCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "seeds [value]",
Short: "Update seeds value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.Seeds = args[0]
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// externalAddressCmd returns configuration cobra Command.
func externalAddressCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "external-address [value]",
Short: "Update external-address value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.ExternalAddress = args[0]
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// persistentPeersCmd returns configuration cobra Command.
func persistentPeersCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "persistent-peers [value]",
Short: "Update persistent-peers value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.PersistentPeers = args[0]
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// sendRateCmd returns configuration cobra Command.
func sendRateCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "send-rate [value]",
Short: "Update send-rate value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

value, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return errors.Wrap(err, "can't parse send rate")
}

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.SendRate = value
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// recvRateCmd returns configuration cobra Command.
func recvRateCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "recv-rate [value]",
Short: "Update recv-rate value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

value, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return errors.Wrap(err, "can't parse recv rate")
}

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.RecvRate = value
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// maxPacketMsgPayloadSizeCmd returns configuration cobra Command.
func maxPacketMsgPayloadSizeCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "max-packet-msg-payload-size [value]",
Short: "Update max-packet-msg-payload-size value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

value, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return errors.Wrap(err, "can't parse max-packet-msg-payload-size")
}

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.P2P.MaxPacketMsgPayloadSize = int(value)
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// createEmptyBlocksCmd returns configuration cobra Command.
func createEmptyBlocksCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "create-empty-blocks [value]",
Short: "Update create-empty-blocks value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

value, err := strconv.ParseBool(args[0])
if err != nil {
return errors.Wrap(err, "can't parse create-empty-blocks")
}

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.Consensus.CreateEmptyBlocks = value
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}

// createEmptyBlocksCmd returns configuration cobra Command.
func rpcLaddrCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "rpc-laddr [value]",
Short: "Update rpc.laddr value in config.toml",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

return updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.RPC.ListenAddress = args[0]
})
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}
51 changes: 51 additions & 0 deletions cmd/cheqd-noded/cmd/init_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"github.com/cosmos/cosmos-sdk/client"
cosmcfg "github.com/cosmos/cosmos-sdk/server/config"
"github.com/spf13/cobra"
tmcfg "github.com/tendermint/tendermint/config"
)

func extendInit(initCmd *cobra.Command) *cobra.Command {
baseRunE := initCmd.RunE

initCmd.RunE = func(cmd *cobra.Command, args []string) error {
err := baseRunE(cmd, args)
if err != nil {
return err
}

err = applyConfigDefaults(cmd)
if err != nil {
return err
}

return nil
}

return initCmd
}

func applyConfigDefaults(cmd *cobra.Command) error {
clientCtx := client.GetClientContextFromCmd(cmd)

err := updateTmConfig(clientCtx.HomeDir, func(config *tmcfg.Config) {
config.FastSync.Version = "v2"
config.P2P.SendRate = 20000000
config.P2P.RecvRate = 20000000
config.P2P.MaxPacketMsgPayloadSize = 10240
})
if err != nil {
return err
}

err = updateCosmConfig(clientCtx.HomeDir, func(config *cosmcfg.Config) {
config.BaseConfig.MinGasPrices = "25ncheq"
})
if err != nil {
return err
}

return nil
}
12 changes: 5 additions & 7 deletions cmd/cheqd-noded/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
return err
}

// Allow us to overwrite the SDK's default server config.
srvCfg := serverconfig.DefaultConfig()
// TODO: We can overwrite default min_gas_price here

// Allows us to overwrite the SDK's default server config.
customAppTemplate := serverconfig.DefaultConfigTemplate
customAppConfig := *srvCfg
customAppConfig := serverconfig.DefaultConfig()

return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig)
},
Expand All @@ -79,7 +76,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
initRootCmd(rootCmd, encodingConfig)
overwriteFlagDefaults(rootCmd, map[string]string{
flags.FlagChainID: ChainID,
flags.FlagKeyringBackend: "test",
flags.FlagKeyringBackend: "os",
})

return rootCmd, encodingConfig
Expand All @@ -90,7 +87,8 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
cfg.Seal()

rootCmd.AddCommand(
genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome),
extendInit(genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome)),
configureCmd(app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.MigrateGenesisCmd(),
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
Expand Down
Loading

0 comments on commit 43ebbeb

Please sign in to comment.