github.com/okex/exchain@v1.8.0/libs/ibc-go/modules/core/04-channel/client/cli/query.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "github.com/okex/exchain/libs/cosmos-sdk/client" 6 "github.com/okex/exchain/libs/cosmos-sdk/client/context" 7 "github.com/okex/exchain/libs/cosmos-sdk/client/flags" 8 "github.com/okex/exchain/libs/cosmos-sdk/codec" 9 interfacetypes "github.com/okex/exchain/libs/cosmos-sdk/codec/types" 10 "github.com/okex/exchain/libs/cosmos-sdk/version" 11 "github.com/okex/exchain/libs/ibc-go/modules/core/04-channel/client/utils" 12 "github.com/okex/exchain/libs/ibc-go/modules/core/04-channel/types" 13 host "github.com/okex/exchain/libs/ibc-go/modules/core/24-host" 14 "github.com/spf13/cobra" 15 "strconv" 16 ) 17 18 const ( 19 flagSequences = "sequences" 20 ) 21 22 // GetCmdQueryChannels defines the command to query all the channels ends 23 // that this chain mantains. 24 func GetCmdQueryChannels(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 25 cmd := &cobra.Command{ 26 Use: "channels", 27 Short: "Query all channels", 28 Long: "Query all channels from a chain", 29 Example: fmt.Sprintf("%s query %s %s channels", version.ServerName, host.ModuleName, types.SubModuleName), 30 Args: cobra.NoArgs, 31 RunE: func(cmd *cobra.Command, _ []string) error { 32 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 33 34 queryClient := types.NewQueryClient(clientCtx) 35 pageReq, err := client.ReadPageRequest(cmd.Flags()) 36 if err != nil { 37 return err 38 } 39 40 req := &types.QueryChannelsRequest{ 41 Pagination: pageReq, 42 } 43 44 res, err := queryClient.Channels(cmd.Context(), req) 45 if err != nil { 46 return err 47 } 48 49 return clientCtx.PrintProto(res) 50 }, 51 } 52 53 flags.AddQueryFlagsToCmd(cmd) 54 flags.AddPaginationFlagsToCmd(cmd, "channels") 55 56 return cmd 57 } 58 59 // GetCmdQueryChannel defines the command to query a channel end 60 func GetCmdQueryChannel(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 61 cmd := &cobra.Command{ 62 Use: "end [port-id] [channel-id]", 63 Short: "Query a channel end", 64 Long: "Query an IBC channel end from a port and channel identifiers", 65 Example: fmt.Sprintf( 66 "%s query %s %s end [port-id] [channel-id]", version.ServerName, host.ModuleName, types.SubModuleName, 67 ), 68 Args: cobra.ExactArgs(2), 69 RunE: func(cmd *cobra.Command, args []string) error { 70 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 71 portID := args[0] 72 channelID := args[1] 73 prove, _ := cmd.Flags().GetBool(flags.FlagProve) 74 75 channelRes, err := utils.QueryChannel(clientCtx, portID, channelID, prove) 76 if err != nil { 77 return err 78 } 79 80 return clientCtx.PrintProto(channelRes) 81 }, 82 } 83 84 cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results") 85 flags.AddQueryFlagsToCmd(cmd) 86 87 return cmd 88 } 89 90 // GetCmdQueryConnectionChannels defines the command to query all the channels associated with a 91 // connection 92 func GetCmdQueryConnectionChannels(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 93 cmd := &cobra.Command{ 94 Use: "connections [connection-id]", 95 Short: "Query all channels associated with a connection", 96 Long: "Query all channels associated with a connection", 97 Example: fmt.Sprintf("%s query %s %s connections [connection-id]", version.ServerName, host.ModuleName, types.SubModuleName), 98 Args: cobra.ExactArgs(1), 99 RunE: func(cmd *cobra.Command, args []string) error { 100 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 101 queryClient := types.NewQueryClient(clientCtx) 102 pageReq, err := client.ReadPageRequest(cmd.Flags()) 103 if err != nil { 104 return err 105 } 106 107 req := &types.QueryConnectionChannelsRequest{ 108 Connection: args[0], 109 Pagination: pageReq, 110 } 111 112 res, err := queryClient.ConnectionChannels(cmd.Context(), req) 113 if err != nil { 114 return err 115 } 116 117 return clientCtx.PrintProto(res) 118 }, 119 } 120 121 flags.AddQueryFlagsToCmd(cmd) 122 flags.AddPaginationFlagsToCmd(cmd, "channels associated with a connection") 123 124 return cmd 125 } 126 127 // GetCmdQueryChannelClientState defines the command to query a client state from a channel 128 func GetCmdQueryChannelClientState(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 129 cmd := &cobra.Command{ 130 Use: "client-state [port-id] [channel-id]", 131 Short: "Query the client state associated with a channel", 132 Long: "Query the client state associated with a channel, by providing its port and channel identifiers.", 133 Example: fmt.Sprintf("%s query ibc channel client-state [port-id] [channel-id]", version.ServerName), 134 Args: cobra.ExactArgs(2), 135 RunE: func(cmd *cobra.Command, args []string) error { 136 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 137 portID := args[0] 138 channelID := args[1] 139 140 res, err := utils.QueryChannelClientState(clientCtx, portID, channelID, false) 141 if err != nil { 142 return err 143 } 144 145 return clientCtx.PrintProto(res.IdentifiedClientState) 146 }, 147 } 148 149 flags.AddQueryFlagsToCmd(cmd) 150 151 return cmd 152 } 153 154 // GetCmdQueryPacketCommitments defines the command to query all packet commitments associated with 155 // a channel 156 func GetCmdQueryPacketCommitments(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 157 cmd := &cobra.Command{ 158 Use: "packet-commitments [port-id] [channel-id]", 159 Short: "Query all packet commitments associated with a channel", 160 Long: "Query all packet commitments associated with a channel", 161 Example: fmt.Sprintf("%s query %s %s packet-commitments [port-id] [channel-id]", version.ServerName, host.ModuleName, types.SubModuleName), 162 Args: cobra.ExactArgs(2), 163 RunE: func(cmd *cobra.Command, args []string) error { 164 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 165 queryClient := types.NewQueryClient(clientCtx) 166 pageReq, err := client.ReadPageRequest(cmd.Flags()) 167 if err != nil { 168 return err 169 } 170 171 req := &types.QueryPacketCommitmentsRequest{ 172 PortId: args[0], 173 ChannelId: args[1], 174 Pagination: pageReq, 175 } 176 177 res, err := queryClient.PacketCommitments(cmd.Context(), req) 178 if err != nil { 179 return err 180 } 181 182 return clientCtx.PrintProto(res) 183 }, 184 } 185 186 flags.AddQueryFlagsToCmd(cmd) 187 flags.AddPaginationFlagsToCmd(cmd, "packet commitments associated with a channel") 188 189 return cmd 190 } 191 192 // GetCmdQueryPacketCommitment defines the command to query a packet commitment 193 func GetCmdQueryPacketCommitment(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 194 cmd := &cobra.Command{ 195 Use: "packet-commitment [port-id] [channel-id] [sequence]", 196 Short: "Query a packet commitment", 197 Long: "Query a packet commitment", 198 Example: fmt.Sprintf( 199 "%s query %s %s packet-commitment [port-id] [channel-id] [sequence]", version.ServerName, host.ModuleName, types.SubModuleName, 200 ), 201 Args: cobra.ExactArgs(3), 202 RunE: func(cmd *cobra.Command, args []string) error { 203 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 204 portID := args[0] 205 channelID := args[1] 206 prove, _ := cmd.Flags().GetBool(flags.FlagProve) 207 208 seq, err := strconv.ParseUint(args[2], 10, 64) 209 if err != nil { 210 return err 211 } 212 213 res, err := utils.QueryPacketCommitment(clientCtx, portID, channelID, seq, prove) 214 if err != nil { 215 return err 216 } 217 218 return clientCtx.PrintProto(res) 219 }, 220 } 221 222 cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results") 223 flags.AddQueryFlagsToCmd(cmd) 224 225 return cmd 226 } 227 228 // GetCmdQueryPacketReceipt defines the command to query a packet receipt 229 func GetCmdQueryPacketReceipt(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 230 cmd := &cobra.Command{ 231 Use: "packet-receipt [port-id] [channel-id] [sequence]", 232 Short: "Query a packet receipt", 233 Long: "Query a packet receipt", 234 Example: fmt.Sprintf( 235 "%s query %s %s packet-receipt [port-id] [channel-id] [sequence]", version.ServerName, host.ModuleName, types.SubModuleName, 236 ), 237 Args: cobra.ExactArgs(3), 238 RunE: func(cmd *cobra.Command, args []string) error { 239 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 240 portID := args[0] 241 channelID := args[1] 242 prove, _ := cmd.Flags().GetBool(flags.FlagProve) 243 244 seq, err := strconv.ParseUint(args[2], 10, 64) 245 if err != nil { 246 return err 247 } 248 249 res, err := utils.QueryPacketReceipt(clientCtx, portID, channelID, seq, prove) 250 if err != nil { 251 return err 252 } 253 254 return clientCtx.PrintProto(res) 255 }, 256 } 257 258 cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results") 259 flags.AddQueryFlagsToCmd(cmd) 260 261 return cmd 262 } 263 264 // GetCmdQueryPacketAcknowledgement defines the command to query a packet acknowledgement 265 func GetCmdQueryPacketAcknowledgement(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 266 cmd := &cobra.Command{ 267 Use: "packet-ack [port-id] [channel-id] [sequence]", 268 Short: "Query a packet acknowledgement", 269 Long: "Query a packet acknowledgement", 270 Example: fmt.Sprintf( 271 "%s query %s %s packet-ack [port-id] [channel-id] [sequence]", version.ServerName, host.ModuleName, types.SubModuleName, 272 ), 273 Args: cobra.ExactArgs(3), 274 RunE: func(cmd *cobra.Command, args []string) error { 275 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 276 portID := args[0] 277 channelID := args[1] 278 prove, _ := cmd.Flags().GetBool(flags.FlagProve) 279 280 seq, err := strconv.ParseUint(args[2], 10, 64) 281 if err != nil { 282 return err 283 } 284 285 res, err := utils.QueryPacketAcknowledgement(clientCtx, portID, channelID, seq, prove) 286 if err != nil { 287 return err 288 } 289 290 return clientCtx.PrintProto(res) 291 }, 292 } 293 294 cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results") 295 flags.AddQueryFlagsToCmd(cmd) 296 297 return cmd 298 } 299 300 // GetCmdQueryUnreceivedPackets defines the command to query all the unreceived 301 // packets on the receiving chain 302 func GetCmdQueryUnreceivedPackets(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 303 cmd := &cobra.Command{ 304 Use: "unreceived-packets [port-id] [channel-id]", 305 Short: "Query all the unreceived packets associated with a channel", 306 Long: `Determine if a packet, given a list of packet commitment sequences, is unreceived. 307 308 The return value represents: 309 - Unreceived packet commitments: no acknowledgement exists on receiving chain for the given packet commitment sequence on sending chain. 310 `, 311 Example: fmt.Sprintf("%s query %s %s unreceived-packets [port-id] [channel-id] --sequences=1,2,3", version.ServerName, host.ModuleName, types.SubModuleName), 312 Args: cobra.ExactArgs(2), 313 RunE: func(cmd *cobra.Command, args []string) error { 314 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 315 queryClient := types.NewQueryClient(clientCtx) 316 317 seqSlice, err := cmd.Flags().GetInt64Slice(flagSequences) 318 if err != nil { 319 return err 320 } 321 322 seqs := make([]uint64, len(seqSlice)) 323 for i := range seqSlice { 324 seqs[i] = uint64(seqSlice[i]) 325 } 326 327 req := &types.QueryUnreceivedPacketsRequest{ 328 PortId: args[0], 329 ChannelId: args[1], 330 PacketCommitmentSequences: seqs, 331 } 332 333 res, err := queryClient.UnreceivedPackets(cmd.Context(), req) 334 if err != nil { 335 return err 336 } 337 338 return clientCtx.PrintProto(res) 339 }, 340 } 341 342 cmd.Flags().Int64Slice(flagSequences, []int64{}, "comma separated list of packet sequence numbers") 343 flags.AddQueryFlagsToCmd(cmd) 344 345 return cmd 346 } 347 348 // GetCmdQueryUnreceivedAcks defines the command to query all the unreceived acks on the original sending chain 349 func GetCmdQueryUnreceivedAcks(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 350 cmd := &cobra.Command{ 351 Use: "unreceived-acks [port-id] [channel-id]", 352 Short: "Query all the unreceived acks associated with a channel", 353 Long: `Given a list of acknowledgement sequences from counterparty, determine if an ack on the counterparty chain has been received on the executing chain. 354 355 The return value represents: 356 - Unreceived packet acknowledgement: packet commitment exists on original sending (executing) chain and ack exists on receiving chain. 357 `, 358 Example: fmt.Sprintf("%s query %s %s unreceived-acks [port-id] [channel-id] --sequences=1,2,3", version.ServerName, host.ModuleName, types.SubModuleName), 359 Args: cobra.ExactArgs(2), 360 RunE: func(cmd *cobra.Command, args []string) error { 361 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 362 queryClient := types.NewQueryClient(clientCtx) 363 364 seqSlice, err := cmd.Flags().GetInt64Slice(flagSequences) 365 if err != nil { 366 return err 367 } 368 369 seqs := make([]uint64, len(seqSlice)) 370 for i := range seqSlice { 371 seqs[i] = uint64(seqSlice[i]) 372 } 373 374 req := &types.QueryUnreceivedAcksRequest{ 375 PortId: args[0], 376 ChannelId: args[1], 377 PacketAckSequences: seqs, 378 } 379 380 res, err := queryClient.UnreceivedAcks(cmd.Context(), req) 381 if err != nil { 382 return err 383 } 384 385 return clientCtx.PrintProto(res) 386 }, 387 } 388 389 cmd.Flags().Int64Slice(flagSequences, []int64{}, "comma separated list of packet sequence numbers") 390 flags.AddQueryFlagsToCmd(cmd) 391 392 return cmd 393 } 394 395 // GetCmdQueryNextSequenceReceive defines the command to query a next receive sequence for a given channel 396 func GetCmdQueryNextSequenceReceive(m *codec.CodecProxy, reg interfacetypes.InterfaceRegistry) *cobra.Command { 397 cmd := &cobra.Command{ 398 Use: "next-sequence-receive [port-id] [channel-id]", 399 Short: "Query a next receive sequence", 400 Long: "Query the next receive sequence for a given channel", 401 Example: fmt.Sprintf( 402 "%s query %s %s next-sequence-receive [port-id] [channel-id]", version.ServerName, host.ModuleName, types.SubModuleName, 403 ), 404 Args: cobra.ExactArgs(2), 405 RunE: func(cmd *cobra.Command, args []string) error { 406 clientCtx := context.NewCLIContext().WithProxy(m).WithInterfaceRegistry(reg) 407 portID := args[0] 408 channelID := args[1] 409 prove, _ := cmd.Flags().GetBool(flags.FlagProve) 410 411 sequenceRes, err := utils.QueryNextSequenceReceive(clientCtx, portID, channelID, prove) 412 if err != nil { 413 return err 414 } 415 416 clientCtx = clientCtx.WithHeight(int64(sequenceRes.ProofHeight.RevisionHeight)) 417 return clientCtx.PrintProto(sequenceRes) 418 }, 419 } 420 421 cmd.Flags().Bool(flags.FlagProve, true, "show proofs for the query results") 422 flags.AddQueryFlagsToCmd(cmd) 423 424 return cmd 425 }