github.com/Finschia/finschia-sdk@v0.48.1/x/upgrade/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/Finschia/finschia-sdk/client"
     9  	"github.com/Finschia/finschia-sdk/client/flags"
    10  	"github.com/Finschia/finschia-sdk/types/errors"
    11  	"github.com/Finschia/finschia-sdk/x/upgrade/types"
    12  )
    13  
    14  // GetQueryCmd returns the parent command for all x/upgrade CLi query commands.
    15  func GetQueryCmd() *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   types.ModuleName,
    18  		Short: "Querying commands for the upgrade module",
    19  	}
    20  
    21  	cmd.AddCommand(
    22  		GetCurrentPlanCmd(),
    23  		GetAppliedPlanCmd(),
    24  		GetModuleVersionsCmd(),
    25  	)
    26  
    27  	return cmd
    28  }
    29  
    30  // GetCurrentPlanCmd returns the query upgrade plan command.
    31  func GetCurrentPlanCmd() *cobra.Command {
    32  	cmd := &cobra.Command{
    33  		Use:   "plan",
    34  		Short: "get upgrade plan (if one exists)",
    35  		Long:  "Gets the currently scheduled upgrade plan, if one exists",
    36  		Args:  cobra.ExactArgs(0),
    37  		RunE: func(cmd *cobra.Command, args []string) error {
    38  			clientCtx, err := client.GetClientQueryContext(cmd)
    39  			if err != nil {
    40  				return err
    41  			}
    42  			queryClient := types.NewQueryClient(clientCtx)
    43  
    44  			params := types.QueryCurrentPlanRequest{}
    45  			res, err := queryClient.CurrentPlan(cmd.Context(), &params)
    46  			if err != nil {
    47  				return err
    48  			}
    49  
    50  			if res.Plan == nil {
    51  				return fmt.Errorf("no upgrade scheduled")
    52  			}
    53  
    54  			return clientCtx.PrintProto(res.GetPlan())
    55  		},
    56  	}
    57  
    58  	flags.AddQueryFlagsToCmd(cmd)
    59  
    60  	return cmd
    61  }
    62  
    63  // GetAppliedPlanCmd returns information about the block at which a completed
    64  // upgrade was applied.
    65  func GetAppliedPlanCmd() *cobra.Command {
    66  	cmd := &cobra.Command{
    67  		Use:   "applied [upgrade-name]",
    68  		Short: "block header for height at which a completed upgrade was applied",
    69  		Long: "If upgrade-name was previously executed on the chain, this returns the header for the block at which it was applied.\n" +
    70  			"This helps a client determine which binary was valid over a given range of blocks, as well as more context to understand past migrations.",
    71  		Args: cobra.ExactArgs(1),
    72  		RunE: func(cmd *cobra.Command, args []string) error {
    73  			clientCtx, err := client.GetClientQueryContext(cmd)
    74  			if err != nil {
    75  				return err
    76  			}
    77  			queryClient := types.NewQueryClient(clientCtx)
    78  			ctx := cmd.Context()
    79  			params := types.QueryAppliedPlanRequest{Name: args[0]}
    80  			res, err := queryClient.AppliedPlan(ctx, &params)
    81  			if err != nil {
    82  				return err
    83  			}
    84  
    85  			if res.Height == 0 {
    86  				return fmt.Errorf("no upgrade found")
    87  			}
    88  
    89  			// we got the height, now let's return the headers
    90  			node, err := clientCtx.GetNode()
    91  			if err != nil {
    92  				return err
    93  			}
    94  			headers, err := node.BlockchainInfo(ctx, res.Height, res.Height)
    95  			if err != nil {
    96  				return err
    97  			}
    98  			if len(headers.BlockMetas) == 0 {
    99  				return fmt.Errorf("no headers returned for height %d", res.Height)
   100  			}
   101  
   102  			// always output json as Header is unreable in toml ([]byte is a long list of numbers)
   103  			bz, err := clientCtx.LegacyAmino.MarshalJSONIndent(headers.BlockMetas[0], "", "  ")
   104  			if err != nil {
   105  				return err
   106  			}
   107  			return clientCtx.PrintString(fmt.Sprintf("%s\n", string(bz)))
   108  		},
   109  	}
   110  
   111  	flags.AddQueryFlagsToCmd(cmd)
   112  
   113  	return cmd
   114  }
   115  
   116  // GetModuleVersionsCmd returns the module version list from state
   117  func GetModuleVersionsCmd() *cobra.Command {
   118  	cmd := &cobra.Command{
   119  		Use:   "module_versions [optional module_name]",
   120  		Short: "get the list of module versions",
   121  		Long: "Gets a list of module names and their respective consensus versions.\n" +
   122  			"Following the command with a specific module name will return only\n" +
   123  			"that module's information.",
   124  		Args: cobra.MaximumNArgs(1),
   125  		RunE: func(cmd *cobra.Command, args []string) error {
   126  			clientCtx, err := client.GetClientQueryContext(cmd)
   127  			if err != nil {
   128  				return err
   129  			}
   130  
   131  			queryClient := types.NewQueryClient(clientCtx)
   132  			var params types.QueryModuleVersionsRequest
   133  
   134  			if len(args) == 1 {
   135  				params = types.QueryModuleVersionsRequest{ModuleName: args[0]}
   136  			} else {
   137  				params = types.QueryModuleVersionsRequest{}
   138  			}
   139  
   140  			res, err := queryClient.ModuleVersions(cmd.Context(), &params)
   141  			if err != nil {
   142  				return err
   143  			}
   144  
   145  			if res.ModuleVersions == nil {
   146  				return errors.ErrNotFound
   147  			}
   148  
   149  			return clientCtx.PrintProto(res)
   150  		},
   151  	}
   152  
   153  	flags.AddQueryFlagsToCmd(cmd)
   154  
   155  	return cmd
   156  }