github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/upgrade/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  	upgrade "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/upgrade/internal/types"
    12  )
    13  
    14  // GetPlanCmd returns the query upgrade plan command
    15  func GetPlanCmd(storeName string, cdc *codec.Codec) *cobra.Command {
    16  	return &cobra.Command{
    17  		Use:   "plan",
    18  		Short: "get upgrade plan (if one exists)",
    19  		Long:  "Gets the currently scheduled upgrade plan, if one exists",
    20  		Args:  cobra.ExactArgs(0),
    21  		RunE: func(cmd *cobra.Command, args []string) error {
    22  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    23  
    24  			// ignore height for now
    25  			res, _, err := cliCtx.Query(fmt.Sprintf("custom/%s/%s", upgrade.QuerierKey, upgrade.QueryCurrent))
    26  			if err != nil {
    27  				return err
    28  			}
    29  
    30  			if len(res) == 0 {
    31  				return fmt.Errorf("no upgrade scheduled")
    32  			}
    33  
    34  			var plan upgrade.Plan
    35  			err = cdc.UnmarshalJSON(res, &plan)
    36  			if err != nil {
    37  				return err
    38  			}
    39  			return cliCtx.PrintOutput(plan)
    40  		},
    41  	}
    42  }
    43  
    44  // GetAppliedHeightCmd returns the height at which a completed upgrade was applied
    45  func GetAppliedHeightCmd(storeName string, cdc *codec.Codec) *cobra.Command {
    46  	return &cobra.Command{
    47  		Use:   "applied [upgrade-name]",
    48  		Short: "block header for height at which a completed upgrade was applied",
    49  		Long: "If upgrade-name was previously executed on the chain, this returns the header for the block at which it was applied.\n" +
    50  			"This helps a client determine which binary was valid over a given range of blocks, as well as more context to understand past migrations.",
    51  		Args: cobra.ExactArgs(1),
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			cliCtx := context.NewCLIContext().WithCodec(cdc)
    54  
    55  			name := args[0]
    56  			params := upgrade.NewQueryAppliedParams(name)
    57  			bz, err := cliCtx.Codec.MarshalJSON(params)
    58  			if err != nil {
    59  				return err
    60  			}
    61  
    62  			res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", upgrade.QuerierKey, upgrade.QueryApplied), bz)
    63  			if err != nil {
    64  				return err
    65  			}
    66  
    67  			if len(res) == 0 {
    68  				return fmt.Errorf("no upgrade found")
    69  			}
    70  			if len(res) != 8 {
    71  				return fmt.Errorf("unknown format for applied-upgrade")
    72  			}
    73  			applied := int64(binary.BigEndian.Uint64(res))
    74  
    75  			// we got the height, now let's return the headers
    76  			node, err := cliCtx.GetNode()
    77  			if err != nil {
    78  				return err
    79  			}
    80  			headers, err := node.BlockchainInfo(applied, applied)
    81  			if err != nil {
    82  				return err
    83  			}
    84  			if len(headers.BlockMetas) == 0 {
    85  				return fmt.Errorf("no headers returned for height %d", applied)
    86  			}
    87  
    88  			// always output json as Header is unreable in toml ([]byte is a long list of numbers)
    89  			bz, err = cdc.MarshalJSONIndent(headers.BlockMetas[0], "", "  ")
    90  			if err != nil {
    91  				return err
    92  			}
    93  			fmt.Println(string(bz))
    94  			return nil
    95  		},
    96  	}
    97  }