-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathpaths.go
668 lines (598 loc) · 17.5 KB
/
paths.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
conntypes "github.com/cosmos/cosmos-sdk/x/ibc/core/03-connection/types"
chantypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
"github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
tmclient "github.com/cosmos/cosmos-sdk/x/ibc/light-clients/07-tendermint/types"
"github.com/cosmos/relayer/relayer"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
)
func pathsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "paths",
Aliases: []string{"pth"},
Short: "manage path configurations",
Long: `
A path represents the "full path" or "link" for communication between two chains. This includes the client,
connection, and channel ids from both the source and destination chains as well as the strategy to use when relaying`,
}
cmd.AddCommand(
pathsListCmd(),
pathsShowCmd(),
pathsAddCmd(),
pathsGenCmd(),
pathsDeleteCmd(),
)
return cmd
}
func pathsGenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate [src-chain-id] [dst-chain-id] [name]",
Aliases: []string{"gen"},
Short: "generate identifiers for a new path between src and dst, reusing any that exist",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths generate ibc-0 ibc-1 demo-path --force
$ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) (err error) {
var (
src, dst, pth = args[0], args[1], args[2]
c map[string]*relayer.Chain
eg errgroup.Group
srcClients, dstClients *clienttypes.QueryClientStatesResponse
srcConns, dstConns *conntypes.QueryConnectionsResponse
srcCon, dstCon *conntypes.IdentifiedConnection
srcChans, dstChans *chantypes.QueryChannelsResponse
srcChan, dstChan *chantypes.IdentifiedChannel
)
if c, err = config.Chains.Gets(src, dst); err != nil {
return fmt.Errorf("chains need to be configured before paths to them can be added: %w", err)
}
version, _ := cmd.Flags().GetString(flagVersion)
strategy, _ := cmd.Flags().GetString(flagStrategy)
port, _ := cmd.Flags().GetString(flagPort)
path := &relayer.Path{
Src: &relayer.PathEnd{ChainID: src, PortID: port, Version: version},
Dst: &relayer.PathEnd{ChainID: dst, PortID: port, Version: version},
Strategy: &relayer.StrategyCfg{Type: strategy},
}
// get desired order of the channel
if unordered, _ := cmd.Flags().GetBool(flagOrder); unordered {
path.Src.Order = UNORDERED
path.Dst.Order = UNORDERED
} else {
path.Src.Order = ORDERED
path.Dst.Order = ORDERED
}
// if -f is passed, generate a random path between the two chains
if force, _ := cmd.Flags().GetBool(flagForce); force {
path.GenSrcClientID()
path.GenDstClientID()
path.GenSrcConnID()
path.GenDstConnID()
path.GenSrcChanID()
path.GenDstChanID()
// validate it...
if err = config.Paths.AddForce(pth, path); err != nil {
return err
}
logPathGen(pth)
// ...then add it to the config file
return overWriteConfig(cmd, config)
}
// see if there are existing clients that can be reused
eg.Go(func() error {
srcClients, err = c[src].QueryClients(0, 1000)
return err
})
eg.Go(func() error {
dstClients, err = c[dst].QueryClients(0, 1000)
return err
})
if err := eg.Wait(); err != nil {
return err
}
for _, idCs := range srcClients.ClientStates {
var clnt exported.ClientState
if err = idCs.UnpackInterfaces(c[src].Encoding.Marshaler); err != nil {
return err
}
if clnt, err = clienttypes.UnpackClientState(idCs.ClientState); err != nil {
return err
}
switch cs := clnt.(type) {
case *tmclient.ClientState:
// if the client is an active tendermint client for the counterparty chain then we reuse it
if cs.ChainId == c[dst].ChainID && !cs.IsFrozen() {
path.Src.ClientID = idCs.ClientId
}
default:
}
}
for _, idCs := range dstClients.ClientStates {
var clnt exported.ClientState
if err = idCs.UnpackInterfaces(c[dst].Encoding.Marshaler); err != nil {
return err
}
if clnt, err = clienttypes.UnpackClientState(idCs.ClientState); err != nil {
return err
}
switch cs := clnt.(type) {
case *tmclient.ClientState:
// if the client is an active tendermint client for the counterparty chain then we reuse it
if cs.ChainId == c[src].ChainID && !cs.IsFrozen() {
path.Dst.ClientID = idCs.ClientId
}
default:
}
}
switch {
// If there aren't any matching clients between chains, generate
case path.Src.ClientID == "" && path.Dst.ClientID == "":
path.GenSrcClientID()
path.GenDstClientID()
path.GenSrcConnID()
path.GenSrcConnID()
path.GenSrcChanID()
path.GenDstChanID()
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
case path.Src.ClientID == "" && path.Dst.ClientID != "":
path.GenSrcClientID()
path.GenSrcConnID()
path.GenSrcConnID()
path.GenSrcChanID()
path.GenDstChanID()
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
case path.Dst.ClientID == "" && path.Src.ClientID != "":
path.GenDstClientID()
path.GenSrcConnID()
path.GenSrcConnID()
path.GenSrcChanID()
path.GenDstChanID()
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
}
// see if there are existing connections that can be reused
eg.Go(func() error {
srcConns, err = c[src].QueryConnections(0, 1000)
return err
})
eg.Go(func() error {
dstConns, err = c[dst].QueryConnections(0, 1000)
return err
})
if err = eg.Wait(); err != nil {
return err
}
// find connections with the appropriate client id
for _, c := range srcConns.Connections {
if c.ClientId == path.Src.ClientID {
srcCon = c
path.Src.ConnectionID = c.Id
}
}
for _, c := range dstConns.Connections {
if c.ClientId == path.Dst.ClientID {
dstCon = c
path.Dst.ConnectionID = c.Id
}
}
switch {
case path.Src.ConnectionID != "" && path.Dst.ConnectionID != "":
// If we have identified a connection, make sure that each end is the
// other's counterparty and that the connection is open. In the failure case
// we should generate a new connection identifier
dstCpForSrc := srcCon.Counterparty.ConnectionId == dstCon.Id
srcCpForDst := dstCon.Counterparty.ConnectionId == srcCon.Id
srcOpen := srcCon.State == conntypes.OPEN
dstOpen := dstCon.State == conntypes.OPEN
if !(dstCpForSrc && srcCpForDst && srcOpen && dstOpen) {
path.GenSrcConnID()
path.GenDstConnID()
path.GenSrcChanID()
path.GenDstChanID()
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
}
default:
path.GenSrcConnID()
path.GenDstConnID()
path.GenSrcChanID()
path.GenDstChanID()
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
}
eg.Go(func() error {
srcChans, err = c[src].QueryChannels(0, 1000)
return err
})
eg.Go(func() error {
dstChans, err = c[dst].QueryChannels(0, 1000)
return err
})
if err = eg.Wait(); err != nil {
return err
}
for _, c := range srcChans.Channels {
if c.ConnectionHops[0] == path.Src.ConnectionID {
srcChan = c
path.Src.ChannelID = c.ChannelId
}
}
for _, c := range dstChans.Channels {
if c.ConnectionHops[0] == path.Dst.ConnectionID {
dstChan = c
path.Dst.ChannelID = c.ChannelId
}
}
switch {
case path.Src.ChannelID != "" && path.Dst.ChannelID != "":
// If we have identified a channel, make sure that each end is the
// other's counterparty and that the channel is open. In the failure case
// we should generate a new channel identifier
dstCpForSrc := srcChan.Counterparty.ChannelId == dstChan.ChannelId
srcCpForDst := dstChan.Counterparty.ChannelId == srcChan.ChannelId
srcOpen := srcChan.State == chantypes.OPEN
dstOpen := dstChan.State == chantypes.OPEN
srcPort := srcChan.PortId == path.Src.PortID
dstPort := dstChan.PortId == path.Dst.PortID
srcOrder := srcChan.Ordering == path.Src.GetOrder()
dstOrder := dstChan.Ordering == path.Dst.GetOrder()
srcVersion := srcChan.Version == path.Src.Version
dstVersion := dstChan.Version == path.Dst.Version
if !(dstCpForSrc && srcCpForDst && srcOpen && dstOpen && srcPort && dstPort &&
srcOrder && dstOrder && srcVersion && dstVersion) {
path.GenSrcChanID()
path.GenDstChanID()
}
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
default:
path.GenSrcChanID()
path.GenDstChanID()
if err = config.ValidatePath(path); err != nil {
return err
}
if err = config.Paths.Add(pth, path); err != nil {
return err
}
logPathGen(pth)
return overWriteConfig(cmd, config)
}
},
}
return forceFlag(orderFlag(versionFlag(pathStrategy(portFlag(cmd)))))
}
func logPathGen(pth string) {
fmt.Printf("Generated path(%s), run 'rly paths show %s --yaml' to see details\n", pth, pth)
}
func pathsDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [index]",
Aliases: []string{"d"},
Short: "delete a path with a given index",
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths delete demo-path
$ %s pth d path-name`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := config.Paths.Get(args[0]); err != nil {
return err
}
cfg := config
delete(cfg.Paths, args[0])
return overWriteConfig(cmd, cfg)
},
}
return cmd
}
func pathsListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "print out configured paths",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths list --yaml
$ %s paths list --json
$ %s pth l`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
jsn, _ := cmd.Flags().GetBool(flagJSON)
yml, _ := cmd.Flags().GetBool(flagYAML)
switch {
case yml && jsn:
return fmt.Errorf("can't pass both --json and --yaml, must pick one")
case yml:
out, err := yaml.Marshal(config.Paths)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
case jsn:
out, err := json.Marshal(config.Paths)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
default:
i := 0
for k, pth := range config.Paths {
chains, err := config.Chains.Gets(pth.Src.ChainID, pth.Dst.ChainID)
if err != nil {
return err
}
stat := pth.QueryPathStatus(chains[pth.Src.ChainID], chains[pth.Dst.ChainID]).Status
printPath(i, k, pth, checkmark(stat.Chains), checkmark(stat.Clients),
checkmark(stat.Connection), checkmark(stat.Channel))
i++
}
return nil
}
},
}
return yamlFlag(jsonFlag(cmd))
}
func printPath(i int, k string, pth *relayer.Path, chains, clients, connection, channel string) {
fmt.Printf("%2d: %-20s -> chns(%s) clnts(%s) conn(%s) chan(%s) (%s:%s<>%s:%s)\n",
i, k, chains, clients, connection, channel, pth.Src.ChainID, pth.Src.PortID, pth.Dst.ChainID, pth.Dst.PortID)
}
func checkmark(status bool) string {
if status {
return check
}
return xIcon
}
func pathsShowCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show [path-name]",
Aliases: []string{"s"},
Short: "show a path given its name",
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths show demo-path --yaml
$ %s paths show demo-path --json
$ %s pth s path-name`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
path, err := config.Paths.Get(args[0])
if err != nil {
return err
}
chains, err := config.Chains.Gets(path.Src.ChainID, path.Dst.ChainID)
if err != nil {
return err
}
jsn, _ := cmd.Flags().GetBool(flagJSON)
yml, _ := cmd.Flags().GetBool(flagYAML)
pathWithStatus := path.QueryPathStatus(chains[path.Src.ChainID], chains[path.Dst.ChainID])
switch {
case yml && jsn:
return fmt.Errorf("can't pass both --json and --yaml, must pick one")
case yml:
out, err := yaml.Marshal(pathWithStatus)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
case jsn:
out, err := json.Marshal(pathWithStatus)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
default:
fmt.Println(pathWithStatus.PrintString(args[0]))
}
return nil
},
}
return yamlFlag(jsonFlag(cmd))
}
func pathsAddCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add [src-chain-id] [dst-chain-id] [path-name]",
Aliases: []string{"a"},
Short: "add a path to the list of paths",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths add ibc-0 ibc-1 demo-path
$ %s paths add ibc-0 ibc-1 demo-path --file paths/demo.json
$ %s pth a ibc-0 ibc-1 demo-path`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
src, dst := args[0], args[1]
_, err := config.Chains.Gets(src, dst)
if err != nil {
return fmt.Errorf("chains need to be configured before paths to them can be added: %w", err)
}
var out *Config
file, err := cmd.Flags().GetString(flagFile)
if err != nil {
return err
}
if file != "" {
if out, err = fileInputPathAdd(file, args[2]); err != nil {
return err
}
} else {
if out, err = userInputPathAdd(src, dst, args[2]); err != nil {
return err
}
}
return overWriteConfig(cmd, out)
},
}
return fileFlag(cmd)
}
func fileInputPathAdd(file, name string) (cfg *Config, err error) {
// If the user passes in a file, attempt to read the chain config from that file
p := &relayer.Path{}
if _, err := os.Stat(file); err != nil {
return nil, err
}
byt, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if err = json.Unmarshal(byt, &p); err != nil {
return nil, err
}
if err = config.ValidatePath(p); err != nil {
return nil, err
}
if err = config.Paths.Add(name, p); err != nil {
return nil, err
}
return config, nil
}
func userInputPathAdd(src, dst, name string) (*Config, error) {
var (
value string
err error
path = &relayer.Path{
Strategy: relayer.NewNaiveStrategy(),
Src: &relayer.PathEnd{
ChainID: src,
Order: "ORDERED",
},
Dst: &relayer.PathEnd{
ChainID: dst,
Order: "ORDERED",
},
}
)
fmt.Printf("enter src(%s) client-id...\n", src)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Src.ClientID = value
if err = path.Src.Vclient(); err != nil {
return nil, err
}
fmt.Printf("enter src(%s) connection-id...\n", src)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Src.ConnectionID = value
if err = path.Src.Vconn(); err != nil {
return nil, err
}
fmt.Printf("enter src(%s) channel-id...\n", src)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Src.ChannelID = value
if err = path.Src.Vchan(); err != nil {
return nil, err
}
fmt.Printf("enter src(%s) port-id...\n", src)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Src.PortID = value
if err = path.Src.Vport(); err != nil {
return nil, err
}
fmt.Printf("enter src(%s) version...\n", src)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Src.Version = value
if err = path.Src.Vversion(); err != nil {
return nil, err
}
fmt.Printf("enter dst(%s) client-id...\n", dst)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Dst.ClientID = value
if err = path.Dst.Vclient(); err != nil {
return nil, err
}
fmt.Printf("enter dst(%s) connection-id...\n", dst)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Dst.ConnectionID = value
if err = path.Dst.Vconn(); err != nil {
return nil, err
}
fmt.Printf("enter dst(%s) channel-id...\n", dst)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Dst.ChannelID = value
if err = path.Dst.Vchan(); err != nil {
return nil, err
}
fmt.Printf("enter dst(%s) port-id...\n", dst)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Dst.PortID = value
if err = path.Dst.Vport(); err != nil {
return nil, err
}
fmt.Printf("enter dst(%s) version...\n", dst)
if value, err = readStdin(); err != nil {
return nil, err
}
path.Dst.Version = value
if err = path.Dst.Vversion(); err != nil {
return nil, err
}
if err = config.ValidatePath(path); err != nil {
return nil, err
}
if err = config.Paths.Add(name, path); err != nil {
return nil, err
}
return config, nil
}