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