cosmos-sdk/x/upgrade/client/cli/query.go
SaReN 39f53ac22f
client: rename CliContext to Context (#6290)
* Refactor CliContext as Context

* Fix lint issues

* Fix goimports

* Fix gov tests

* Resolved ci-lint issues

* Add changelog

* Rename cliCtx to clientCtx

* Fix mocks and routes

* Add changelog

* Update changelog

* Apply suggestions from code review

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* merge client/rpc/ro{ot,utes}.go

* Update docs

* client/rpc: remove redundant client/rpc.RegisterRPCRoutes

* regenerate mocks

* Update ADRs

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-06-01 12:46:03 +00:00

99 lines
2.8 KiB
Go

package cli
import (
"encoding/binary"
"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"
)
// GetPlanCmd returns the query upgrade plan command
func GetPlanCmd(storeName string, cdc *codec.Codec) *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)
// ignore height for now
res, _, err := clientCtx.Query(fmt.Sprintf("custom/%s/%s", types.QuerierKey, types.QueryCurrent))
if err != nil {
return err
}
if len(res) == 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)
},
}
}
// GetAppliedHeightCmd returns the height at which a completed upgrade was applied
func GetAppliedHeightCmd(storeName string, cdc *codec.Codec) *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)
name := args[0]
params := types.NewQueryAppliedParams(name)
bz, err := clientCtx.Codec.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierKey, types.QueryApplied), bz)
if err != nil {
return err
}
if len(res) == 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)
if err != nil {
return err
}
if len(headers.BlockMetas) == 0 {
return fmt.Errorf("no headers returned for height %d", applied)
}
// always output json as Header is unreable in toml ([]byte is a long list of numbers)
bz, err = cdc.MarshalJSONIndent(headers.BlockMetas[0], "", " ")
if err != nil {
return err
}
fmt.Println(string(bz))
return nil
},
}
}