From 170595fc88ec223df4c024bbcd81714ecf9d22c5 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 26 Jun 2024 03:02:50 +0200 Subject: [PATCH 01/14] use default extensions commands instead of cobra commands --- hermes/cmd/hermes.go | 156 ++++++++++++++++----- hermes/cmd/hermes_configure.go | 244 ++++++++++++--------------------- hermes/cmd/hermes_exec.go | 29 ++-- hermes/cmd/hermes_keys.go | 158 +++++++++------------ hermes/cmd/hermes_start.go | 29 ++-- hermes/go.mod | 4 +- hermes/main.go | 23 +++- 7 files changed, 306 insertions(+), 337 deletions(-) diff --git a/hermes/cmd/hermes.go b/hermes/cmd/hermes.go index 7567c7e0..5b19a32c 100644 --- a/hermes/cmd/hermes.go +++ b/hermes/cmd/hermes.go @@ -1,50 +1,134 @@ package cmd import ( - "github.com/spf13/cobra" + "github.com/ignite/cli/v28/ignite/services/plugin" + "github.com/spf13/pflag" ) const ( flagConfig = "config" ) -// NewRelayer creates a new Relayer command that holds -func NewRelayer() *cobra.Command { - root := &cobra.Command{ - Use: "relayer [command]", - Aliases: []string{"r"}, - Short: "Connect blockchains with an IBC relayer", - SilenceUsage: true, - SilenceErrors: true, +// GetCommands returns the list of explorer app commands. +func GetCommands() []*plugin.Command { + return []*plugin.Command{ + { + Use: "hermes [command]", + Short: "Connect blockchains with an IBC Hermes Relayer", + Aliases: []string{"h"}, + Flags: []*plugin.Flag{ + {Name: flagConfig, Usage: "set a custom Hermes config file", Shorthand: "c", Type: plugin.FlagTypeString}, + }, + Commands: []*plugin.Command{ + { + Use: "exec [args...]", + Short: "Execute a hermes raw command", + }, + { + Use: "start [chain-a-id] [chain-a-rpc]", + Short: "Start the Hermes relayer", + }, + { + Use: "keys [command]", + Short: "Start the Hermes relayer", + Commands: []*plugin.Command{ + { + Use: "add [chain-id] [mnemonic]", + Short: "Add a new key from mnemonic to Hermes relayer", + }, + { + Use: "file [chain-id] [filepath]", + Short: "Add a new key from a key file to Hermes relayer", + }, + { + Use: "list [chain-id]", + Short: "List Hermes relayer keys", + }, + { + Use: "delete [chain-id] [key-name]", + Short: "Delete a key from Hermes relayer", + }, + }, + }, + { + Use: "configure [chain-a-id] [chain-a-rpc] [chain-a-grpc] [chain-b-id] [chain-b-rpc] [chain-b-grpc]", + Short: "Configure the Hermes relayer creating the config file, client, channels and connection", + Flags: []*plugin.Flag{ + {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBPortID, DefaultValue: "transfer", Usage: "port ID of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainACCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain A is a CCV consumer", Type: plugin.FlagTypeBool}, + {Name: flagChainBCCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain B is a CCV consumer", Type: plugin.FlagTypeBool}, + {Name: flagChainAEventSourceURL, Usage: "WS event source url of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBEventSourceURL, Usage: "WS event source url of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAEventSourceMode, DefaultValue: "push", Usage: "WS event source mode of the chain A (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainBEventSourceMode, DefaultValue: "push", Usage: "WS event source mode of the chain B (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainAEventSourceBatchDelay, DefaultValue: "500ms", Usage: "WS event source batch delay time of the chain A (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainBEventSourceBatchDelay, DefaultValue: "500ms", Usage: "WS event source batch delay time of the chain B (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainARPCTimeout, DefaultValue: "10s", Usage: "RPC timeout of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBRPCTimeout, DefaultValue: "10s", Usage: "RPC timeout of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainATrustedNode, DefaultValue: "false", Usage: "enable trusted node on the chain A", Type: plugin.FlagTypeBool}, + {Name: flagChainBTrustedNode, DefaultValue: "false", Usage: "enable trusted node on the chain B", Type: plugin.FlagTypeBool}, + {Name: flagChainAAccountPrefix, DefaultValue: "cosmos", Usage: "account prefix of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBAccountPrefix, DefaultValue: "cosmos", Usage: "account prefix of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAKeyName, DefaultValue: "wallet", Usage: "hermes account name of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBKeyName, DefaultValue: "wallet", Usage: "hermes account name of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAAddressType, DefaultValue: "cosmos", Usage: "address type of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBAddressType, DefaultValue: "cosmos", Usage: "address type of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAStorePrefix, DefaultValue: "ibc", Usage: "store prefix of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBStorePrefix, DefaultValue: "ibc", Usage: "store prefix of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainADefaultGas, DefaultValue: "100000", Usage: "default gas used for transactions on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBDefaultGas, DefaultValue: "100000", Usage: "default gas used for transactions on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAMaxGas, DefaultValue: "400000", Usage: "max gas used for transactions on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBMaxGas, DefaultValue: "400000", Usage: "max gas used for transactions on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAGasPrice, DefaultValue: "0.025stake", Usage: "gas price used for transactions on chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBGasPrice, DefaultValue: "0.025stake", Usage: "gas price used for transactions on chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAGasMultiplier, DefaultValue: "1.1", Usage: "gas multiplier used for transactions on chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBGasMultiplier, DefaultValue: "1.1", Usage: "gas multiplier used for transactions on chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAMaxMsgNum, DefaultValue: "30", Usage: "max message number used for transactions on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBMaxMsgNum, DefaultValue: "30", Usage: "max message number used for transactions on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAMaxTxSize, DefaultValue: "2097152", Usage: "max transaction size on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBMaxTxSize, DefaultValue: "2097152", Usage: "max transaction size on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAClockDrift, DefaultValue: "5s", Usage: "clock drift of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBClockDrift, DefaultValue: "5s", Usage: "clock drift of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAMaxBlockTime, DefaultValue: "30s", Usage: "maximum block time of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBMaxBlockTime, DefaultValue: "30s", Usage: "maximum block time of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainATrustingPeriod, DefaultValue: "14days", Usage: "trusting period of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBTrustingPeriod, DefaultValue: "14days", Usage: "trusting period of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainATrustThresholdNumerator, DefaultValue: "2", Usage: "trusting threshold numerator of the chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBTrustThresholdNumerator, DefaultValue: "2", Usage: "trusting threshold numerator of the chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainATrustThresholdDenominator, DefaultValue: "3", Usage: "trusting threshold denominator of the chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBTrustThresholdDenominator, DefaultValue: "3", Usage: "trusting threshold denominator of the chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAMemoPrefix, Usage: "memo prefix of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBMemoPrefix, Usage: "memo prefix of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAFaucet, Usage: "faucet URL of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBFaucet, Usage: "faucet URL of the chain B", Type: plugin.FlagTypeString}, + {Name: flagTelemetryEnabled, DefaultValue: "false", Usage: "enable hermes telemetry", Type: plugin.FlagTypeBool}, + {Name: flagTelemetryHost, DefaultValue: "127.0.0.1", Usage: "hermes telemetry host", Type: plugin.FlagTypeString}, + {Name: flagTelemetryPort, DefaultValue: "3001", Usage: "hermes telemetry port", Type: plugin.FlagTypeUint64}, + {Name: flagRestEnabled, DefaultValue: "false", Usage: "enable hermes rest", Type: plugin.FlagTypeBool}, + {Name: flagRestHost, DefaultValue: "127.0.0.1", Usage: "hermes rest host", Type: plugin.FlagTypeString}, + {Name: flagRestPort, DefaultValue: "3000", Usage: "hermes rest port", Type: plugin.FlagTypeUint64}, + {Name: flagModeChannelsEnabled, DefaultValue: "true", Usage: "enable hermes channels", Type: plugin.FlagTypeBool}, + {Name: flagModeClientsEnabled, DefaultValue: "true", Usage: "enable hermes clients", Type: plugin.FlagTypeBool}, + {Name: flagModeClientsMisbehaviour, DefaultValue: "true", Usage: "enable hermes clients misbehaviour", Type: plugin.FlagTypeBool}, + {Name: flagModeClientsRefresh, DefaultValue: "true", Usage: "enable hermes client refresh time", Type: plugin.FlagTypeBool}, + {Name: flagModeConnectionsEnabled, DefaultValue: "true", Usage: "enable hermes connections", Type: plugin.FlagTypeBool}, + {Name: flagModePacketsEnabled, DefaultValue: "true", Usage: "enable hermes packets", Type: plugin.FlagTypeBool}, + {Name: flagModePacketsClearInterval, DefaultValue: "100", Usage: "hermes packet clear interval", Type: plugin.FlagTypeUint64}, + {Name: flagModePacketsClearOnStart, DefaultValue: "true", Usage: "enable hermes packets clear on start", Type: plugin.FlagTypeBool}, + {Name: flagModePacketsTxConfirmation, DefaultValue: "true", Usage: "hermes packet transaction confirmation", Type: plugin.FlagTypeBool}, + {Name: flagAutoRegisterCounterpartyPayee, DefaultValue: "false", Usage: "auto register the counterparty payee on a destination chain to the relayer's address on the source chain", Type: plugin.FlagTypeBool}, + {Name: flagGenerateWallets, DefaultValue: "false", Usage: "automatically generate wallets if they do not exist", Type: plugin.FlagTypeBool}, + {Name: flagOverwriteConfig, DefaultValue: "false", Usage: "overwrite the current config if it already exists", Type: plugin.FlagTypeBool}, + }, + }, + }, + }, } - root.AddCommand(NewHermes()) - return root } -// NewHermes creates a new Hermes relayer command that holds -// some other sub commands related to hermes relayer. -func NewHermes() *cobra.Command { - c := &cobra.Command{ - Use: "hermes [command]", - Aliases: []string{"h"}, - Short: "Hermes relayer wrapper", - } - - // configure flags. - c.PersistentFlags().StringP(flagConfig, "c", "", "set a custom Hermes config file") - - // add sub commands. - c.AddCommand( - NewHermesConfigure(), - NewHermesStart(), - NewHermesKeys(), - NewHermesExecute(), - ) - - return c -} - -func getConfig(cmd *cobra.Command) string { - config, _ := cmd.Flags().GetString(flagConfig) +func getConfig(flags *pflag.FlagSet) string { + config, _ := flags.GetString(flagConfig) return config } diff --git a/hermes/cmd/hermes_configure.go b/hermes/cmd/hermes_configure.go index de1023e1..f8223394 100644 --- a/hermes/cmd/hermes_configure.go +++ b/hermes/cmd/hermes_configure.go @@ -13,8 +13,9 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/cliui/cliquiz" "github.com/ignite/cli/v28/ignite/pkg/errors" + "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/manifoldco/promptui" - "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/ignite/apps/hermes/pkg/hermes" ) @@ -92,87 +93,13 @@ const ( mnemonicEntropySize = 256 ) -// NewHermesConfigure configure the hermes relayer and create the config file. -func NewHermesConfigure() *cobra.Command { - c := &cobra.Command{ - Use: "configure [chain-a-id] [chain-a-rpc] [chain-a-grpc] [chain-b-id] [chain-b-rpc] [chain-b-grpc]", - Short: "Configure the Hermes realyer creating the config file, client, channels and connection", - Args: cobra.ExactArgs(6), - RunE: hermesConfigureHandler, - } - - c.Flags().String(flagChainAPortID, "transfer", "Port ID of the chain A") - c.Flags().String(flagChainBPortID, "transfer", "Port ID of the chain B") - c.Flags().Bool(flagChainACCVConsumerChain, false, "only specify true if the chain A is a CCV consumer") - c.Flags().Bool(flagChainBCCVConsumerChain, false, "only specify true if the chain B is a CCV consumer") - c.Flags().String(flagChainAEventSourceURL, "", "WS event source url of the chain A") - c.Flags().String(flagChainBEventSourceURL, "", "WS event source url of the chain B") - c.Flags().String(flagChainAEventSourceMode, "push", "WS event source mode of the chain A (event source url should be set to use this flag)") - c.Flags().String(flagChainBEventSourceMode, "push", "WS event source mode of the chain B (event source url should be set to use this flag)") - c.Flags().String(flagChainAEventSourceBatchDelay, "500ms", "WS event source batch delay time of the chain A (event source url should be set to use this flag)") - c.Flags().String(flagChainBEventSourceBatchDelay, "500ms", "WS event source batch delay time of the chain B (event source url should be set to use this flag)") - c.Flags().String(flagChainARPCTimeout, "10s", "RPC timeout of the chain A") - c.Flags().String(flagChainBRPCTimeout, "10s", "RPC timeout of the chain B") - c.Flags().Bool(flagChainATrustedNode, false, "enable trusted node on the chain A") - c.Flags().Bool(flagChainBTrustedNode, false, "enable trusted node on the chain B") - c.Flags().String(flagChainAAccountPrefix, "cosmos", "account prefix of the chain A") - c.Flags().String(flagChainBAccountPrefix, "cosmos", "account prefix of the chain B") - c.Flags().String(flagChainAKeyName, "wallet", "hermes account name of the chain A") - c.Flags().String(flagChainBKeyName, "wallet", "hermes account name of the chain B") - c.Flags().String(flagChainAAddressType, "cosmos", "address type of the chain A") - c.Flags().String(flagChainBAddressType, "cosmos", "address type of the chain B") - c.Flags().String(flagChainAStorePrefix, "ibc", "store prefix of the chain A") - c.Flags().String(flagChainBStorePrefix, "ibc", "store prefix of the chain B") - c.Flags().Uint64(flagChainADefaultGas, 100000, "default gas used for transactions on chain A") - c.Flags().Uint64(flagChainBDefaultGas, 100000, "default gas used for transactions on chain B") - c.Flags().Uint64(flagChainAMaxGas, 400000, "max gas used for transactions on chain A") - c.Flags().Uint64(flagChainBMaxGas, 400000, "max gas used for transactions on chain B") - c.Flags().String(flagChainAGasPrice, "0.025stake", "gas price used for transactions on chain A") - c.Flags().String(flagChainBGasPrice, "0.025stake", "gas price used for transactions on chain B") - c.Flags().String(flagChainAGasMultiplier, "1.1", "gas multiplier used for transactions on chain A") - c.Flags().String(flagChainBGasMultiplier, "1.1", "gas multiplier used for transactions on chain B") - c.Flags().Uint64(flagChainAMaxMsgNum, 30, "max message number used for transactions on chain A") - c.Flags().Uint64(flagChainBMaxMsgNum, 30, "max message number used for transactions on chain B") - c.Flags().Uint64(flagChainAMaxTxSize, 2097152, "max transaction size on chain A") - c.Flags().Uint64(flagChainBMaxTxSize, 2097152, "max transaction size on chain B") - c.Flags().String(flagChainAClockDrift, "5s", "clock drift of the chain A") - c.Flags().String(flagChainBClockDrift, "5s", "clock drift of the chain B") - c.Flags().String(flagChainAMaxBlockTime, "30s", "maximum block time of the chain A") - c.Flags().String(flagChainBMaxBlockTime, "30s", "maximum block time of the chain B") - c.Flags().String(flagChainATrustingPeriod, "14days", "trusting period of the chain A") - c.Flags().String(flagChainBTrustingPeriod, "14days", "trusting period of the chain B") - c.Flags().Uint64(flagChainATrustThresholdNumerator, 2, "trusting threshold numerator of the chain A") - c.Flags().Uint64(flagChainBTrustThresholdNumerator, 2, "trusting threshold numerator of the chain B") - c.Flags().Uint64(flagChainATrustThresholdDenominator, 3, "trusting threshold denominator of the chain A") - c.Flags().Uint64(flagChainBTrustThresholdDenominator, 3, "trusting threshold denominator of the chain B") - c.Flags().String(flagChainAMemoPrefix, "", "memo prefix of the chain A") - c.Flags().String(flagChainBMemoPrefix, "", "memo prefix of the chain B") - c.Flags().String(flagChainAFaucet, "", "faucet URL of the chain A") - c.Flags().String(flagChainBFaucet, "", "faucet URL of the chain B") - - c.Flags().Bool(flagTelemetryEnabled, false, "enable hermes telemetry") - c.Flags().String(flagTelemetryHost, "127.0.0.1", "hermes telemetry host") - c.Flags().Uint64(flagTelemetryPort, 3001, "hermes telemetry port") - c.Flags().Bool(flagRestEnabled, false, "enable hermes rest") - c.Flags().String(flagRestHost, "127.0.0.1", "hermes rest host") - c.Flags().Uint64(flagRestPort, 3000, "hermes rest port") - c.Flags().Bool(flagModeChannelsEnabled, true, "enable hermes channels") - c.Flags().Bool(flagModeClientsEnabled, true, "enable hermes clients") - c.Flags().Bool(flagModeClientsMisbehaviour, true, "enable hermes clients misbehaviour") - c.Flags().Bool(flagModeClientsRefresh, true, "enable hermes client refresh time") - c.Flags().Bool(flagModeConnectionsEnabled, true, "enable hermes connections") - c.Flags().Bool(flagModePacketsEnabled, true, "enable hermes packets") - c.Flags().Uint64(flagModePacketsClearInterval, 100, "hermes packet clear interval") - c.Flags().Bool(flagModePacketsClearOnStart, true, "enable hermes packets clear on start") - c.Flags().Bool(flagModePacketsTxConfirmation, true, "hermes packet transaction confirmation") - c.Flags().Bool(flagAutoRegisterCounterpartyPayee, false, "auto register the counterparty payee on a destination chain to the relayer's address on the source chain") - c.Flags().Bool(flagGenerateWallets, false, "automatically generate wallets if they do not exist") - c.Flags().Bool(flagOverwriteConfig, false, "overwrite the current config if it already exists") - - return c -} +func ConfigureHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { + args := cmd.Args + flags, err := cmd.NewFlags() + if err != nil { + return err + } -func hermesConfigureHandler(cmd *cobra.Command, args []string) error { session := cliui.New(cliui.StartSpinner()) defer session.End() @@ -182,26 +109,23 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { chainAID = args[0] chainBID = args[3] - generateWallets, _ = cmd.Flags().GetBool(flagGenerateWallets) - overwriteConfig, _ = cmd.Flags().GetBool(flagOverwriteConfig) - chainAPortID, _ = cmd.Flags().GetString(flagChainAPortID) - chainAFaucet, _ = cmd.Flags().GetString(flagChainAFaucet) - chainBPortID, _ = cmd.Flags().GetString(flagChainBPortID) - chainBFaucet, _ = cmd.Flags().GetString(flagChainBFaucet) - customCfg = getConfig(cmd) + generateWallets, _ = flags.GetBool(flagGenerateWallets) + overwriteConfig, _ = flags.GetBool(flagOverwriteConfig) + chainAPortID, _ = flags.GetString(flagChainAPortID) + chainAFaucet, _ = flags.GetString(flagChainAFaucet) + chainBPortID, _ = flags.GetString(flagChainBPortID) + chainBFaucet, _ = flags.GetString(flagChainBFaucet) + customCfg = getConfig(flags) ) - var ( - hermesCfg *hermes.Config - err error - ) + var hermesCfg *hermes.Config if customCfg != "" { hermesCfg, err = hermes.LoadConfig(customCfg) if err != nil { return err } } else { - hermesCfg, err = newHermesConfig(cmd, args, customCfg) + hermesCfg, err = newHermesConfig(flags, args, customCfg) if err != nil { return err } @@ -242,7 +166,7 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { session.StartSpinner("Verifying chain keys") if err := ensureAccount( - cmd.Context(), + ctx, session, hermesCfg, h, @@ -255,7 +179,7 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { } if err := ensureAccount( - cmd.Context(), + ctx, session, hermesCfg, h, @@ -275,7 +199,7 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { clientAResult = hermes.ClientResult{} ) if err := h.CreateClient( - cmd.Context(), + ctx, chainAID, chainBID, hermes.WithConfigFile(cfgPath), @@ -301,7 +225,7 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { clientBResult = hermes.ClientResult{} ) if err := h.CreateClient( - cmd.Context(), + ctx, chainBID, chainAID, hermes.WithConfigFile(cfgPath), @@ -328,7 +252,7 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { connection = hermes.ConnectionResult{} ) if err := h.CreateConnection( - cmd.Context(), + ctx, chainAID, clientAResult.CreateClient.ClientID, clientBResult.CreateClient.ClientID, @@ -357,7 +281,7 @@ func hermesConfigureHandler(cmd *cobra.Command, args []string) error { channel = hermes.ConnectionResult{} ) if err := h.CreateChannel( - cmd.Context(), + ctx, chainAID, connection.ASide.ConnectionID, chainAPortID, @@ -506,7 +430,7 @@ GetKey: } // newHermesConfig create a new hermes config based in the cmd args. -func newHermesConfig(cmd *cobra.Command, args []string, customCfg string) (*hermes.Config, error) { +func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*hermes.Config, error) { // if a custom config was set, save it in the ignite hermes folder if customCfg != "" { c, err := hermes.LoadConfig(customCfg) @@ -518,22 +442,22 @@ func newHermesConfig(cmd *cobra.Command, args []string, customCfg string) (*herm // Create the default hermes config var ( - telemetryEnabled, _ = cmd.Flags().GetBool(flagTelemetryEnabled) - telemetryHost, _ = cmd.Flags().GetString(flagTelemetryHost) - telemetryPort, _ = cmd.Flags().GetUint64(flagTelemetryPort) - restEnabled, _ = cmd.Flags().GetBool(flagRestEnabled) - restHost, _ = cmd.Flags().GetString(flagRestHost) - restPort, _ = cmd.Flags().GetUint64(flagRestPort) - modeChannelsEnabled, _ = cmd.Flags().GetBool(flagModeChannelsEnabled) - modeClientsEnabled, _ = cmd.Flags().GetBool(flagModeClientsEnabled) - modeClientsMisbehaviour, _ = cmd.Flags().GetBool(flagModeClientsMisbehaviour) - modeClientsRefresh, _ = cmd.Flags().GetBool(flagModeClientsRefresh) - modeConnectionsEnabled, _ = cmd.Flags().GetBool(flagModeConnectionsEnabled) - modePacketsEnabled, _ = cmd.Flags().GetBool(flagModePacketsEnabled) - modePacketsClearInterval, _ = cmd.Flags().GetUint64(flagModePacketsClearInterval) - modePacketsClearOnStart, _ = cmd.Flags().GetBool(flagModePacketsClearOnStart) - modePacketsTxConfirmation, _ = cmd.Flags().GetBool(flagModePacketsTxConfirmation) - modePacketsAutoRegisterCounterpartyPayee, _ = cmd.Flags().GetBool(flagAutoRegisterCounterpartyPayee) + telemetryEnabled, _ = flags.GetBool(flagTelemetryEnabled) + telemetryHost, _ = flags.GetString(flagTelemetryHost) + telemetryPort, _ = flags.GetUint64(flagTelemetryPort) + restEnabled, _ = flags.GetBool(flagRestEnabled) + restHost, _ = flags.GetString(flagRestHost) + restPort, _ = flags.GetUint64(flagRestPort) + modeChannelsEnabled, _ = flags.GetBool(flagModeChannelsEnabled) + modeClientsEnabled, _ = flags.GetBool(flagModeClientsEnabled) + modeClientsMisbehaviour, _ = flags.GetBool(flagModeClientsMisbehaviour) + modeClientsRefresh, _ = flags.GetBool(flagModeClientsRefresh) + modeConnectionsEnabled, _ = flags.GetBool(flagModeConnectionsEnabled) + modePacketsEnabled, _ = flags.GetBool(flagModePacketsEnabled) + modePacketsClearInterval, _ = flags.GetUint64(flagModePacketsClearInterval) + modePacketsClearOnStart, _ = flags.GetBool(flagModePacketsClearOnStart) + modePacketsTxConfirmation, _ = flags.GetBool(flagModePacketsTxConfirmation) + modePacketsAutoRegisterCounterpartyPayee, _ = flags.GetBool(flagAutoRegisterCounterpartyPayee) ) c := hermes.DefaultConfig( @@ -561,28 +485,28 @@ func newHermesConfig(cmd *cobra.Command, args []string, customCfg string) (*herm chainARPCAddr = args[1] chainAGRPCAddr = args[2] - chainAEventSourceMode, _ = cmd.Flags().GetString(flagChainAEventSourceMode) - chainAEventSourceURL, _ = cmd.Flags().GetString(flagChainAEventSourceURL) - chainAEventSourceBatchDelay, _ = cmd.Flags().GetString(flagChainAEventSourceBatchDelay) - chainARPCTimeout, _ = cmd.Flags().GetString(flagChainARPCTimeout) - chainAAccountPrefix, _ = cmd.Flags().GetString(flagChainAAccountPrefix) - chainAAddressType, _ = cmd.Flags().GetString(flagChainAAddressType) - chainAKeyName, _ = cmd.Flags().GetString(flagChainAKeyName) - chainAStorePrefix, _ = cmd.Flags().GetString(flagChainAStorePrefix) - chainADefaultGas, _ = cmd.Flags().GetUint64(flagChainADefaultGas) - chainAMaxGas, _ = cmd.Flags().GetUint64(flagChainAMaxGas) - chainAGasPrice, _ = cmd.Flags().GetString(flagChainAGasPrice) - chainAGasMultiplier, _ = cmd.Flags().GetString(flagChainAGasMultiplier) - chainAMaxMsgNum, _ = cmd.Flags().GetUint64(flagChainAMaxMsgNum) - chainAMaxTxSize, _ = cmd.Flags().GetUint64(flagChainAMaxTxSize) - chainAClockDrift, _ = cmd.Flags().GetString(flagChainAClockDrift) - chainAMaxBlockTime, _ = cmd.Flags().GetString(flagChainAMaxBlockTime) - chainATrustingPeriod, _ = cmd.Flags().GetString(flagChainATrustingPeriod) - chainATrustThresholdNumerator, _ = cmd.Flags().GetUint64(flagChainATrustThresholdNumerator) - chainATrustThresholdDenominator, _ = cmd.Flags().GetUint64(flagChainATrustThresholdDenominator) - chainACCVConsumerChain, _ = cmd.Flags().GetBool(flagChainACCVConsumerChain) - chainATrustedNode, _ = cmd.Flags().GetBool(flagChainATrustedNode) - chainAMemoPrefix, _ = cmd.Flags().GetString(flagChainAMemoPrefix) + chainAEventSourceMode, _ = flags.GetString(flagChainAEventSourceMode) + chainAEventSourceURL, _ = flags.GetString(flagChainAEventSourceURL) + chainAEventSourceBatchDelay, _ = flags.GetString(flagChainAEventSourceBatchDelay) + chainARPCTimeout, _ = flags.GetString(flagChainARPCTimeout) + chainAAccountPrefix, _ = flags.GetString(flagChainAAccountPrefix) + chainAAddressType, _ = flags.GetString(flagChainAAddressType) + chainAKeyName, _ = flags.GetString(flagChainAKeyName) + chainAStorePrefix, _ = flags.GetString(flagChainAStorePrefix) + chainADefaultGas, _ = flags.GetUint64(flagChainADefaultGas) + chainAMaxGas, _ = flags.GetUint64(flagChainAMaxGas) + chainAGasPrice, _ = flags.GetString(flagChainAGasPrice) + chainAGasMultiplier, _ = flags.GetString(flagChainAGasMultiplier) + chainAMaxMsgNum, _ = flags.GetUint64(flagChainAMaxMsgNum) + chainAMaxTxSize, _ = flags.GetUint64(flagChainAMaxTxSize) + chainAClockDrift, _ = flags.GetString(flagChainAClockDrift) + chainAMaxBlockTime, _ = flags.GetString(flagChainAMaxBlockTime) + chainATrustingPeriod, _ = flags.GetString(flagChainATrustingPeriod) + chainATrustThresholdNumerator, _ = flags.GetUint64(flagChainATrustThresholdNumerator) + chainATrustThresholdDenominator, _ = flags.GetUint64(flagChainATrustThresholdDenominator) + chainACCVConsumerChain, _ = flags.GetBool(flagChainACCVConsumerChain) + chainATrustedNode, _ = flags.GetBool(flagChainATrustedNode) + chainAMemoPrefix, _ = flags.GetString(flagChainAMemoPrefix) ) chainAGasMulti := new(big.Float) @@ -666,28 +590,28 @@ func newHermesConfig(cmd *cobra.Command, args []string, customCfg string) (*herm chainBRPCAddr = args[4] chainBGRPCAddr = args[5] - chainBEventSourceMode, _ = cmd.Flags().GetString(flagChainBEventSourceMode) - chainBEventSourceURL, _ = cmd.Flags().GetString(flagChainBEventSourceURL) - chainBEventSourceBatchDelay, _ = cmd.Flags().GetString(flagChainBEventSourceBatchDelay) - chainBRPCTimeout, _ = cmd.Flags().GetString(flagChainBRPCTimeout) - chainBAccountPrefix, _ = cmd.Flags().GetString(flagChainBAccountPrefix) - chainBAddressType, _ = cmd.Flags().GetString(flagChainBAddressType) - chainBKeyName, _ = cmd.Flags().GetString(flagChainBKeyName) - chainBStorePrefix, _ = cmd.Flags().GetString(flagChainBStorePrefix) - chainBDefaultGas, _ = cmd.Flags().GetUint64(flagChainBDefaultGas) - chainBMaxGas, _ = cmd.Flags().GetUint64(flagChainBMaxGas) - chainBGasPrice, _ = cmd.Flags().GetString(flagChainBGasPrice) - chainBGasMultiplier, _ = cmd.Flags().GetString(flagChainBGasMultiplier) - chainBMaxMsgNum, _ = cmd.Flags().GetUint64(flagChainBMaxMsgNum) - chainBMaxTxSize, _ = cmd.Flags().GetUint64(flagChainBMaxTxSize) - chainBClockDrift, _ = cmd.Flags().GetString(flagChainBClockDrift) - chainBMaxBlockTime, _ = cmd.Flags().GetString(flagChainBMaxBlockTime) - chainBTrustingPeriod, _ = cmd.Flags().GetString(flagChainBTrustingPeriod) - chainBTrustThresholdNumerator, _ = cmd.Flags().GetUint64(flagChainBTrustThresholdNumerator) - chainBTrustThresholdDenominator, _ = cmd.Flags().GetUint64(flagChainBTrustThresholdDenominator) - chainBCCVConsumerChain, _ = cmd.Flags().GetBool(flagChainBCCVConsumerChain) - chainBTrustedNode, _ = cmd.Flags().GetBool(flagChainBTrustedNode) - chainBMemoPrefix, _ = cmd.Flags().GetString(flagChainBMemoPrefix) + chainBEventSourceMode, _ = flags.GetString(flagChainBEventSourceMode) + chainBEventSourceURL, _ = flags.GetString(flagChainBEventSourceURL) + chainBEventSourceBatchDelay, _ = flags.GetString(flagChainBEventSourceBatchDelay) + chainBRPCTimeout, _ = flags.GetString(flagChainBRPCTimeout) + chainBAccountPrefix, _ = flags.GetString(flagChainBAccountPrefix) + chainBAddressType, _ = flags.GetString(flagChainBAddressType) + chainBKeyName, _ = flags.GetString(flagChainBKeyName) + chainBStorePrefix, _ = flags.GetString(flagChainBStorePrefix) + chainBDefaultGas, _ = flags.GetUint64(flagChainBDefaultGas) + chainBMaxGas, _ = flags.GetUint64(flagChainBMaxGas) + chainBGasPrice, _ = flags.GetString(flagChainBGasPrice) + chainBGasMultiplier, _ = flags.GetString(flagChainBGasMultiplier) + chainBMaxMsgNum, _ = flags.GetUint64(flagChainBMaxMsgNum) + chainBMaxTxSize, _ = flags.GetUint64(flagChainBMaxTxSize) + chainBClockDrift, _ = flags.GetString(flagChainBClockDrift) + chainBMaxBlockTime, _ = flags.GetString(flagChainBMaxBlockTime) + chainBTrustingPeriod, _ = flags.GetString(flagChainBTrustingPeriod) + chainBTrustThresholdNumerator, _ = flags.GetUint64(flagChainBTrustThresholdNumerator) + chainBTrustThresholdDenominator, _ = flags.GetUint64(flagChainBTrustThresholdDenominator) + chainBCCVConsumerChain, _ = flags.GetBool(flagChainBCCVConsumerChain) + chainBTrustedNode, _ = flags.GetBool(flagChainBTrustedNode) + chainBMemoPrefix, _ = flags.GetString(flagChainBMemoPrefix) ) chainBGasMulti := new(big.Float) diff --git a/hermes/cmd/hermes_exec.go b/hermes/cmd/hermes_exec.go index aa8ddfd4..b6e1fbce 100644 --- a/hermes/cmd/hermes_exec.go +++ b/hermes/cmd/hermes_exec.go @@ -1,24 +1,15 @@ package cmd import ( - "github.com/spf13/cobra" + "context" + "os" + + "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/ignite/apps/hermes/pkg/hermes" ) -// NewHermesExecute execute hermes relayer commands. -func NewHermesExecute() *cobra.Command { - c := &cobra.Command{ - Use: "exec [args...]", - Short: "Execute a hermes raw command", - Args: cobra.MinimumNArgs(1), - RunE: hermesExecuteHandler, - } - - return c -} - -func hermesExecuteHandler(cmd *cobra.Command, args []string) error { +func ExecuteHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { h, err := hermes.New() if err != nil { return err @@ -26,10 +17,10 @@ func hermesExecuteHandler(cmd *cobra.Command, args []string) error { defer h.Cleanup() return h.Run( - cmd.Context(), - hermes.WithArgs(args...), - hermes.WithStdIn(cmd.InOrStdin()), - hermes.WithStdOut(cmd.OutOrStdout()), - hermes.WithStdErr(cmd.ErrOrStderr()), + ctx, + hermes.WithArgs(cmd.Args...), + hermes.WithStdIn(os.Stdin), + hermes.WithStdOut(os.Stdout), + hermes.WithStdErr(os.Stderr), ) } diff --git a/hermes/cmd/hermes_keys.go b/hermes/cmd/hermes_keys.go index 36f826db..e321d74c 100644 --- a/hermes/cmd/hermes_keys.go +++ b/hermes/cmd/hermes_keys.go @@ -1,117 +1,81 @@ package cmd import ( - "github.com/spf13/cobra" + "context" + "os" + + "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/ignite/apps/hermes/pkg/hermes" ) -// NewHermesKeys manage the hermes relayer keys. -func NewHermesKeys() *cobra.Command { - c := &cobra.Command{ - Use: "keys", - Short: "Manage the Hermes keys", +func KeysAddMnemonicHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { + args := cmd.Args + h, err := hermes.New() + if err != nil { + return err } - c.AddCommand( - NewHermesKeysAddMnemonic(), - NewHermesKeysAddFile(), - NewHermesKeysList(), - NewHermesKeysDelete(), + defer h.Cleanup() + + return h.AddMnemonic( + ctx, + args[0], + args[1], + hermes.WithStdIn(os.Stdin), + hermes.WithStdOut(os.Stdout), + hermes.WithStdErr(os.Stderr), ) - return c } -// NewHermesKeysAddMnemonic add a hermes relayer mnemonic key. -func NewHermesKeysAddMnemonic() *cobra.Command { - c := &cobra.Command{ - Use: "add [chain-id] [mnemonic]", - Short: "Add a new key from mnemonic to Hermes relayer", - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - h, err := hermes.New() - if err != nil { - return err - } - defer h.Cleanup() - - return h.AddMnemonic( - cmd.Context(), - args[0], - args[1], - hermes.WithStdOut(cmd.OutOrStdout()), - ) - }, +func KeysAddFileHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { + args := cmd.Args + h, err := hermes.New() + if err != nil { + return err } - return c -} + defer h.Cleanup() -// NewHermesKeysAddFile add a hermes relayer key file. -func NewHermesKeysAddFile() *cobra.Command { - c := &cobra.Command{ - Use: "file [chain-id] [filepath]", - Short: "Add a new key from a key file to Hermes relayer", - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - h, err := hermes.New() - if err != nil { - return err - } - defer h.Cleanup() - - return h.AddKey( - cmd.Context(), - args[0], - args[1], - hermes.WithStdOut(cmd.OutOrStdout()), - ) - }, - } - return c + return h.AddKey( + ctx, + args[0], + args[1], + hermes.WithStdIn(os.Stdin), + hermes.WithStdOut(os.Stdout), + hermes.WithStdErr(os.Stderr), + ) } -// NewHermesKeysList list hermes relayer keys. -func NewHermesKeysList() *cobra.Command { - c := &cobra.Command{ - Use: "list [chain-id]", - Short: "List Hermes relayer keys", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - h, err := hermes.New() - if err != nil { - return err - } - defer h.Cleanup() - - return h.KeysList( - cmd.Context(), - args[0], - hermes.WithStdOut(cmd.OutOrStdout()), - ) - }, +func KeysList(ctx context.Context, cmd *plugin.ExecutedCommand) error { + args := cmd.Args + h, err := hermes.New() + if err != nil { + return err } - return c -} + defer h.Cleanup() -// NewHermesKeysDelete deletes a hermes relayer key. -func NewHermesKeysDelete() *cobra.Command { - c := &cobra.Command{ - Use: "delete [chain-id] [key-name]", - Short: "Delete a key from Hermes relayer", - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - h, err := hermes.New() - if err != nil { - return err - } - defer h.Cleanup() + return h.KeysList( + ctx, + args[0], + hermes.WithStdIn(os.Stdin), + hermes.WithStdOut(os.Stdout), + hermes.WithStdErr(os.Stderr), + ) +} - return h.DeleteKey( - cmd.Context(), - args[0], - args[1], - hermes.WithStdOut(cmd.OutOrStdout()), - ) - }, +func NewHermesKeysDelete(ctx context.Context, cmd *plugin.ExecutedCommand) error { + h, err := hermes.New() + if err != nil { + return err } - return c + defer h.Cleanup() + + args := cmd.Args + return h.DeleteKey( + ctx, + args[0], + args[1], + hermes.WithStdIn(os.Stdin), + hermes.WithStdOut(os.Stdout), + hermes.WithStdErr(os.Stderr), + ) } diff --git a/hermes/cmd/hermes_start.go b/hermes/cmd/hermes_start.go index bc97de21..33140f89 100644 --- a/hermes/cmd/hermes_start.go +++ b/hermes/cmd/hermes_start.go @@ -1,30 +1,25 @@ package cmd import ( + "context" "os" "strings" "github.com/ignite/cli/v28/ignite/pkg/errors" - "github.com/spf13/cobra" + "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/ignite/apps/hermes/pkg/hermes" ) -// NewHermesStart start the hermes relayer. -func NewHermesStart() *cobra.Command { - c := &cobra.Command{ - Use: "start [chain-a-id] [chain-a-rpc]", - Short: "Start the Hermes realyer", - Args: cobra.ExactArgs(2), - RunE: hermesStartHandler, +func StartHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { + flags, err := cmd.NewFlags() + if err != nil { + return err } - return c -} - -func hermesStartHandler(cmd *cobra.Command, args []string) (err error) { var ( - customCfg = getConfig(cmd) + args = cmd.Args + customCfg = getConfig(flags) cfgName = strings.Join(args, hermes.ConfigNameSeparator) ) @@ -47,10 +42,10 @@ func hermesStartHandler(cmd *cobra.Command, args []string) (err error) { defer h.Cleanup() return h.Start( - cmd.Context(), + ctx, hermes.WithConfigFile(cfgPath), - hermes.WithStdIn(cmd.InOrStdin()), - hermes.WithStdOut(cmd.OutOrStdout()), - hermes.WithStdErr(cmd.ErrOrStderr()), + hermes.WithStdIn(os.Stdin), + hermes.WithStdOut(os.Stdout), + hermes.WithStdErr(os.Stderr), ) } diff --git a/hermes/go.mod b/hermes/go.mod index e7467ffd..1c5fa696 100644 --- a/hermes/go.mod +++ b/hermes/go.mod @@ -11,7 +11,7 @@ require ( github.com/ignite/ignite-files/hermes v1.8.1 github.com/manifoldco/promptui v0.9.0 github.com/pelletier/go-toml/v2 v2.1.0 - github.com/spf13/cobra v1.8.0 + github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -179,7 +179,7 @@ require ( github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/cobra v1.8.0 // indirect github.com/spf13/viper v1.18.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect diff --git a/hermes/main.go b/hermes/main.go index 906e274f..5a270ea8 100644 --- a/hermes/main.go +++ b/hermes/main.go @@ -2,9 +2,9 @@ package main import ( "context" - "os" hplugin "github.com/hashicorp/go-plugin" + "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/ignite/apps/hermes/cmd" @@ -15,19 +15,30 @@ var _ plugin.Interface = app{} type app struct{} func (app) Manifest(context.Context) (*plugin.Manifest, error) { - m := &plugin.Manifest{Name: "hermes"} - m.ImportCobraCommand(cmd.NewRelayer(), "ignite") + m := &plugin.Manifest{ + Name: "hermes", + Commands: cmd.GetCommands(), + } return m, nil } -func (app) Execute(_ context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { +func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { // Instead of a switch on c.Use, we run the root command like if // we were in a command line context. This implies to set os.Args // correctly. // Remove the first arg "ignite" from OsArgs because our hermes // command root is "relayer" not "ignite". - os.Args = c.OsArgs[1:] - return cmd.NewRelayer().Execute() + args := c.OsArgs[1:] + switch args[0] { + case "configure": + return cmd.ConfigureHandler(ctx, c) + case "exec": + return cmd.ExecuteHandler(ctx, c) + case "start": + return cmd.StartHandler(ctx, c) + default: + return errors.Errorf("unknown command: %s", c.Path) + } } func (app) ExecuteHookPre(context.Context, *plugin.ExecutedHook, plugin.ClientAPI) error { From 82470a35fdfd7561ccb78562379666d693bdd32e Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 26 Jun 2024 03:27:19 +0200 Subject: [PATCH 02/14] remove duplicated flag --- hermes/cmd/hermes.go | 1 - hermes/cmd/hermes_keys.go | 4 ++-- hermes/main.go | 27 ++++++++++++++++----------- tools/debug/main.go | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/hermes/cmd/hermes.go b/hermes/cmd/hermes.go index 5b19a32c..3aac2c8e 100644 --- a/hermes/cmd/hermes.go +++ b/hermes/cmd/hermes.go @@ -56,7 +56,6 @@ func GetCommands() []*plugin.Command { Flags: []*plugin.Flag{ {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, {Name: flagChainBPortID, DefaultValue: "transfer", Usage: "port ID of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, {Name: flagChainACCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain A is a CCV consumer", Type: plugin.FlagTypeBool}, {Name: flagChainBCCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain B is a CCV consumer", Type: plugin.FlagTypeBool}, {Name: flagChainAEventSourceURL, Usage: "WS event source url of the chain A", Type: plugin.FlagTypeString}, diff --git a/hermes/cmd/hermes_keys.go b/hermes/cmd/hermes_keys.go index e321d74c..7e309123 100644 --- a/hermes/cmd/hermes_keys.go +++ b/hermes/cmd/hermes_keys.go @@ -45,7 +45,7 @@ func KeysAddFileHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error ) } -func KeysList(ctx context.Context, cmd *plugin.ExecutedCommand) error { +func KeysListHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { args := cmd.Args h, err := hermes.New() if err != nil { @@ -62,7 +62,7 @@ func KeysList(ctx context.Context, cmd *plugin.ExecutedCommand) error { ) } -func NewHermesKeysDelete(ctx context.Context, cmd *plugin.ExecutedCommand) error { +func KeysDeleteHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { h, err := hermes.New() if err != nil { return err diff --git a/hermes/main.go b/hermes/main.go index 5a270ea8..357ddd04 100644 --- a/hermes/main.go +++ b/hermes/main.go @@ -10,25 +10,18 @@ import ( "github.com/ignite/apps/hermes/cmd" ) -var _ plugin.Interface = app{} - type app struct{} func (app) Manifest(context.Context) (*plugin.Manifest, error) { - m := &plugin.Manifest{ + return &plugin.Manifest{ Name: "hermes", Commands: cmd.GetCommands(), - } - return m, nil + }, nil } func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { - // Instead of a switch on c.Use, we run the root command like if - // we were in a command line context. This implies to set os.Args - // correctly. - // Remove the first arg "ignite" from OsArgs because our hermes - // command root is "relayer" not "ignite". - args := c.OsArgs[1:] + // Remove the three two elements "ignite", "relayer" and "hermes" from OsArgs. + args := c.OsArgs[3:] switch args[0] { case "configure": return cmd.ConfigureHandler(ctx, c) @@ -36,6 +29,18 @@ func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.Clie return cmd.ExecuteHandler(ctx, c) case "start": return cmd.StartHandler(ctx, c) + case "keys": + switch args[1] { + case "add": + return cmd.KeysAddMnemonicHandler(ctx, c) + case "file": + return cmd.KeysAddFileHandler(ctx, c) + case "list": + return cmd.KeysListHandler(ctx, c) + case "delete": + return cmd.KeysDeleteHandler(ctx, c) + } + return cmd.StartHandler(ctx, c) default: return errors.Errorf("unknown command: %s", c.Path) } diff --git a/tools/debug/main.go b/tools/debug/main.go index 323cbde8..adeea77a 100644 --- a/tools/debug/main.go +++ b/tools/debug/main.go @@ -31,11 +31,11 @@ func main() { newCmdFromApp(rootCmd, helloworld.GetCommands()) newCmdFromApp(rootCmd, hooks.GetCommands()) newCmdFromApp(rootCmd, explorer.GetCommands()) + newCmdFromApp(rootCmd, hermes.GetCommands()) // Add ignite app commands for debugging here. // Add apps with cobra commands. rootCmd.AddCommand( - hermes.NewRelayer(), appregistry.NewAppRegistry(), wasm.NewWasm(), // Add cobra commands for debugging here. From 52f1cc71f33152c6bf4b819092a3d79ec59cf89c Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 26 Jun 2024 03:53:37 +0200 Subject: [PATCH 03/14] add under relayer command --- hermes/cmd/hermes.go | 205 +++++++++++++++++++++++-------------------- 1 file changed, 109 insertions(+), 96 deletions(-) diff --git a/hermes/cmd/hermes.go b/hermes/cmd/hermes.go index 3aac2c8e..3407afe9 100644 --- a/hermes/cmd/hermes.go +++ b/hermes/cmd/hermes.go @@ -13,115 +13,128 @@ const ( func GetCommands() []*plugin.Command { return []*plugin.Command{ { - Use: "hermes [command]", - Short: "Connect blockchains with an IBC Hermes Relayer", - Aliases: []string{"h"}, - Flags: []*plugin.Flag{ - {Name: flagConfig, Usage: "set a custom Hermes config file", Shorthand: "c", Type: plugin.FlagTypeString}, - }, + Use: "relayer [command]", + Short: "Connect blockchains with an IBC Relayer", + Aliases: []string{"r"}, Commands: []*plugin.Command{ { - Use: "exec [args...]", - Short: "Execute a hermes raw command", - }, - { - Use: "start [chain-a-id] [chain-a-rpc]", - Short: "Start the Hermes relayer", - }, - { - Use: "keys [command]", - Short: "Start the Hermes relayer", + Use: "hermes [command]", + Short: "Hermes Relayer wrapper", + Aliases: []string{"h"}, + Flags: []*plugin.Flag{ + { + Name: flagConfig, + Usage: "set a custom Hermes config file", + Shorthand: "c", + Persistent: true, + Type: plugin.FlagTypeString, + }, + }, Commands: []*plugin.Command{ { - Use: "add [chain-id] [mnemonic]", - Short: "Add a new key from mnemonic to Hermes relayer", + Use: "exec [args...]", + Short: "Execute a hermes raw command", }, { - Use: "file [chain-id] [filepath]", - Short: "Add a new key from a key file to Hermes relayer", + Use: "start [chain-a-id] [chain-a-rpc]", + Short: "Start the Hermes relayer", }, { - Use: "list [chain-id]", - Short: "List Hermes relayer keys", + Use: "keys [command]", + Short: "Start the Hermes relayer", + Commands: []*plugin.Command{ + { + Use: "add [chain-id] [mnemonic]", + Short: "Add a new key from mnemonic to Hermes relayer", + }, + { + Use: "file [chain-id] [filepath]", + Short: "Add a new key from a key file to Hermes relayer", + }, + { + Use: "list [chain-id]", + Short: "List Hermes relayer keys", + }, + { + Use: "delete [chain-id] [key-name]", + Short: "Delete a key from Hermes relayer", + }, + }, }, { - Use: "delete [chain-id] [key-name]", - Short: "Delete a key from Hermes relayer", + Use: "configure [chain-a-id] [chain-a-rpc] [chain-a-grpc] [chain-b-id] [chain-b-rpc] [chain-b-grpc]", + Short: "Configure the Hermes relayer creating the config file, client, channels and connection", + Flags: []*plugin.Flag{ + {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBPortID, DefaultValue: "transfer", Usage: "port ID of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainACCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain A is a CCV consumer", Type: plugin.FlagTypeBool}, + {Name: flagChainBCCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain B is a CCV consumer", Type: plugin.FlagTypeBool}, + {Name: flagChainAEventSourceURL, Usage: "WS event source url of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBEventSourceURL, Usage: "WS event source url of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAEventSourceMode, DefaultValue: "push", Usage: "WS event source mode of the chain A (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainBEventSourceMode, DefaultValue: "push", Usage: "WS event source mode of the chain B (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainAEventSourceBatchDelay, DefaultValue: "500ms", Usage: "WS event source batch delay time of the chain A (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainBEventSourceBatchDelay, DefaultValue: "500ms", Usage: "WS event source batch delay time of the chain B (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, + {Name: flagChainARPCTimeout, DefaultValue: "10s", Usage: "RPC timeout of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBRPCTimeout, DefaultValue: "10s", Usage: "RPC timeout of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainATrustedNode, DefaultValue: "false", Usage: "enable trusted node on the chain A", Type: plugin.FlagTypeBool}, + {Name: flagChainBTrustedNode, DefaultValue: "false", Usage: "enable trusted node on the chain B", Type: plugin.FlagTypeBool}, + {Name: flagChainAAccountPrefix, DefaultValue: "cosmos", Usage: "account prefix of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBAccountPrefix, DefaultValue: "cosmos", Usage: "account prefix of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAKeyName, DefaultValue: "wallet", Usage: "hermes account name of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBKeyName, DefaultValue: "wallet", Usage: "hermes account name of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAAddressType, DefaultValue: "cosmos", Usage: "address type of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBAddressType, DefaultValue: "cosmos", Usage: "address type of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAStorePrefix, DefaultValue: "ibc", Usage: "store prefix of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBStorePrefix, DefaultValue: "ibc", Usage: "store prefix of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainADefaultGas, DefaultValue: "100000", Usage: "default gas used for transactions on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBDefaultGas, DefaultValue: "100000", Usage: "default gas used for transactions on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAMaxGas, DefaultValue: "400000", Usage: "max gas used for transactions on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBMaxGas, DefaultValue: "400000", Usage: "max gas used for transactions on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAGasPrice, DefaultValue: "0.025stake", Usage: "gas price used for transactions on chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBGasPrice, DefaultValue: "0.025stake", Usage: "gas price used for transactions on chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAGasMultiplier, DefaultValue: "1.1", Usage: "gas multiplier used for transactions on chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBGasMultiplier, DefaultValue: "1.1", Usage: "gas multiplier used for transactions on chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAMaxMsgNum, DefaultValue: "30", Usage: "max message number used for transactions on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBMaxMsgNum, DefaultValue: "30", Usage: "max message number used for transactions on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAMaxTxSize, DefaultValue: "2097152", Usage: "max transaction size on chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBMaxTxSize, DefaultValue: "2097152", Usage: "max transaction size on chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAClockDrift, DefaultValue: "5s", Usage: "clock drift of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBClockDrift, DefaultValue: "5s", Usage: "clock drift of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAMaxBlockTime, DefaultValue: "30s", Usage: "maximum block time of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBMaxBlockTime, DefaultValue: "30s", Usage: "maximum block time of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainATrustingPeriod, DefaultValue: "14days", Usage: "trusting period of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBTrustingPeriod, DefaultValue: "14days", Usage: "trusting period of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainATrustThresholdNumerator, DefaultValue: "2", Usage: "trusting threshold numerator of the chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBTrustThresholdNumerator, DefaultValue: "2", Usage: "trusting threshold numerator of the chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainATrustThresholdDenominator, DefaultValue: "3", Usage: "trusting threshold denominator of the chain A", Type: plugin.FlagTypeUint64}, + {Name: flagChainBTrustThresholdDenominator, DefaultValue: "3", Usage: "trusting threshold denominator of the chain B", Type: plugin.FlagTypeUint64}, + {Name: flagChainAMemoPrefix, Usage: "memo prefix of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBMemoPrefix, Usage: "memo prefix of the chain B", Type: plugin.FlagTypeString}, + {Name: flagChainAFaucet, Usage: "faucet URL of the chain A", Type: plugin.FlagTypeString}, + {Name: flagChainBFaucet, Usage: "faucet URL of the chain B", Type: plugin.FlagTypeString}, + {Name: flagTelemetryEnabled, DefaultValue: "false", Usage: "enable hermes telemetry", Type: plugin.FlagTypeBool}, + {Name: flagTelemetryHost, DefaultValue: "127.0.0.1", Usage: "hermes telemetry host", Type: plugin.FlagTypeString}, + {Name: flagTelemetryPort, DefaultValue: "3001", Usage: "hermes telemetry port", Type: plugin.FlagTypeUint64}, + {Name: flagRestEnabled, DefaultValue: "false", Usage: "enable hermes rest", Type: plugin.FlagTypeBool}, + {Name: flagRestHost, DefaultValue: "127.0.0.1", Usage: "hermes rest host", Type: plugin.FlagTypeString}, + {Name: flagRestPort, DefaultValue: "3000", Usage: "hermes rest port", Type: plugin.FlagTypeUint64}, + {Name: flagModeChannelsEnabled, DefaultValue: "true", Usage: "enable hermes channels", Type: plugin.FlagTypeBool}, + {Name: flagModeClientsEnabled, DefaultValue: "true", Usage: "enable hermes clients", Type: plugin.FlagTypeBool}, + {Name: flagModeClientsMisbehaviour, DefaultValue: "true", Usage: "enable hermes clients misbehaviour", Type: plugin.FlagTypeBool}, + {Name: flagModeClientsRefresh, DefaultValue: "true", Usage: "enable hermes client refresh time", Type: plugin.FlagTypeBool}, + {Name: flagModeConnectionsEnabled, DefaultValue: "true", Usage: "enable hermes connections", Type: plugin.FlagTypeBool}, + {Name: flagModePacketsEnabled, DefaultValue: "true", Usage: "enable hermes packets", Type: plugin.FlagTypeBool}, + {Name: flagModePacketsClearInterval, DefaultValue: "100", Usage: "hermes packet clear interval", Type: plugin.FlagTypeUint64}, + {Name: flagModePacketsClearOnStart, DefaultValue: "true", Usage: "enable hermes packets clear on start", Type: plugin.FlagTypeBool}, + {Name: flagModePacketsTxConfirmation, DefaultValue: "true", Usage: "hermes packet transaction confirmation", Type: plugin.FlagTypeBool}, + {Name: flagAutoRegisterCounterpartyPayee, DefaultValue: "false", Usage: "auto register the counterparty payee on a destination chain to the relayer's address on the source chain", Type: plugin.FlagTypeBool}, + {Name: flagGenerateWallets, DefaultValue: "false", Usage: "automatically generate wallets if they do not exist", Type: plugin.FlagTypeBool}, + {Name: flagOverwriteConfig, DefaultValue: "false", Usage: "overwrite the current config if it already exists", Type: plugin.FlagTypeBool}, + }, }, }, }, - { - Use: "configure [chain-a-id] [chain-a-rpc] [chain-a-grpc] [chain-b-id] [chain-b-rpc] [chain-b-grpc]", - Short: "Configure the Hermes relayer creating the config file, client, channels and connection", - Flags: []*plugin.Flag{ - {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBPortID, DefaultValue: "transfer", Usage: "port ID of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainACCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain A is a CCV consumer", Type: plugin.FlagTypeBool}, - {Name: flagChainBCCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain B is a CCV consumer", Type: plugin.FlagTypeBool}, - {Name: flagChainAEventSourceURL, Usage: "WS event source url of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBEventSourceURL, Usage: "WS event source url of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAEventSourceMode, DefaultValue: "push", Usage: "WS event source mode of the chain A (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, - {Name: flagChainBEventSourceMode, DefaultValue: "push", Usage: "WS event source mode of the chain B (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, - {Name: flagChainAEventSourceBatchDelay, DefaultValue: "500ms", Usage: "WS event source batch delay time of the chain A (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, - {Name: flagChainBEventSourceBatchDelay, DefaultValue: "500ms", Usage: "WS event source batch delay time of the chain B (event source url should be set to use this flag)", Type: plugin.FlagTypeString}, - {Name: flagChainARPCTimeout, DefaultValue: "10s", Usage: "RPC timeout of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBRPCTimeout, DefaultValue: "10s", Usage: "RPC timeout of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainATrustedNode, DefaultValue: "false", Usage: "enable trusted node on the chain A", Type: plugin.FlagTypeBool}, - {Name: flagChainBTrustedNode, DefaultValue: "false", Usage: "enable trusted node on the chain B", Type: plugin.FlagTypeBool}, - {Name: flagChainAAccountPrefix, DefaultValue: "cosmos", Usage: "account prefix of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBAccountPrefix, DefaultValue: "cosmos", Usage: "account prefix of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAKeyName, DefaultValue: "wallet", Usage: "hermes account name of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBKeyName, DefaultValue: "wallet", Usage: "hermes account name of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAAddressType, DefaultValue: "cosmos", Usage: "address type of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBAddressType, DefaultValue: "cosmos", Usage: "address type of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAStorePrefix, DefaultValue: "ibc", Usage: "store prefix of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBStorePrefix, DefaultValue: "ibc", Usage: "store prefix of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainADefaultGas, DefaultValue: "100000", Usage: "default gas used for transactions on chain A", Type: plugin.FlagTypeUint64}, - {Name: flagChainBDefaultGas, DefaultValue: "100000", Usage: "default gas used for transactions on chain B", Type: plugin.FlagTypeUint64}, - {Name: flagChainAMaxGas, DefaultValue: "400000", Usage: "max gas used for transactions on chain A", Type: plugin.FlagTypeUint64}, - {Name: flagChainBMaxGas, DefaultValue: "400000", Usage: "max gas used for transactions on chain B", Type: plugin.FlagTypeUint64}, - {Name: flagChainAGasPrice, DefaultValue: "0.025stake", Usage: "gas price used for transactions on chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBGasPrice, DefaultValue: "0.025stake", Usage: "gas price used for transactions on chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAGasMultiplier, DefaultValue: "1.1", Usage: "gas multiplier used for transactions on chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBGasMultiplier, DefaultValue: "1.1", Usage: "gas multiplier used for transactions on chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAMaxMsgNum, DefaultValue: "30", Usage: "max message number used for transactions on chain A", Type: plugin.FlagTypeUint64}, - {Name: flagChainBMaxMsgNum, DefaultValue: "30", Usage: "max message number used for transactions on chain B", Type: plugin.FlagTypeUint64}, - {Name: flagChainAMaxTxSize, DefaultValue: "2097152", Usage: "max transaction size on chain A", Type: plugin.FlagTypeUint64}, - {Name: flagChainBMaxTxSize, DefaultValue: "2097152", Usage: "max transaction size on chain B", Type: plugin.FlagTypeUint64}, - {Name: flagChainAClockDrift, DefaultValue: "5s", Usage: "clock drift of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBClockDrift, DefaultValue: "5s", Usage: "clock drift of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAMaxBlockTime, DefaultValue: "30s", Usage: "maximum block time of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBMaxBlockTime, DefaultValue: "30s", Usage: "maximum block time of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainATrustingPeriod, DefaultValue: "14days", Usage: "trusting period of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBTrustingPeriod, DefaultValue: "14days", Usage: "trusting period of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainATrustThresholdNumerator, DefaultValue: "2", Usage: "trusting threshold numerator of the chain A", Type: plugin.FlagTypeUint64}, - {Name: flagChainBTrustThresholdNumerator, DefaultValue: "2", Usage: "trusting threshold numerator of the chain B", Type: plugin.FlagTypeUint64}, - {Name: flagChainATrustThresholdDenominator, DefaultValue: "3", Usage: "trusting threshold denominator of the chain A", Type: plugin.FlagTypeUint64}, - {Name: flagChainBTrustThresholdDenominator, DefaultValue: "3", Usage: "trusting threshold denominator of the chain B", Type: plugin.FlagTypeUint64}, - {Name: flagChainAMemoPrefix, Usage: "memo prefix of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBMemoPrefix, Usage: "memo prefix of the chain B", Type: plugin.FlagTypeString}, - {Name: flagChainAFaucet, Usage: "faucet URL of the chain A", Type: plugin.FlagTypeString}, - {Name: flagChainBFaucet, Usage: "faucet URL of the chain B", Type: plugin.FlagTypeString}, - {Name: flagTelemetryEnabled, DefaultValue: "false", Usage: "enable hermes telemetry", Type: plugin.FlagTypeBool}, - {Name: flagTelemetryHost, DefaultValue: "127.0.0.1", Usage: "hermes telemetry host", Type: plugin.FlagTypeString}, - {Name: flagTelemetryPort, DefaultValue: "3001", Usage: "hermes telemetry port", Type: plugin.FlagTypeUint64}, - {Name: flagRestEnabled, DefaultValue: "false", Usage: "enable hermes rest", Type: plugin.FlagTypeBool}, - {Name: flagRestHost, DefaultValue: "127.0.0.1", Usage: "hermes rest host", Type: plugin.FlagTypeString}, - {Name: flagRestPort, DefaultValue: "3000", Usage: "hermes rest port", Type: plugin.FlagTypeUint64}, - {Name: flagModeChannelsEnabled, DefaultValue: "true", Usage: "enable hermes channels", Type: plugin.FlagTypeBool}, - {Name: flagModeClientsEnabled, DefaultValue: "true", Usage: "enable hermes clients", Type: plugin.FlagTypeBool}, - {Name: flagModeClientsMisbehaviour, DefaultValue: "true", Usage: "enable hermes clients misbehaviour", Type: plugin.FlagTypeBool}, - {Name: flagModeClientsRefresh, DefaultValue: "true", Usage: "enable hermes client refresh time", Type: plugin.FlagTypeBool}, - {Name: flagModeConnectionsEnabled, DefaultValue: "true", Usage: "enable hermes connections", Type: plugin.FlagTypeBool}, - {Name: flagModePacketsEnabled, DefaultValue: "true", Usage: "enable hermes packets", Type: plugin.FlagTypeBool}, - {Name: flagModePacketsClearInterval, DefaultValue: "100", Usage: "hermes packet clear interval", Type: plugin.FlagTypeUint64}, - {Name: flagModePacketsClearOnStart, DefaultValue: "true", Usage: "enable hermes packets clear on start", Type: plugin.FlagTypeBool}, - {Name: flagModePacketsTxConfirmation, DefaultValue: "true", Usage: "hermes packet transaction confirmation", Type: plugin.FlagTypeBool}, - {Name: flagAutoRegisterCounterpartyPayee, DefaultValue: "false", Usage: "auto register the counterparty payee on a destination chain to the relayer's address on the source chain", Type: plugin.FlagTypeBool}, - {Name: flagGenerateWallets, DefaultValue: "false", Usage: "automatically generate wallets if they do not exist", Type: plugin.FlagTypeBool}, - {Name: flagOverwriteConfig, DefaultValue: "false", Usage: "overwrite the current config if it already exists", Type: plugin.FlagTypeBool}, - }, - }, }, }, } From 542fac0e7845000fe927006e37d7dafaedbaf4dc Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 26 Jun 2024 04:05:59 +0200 Subject: [PATCH 04/14] rename files from cmd pkg --- hermes/cmd/{hermes.go => cmd.go} | 0 hermes/cmd/{hermes_configure.go => configure.go} | 0 hermes/cmd/{hermes_exec.go => exec.go} | 0 hermes/cmd/{hermes_keys.go => leys.go} | 0 hermes/cmd/{hermes_start.go => start.go} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename hermes/cmd/{hermes.go => cmd.go} (100%) rename hermes/cmd/{hermes_configure.go => configure.go} (100%) rename hermes/cmd/{hermes_exec.go => exec.go} (100%) rename hermes/cmd/{hermes_keys.go => leys.go} (100%) rename hermes/cmd/{hermes_start.go => start.go} (100%) diff --git a/hermes/cmd/hermes.go b/hermes/cmd/cmd.go similarity index 100% rename from hermes/cmd/hermes.go rename to hermes/cmd/cmd.go diff --git a/hermes/cmd/hermes_configure.go b/hermes/cmd/configure.go similarity index 100% rename from hermes/cmd/hermes_configure.go rename to hermes/cmd/configure.go diff --git a/hermes/cmd/hermes_exec.go b/hermes/cmd/exec.go similarity index 100% rename from hermes/cmd/hermes_exec.go rename to hermes/cmd/exec.go diff --git a/hermes/cmd/hermes_keys.go b/hermes/cmd/leys.go similarity index 100% rename from hermes/cmd/hermes_keys.go rename to hermes/cmd/leys.go diff --git a/hermes/cmd/hermes_start.go b/hermes/cmd/start.go similarity index 100% rename from hermes/cmd/hermes_start.go rename to hermes/cmd/start.go From 926251f76ba44d539909b093a71fa9a6dfedeae6 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 26 Jun 2024 04:25:04 +0200 Subject: [PATCH 05/14] fix GetCommands comment --- hermes/cmd/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermes/cmd/cmd.go b/hermes/cmd/cmd.go index 3407afe9..a00fc951 100644 --- a/hermes/cmd/cmd.go +++ b/hermes/cmd/cmd.go @@ -9,7 +9,7 @@ const ( flagConfig = "config" ) -// GetCommands returns the list of explorer app commands. +// GetCommands returns the list of app commands. func GetCommands() []*plugin.Command { return []*plugin.Command{ { From 8e66b8d6fdf2166b954bd1275ec7d9a07a60c703 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Thu, 18 Jul 2024 03:12:25 +0200 Subject: [PATCH 06/14] rename typo from cmd/keys file --- hermes/cmd/{leys.go => keys.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename hermes/cmd/{leys.go => keys.go} (100%) diff --git a/hermes/cmd/leys.go b/hermes/cmd/keys.go similarity index 100% rename from hermes/cmd/leys.go rename to hermes/cmd/keys.go From e5b9dc97403e7d2b9ba70ad0e5a734da5ea579b9 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Thu, 18 Jul 2024 20:54:01 +0200 Subject: [PATCH 07/14] fix flag issues --- hermes/cmd/cmd.go | 10 -- hermes/cmd/configure.go | 250 ++++++++++++++-------------------------- hermes/cmd/flag.go | 173 +++++++++++++++++++++++++++ hermes/cmd/start.go | 8 +- hermes/go.mod | 2 +- 5 files changed, 261 insertions(+), 182 deletions(-) create mode 100644 hermes/cmd/flag.go diff --git a/hermes/cmd/cmd.go b/hermes/cmd/cmd.go index 1930ce04..70c8c6d7 100644 --- a/hermes/cmd/cmd.go +++ b/hermes/cmd/cmd.go @@ -2,11 +2,6 @@ package cmd import ( "github.com/ignite/cli/v28/ignite/services/plugin" - "github.com/spf13/pflag" -) - -const ( - flagConfig = "config" ) // GetCommands returns the list of app commands. @@ -146,8 +141,3 @@ func GetCommands() []*plugin.Command { }, } } - -func getConfig(flags *pflag.FlagSet) string { - config, _ := flags.GetString(flagConfig) - return config -} diff --git a/hermes/cmd/configure.go b/hermes/cmd/configure.go index c6b9bc70..1f03ddc1 100644 --- a/hermes/cmd/configure.go +++ b/hermes/cmd/configure.go @@ -15,97 +15,15 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/manifoldco/promptui" - "github.com/spf13/pflag" "github.com/ignite/apps/hermes/pkg/hermes" ) -const ( - flagChainAPortID = "chain-a-port-id" - flagChainAEventSourceMode = "chain-a-event-source-mode" - flagChainAEventSourceURL = "chain-a-event-source-url" - flagChainAEventSourceBatchDelay = "chain-a-event-source-batch-delay" - flagChainARPCTimeout = "chain-a-rpc-timeout" - flagChainAAccountPrefix = "chain-a-account-prefix" - flagChainAAddressType = "chain-a-address-types" - flagChainAKeyName = "chain-a-key-name" - flagChainAKeyStoreType = "chain-a-key-store-type" - flagChainAStorePrefix = "chain-a-store-prefix" - flagChainADefaultGas = "chain-a-default-gas" - flagChainAMaxGas = "chain-a-max-gas" - flagChainAGasPrice = "chain-a-gas-price" - flagChainAGasMultiplier = "chain-a-gas-multiplier" - flagChainAMaxMsgNum = "chain-a-max-msg-num" - flagChainAMaxTxSize = "chain-a-tx-size" - flagChainAClockDrift = "chain-a-clock-drift" - flagChainAMaxBlockTime = "chain-a-max-block-time" - flagChainATrustingPeriod = "chain-a-trusting-period" - flagChainATrustThresholdNumerator = "chain-a-trust-threshold-numerator" - flagChainATrustThresholdDenominator = "chain-a-trust-threshold-denominator" - flagChainAFaucet = "chain-a-faucet" - flagChainACCVConsumerChain = "chain-a-ccv-consumer-chain" - flagChainATrustedNode = "chain-a-trusted-node" - flagChainAMemoPrefix = "chain-a-memo-prefix" - flagChainAType = "chain-a-type" - flagChainASequentialBatchTx = "chain-a-sequential-batch-tx" - - flagChainBPortID = "chain-b-port-id" - flagChainBEventSourceMode = "chain-b-event-source-mode" - flagChainBEventSourceURL = "chain-b-event-source-url" - flagChainBEventSourceBatchDelay = "chain-b-event-source-batch-delay" - flagChainBRPCTimeout = "chain-b-rpc-timeout" - flagChainBAccountPrefix = "chain-b-account-prefix" - flagChainBAddressType = "chain-b-address-types" - flagChainBKeyName = "chain-b-key-name" - flagChainBKeyStoreType = "chain-b-key-store-type" - flagChainBStorePrefix = "chain-b-store-prefix" - flagChainBDefaultGas = "chain-b-default-gas" - flagChainBMaxGas = "chain-b-max-gas" - flagChainBGasPrice = "chain-b-gas-price" - flagChainBGasMultiplier = "chain-b-gas-multiplier" - flagChainBMaxMsgNum = "chain-b-max-msg-num" - flagChainBMaxTxSize = "chain-b-tx-size" - flagChainBClockDrift = "chain-b-clock-drift" - flagChainBMaxBlockTime = "chain-b-max-block-time" - flagChainBTrustingPeriod = "chain-b-trusting-period" - flagChainBTrustThresholdNumerator = "chain-b-trust-threshold-numerator" - flagChainBTrustThresholdDenominator = "chain-b-trust-threshold-denominator" - flagChainBFaucet = "chain-b-faucet" - flagChainBCCVConsumerChain = "chain-b-ccv-consumer-chain" - flagChainBTrustedNode = "chain-b-trusted-node" - flagChainBMemoPrefix = "chain-b-memo-prefix" - flagChainBType = "chain-b-type" - flagChainBSequentialBatchTx = "chain-b-sequential-batch-tx" - - flagTelemetryEnabled = "telemetry-enabled" - flagTelemetryHost = "telemetry-host" - flagTelemetryPort = "telemetry-port" - flagRestEnabled = "rest-enabled" - flagRestHost = "rest-host" - flagRestPort = "rest-port" - flagModeChannelsEnabled = "mode-channels-enabled" - flagModeClientsEnabled = "mode-clients-enabled" - flagModeClientsMisbehaviour = "mode-clients-misbehaviour" - flagModeClientsRefresh = "mode-clients-refresh" - flagModeConnectionsEnabled = "mode-connections-enabled" - flagModePacketsEnabled = "mode-packets-enabled" - flagModePacketsClearInterval = "mode-packets-clear-interval" - flagModePacketsClearOnStart = "mode-packets-clear-on-start" - flagModePacketsTxConfirmation = "mode-packets-tx-confirmation" - flagAutoRegisterCounterpartyPayee = "auto_register_counterparty_payee" - flagGenerateWallets = "generate-wallets" - flagOverwriteConfig = "overwrite-config" - flagChannelVersion = "channel-version" - - mnemonicEntropySize = 256 -) - func ConfigureHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { - args := cmd.Args - flags, err := cmd.NewFlags() - if err != nil { - return err - } + var ( + args = cmd.Args + flags = cmd.Flags + ) session := cliui.New(cliui.StartSpinnerWithText("Generating Hermes config")) defer session.End() @@ -114,17 +32,20 @@ func ConfigureHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { chainAID = args[0] chainBID = args[3] - generateWallets, _ = flags.GetBool(flagGenerateWallets) - overwriteConfig, _ = flags.GetBool(flagOverwriteConfig) - chainAPortID, _ = flags.GetString(flagChainAPortID) - chainAFaucet, _ = flags.GetString(flagChainAFaucet) - chainBPortID, _ = flags.GetString(flagChainBPortID) - chainBFaucet, _ = flags.GetString(flagChainBFaucet) - channelVersion, _ = flags.GetString(flagChannelVersion) + generateWallets, _ = getFlag[bool](flags, flagGenerateWallets) + overwriteConfig, _ = getFlag[bool](flags, flagOverwriteConfig) + chainAPortID, _ = getFlag[string](flags, flagChainAPortID) + chainAFaucet, _ = getFlag[string](flags, flagChainAFaucet) + chainBPortID, _ = getFlag[string](flags, flagChainBPortID) + chainBFaucet, _ = getFlag[string](flags, flagChainBFaucet) + channelVersion, _ = getFlag[string](flags, flagChannelVersion) customCfg = getConfig(flags) ) - var hermesCfg *hermes.Config + var ( + hermesCfg *hermes.Config + err error + ) if customCfg != "" { hermesCfg, err = hermes.LoadConfig(customCfg) if err != nil { @@ -474,7 +395,7 @@ GetKey: } // newHermesConfig create a new hermes config based in the cmd args. -func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*hermes.Config, error) { +func newHermesConfig(flags []*plugin.Flag, args []string, customCfg string) (*hermes.Config, error) { // if a custom config was set, save it in the ignite hermes folder if customCfg != "" { c, err := hermes.LoadConfig(customCfg) @@ -486,22 +407,22 @@ func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*he // Create the default hermes config var ( - telemetryEnabled, _ = flags.GetBool(flagTelemetryEnabled) - telemetryHost, _ = flags.GetString(flagTelemetryHost) - telemetryPort, _ = flags.GetUint64(flagTelemetryPort) - restEnabled, _ = flags.GetBool(flagRestEnabled) - restHost, _ = flags.GetString(flagRestHost) - restPort, _ = flags.GetUint64(flagRestPort) - modeChannelsEnabled, _ = flags.GetBool(flagModeChannelsEnabled) - modeClientsEnabled, _ = flags.GetBool(flagModeClientsEnabled) - modeClientsMisbehaviour, _ = flags.GetBool(flagModeClientsMisbehaviour) - modeClientsRefresh, _ = flags.GetBool(flagModeClientsRefresh) - modeConnectionsEnabled, _ = flags.GetBool(flagModeConnectionsEnabled) - modePacketsEnabled, _ = flags.GetBool(flagModePacketsEnabled) - modePacketsClearInterval, _ = flags.GetUint64(flagModePacketsClearInterval) - modePacketsClearOnStart, _ = flags.GetBool(flagModePacketsClearOnStart) - modePacketsTxConfirmation, _ = flags.GetBool(flagModePacketsTxConfirmation) - modePacketsAutoRegisterCounterpartyPayee, _ = flags.GetBool(flagAutoRegisterCounterpartyPayee) + telemetryEnabled, _ = getFlag[bool](flags, flagTelemetryEnabled) + telemetryHost, _ = getFlag[string](flags, flagTelemetryHost) + telemetryPort, _ = getFlag[uint64](flags, flagTelemetryPort) + restEnabled, _ = getFlag[bool](flags, flagRestEnabled) + restHost, _ = getFlag[string](flags, flagRestHost) + restPort, _ = getFlag[uint64](flags, flagRestPort) + modeChannelsEnabled, _ = getFlag[bool](flags, flagModeChannelsEnabled) + modeClientsEnabled, _ = getFlag[bool](flags, flagModeClientsEnabled) + modeClientsMisbehaviour, _ = getFlag[bool](flags, flagModeClientsMisbehaviour) + modeClientsRefresh, _ = getFlag[bool](flags, flagModeClientsRefresh) + modeConnectionsEnabled, _ = getFlag[bool](flags, flagModeConnectionsEnabled) + modePacketsEnabled, _ = getFlag[bool](flags, flagModePacketsEnabled) + modePacketsClearInterval, _ = getFlag[uint64](flags, flagModePacketsClearInterval) + modePacketsClearOnStart, _ = getFlag[bool](flags, flagModePacketsClearOnStart) + modePacketsTxConfirmation, _ = getFlag[bool](flags, flagModePacketsTxConfirmation) + modePacketsAutoRegisterCounterpartyPayee, _ = getFlag[bool](flags, flagAutoRegisterCounterpartyPayee) ) c := hermes.DefaultConfig( @@ -529,33 +450,34 @@ func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*he chainARPCAddr = args[1] chainAGRPCAddr = args[2] - chainAEventSourceMode, _ = flags.GetString(flagChainAEventSourceMode) - chainAEventSourceURL, _ = flags.GetString(flagChainAEventSourceURL) - chainAEventSourceBatchDelay, _ = flags.GetString(flagChainAEventSourceBatchDelay) - chainARPCTimeout, _ = flags.GetString(flagChainARPCTimeout) - chainAAccountPrefix, _ = flags.GetString(flagChainAAccountPrefix) - chainAAddressType, _ = flags.GetString(flagChainAAddressType) - chainAKeyName, _ = flags.GetString(flagChainAKeyName) - chainAKeyStoreType, _ = flags.GetString(flagChainAKeyStoreType) - chainAStorePrefix, _ = flags.GetString(flagChainAStorePrefix) - chainADefaultGas, _ = flags.GetUint64(flagChainADefaultGas) - chainAMaxGas, _ = flags.GetUint64(flagChainAMaxGas) - chainAGasPrice, _ = flags.GetString(flagChainAGasPrice) - chainAGasMultiplier, _ = flags.GetString(flagChainAGasMultiplier) - chainAMaxMsgNum, _ = flags.GetUint64(flagChainAMaxMsgNum) - chainAMaxTxSize, _ = flags.GetUint64(flagChainAMaxTxSize) - chainAClockDrift, _ = flags.GetString(flagChainAClockDrift) - chainAMaxBlockTime, _ = flags.GetString(flagChainAMaxBlockTime) - chainATrustingPeriod, _ = flags.GetString(flagChainATrustingPeriod) - chainATrustThresholdNumerator, _ = flags.GetUint64(flagChainATrustThresholdNumerator) - chainATrustThresholdDenominator, _ = flags.GetUint64(flagChainATrustThresholdDenominator) - chainACCVConsumerChain, _ = flags.GetBool(flagChainACCVConsumerChain) - chainATrustedNode, _ = flags.GetBool(flagChainATrustedNode) - chainAMemoPrefix, _ = flags.GetString(flagChainAMemoPrefix) - chainAType, _ = flags.GetString(flagChainAType) - chainASequentialBatchTx, _ = flags.GetBool(flagChainASequentialBatchTx) + chainAEventSourceMode, _ = getFlag[string](flags, flagChainAEventSourceMode) + chainAEventSourceURL, _ = getFlag[string](flags, flagChainAEventSourceURL) + chainAEventSourceBatchDelay, _ = getFlag[string](flags, flagChainAEventSourceBatchDelay) + chainARPCTimeout, _ = getFlag[string](flags, flagChainARPCTimeout) + chainAAccountPrefix, _ = getFlag[string](flags, flagChainAAccountPrefix) + chainAAddressType, _ = getFlag[string](flags, flagChainAAddressType) + chainAKeyName, _ = getFlag[string](flags, flagChainAKeyName) + chainAKeyStoreType, _ = getFlag[string](flags, flagChainAKeyStoreType) + chainAStorePrefix, _ = getFlag[string](flags, flagChainAStorePrefix) + chainADefaultGas, _ = getFlag[uint64](flags, flagChainADefaultGas) + chainAMaxGas, _ = getFlag[uint64](flags, flagChainAMaxGas) + chainAGasPrice, _ = getFlag[string](flags, flagChainAGasPrice) + chainAGasMultiplier, _ = getFlag[string](flags, flagChainAGasMultiplier) + chainAMaxMsgNum, _ = getFlag[uint64](flags, flagChainAMaxMsgNum) + chainAMaxTxSize, _ = getFlag[uint64](flags, flagChainAMaxTxSize) + chainAClockDrift, _ = getFlag[string](flags, flagChainAClockDrift) + chainAMaxBlockTime, _ = getFlag[string](flags, flagChainAMaxBlockTime) + chainATrustingPeriod, _ = getFlag[string](flags, flagChainATrustingPeriod) + chainATrustThresholdNumerator, _ = getFlag[uint64](flags, flagChainATrustThresholdNumerator) + chainATrustThresholdDenominator, _ = getFlag[uint64](flags, flagChainATrustThresholdDenominator) + chainACCVConsumerChain, _ = getFlag[bool](flags, flagChainACCVConsumerChain) + chainATrustedNode, _ = getFlag[bool](flags, flagChainATrustedNode) + chainAMemoPrefix, _ = getFlag[string](flags, flagChainAMemoPrefix) + chainAType, _ = getFlag[string](flags, flagChainAType) + chainASequentialBatchTx, _ = getFlag[bool](flags, flagChainASequentialBatchTx) ) + fmt.Println("aefaefaefeaf _ " + chainAGasMultiplier) chainAGasMulti := new(big.Float) chainAGasMulti, ok := chainAGasMulti.SetString(chainAGasMultiplier) if !ok { @@ -629,8 +551,7 @@ func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*he optChainA = append(optChainA, hermes.WithChainType(chainAType)) } - _, err := c.AddChain(chainAID, chainARPCAddr, chainAGRPCAddr, optChainA...) - if err != nil { + if _, err := c.AddChain(chainAID, chainARPCAddr, chainAGRPCAddr, optChainA...); err != nil { return nil, err } @@ -640,31 +561,31 @@ func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*he chainBRPCAddr = args[4] chainBGRPCAddr = args[5] - chainBEventSourceMode, _ = flags.GetString(flagChainBEventSourceMode) - chainBEventSourceURL, _ = flags.GetString(flagChainBEventSourceURL) - chainBEventSourceBatchDelay, _ = flags.GetString(flagChainBEventSourceBatchDelay) - chainBRPCTimeout, _ = flags.GetString(flagChainBRPCTimeout) - chainBAccountPrefix, _ = flags.GetString(flagChainBAccountPrefix) - chainBAddressType, _ = flags.GetString(flagChainBAddressType) - chainBKeyName, _ = flags.GetString(flagChainBKeyName) - chainBKeyStoreType, _ = flags.GetString(flagChainBKeyStoreType) - chainBStorePrefix, _ = flags.GetString(flagChainBStorePrefix) - chainBDefaultGas, _ = flags.GetUint64(flagChainBDefaultGas) - chainBMaxGas, _ = flags.GetUint64(flagChainBMaxGas) - chainBGasPrice, _ = flags.GetString(flagChainBGasPrice) - chainBGasMultiplier, _ = flags.GetString(flagChainBGasMultiplier) - chainBMaxMsgNum, _ = flags.GetUint64(flagChainBMaxMsgNum) - chainBMaxTxSize, _ = flags.GetUint64(flagChainBMaxTxSize) - chainBClockDrift, _ = flags.GetString(flagChainBClockDrift) - chainBMaxBlockTime, _ = flags.GetString(flagChainBMaxBlockTime) - chainBTrustingPeriod, _ = flags.GetString(flagChainBTrustingPeriod) - chainBTrustThresholdNumerator, _ = flags.GetUint64(flagChainBTrustThresholdNumerator) - chainBTrustThresholdDenominator, _ = flags.GetUint64(flagChainBTrustThresholdDenominator) - chainBCCVConsumerChain, _ = flags.GetBool(flagChainBCCVConsumerChain) - chainBTrustedNode, _ = flags.GetBool(flagChainBTrustedNode) - chainBMemoPrefix, _ = flags.GetString(flagChainBMemoPrefix) - chainBType, _ = flags.GetString(flagChainBType) - chainBSequentialBatchTx, _ = flags.GetBool(flagChainBSequentialBatchTx) + chainBEventSourceMode, _ = getFlag[string](flags, flagChainBEventSourceMode) + chainBEventSourceURL, _ = getFlag[string](flags, flagChainBEventSourceURL) + chainBEventSourceBatchDelay, _ = getFlag[string](flags, flagChainBEventSourceBatchDelay) + chainBRPCTimeout, _ = getFlag[string](flags, flagChainBRPCTimeout) + chainBAccountPrefix, _ = getFlag[string](flags, flagChainBAccountPrefix) + chainBAddressType, _ = getFlag[string](flags, flagChainBAddressType) + chainBKeyName, _ = getFlag[string](flags, flagChainBKeyName) + chainBKeyStoreType, _ = getFlag[string](flags, flagChainBKeyStoreType) + chainBStorePrefix, _ = getFlag[string](flags, flagChainBStorePrefix) + chainBDefaultGas, _ = getFlag[uint64](flags, flagChainBDefaultGas) + chainBMaxGas, _ = getFlag[uint64](flags, flagChainBMaxGas) + chainBGasPrice, _ = getFlag[string](flags, flagChainBGasPrice) + chainBGasMultiplier, _ = getFlag[string](flags, flagChainBGasMultiplier) + chainBMaxMsgNum, _ = getFlag[uint64](flags, flagChainBMaxMsgNum) + chainBMaxTxSize, _ = getFlag[uint64](flags, flagChainBMaxTxSize) + chainBClockDrift, _ = getFlag[string](flags, flagChainBClockDrift) + chainBMaxBlockTime, _ = getFlag[string](flags, flagChainBMaxBlockTime) + chainBTrustingPeriod, _ = getFlag[string](flags, flagChainBTrustingPeriod) + chainBTrustThresholdNumerator, _ = getFlag[uint64](flags, flagChainBTrustThresholdNumerator) + chainBTrustThresholdDenominator, _ = getFlag[uint64](flags, flagChainBTrustThresholdDenominator) + chainBCCVConsumerChain, _ = getFlag[bool](flags, flagChainBCCVConsumerChain) + chainBTrustedNode, _ = getFlag[bool](flags, flagChainBTrustedNode) + chainBMemoPrefix, _ = getFlag[string](flags, flagChainBMemoPrefix) + chainBType, _ = getFlag[string](flags, flagChainBType) + chainBSequentialBatchTx, _ = getFlag[bool](flags, flagChainBSequentialBatchTx) ) chainBGasMulti := new(big.Float) @@ -740,8 +661,7 @@ func newHermesConfig(flags *pflag.FlagSet, args []string, customCfg string) (*he optChainB = append(optChainB, hermes.WithChainType(chainBType)) } - _, err = c.AddChain(chainBID, chainBRPCAddr, chainBGRPCAddr, optChainB...) - if err != nil { + if _, err := c.AddChain(chainBID, chainBRPCAddr, chainBGRPCAddr, optChainB...); err != nil { return nil, err } diff --git a/hermes/cmd/flag.go b/hermes/cmd/flag.go new file mode 100644 index 00000000..da2ef922 --- /dev/null +++ b/hermes/cmd/flag.go @@ -0,0 +1,173 @@ +package cmd + +import ( + "strconv" + "strings" + + "github.com/ignite/cli/v28/ignite/pkg/errors" + "github.com/ignite/cli/v28/ignite/services/plugin" +) + +const ( + flagChainAPortID = "chain-a-port-id" + flagChainAEventSourceMode = "chain-a-event-source-mode" + flagChainAEventSourceURL = "chain-a-event-source-url" + flagChainAEventSourceBatchDelay = "chain-a-event-source-batch-delay" + flagChainARPCTimeout = "chain-a-rpc-timeout" + flagChainAAccountPrefix = "chain-a-account-prefix" + flagChainAAddressType = "chain-a-address-types" + flagChainAKeyName = "chain-a-key-name" + flagChainAKeyStoreType = "chain-a-key-store-type" + flagChainAStorePrefix = "chain-a-store-prefix" + flagChainADefaultGas = "chain-a-default-gas" + flagChainAMaxGas = "chain-a-max-gas" + flagChainAGasPrice = "chain-a-gas-price" + flagChainAGasMultiplier = "chain-a-gas-multiplier" + flagChainAMaxMsgNum = "chain-a-max-msg-num" + flagChainAMaxTxSize = "chain-a-tx-size" + flagChainAClockDrift = "chain-a-clock-drift" + flagChainAMaxBlockTime = "chain-a-max-block-time" + flagChainATrustingPeriod = "chain-a-trusting-period" + flagChainATrustThresholdNumerator = "chain-a-trust-threshold-numerator" + flagChainATrustThresholdDenominator = "chain-a-trust-threshold-denominator" + flagChainAFaucet = "chain-a-faucet" + flagChainACCVConsumerChain = "chain-a-ccv-consumer-chain" + flagChainATrustedNode = "chain-a-trusted-node" + flagChainAMemoPrefix = "chain-a-memo-prefix" + flagChainAType = "chain-a-type" + flagChainASequentialBatchTx = "chain-a-sequential-batch-tx" + + flagChainBPortID = "chain-b-port-id" + flagChainBEventSourceMode = "chain-b-event-source-mode" + flagChainBEventSourceURL = "chain-b-event-source-url" + flagChainBEventSourceBatchDelay = "chain-b-event-source-batch-delay" + flagChainBRPCTimeout = "chain-b-rpc-timeout" + flagChainBAccountPrefix = "chain-b-account-prefix" + flagChainBAddressType = "chain-b-address-types" + flagChainBKeyName = "chain-b-key-name" + flagChainBKeyStoreType = "chain-b-key-store-type" + flagChainBStorePrefix = "chain-b-store-prefix" + flagChainBDefaultGas = "chain-b-default-gas" + flagChainBMaxGas = "chain-b-max-gas" + flagChainBGasPrice = "chain-b-gas-price" + flagChainBGasMultiplier = "chain-b-gas-multiplier" + flagChainBMaxMsgNum = "chain-b-max-msg-num" + flagChainBMaxTxSize = "chain-b-tx-size" + flagChainBClockDrift = "chain-b-clock-drift" + flagChainBMaxBlockTime = "chain-b-max-block-time" + flagChainBTrustingPeriod = "chain-b-trusting-period" + flagChainBTrustThresholdNumerator = "chain-b-trust-threshold-numerator" + flagChainBTrustThresholdDenominator = "chain-b-trust-threshold-denominator" + flagChainBFaucet = "chain-b-faucet" + flagChainBCCVConsumerChain = "chain-b-ccv-consumer-chain" + flagChainBTrustedNode = "chain-b-trusted-node" + flagChainBMemoPrefix = "chain-b-memo-prefix" + flagChainBType = "chain-b-type" + flagChainBSequentialBatchTx = "chain-b-sequential-batch-tx" + + flagTelemetryEnabled = "telemetry-enabled" + flagTelemetryHost = "telemetry-host" + flagTelemetryPort = "telemetry-port" + flagRestEnabled = "rest-enabled" + flagRestHost = "rest-host" + flagRestPort = "rest-port" + flagModeChannelsEnabled = "mode-channels-enabled" + flagModeClientsEnabled = "mode-clients-enabled" + flagModeClientsMisbehaviour = "mode-clients-misbehaviour" + flagModeClientsRefresh = "mode-clients-refresh" + flagModeConnectionsEnabled = "mode-connections-enabled" + flagModePacketsEnabled = "mode-packets-enabled" + flagModePacketsClearInterval = "mode-packets-clear-interval" + flagModePacketsClearOnStart = "mode-packets-clear-on-start" + flagModePacketsTxConfirmation = "mode-packets-tx-confirmation" + flagAutoRegisterCounterpartyPayee = "auto_register_counterparty_payee" + flagGenerateWallets = "generate-wallets" + flagOverwriteConfig = "overwrite-config" + flagChannelVersion = "channel-version" + + flagConfig = "config" + + mnemonicEntropySize = 256 +) + +var ( + ErrFlagNotFound = errors.New("flag not found") + ErrFlagAssertion = errors.New("flag type assertion to failed") +) + +func getConfig(flags []*plugin.Flag) string { + config, _ := getFlag[string](flags, flagConfig) + return config +} + +// getFlag function to get flag with a generic return type +// TODO remove these helpers for flags after we fix this issue: +// https://github.com/ignite/apps/issues/116 +func getFlag[A any](flags []*plugin.Flag, key string) (result A, err error) { + v, err := getValue(flags, key) + if err != nil { + return result, err + } + + value, ok := v.(A) + if !ok { + return result, errors.Wrapf(ErrFlagAssertion, "type assertion to %T failed", v) + } + return value, nil +} + +func getValue(flags []*plugin.Flag, key string) (interface{}, error) { + for _, flag := range flags { + if flag.Name == key { + return exportToFlagValue(flag) + } + } + return nil, errors.Wrapf(ErrFlagNotFound, "flag %s not found", key) +} + +func exportToFlagValue(f *plugin.Flag) (interface{}, error) { + switch f.Type { + case plugin.FlagTypeBool: + v, err := strconv.ParseBool(flagValue(f)) + if err != nil { + return false, err + } + return v, nil + case plugin.FlagTypeInt: + v, err := strconv.Atoi(flagValue(f)) + if err != nil { + return 0, err + } + return v, nil + case plugin.FlagTypeUint: + v, err := strconv.ParseUint(flagValue(f), 10, 64) + if err != nil { + return 0, err + } + return v, nil + case plugin.FlagTypeInt64: + v, err := strconv.ParseInt(flagValue(f), 10, 64) + if err != nil { + return 0, err + } + return v, nil + case plugin.FlagTypeUint64: + v, err := strconv.ParseUint(flagValue(f), 10, 64) + if err != nil { + return 0, err + } + return v, nil + case plugin.FlagTypeStringSlice: + s := strings.Trim(flagValue(f), "[]") + return strings.Fields(s), nil + default: + return strings.TrimSpace(flagValue(f)), nil + } +} + +func flagValue(flag *plugin.Flag) string { + if flag.Value != "" { + return flag.Value + } + return flag.DefaultValue +} diff --git a/hermes/cmd/start.go b/hermes/cmd/start.go index 33140f89..9abdb970 100644 --- a/hermes/cmd/start.go +++ b/hermes/cmd/start.go @@ -11,13 +11,9 @@ import ( "github.com/ignite/apps/hermes/pkg/hermes" ) -func StartHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { - flags, err := cmd.NewFlags() - if err != nil { - return err - } - +func StartHandler(ctx context.Context, cmd *plugin.ExecutedCommand) (err error) { var ( + flags = cmd.Flags args = cmd.Args customCfg = getConfig(flags) cfgName = strings.Join(args, hermes.ConfigNameSeparator) diff --git a/hermes/go.mod b/hermes/go.mod index d481aa0f..4ae4c0d0 100644 --- a/hermes/go.mod +++ b/hermes/go.mod @@ -11,7 +11,6 @@ require ( github.com/ignite/ignite-files/hermes v1.10.0 github.com/manifoldco/promptui v0.9.0 github.com/pelletier/go-toml/v2 v2.2.0 - github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -178,6 +177,7 @@ require ( github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.18.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect From 3d311b65326da26c3ba237f0e795e8513ed73b52 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Thu, 18 Jul 2024 21:37:37 +0200 Subject: [PATCH 08/14] cast results from exportToFlagValue function --- hermes/cmd/flag.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hermes/cmd/flag.go b/hermes/cmd/flag.go index da2ef922..cbad9e80 100644 --- a/hermes/cmd/flag.go +++ b/hermes/cmd/flag.go @@ -111,7 +111,7 @@ func getFlag[A any](flags []*plugin.Flag, key string) (result A, err error) { value, ok := v.(A) if !ok { - return result, errors.Wrapf(ErrFlagAssertion, "type assertion to %T failed", v) + return result, errors.Wrapf(ErrFlagAssertion, "type assertion to %T failed for field %s", v, key) } return value, nil } @@ -136,25 +136,25 @@ func exportToFlagValue(f *plugin.Flag) (interface{}, error) { case plugin.FlagTypeInt: v, err := strconv.Atoi(flagValue(f)) if err != nil { - return 0, err + return int(0), err } return v, nil case plugin.FlagTypeUint: v, err := strconv.ParseUint(flagValue(f), 10, 64) if err != nil { - return 0, err + return uint(0), err } return v, nil case plugin.FlagTypeInt64: v, err := strconv.ParseInt(flagValue(f), 10, 64) if err != nil { - return 0, err + return int64(0), err } return v, nil case plugin.FlagTypeUint64: v, err := strconv.ParseUint(flagValue(f), 10, 64) if err != nil { - return 0, err + return uint64(0), err } return v, nil case plugin.FlagTypeStringSlice: From 572e8f370b54c1e8d42615e57dba0c1da323f3f7 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Thu, 18 Jul 2024 21:40:00 +0200 Subject: [PATCH 09/14] unused casting --- hermes/cmd/flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermes/cmd/flag.go b/hermes/cmd/flag.go index cbad9e80..ce057de2 100644 --- a/hermes/cmd/flag.go +++ b/hermes/cmd/flag.go @@ -136,7 +136,7 @@ func exportToFlagValue(f *plugin.Flag) (interface{}, error) { case plugin.FlagTypeInt: v, err := strconv.Atoi(flagValue(f)) if err != nil { - return int(0), err + return 0, err } return v, nil case plugin.FlagTypeUint: From 3d2f26c1dda2b3c4eb238cdf26c0e940c8dd9b4e Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 19 Jul 2024 04:16:05 +0200 Subject: [PATCH 10/14] fix uint value --- hermes/cmd/flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermes/cmd/flag.go b/hermes/cmd/flag.go index ce057de2..16cedcda 100644 --- a/hermes/cmd/flag.go +++ b/hermes/cmd/flag.go @@ -144,7 +144,7 @@ func exportToFlagValue(f *plugin.Flag) (interface{}, error) { if err != nil { return uint(0), err } - return v, nil + return uint(v), nil case plugin.FlagTypeInt64: v, err := strconv.ParseInt(flagValue(f), 10, 64) if err != nil { From 4091afe27ef829ac35a913f7a9bc67671a268eb2 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 19 Jul 2024 04:18:43 +0200 Subject: [PATCH 11/14] fix string slices parse --- hermes/cmd/flag.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hermes/cmd/flag.go b/hermes/cmd/flag.go index 16cedcda..d67e1265 100644 --- a/hermes/cmd/flag.go +++ b/hermes/cmd/flag.go @@ -158,8 +158,13 @@ func exportToFlagValue(f *plugin.Flag) (interface{}, error) { } return v, nil case plugin.FlagTypeStringSlice: - s := strings.Trim(flagValue(f), "[]") - return strings.Fields(s), nil + v := strings.Trim(flagValue(f), "[]") + s := strings.Split(v, ",") + if len(s) == 0 || (len(s) == 1 && s[0] == "") { + return []string{}, nil + } + + return s, nil default: return strings.TrimSpace(flagValue(f)), nil } From 86b646e319f1f1cd03383efcf25020a4ce65b70d Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 6 Aug 2024 00:08:17 +0200 Subject: [PATCH 12/14] use plugin.Flags parser --- hermes/cmd/cmd.go | 4 +- hermes/cmd/configure.go | 150 ++++++++++++++++++++-------------------- hermes/cmd/flag.go | 90 +----------------------- hermes/go.mod | 2 +- hermes/go.sum | 4 +- 5 files changed, 82 insertions(+), 168 deletions(-) diff --git a/hermes/cmd/cmd.go b/hermes/cmd/cmd.go index 70c8c6d7..6816421c 100644 --- a/hermes/cmd/cmd.go +++ b/hermes/cmd/cmd.go @@ -16,7 +16,7 @@ func GetCommands() []*plugin.Command { Use: "hermes [command]", Short: "Hermes Relayer wrapper", Aliases: []string{"h"}, - Flags: []*plugin.Flag{ + Flags: plugin.Flags{ { Name: flagConfig, Usage: "set a custom Hermes config file", @@ -59,7 +59,7 @@ func GetCommands() []*plugin.Command { { Use: "configure [chain-a-id] [chain-a-rpc] [chain-a-grpc] [chain-b-id] [chain-b-rpc] [chain-b-grpc]", Short: "Configure the Hermes relayer creating the config file, client, channels and connection", - Flags: []*plugin.Flag{ + Flags: plugin.Flags{ {Name: flagChainAPortID, DefaultValue: "transfer", Usage: "port ID of the chain A", Type: plugin.FlagTypeString}, {Name: flagChainBPortID, DefaultValue: "transfer", Usage: "port ID of the chain B", Type: plugin.FlagTypeString}, {Name: flagChainACCVConsumerChain, DefaultValue: "false", Usage: "only specify true if the chain A is a CCV consumer", Type: plugin.FlagTypeBool}, diff --git a/hermes/cmd/configure.go b/hermes/cmd/configure.go index 1f03ddc1..0e5af86c 100644 --- a/hermes/cmd/configure.go +++ b/hermes/cmd/configure.go @@ -22,7 +22,7 @@ import ( func ConfigureHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { var ( args = cmd.Args - flags = cmd.Flags + flags = plugin.Flags(cmd.Flags) ) session := cliui.New(cliui.StartSpinnerWithText("Generating Hermes config")) @@ -32,13 +32,13 @@ func ConfigureHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error { chainAID = args[0] chainBID = args[3] - generateWallets, _ = getFlag[bool](flags, flagGenerateWallets) - overwriteConfig, _ = getFlag[bool](flags, flagOverwriteConfig) - chainAPortID, _ = getFlag[string](flags, flagChainAPortID) - chainAFaucet, _ = getFlag[string](flags, flagChainAFaucet) - chainBPortID, _ = getFlag[string](flags, flagChainBPortID) - chainBFaucet, _ = getFlag[string](flags, flagChainBFaucet) - channelVersion, _ = getFlag[string](flags, flagChannelVersion) + generateWallets, _ = flags.GetBool(flagGenerateWallets) + overwriteConfig, _ = flags.GetBool(flagOverwriteConfig) + chainAPortID, _ = flags.GetString(flagChainAPortID) + chainAFaucet, _ = flags.GetString(flagChainAFaucet) + chainBPortID, _ = flags.GetString(flagChainBPortID) + chainBFaucet, _ = flags.GetString(flagChainBFaucet) + channelVersion, _ = flags.GetString(flagChannelVersion) customCfg = getConfig(flags) ) @@ -395,7 +395,7 @@ GetKey: } // newHermesConfig create a new hermes config based in the cmd args. -func newHermesConfig(flags []*plugin.Flag, args []string, customCfg string) (*hermes.Config, error) { +func newHermesConfig(flags plugin.Flags, args []string, customCfg string) (*hermes.Config, error) { // if a custom config was set, save it in the ignite hermes folder if customCfg != "" { c, err := hermes.LoadConfig(customCfg) @@ -407,22 +407,22 @@ func newHermesConfig(flags []*plugin.Flag, args []string, customCfg string) (*he // Create the default hermes config var ( - telemetryEnabled, _ = getFlag[bool](flags, flagTelemetryEnabled) - telemetryHost, _ = getFlag[string](flags, flagTelemetryHost) - telemetryPort, _ = getFlag[uint64](flags, flagTelemetryPort) - restEnabled, _ = getFlag[bool](flags, flagRestEnabled) - restHost, _ = getFlag[string](flags, flagRestHost) - restPort, _ = getFlag[uint64](flags, flagRestPort) - modeChannelsEnabled, _ = getFlag[bool](flags, flagModeChannelsEnabled) - modeClientsEnabled, _ = getFlag[bool](flags, flagModeClientsEnabled) - modeClientsMisbehaviour, _ = getFlag[bool](flags, flagModeClientsMisbehaviour) - modeClientsRefresh, _ = getFlag[bool](flags, flagModeClientsRefresh) - modeConnectionsEnabled, _ = getFlag[bool](flags, flagModeConnectionsEnabled) - modePacketsEnabled, _ = getFlag[bool](flags, flagModePacketsEnabled) - modePacketsClearInterval, _ = getFlag[uint64](flags, flagModePacketsClearInterval) - modePacketsClearOnStart, _ = getFlag[bool](flags, flagModePacketsClearOnStart) - modePacketsTxConfirmation, _ = getFlag[bool](flags, flagModePacketsTxConfirmation) - modePacketsAutoRegisterCounterpartyPayee, _ = getFlag[bool](flags, flagAutoRegisterCounterpartyPayee) + telemetryEnabled, _ = flags.GetBool(flagTelemetryEnabled) + telemetryHost, _ = flags.GetString(flagTelemetryHost) + telemetryPort, _ = flags.GetUint64(flagTelemetryPort) + restEnabled, _ = flags.GetBool(flagRestEnabled) + restHost, _ = flags.GetString(flagRestHost) + restPort, _ = flags.GetUint64(flagRestPort) + modeChannelsEnabled, _ = flags.GetBool(flagModeChannelsEnabled) + modeClientsEnabled, _ = flags.GetBool(flagModeClientsEnabled) + modeClientsMisbehaviour, _ = flags.GetBool(flagModeClientsMisbehaviour) + modeClientsRefresh, _ = flags.GetBool(flagModeClientsRefresh) + modeConnectionsEnabled, _ = flags.GetBool(flagModeConnectionsEnabled) + modePacketsEnabled, _ = flags.GetBool(flagModePacketsEnabled) + modePacketsClearInterval, _ = flags.GetUint64(flagModePacketsClearInterval) + modePacketsClearOnStart, _ = flags.GetBool(flagModePacketsClearOnStart) + modePacketsTxConfirmation, _ = flags.GetBool(flagModePacketsTxConfirmation) + modePacketsAutoRegisterCounterpartyPayee, _ = flags.GetBool(flagAutoRegisterCounterpartyPayee) ) c := hermes.DefaultConfig( @@ -450,31 +450,31 @@ func newHermesConfig(flags []*plugin.Flag, args []string, customCfg string) (*he chainARPCAddr = args[1] chainAGRPCAddr = args[2] - chainAEventSourceMode, _ = getFlag[string](flags, flagChainAEventSourceMode) - chainAEventSourceURL, _ = getFlag[string](flags, flagChainAEventSourceURL) - chainAEventSourceBatchDelay, _ = getFlag[string](flags, flagChainAEventSourceBatchDelay) - chainARPCTimeout, _ = getFlag[string](flags, flagChainARPCTimeout) - chainAAccountPrefix, _ = getFlag[string](flags, flagChainAAccountPrefix) - chainAAddressType, _ = getFlag[string](flags, flagChainAAddressType) - chainAKeyName, _ = getFlag[string](flags, flagChainAKeyName) - chainAKeyStoreType, _ = getFlag[string](flags, flagChainAKeyStoreType) - chainAStorePrefix, _ = getFlag[string](flags, flagChainAStorePrefix) - chainADefaultGas, _ = getFlag[uint64](flags, flagChainADefaultGas) - chainAMaxGas, _ = getFlag[uint64](flags, flagChainAMaxGas) - chainAGasPrice, _ = getFlag[string](flags, flagChainAGasPrice) - chainAGasMultiplier, _ = getFlag[string](flags, flagChainAGasMultiplier) - chainAMaxMsgNum, _ = getFlag[uint64](flags, flagChainAMaxMsgNum) - chainAMaxTxSize, _ = getFlag[uint64](flags, flagChainAMaxTxSize) - chainAClockDrift, _ = getFlag[string](flags, flagChainAClockDrift) - chainAMaxBlockTime, _ = getFlag[string](flags, flagChainAMaxBlockTime) - chainATrustingPeriod, _ = getFlag[string](flags, flagChainATrustingPeriod) - chainATrustThresholdNumerator, _ = getFlag[uint64](flags, flagChainATrustThresholdNumerator) - chainATrustThresholdDenominator, _ = getFlag[uint64](flags, flagChainATrustThresholdDenominator) - chainACCVConsumerChain, _ = getFlag[bool](flags, flagChainACCVConsumerChain) - chainATrustedNode, _ = getFlag[bool](flags, flagChainATrustedNode) - chainAMemoPrefix, _ = getFlag[string](flags, flagChainAMemoPrefix) - chainAType, _ = getFlag[string](flags, flagChainAType) - chainASequentialBatchTx, _ = getFlag[bool](flags, flagChainASequentialBatchTx) + chainAEventSourceMode, _ = flags.GetString(flagChainAEventSourceMode) + chainAEventSourceURL, _ = flags.GetString(flagChainAEventSourceURL) + chainAEventSourceBatchDelay, _ = flags.GetString(flagChainAEventSourceBatchDelay) + chainARPCTimeout, _ = flags.GetString(flagChainARPCTimeout) + chainAAccountPrefix, _ = flags.GetString(flagChainAAccountPrefix) + chainAAddressType, _ = flags.GetString(flagChainAAddressType) + chainAKeyName, _ = flags.GetString(flagChainAKeyName) + chainAKeyStoreType, _ = flags.GetString(flagChainAKeyStoreType) + chainAStorePrefix, _ = flags.GetString(flagChainAStorePrefix) + chainADefaultGas, _ = flags.GetUint64(flagChainADefaultGas) + chainAMaxGas, _ = flags.GetUint64(flagChainAMaxGas) + chainAGasPrice, _ = flags.GetString(flagChainAGasPrice) + chainAGasMultiplier, _ = flags.GetString(flagChainAGasMultiplier) + chainAMaxMsgNum, _ = flags.GetUint64(flagChainAMaxMsgNum) + chainAMaxTxSize, _ = flags.GetUint64(flagChainAMaxTxSize) + chainAClockDrift, _ = flags.GetString(flagChainAClockDrift) + chainAMaxBlockTime, _ = flags.GetString(flagChainAMaxBlockTime) + chainATrustingPeriod, _ = flags.GetString(flagChainATrustingPeriod) + chainATrustThresholdNumerator, _ = flags.GetUint64(flagChainATrustThresholdNumerator) + chainATrustThresholdDenominator, _ = flags.GetUint64(flagChainATrustThresholdDenominator) + chainACCVConsumerChain, _ = flags.GetBool(flagChainACCVConsumerChain) + chainATrustedNode, _ = flags.GetBool(flagChainATrustedNode) + chainAMemoPrefix, _ = flags.GetString(flagChainAMemoPrefix) + chainAType, _ = flags.GetString(flagChainAType) + chainASequentialBatchTx, _ = flags.GetBool(flagChainASequentialBatchTx) ) fmt.Println("aefaefaefeaf _ " + chainAGasMultiplier) @@ -561,31 +561,31 @@ func newHermesConfig(flags []*plugin.Flag, args []string, customCfg string) (*he chainBRPCAddr = args[4] chainBGRPCAddr = args[5] - chainBEventSourceMode, _ = getFlag[string](flags, flagChainBEventSourceMode) - chainBEventSourceURL, _ = getFlag[string](flags, flagChainBEventSourceURL) - chainBEventSourceBatchDelay, _ = getFlag[string](flags, flagChainBEventSourceBatchDelay) - chainBRPCTimeout, _ = getFlag[string](flags, flagChainBRPCTimeout) - chainBAccountPrefix, _ = getFlag[string](flags, flagChainBAccountPrefix) - chainBAddressType, _ = getFlag[string](flags, flagChainBAddressType) - chainBKeyName, _ = getFlag[string](flags, flagChainBKeyName) - chainBKeyStoreType, _ = getFlag[string](flags, flagChainBKeyStoreType) - chainBStorePrefix, _ = getFlag[string](flags, flagChainBStorePrefix) - chainBDefaultGas, _ = getFlag[uint64](flags, flagChainBDefaultGas) - chainBMaxGas, _ = getFlag[uint64](flags, flagChainBMaxGas) - chainBGasPrice, _ = getFlag[string](flags, flagChainBGasPrice) - chainBGasMultiplier, _ = getFlag[string](flags, flagChainBGasMultiplier) - chainBMaxMsgNum, _ = getFlag[uint64](flags, flagChainBMaxMsgNum) - chainBMaxTxSize, _ = getFlag[uint64](flags, flagChainBMaxTxSize) - chainBClockDrift, _ = getFlag[string](flags, flagChainBClockDrift) - chainBMaxBlockTime, _ = getFlag[string](flags, flagChainBMaxBlockTime) - chainBTrustingPeriod, _ = getFlag[string](flags, flagChainBTrustingPeriod) - chainBTrustThresholdNumerator, _ = getFlag[uint64](flags, flagChainBTrustThresholdNumerator) - chainBTrustThresholdDenominator, _ = getFlag[uint64](flags, flagChainBTrustThresholdDenominator) - chainBCCVConsumerChain, _ = getFlag[bool](flags, flagChainBCCVConsumerChain) - chainBTrustedNode, _ = getFlag[bool](flags, flagChainBTrustedNode) - chainBMemoPrefix, _ = getFlag[string](flags, flagChainBMemoPrefix) - chainBType, _ = getFlag[string](flags, flagChainBType) - chainBSequentialBatchTx, _ = getFlag[bool](flags, flagChainBSequentialBatchTx) + chainBEventSourceMode, _ = flags.GetString(flagChainBEventSourceMode) + chainBEventSourceURL, _ = flags.GetString(flagChainBEventSourceURL) + chainBEventSourceBatchDelay, _ = flags.GetString(flagChainBEventSourceBatchDelay) + chainBRPCTimeout, _ = flags.GetString(flagChainBRPCTimeout) + chainBAccountPrefix, _ = flags.GetString(flagChainBAccountPrefix) + chainBAddressType, _ = flags.GetString(flagChainBAddressType) + chainBKeyName, _ = flags.GetString(flagChainBKeyName) + chainBKeyStoreType, _ = flags.GetString(flagChainBKeyStoreType) + chainBStorePrefix, _ = flags.GetString(flagChainBStorePrefix) + chainBDefaultGas, _ = flags.GetUint64(flagChainBDefaultGas) + chainBMaxGas, _ = flags.GetUint64(flagChainBMaxGas) + chainBGasPrice, _ = flags.GetString(flagChainBGasPrice) + chainBGasMultiplier, _ = flags.GetString(flagChainBGasMultiplier) + chainBMaxMsgNum, _ = flags.GetUint64(flagChainBMaxMsgNum) + chainBMaxTxSize, _ = flags.GetUint64(flagChainBMaxTxSize) + chainBClockDrift, _ = flags.GetString(flagChainBClockDrift) + chainBMaxBlockTime, _ = flags.GetString(flagChainBMaxBlockTime) + chainBTrustingPeriod, _ = flags.GetString(flagChainBTrustingPeriod) + chainBTrustThresholdNumerator, _ = flags.GetUint64(flagChainBTrustThresholdNumerator) + chainBTrustThresholdDenominator, _ = flags.GetUint64(flagChainBTrustThresholdDenominator) + chainBCCVConsumerChain, _ = flags.GetBool(flagChainBCCVConsumerChain) + chainBTrustedNode, _ = flags.GetBool(flagChainBTrustedNode) + chainBMemoPrefix, _ = flags.GetString(flagChainBMemoPrefix) + chainBType, _ = flags.GetString(flagChainBType) + chainBSequentialBatchTx, _ = flags.GetBool(flagChainBSequentialBatchTx) ) chainBGasMulti := new(big.Float) diff --git a/hermes/cmd/flag.go b/hermes/cmd/flag.go index d67e1265..03d8798e 100644 --- a/hermes/cmd/flag.go +++ b/hermes/cmd/flag.go @@ -1,10 +1,6 @@ package cmd import ( - "strconv" - "strings" - - "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/services/plugin" ) @@ -90,89 +86,7 @@ const ( mnemonicEntropySize = 256 ) -var ( - ErrFlagNotFound = errors.New("flag not found") - ErrFlagAssertion = errors.New("flag type assertion to failed") -) - -func getConfig(flags []*plugin.Flag) string { - config, _ := getFlag[string](flags, flagConfig) +func getConfig(flags plugin.Flags) string { + config, _ := flags.GetString(flagConfig) return config } - -// getFlag function to get flag with a generic return type -// TODO remove these helpers for flags after we fix this issue: -// https://github.com/ignite/apps/issues/116 -func getFlag[A any](flags []*plugin.Flag, key string) (result A, err error) { - v, err := getValue(flags, key) - if err != nil { - return result, err - } - - value, ok := v.(A) - if !ok { - return result, errors.Wrapf(ErrFlagAssertion, "type assertion to %T failed for field %s", v, key) - } - return value, nil -} - -func getValue(flags []*plugin.Flag, key string) (interface{}, error) { - for _, flag := range flags { - if flag.Name == key { - return exportToFlagValue(flag) - } - } - return nil, errors.Wrapf(ErrFlagNotFound, "flag %s not found", key) -} - -func exportToFlagValue(f *plugin.Flag) (interface{}, error) { - switch f.Type { - case plugin.FlagTypeBool: - v, err := strconv.ParseBool(flagValue(f)) - if err != nil { - return false, err - } - return v, nil - case plugin.FlagTypeInt: - v, err := strconv.Atoi(flagValue(f)) - if err != nil { - return 0, err - } - return v, nil - case plugin.FlagTypeUint: - v, err := strconv.ParseUint(flagValue(f), 10, 64) - if err != nil { - return uint(0), err - } - return uint(v), nil - case plugin.FlagTypeInt64: - v, err := strconv.ParseInt(flagValue(f), 10, 64) - if err != nil { - return int64(0), err - } - return v, nil - case plugin.FlagTypeUint64: - v, err := strconv.ParseUint(flagValue(f), 10, 64) - if err != nil { - return uint64(0), err - } - return v, nil - case plugin.FlagTypeStringSlice: - v := strings.Trim(flagValue(f), "[]") - s := strings.Split(v, ",") - if len(s) == 0 || (len(s) == 1 && s[0] == "") { - return []string{}, nil - } - - return s, nil - default: - return strings.TrimSpace(flagValue(f)), nil - } -} - -func flagValue(flag *plugin.Flag) string { - if flag.Value != "" { - return flag.Value - } - return flag.DefaultValue -} diff --git a/hermes/go.mod b/hermes/go.mod index 4ae4c0d0..4aadacad 100644 --- a/hermes/go.mod +++ b/hermes/go.mod @@ -7,7 +7,7 @@ require ( github.com/cosmos/go-bip39 v1.0.0 github.com/gookit/color v1.5.4 github.com/hashicorp/go-plugin v1.6.0 - github.com/ignite/cli/v28 v28.5.0 + github.com/ignite/cli/v28 v28.5.1 github.com/ignite/ignite-files/hermes v1.10.0 github.com/manifoldco/promptui v0.9.0 github.com/pelletier/go-toml/v2 v2.2.0 diff --git a/hermes/go.sum b/hermes/go.sum index 9b6cd6d6..2660f6f7 100644 --- a/hermes/go.sum +++ b/hermes/go.sum @@ -486,8 +486,8 @@ github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmK github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/ignite/cli/v28 v28.5.0 h1:MisHTu0cqMaka+A32DDHVVsGK2Hb5m+wYMPYYYpf27A= -github.com/ignite/cli/v28 v28.5.0/go.mod h1:HxY6jFzi4Z0oHjIDYWQ3kgYcg8kcETdAI/kwvxFcfeM= +github.com/ignite/cli/v28 v28.5.1 h1:8FnlB1E+eaT1yD/axdHdzBYPjggca5gQ3KlBwodrCWU= +github.com/ignite/cli/v28 v28.5.1/go.mod h1:HxY6jFzi4Z0oHjIDYWQ3kgYcg8kcETdAI/kwvxFcfeM= github.com/ignite/ignite-files/hermes v1.10.0 h1:mMxQO8Hfncy1GkRL3XZMIwtBcGV/SeM29nGX1TJRN+U= github.com/ignite/ignite-files/hermes v1.10.0/go.mod h1:gEV6t8um6pTNR8A49C7gyXTUjQJdHx50AhpcSij9KN0= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= From 29cf852e8db4d72e411ada99dd1bc18359d6efa4 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 6 Aug 2024 13:48:42 +0200 Subject: [PATCH 13/14] remove redundancy hermes install for integration tests --- hermes/integration/app_test.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/hermes/integration/app_test.go b/hermes/integration/app_test.go index 4590545b..49dbea41 100644 --- a/hermes/integration/app_test.go +++ b/hermes/integration/app_test.go @@ -285,12 +285,13 @@ func TestCustomIBCTx(t *testing.T) { require.NoError(t, err) pluginPath := filepath.Join(filepath.Dir(filepath.Dir(dir)), "hermes") - env.Must(env.Exec("install hermes app locally", - step.NewSteps(step.New( - step.Exec(envtest.IgniteApp, "app", "install", pluginPath), - step.Workdir(app.SourcePath()), - )), - )) + // We don't need to install Hermes again since it's a builtin extension. + // env.Must(env.Exec("install hermes app locally", + // step.NewSteps(step.New( + // step.Exec(envtest.IgniteApp, "app", "install", pluginPath), + // step.Workdir(app.SourcePath()), + // )), + // )) // One local plugin expected assertLocalPlugins(t, app, []pluginsconfig.Plugin{{Path: pluginPath}}) @@ -630,12 +631,13 @@ func TestTransferIBCTx(t *testing.T) { require.NoError(t, err) pluginPath := filepath.Join(filepath.Dir(filepath.Dir(dir)), "hermes") - env.Must(env.Exec("install hermes app locally", - step.NewSteps(step.New( - step.Exec(envtest.IgniteApp, "app", "install", pluginPath), - step.Workdir(app.SourcePath()), - )), - )) + // We don't need to install Hermes again since it's a builtin extension. + // env.Must(env.Exec("install hermes app locally", + // step.NewSteps(step.New( + // step.Exec(envtest.IgniteApp, "app", "install", pluginPath), + // step.Workdir(app.SourcePath()), + // )), + // )) // One local plugin expected assertLocalPlugins(t, app, []pluginsconfig.Plugin{{Path: pluginPath}}) From eed0c77ad35f7642aba86caa6c8d4f0a0267af51 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 6 Aug 2024 13:54:21 +0200 Subject: [PATCH 14/14] remove unused code --- hermes/integration/app_test.go | 50 ---------------------------------- 1 file changed, 50 deletions(-) diff --git a/hermes/integration/app_test.go b/hermes/integration/app_test.go index 49dbea41..88c3a914 100644 --- a/hermes/integration/app_test.go +++ b/hermes/integration/app_test.go @@ -16,14 +16,12 @@ import ( "github.com/ignite/cli/v28/ignite/config/chain" "github.com/ignite/cli/v28/ignite/config/chain/base" v1 "github.com/ignite/cli/v28/ignite/config/chain/v1" - pluginsconfig "github.com/ignite/cli/v28/ignite/config/plugins" "github.com/ignite/cli/v28/ignite/pkg/availableport" "github.com/ignite/cli/v28/ignite/pkg/cmdrunner" "github.com/ignite/cli/v28/ignite/pkg/cmdrunner/step" "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/xast" yamlmap "github.com/ignite/cli/v28/ignite/pkg/yaml" - "github.com/ignite/cli/v28/ignite/services/plugin" envtest "github.com/ignite/cli/v28/integration" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" @@ -281,22 +279,6 @@ func TestCustomIBCTx(t *testing.T) { require.NoError(t, os.RemoveAll(tmpDir)) }) - dir, err := os.Getwd() - require.NoError(t, err) - pluginPath := filepath.Join(filepath.Dir(filepath.Dir(dir)), "hermes") - - // We don't need to install Hermes again since it's a builtin extension. - // env.Must(env.Exec("install hermes app locally", - // step.NewSteps(step.New( - // step.Exec(envtest.IgniteApp, "app", "install", pluginPath), - // step.Workdir(app.SourcePath()), - // )), - // )) - - // One local plugin expected - assertLocalPlugins(t, app, []pluginsconfig.Plugin{{Path: pluginPath}}) - assertGlobalPlugins(t, nil) - // prepare the chain env.Must(env.Exec("create an IBC module", step.NewSteps(step.New( @@ -627,22 +609,6 @@ func TestTransferIBCTx(t *testing.T) { require.NoError(t, os.RemoveAll(tmpDir)) }) - dir, err := os.Getwd() - require.NoError(t, err) - pluginPath := filepath.Join(filepath.Dir(filepath.Dir(dir)), "hermes") - - // We don't need to install Hermes again since it's a builtin extension. - // env.Must(env.Exec("install hermes app locally", - // step.NewSteps(step.New( - // step.Exec(envtest.IgniteApp, "app", "install", pluginPath), - // step.Workdir(app.SourcePath()), - // )), - // )) - - // One local plugin expected - assertLocalPlugins(t, app, []pluginsconfig.Plugin{{Path: pluginPath}}) - assertGlobalPlugins(t, nil) - // prepare the chain env.Must(env.Exec("create an IBC module", step.NewSteps(step.New( @@ -873,19 +839,3 @@ func TestTransferIBCTx(t *testing.T) { ), ))) } - -func assertLocalPlugins(t *testing.T, app envtest.App, expectedPlugins []pluginsconfig.Plugin) { - t.Helper() - cfg, err := pluginsconfig.ParseDir(app.SourcePath()) - require.NoError(t, err) - require.ElementsMatch(t, expectedPlugins, cfg.Apps, "unexpected local apps") -} - -func assertGlobalPlugins(t *testing.T, expectedPlugins []pluginsconfig.Plugin) { - t.Helper() - cfgPath, err := plugin.PluginsPath() - require.NoError(t, err) - cfg, err := pluginsconfig.ParseDir(cfgPath) - require.NoError(t, err) - require.ElementsMatch(t, expectedPlugins, cfg.Apps, "unexpected global apps") -}