-
Notifications
You must be signed in to change notification settings - Fork 4.2k
x/upgrade: Refactor CLI to use protobuf query #6623
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
Changes from 17 commits
323531a
72d0f2d
c45824c
3d60e63
25cae9c
8a4e906
2ee9adb
025f6d4
e8eadd6
3c1321d
50b9c5c
2fd0de2
b8421f3
3fb0ea6
7ab6094
9c005a8
225cb55
144a2a8
1ad236a
1610974
21c369a
820790e
3ecfb9b
bd52bea
3225d18
293dba8
6767917
6435cef
982c573
2a821ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,98 +1,102 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/client" | ||
| "github.com/cosmos/cosmos-sdk/codec" | ||
| "github.com/cosmos/cosmos-sdk/client/flags" | ||
| "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
| ) | ||
|
|
||
| // GetPlanCmd returns the query upgrade plan command | ||
| func GetPlanCmd(storeName string, cdc *codec.Codec) *cobra.Command { | ||
| // GetQueryCmd returns the parent command for all x/upgrade CLi query commands | ||
| func GetQueryCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: types.ModuleName, | ||
| Short: "Querying commands for the upgrade module", | ||
| } | ||
| cmd.AddCommand(flags.GetCommands( | ||
| GetCurrentPlanCmd(), | ||
| GetAppliedPlanCmd(), | ||
| )...) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // GetCurrentPlanCmd returns the query upgrade plan command | ||
| func GetCurrentPlanCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "plan", | ||
| Short: "get upgrade plan (if one exists)", | ||
| Long: "Gets the currently scheduled upgrade plan, if one exists", | ||
| Args: cobra.ExactArgs(0), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc) | ||
| clientCtx := client.GetClientContextFromCmd(cmd) | ||
| queryClient := types.NewQueryClient(clientCtx.Init()) | ||
|
|
||
| // ignore height for now | ||
| res, _, err := clientCtx.Query(fmt.Sprintf("custom/%s/%s", types.QuerierKey, types.QueryCurrent)) | ||
| params := types.NewQueryCurrentPlanRequest() | ||
| res, err := queryClient.CurrentPlan(context.Background(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(res) == 0 { | ||
| if len(res.Plan.Name) == 0 { | ||
| return fmt.Errorf("no upgrade scheduled") | ||
| } | ||
|
|
||
| var plan types.Plan | ||
| err = cdc.UnmarshalJSON(res, &plan) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return clientCtx.PrintOutput(plan) | ||
| return clientCtx.PrintOutput(res.Plan) | ||
amaury1093 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| } | ||
| } | ||
|
|
||
| // GetAppliedHeightCmd returns the height at which a completed upgrade was applied | ||
| func GetAppliedHeightCmd(storeName string, cdc *codec.Codec) *cobra.Command { | ||
| // GetAppliedPlanCmd returns information about the block at which a completed | ||
| // upgrade was applied | ||
| func GetAppliedPlanCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "applied [upgrade-name]", | ||
| Short: "block header for height at which a completed upgrade was applied", | ||
| Long: "If upgrade-name was previously executed on the chain, this returns the header for the block at which it was applied.\n" + | ||
| "This helps a client determine which binary was valid over a given range of blocks, as well as more context to understand past migrations.", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| clientCtx := client.NewContext().WithCodec(cdc).WithJSONMarshaler(cdc) | ||
| clientCtx := client.GetClientContextFromCmd(cmd) | ||
| queryClient := types.NewQueryClient(clientCtx.Init()) | ||
fedekunze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| name := args[0] | ||
| params := types.NewQueryAppliedPlanRequest(name) | ||
| bz, err := clientCtx.JSONMarshaler.MarshalJSON(params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierKey, types.QueryApplied), bz) | ||
| res, err := queryClient.AppliedPlan(context.Background(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(res) == 0 { | ||
| if res.Height == 0 { | ||
| return fmt.Errorf("no upgrade found") | ||
| } | ||
| if len(res) != 8 { | ||
| return fmt.Errorf("unknown format for applied-upgrade") | ||
| } | ||
| applied := int64(binary.BigEndian.Uint64(res)) | ||
|
|
||
| // we got the height, now let's return the headers | ||
| node, err := clientCtx.GetNode() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| headers, err := node.BlockchainInfo(applied, applied) | ||
| headers, err := node.BlockchainInfo(res.Height, res.Height) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(headers.BlockMetas) == 0 { | ||
| return fmt.Errorf("no headers returned for height %d", applied) | ||
| return fmt.Errorf("no headers returned for height %d", res.Height) | ||
| } | ||
|
|
||
| // always output json as Header is unreable in toml ([]byte is a long list of numbers) | ||
| bz, err = cdc.MarshalJSONIndent(headers.BlockMetas[0], "", " ") | ||
| bz, err := clientCtx.Codec.MarshalJSONIndent(headers.BlockMetas[0], "", " ") | ||
|
Contributor
Author
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. didn't find BlockMetas in tm's proto files, so not using protobuf here |
||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Println(string(bz)) | ||
| return nil | ||
| return clientCtx.PrintOutput(string(bz)) | ||
| }, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,14 @@ import ( | |
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/client/tx" | ||
| gov "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/x/gov/client/cli" | ||
|
|
||
| "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/x/gov/client/cli" | ||
| gov "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
| "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
| ) | ||
|
|
||
|
|
@@ -25,9 +24,19 @@ const ( | |
| FlagUpgradeInfo = "info" | ||
| ) | ||
|
|
||
| // GetTxCmd returns the transaction commands for this module | ||
| func GetTxCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: types.ModuleName, | ||
| Short: "Upgrade transaction subcommands", | ||
| } | ||
| cmd.AddCommand(flags.PostCommands()...) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // NewCmdSubmitUpgradeProposal implements a command handler for submitting a software upgrade proposal transaction. | ||
| func NewCmdSubmitUpgradeProposal(clientCtx client.Context) *cobra.Command { | ||
|
Contributor
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. This still needs to be updated: see #6639 for reference
Contributor
Author
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 actually went down this path, but it requires a bigger refactor:
what I propose:
|
||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "software-upgrade [name] (--upgrade-height [height] | --upgrade-time [time]) (--upgrade-info [info]) [flags]", | ||
| Args: cobra.ExactArgs(1), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.