-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cli to submit gov proposal to upgrade multiple channels #5548
Merged
chatton
merged 7 commits into
main
from
cian/issue#5540-add-cli-to-submit-gov-proposal-to-upgrade-multiple-channels
Jan 9, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
024cb07
chore: wip cli
chatton d6b3759
chore: create UpgradeChannels cobra command
chatton 505a6af
chore: added support for comma separated list of channel IDs
chatton 91569f1
chore: addressing PR feedback
chatton 9ebdbe8
chore: merge main
chatton 91e87d2
chore: merge main
chatton 70758b0
chore: additional PR feedback
chatton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,33 @@ package cli | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/version" | ||
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli" | ||
|
||
"github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" | ||
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" | ||
) | ||
|
||
// NewPruneAcknowledgementsTxCmd returns the command to create a new MsgPruneAcknowledgements transaction | ||
func NewPruneAcknowledgementsTxCmd() *cobra.Command { | ||
const ( | ||
flagJSON = "json" | ||
flagPortPattern = "port-pattern" | ||
flagExpedited = "expedited" | ||
flagChannelIDs = "channel-ids" | ||
) | ||
|
||
// newPruneAcknowledgementsTxCmd returns the command to create a new MsgPruneAcknowledgements transaction | ||
func newPruneAcknowledgementsTxCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "prune-acknowledgements [port] [channel] [limit]", | ||
Short: "Prune expired packet acknowledgements stored in IBC state", | ||
|
@@ -45,3 +58,118 @@ func NewPruneAcknowledgementsTxCmd() *cobra.Command { | |
flags.AddTxFlagsToCmd(cmd) | ||
return cmd | ||
} | ||
|
||
func newUpgradeChannelsTxCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "upgrade-channels", | ||
Short: "Upgrade IBC channels", | ||
Long: `Submit a governance proposal to upgrade all open channels whose port matches a specified pattern | ||
(the default is transfer), optionally, an exact list of comma separated channel IDs may be specified.`, | ||
Args: cobra.ExactArgs(1), | ||
Example: fmt.Sprintf(`%s tx %s %s upgrade-channels "{\"fee_version\":\"ics29-1\",\"app_version\":\"ics20-1\"}" --deposit 10stake`, version.AppName, ibcexported.ModuleName, types.SubModuleName), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
versionStr := args[0] | ||
|
||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
portPattern, err := cmd.Flags().GetString(flagPortPattern) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
commaSeparatedChannelIDs, err := cmd.Flags().GetString(flagChannelIDs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
displayJSON, err := cmd.Flags().GetBool(flagJSON) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
resp, err := queryClient.Channels(cmd.Context(), &types.QueryChannelsRequest{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
channelIDs := getChannelIDs(commaSeparatedChannelIDs) | ||
|
||
var msgs []sdk.Msg | ||
pattern := regexp.MustCompile(portPattern) | ||
|
||
for _, ch := range resp.Channels { | ||
|
||
if !channelShouldBeUpgraded(*ch, pattern, channelIDs) { | ||
continue | ||
} | ||
|
||
// construct a MsgChannelUpgradeInit which will upgrade the specified channel to a specific version. | ||
msgUpgradeInit := types.NewMsgChannelUpgradeInit(ch.PortId, ch.ChannelId, types.NewUpgradeFields(ch.Ordering, ch.ConnectionHops, versionStr), clientCtx.GetFromAddress().String()) | ||
msgs = append(msgs, msgUpgradeInit) | ||
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left ordering and connection hops unchanged, and only the version string changes. Technically we can allow these fields to be updated, but I think the version string will be all that we need 99% of the time. As far as bulk creating |
||
} | ||
|
||
if len(msgs) == 0 { | ||
return fmt.Errorf("no channels would be upgraded with pattern %s", portPattern) | ||
} | ||
|
||
msgSubmitProposal, err := govcli.ReadGovPropFlags(clientCtx, cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := msgSubmitProposal.SetMsgs(msgs); err != nil { | ||
return err | ||
} | ||
|
||
dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun) | ||
if displayJSON || dryRun { | ||
out, err := clientCtx.Codec.MarshalJSON(msgSubmitProposal) | ||
if err != nil { | ||
return err | ||
} | ||
return clientCtx.PrintBytes(out) | ||
} | ||
|
||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgSubmitProposal) | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
govcli.AddGovPropFlagsToCmd(cmd) | ||
cmd.Flags().Bool(flagJSON, false, "specify true to output valid proposal.json contents, instead of submitting a governance proposal.") | ||
cmd.Flags().String(flagPortPattern, "transfer", "The pattern to use to match port ids.") | ||
cmd.Flags().String(flagChannelIDs, "", "a comma separated list of channel IDs to upgrade.") | ||
cmd.Flags().Bool(flagExpedited, false, "set the expedited value for the governance proposal.") | ||
|
||
return cmd | ||
} | ||
|
||
// getChannelIDs returns a slice of channel IDs based on a comma separated string of channel IDs. | ||
func getChannelIDs(commaSeparatedList string) []string { | ||
if strings.TrimSpace(commaSeparatedList) == "" { | ||
return nil | ||
} | ||
return strings.Split(commaSeparatedList, ",") | ||
} | ||
|
||
// channelShouldBeUpgraded returns a boolean indicating whether or not the given channel should be upgraded based | ||
// on either the provided regex pattern or list of desired channel IDs. | ||
func channelShouldBeUpgraded(channel types.IdentifiedChannel, pattern *regexp.Regexp, channelIDs []string) bool { | ||
// skip any channel that is not open | ||
if channel.State != types.OPEN { | ||
return false | ||
} | ||
|
||
// if specified, the channel ID must exactly match. | ||
if len(channelIDs) > 0 { | ||
return pattern.MatchString(channel.PortId) && slices.Contains(channelIDs, channel.ChannelId) | ||
} | ||
|
||
// otherwise we only need the port pattern to match. | ||
return pattern.MatchString(channel.PortId) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unexported this to stay consistent, I don't think this needs to be exported.