github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/02-client/client/cli/query.go (about)

     1  package cli
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  	interfacetypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/version"
    13  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/client/utils"
    14  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types"
    15  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  const (
    20  	flagLatestHeight = "latest-height"
    21  )
    22  
    23  func GetCmdQueryClientStates(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
    24  	cmd := &cobra.Command{
    25  		Use:     "states",
    26  		Short:   "Query all available light clients",
    27  		Long:    "Query all available light clients",
    28  		Example: fmt.Sprintf("%s query %s %s states", version.ServerName, host.ModuleName, types.SubModuleName),
    29  		Args:    cobra.NoArgs,
    30  		RunE: func(cmd *cobra.Command, _ []string) error {
    31  			cliCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
    32  
    33  			pageReq, err := client.ReadPageRequest(cmd.Flags())
    34  			if err != nil {
    35  				return err
    36  			}
    37  
    38  			req := &types.QueryClientStatesRequest{
    39  				Pagination: pageReq,
    40  			}
    41  
    42  			queryClient := types.NewQueryClient(cliCtx)
    43  			res, err := queryClient.ClientStates(cmd.Context(), req)
    44  			if err != nil {
    45  				return err
    46  			}
    47  
    48  			return cliCtx.PrintProto(res)
    49  		},
    50  	}
    51  	flags.AddQueryFlagsToCmd(cmd)
    52  	flags.AddPaginationFlagsToCmd(cmd, "client states")
    53  
    54  	return cmd
    55  }
    56  
    57  // GetCmdQueryClientState defines the command to query the state of a client with
    58  // a given id as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-002-client-semantics#query
    59  func GetCmdQueryClientState(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
    60  	cmd := &cobra.Command{
    61  		Use:     "state [client-id]",
    62  		Short:   "Query a client state",
    63  		Long:    "Query stored client state",
    64  		Example: fmt.Sprintf("%s query %s %s state [client-id]", version.ServerName, host.ModuleName, types.SubModuleName),
    65  		Args:    cobra.ExactArgs(1),
    66  		RunE: func(cmd *cobra.Command, args []string) error {
    67  
    68  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
    69  			clientID := args[0]
    70  			prove, _ := cmd.Flags().GetBool(flags.FlagProve)
    71  
    72  			clientStateRes, err := utils.QueryClientState(clientCtx, clientID, prove)
    73  			if err != nil {
    74  				return err
    75  			}
    76  
    77  			return clientCtx.PrintProto(clientStateRes)
    78  		},
    79  	}
    80  
    81  	cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results")
    82  	flags.AddQueryFlagsToCmd(cmd)
    83  
    84  	return cmd
    85  }
    86  
    87  // GetCmdQueryConsensusStates defines the command to query all the consensus states from a given
    88  // client state.
    89  func GetCmdQueryConsensusStates(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
    90  	cmd := &cobra.Command{
    91  		Use:     "consensus-states [client-id]",
    92  		Short:   "Query all the consensus states of a client.",
    93  		Long:    "Query all the consensus states from a given client state.",
    94  		Example: fmt.Sprintf("%s query %s %s consensus-states [client-id]", version.ServerName, host.ModuleName, types.SubModuleName),
    95  		Args:    cobra.ExactArgs(1),
    96  		RunE: func(cmd *cobra.Command, args []string) error {
    97  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
    98  			clientID := args[0]
    99  
   100  			queryClient := types.NewQueryClient(clientCtx)
   101  
   102  			pageReq, err := client.ReadPageRequest(cmd.Flags())
   103  			if err != nil {
   104  				return err
   105  			}
   106  
   107  			req := &types.QueryConsensusStatesRequest{
   108  				ClientId:   clientID,
   109  				Pagination: pageReq,
   110  			}
   111  
   112  			res, err := queryClient.ConsensusStates(cmd.Context(), req)
   113  			if err != nil {
   114  				return err
   115  			}
   116  
   117  			return clientCtx.PrintProto(res)
   118  		},
   119  	}
   120  	flags.AddQueryFlagsToCmd(cmd)
   121  	flags.AddPaginationFlagsToCmd(cmd, "consensus states")
   122  
   123  	return cmd
   124  }
   125  
   126  // GetCmdQueryConsensusState defines the command to query the consensus state of
   127  // the chain as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-002-client-semantics#query
   128  func GetCmdQueryConsensusState(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
   129  	cmd := &cobra.Command{
   130  		Use:   "consensus-state [client-id] [height]",
   131  		Short: "Query the consensus state of a client at a given height",
   132  		Long: `Query the consensus state for a particular light client at a given height.
   133  If the '--latest' flag is included, the query returns the latest consensus state, overriding the height argument.`,
   134  		Example: fmt.Sprintf("%s query %s %s  consensus-state [client-id] [height]", version.ServerName, host.ModuleName, types.SubModuleName),
   135  		Args:    cobra.RangeArgs(1, 2),
   136  		RunE: func(cmd *cobra.Command, args []string) error {
   137  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
   138  			clientID := args[0]
   139  			queryLatestHeight, _ := cmd.Flags().GetBool(flagLatestHeight)
   140  			var height types.Height
   141  
   142  			if !queryLatestHeight {
   143  				if len(args) != 2 {
   144  					return errors.New("must include a second 'height' argument when '--latest-height' flag is not provided")
   145  				}
   146  
   147  				h, err := types.ParseHeight(args[1])
   148  				if err != nil {
   149  					return err
   150  				}
   151  				height = h
   152  			}
   153  
   154  			prove, _ := cmd.Flags().GetBool(flags.FlagProve)
   155  
   156  			csRes, err := utils.QueryConsensusState(clientCtx, clientID, height, prove, queryLatestHeight)
   157  			if err != nil {
   158  				return err
   159  			}
   160  
   161  			return clientCtx.PrintProto(csRes)
   162  		},
   163  	}
   164  
   165  	cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results")
   166  	cmd.Flags().Bool(flagLatestHeight, false, "return latest stored consensus state")
   167  	flags.AddQueryFlagsToCmd(cmd)
   168  
   169  	return cmd
   170  }
   171  
   172  // GetCmdQueryHeader defines the command to query the latest header on the chain
   173  func GetCmdQueryHeader(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
   174  	cmd := &cobra.Command{
   175  		Use:     "header",
   176  		Short:   "Query the latest header of the running chain",
   177  		Long:    "Query the latest Tendermint header of the running chain",
   178  		Example: fmt.Sprintf("%s query %s %s  header", version.ServerName, host.ModuleName, types.SubModuleName),
   179  		Args:    cobra.NoArgs,
   180  		RunE: func(cmd *cobra.Command, _ []string) error {
   181  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
   182  			header, _, err := utils.QueryTendermintHeader(clientCtx)
   183  			if err != nil {
   184  				return err
   185  			}
   186  
   187  			return clientCtx.PrintProto(&header)
   188  		},
   189  	}
   190  
   191  	flags.AddQueryFlagsToCmd(cmd)
   192  
   193  	return cmd
   194  }
   195  
   196  // GetCmdSelfConsensusState defines the command to query the self consensus state of a chain
   197  func GetCmdSelfConsensusState(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
   198  	cmd := &cobra.Command{
   199  		Use:     "self-consensus-state",
   200  		Short:   "Query the self consensus state for this chain",
   201  		Long:    "Query the self consensus state for this chain. This result may be used for verifying IBC clients representing this chain which are hosted on counterparty chains.",
   202  		Example: fmt.Sprintf("%s query %s %s self-consensus-state", version.ServerName, host.ModuleName, types.SubModuleName),
   203  		Args:    cobra.NoArgs,
   204  		RunE: func(cmd *cobra.Command, _ []string) error {
   205  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
   206  			state, _, err := utils.QuerySelfConsensusState(clientCtx)
   207  			if err != nil {
   208  				return err
   209  			}
   210  
   211  			return clientCtx.PrintProto(state)
   212  		},
   213  	}
   214  
   215  	flags.AddQueryFlagsToCmd(cmd)
   216  
   217  	return cmd
   218  }
   219  
   220  // GetCmdParams returns the command handler for ibc client parameter querying.
   221  func GetCmdParams(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
   222  	cmd := &cobra.Command{
   223  		Use:     "params",
   224  		Short:   "Query the current ibc client parameters",
   225  		Long:    "Query the current ibc client parameters",
   226  		Args:    cobra.NoArgs,
   227  		Example: fmt.Sprintf("%s query %s %s params", version.ServerName, host.ModuleName, types.SubModuleName),
   228  		RunE: func(cmd *cobra.Command, _ []string) error {
   229  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
   230  			queryClient := types.NewQueryClient(clientCtx)
   231  
   232  			res, _ := queryClient.ClientParams(cmd.Context(), &types.QueryClientParamsRequest{})
   233  			return clientCtx.PrintProto(res.Params)
   234  		},
   235  	}
   236  
   237  	flags.AddQueryFlagsToCmd(cmd)
   238  
   239  	return cmd
   240  }
   241  
   242  func GetCmdQueryClientStatus(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
   243  	cmd := &cobra.Command{
   244  		Use:     "status [client-id]",
   245  		Short:   "Query client status",
   246  		Long:    "Query client activity status. Any client without an 'Active' status is considered inactive",
   247  		Example: fmt.Sprintf("%s query %s %s status [client-id]", version.ServerName, host.ModuleName, types.SubModuleName),
   248  		Args:    cobra.ExactArgs(1),
   249  		RunE: func(cmd *cobra.Command, args []string) error {
   250  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
   251  
   252  			clientID := args[0]
   253  			queryClient := types.NewQueryClient(clientCtx)
   254  
   255  			req := &types.QueryClientStatusRequest{
   256  				ClientId: clientID,
   257  			}
   258  
   259  			clientStatusRes, err := queryClient.ClientStatus(cmd.Context(), req)
   260  			if err != nil {
   261  				return err
   262  			}
   263  
   264  			return clientCtx.PrintProto(clientStatusRes)
   265  		},
   266  	}
   267  
   268  	return cmd
   269  }
   270  
   271  func GetCmdQueryConsensusStateHeights(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command {
   272  	cmd := &cobra.Command{
   273  		Use:     "consensus-state-heights [client-id]",
   274  		Short:   "Query the heights of all consensus states of a client.",
   275  		Long:    "Query the heights of all consensus states associated with the provided client ID.",
   276  		Example: fmt.Sprintf("%s query %s %s consensus-state-heights [client-id]", version.ServerName, host.ModuleName, types.SubModuleName),
   277  		Args:    cobra.ExactArgs(1),
   278  		RunE: func(cmd *cobra.Command, args []string) error {
   279  			clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg)
   280  
   281  			clientID := args[0]
   282  
   283  			queryClient := types.NewQueryClient(clientCtx)
   284  
   285  			pageReq, err := client.ReadPageRequest(cmd.Flags())
   286  			if err != nil {
   287  				return err
   288  			}
   289  
   290  			req := &types.QueryConsensusStateHeightsRequest{
   291  				ClientId:   clientID,
   292  				Pagination: pageReq,
   293  			}
   294  
   295  			res, err := queryClient.ConsensusStateHeights(cmd.Context(), req)
   296  			if err != nil {
   297  				return err
   298  			}
   299  
   300  			return clientCtx.PrintProto(res)
   301  		},
   302  	}
   303  	flags.AddQueryFlagsToCmd(cmd)
   304  	flags.AddPaginationFlagsToCmd(cmd, "consensus state heights")
   305  
   306  	return cmd
   307  }