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