-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
tx.go
1056 lines (913 loc) · 31 KB
/
tx.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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmd
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/avast/retry-go/v4"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
chantypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
"github.com/cosmos/relayer/v2/relayer"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
// transactionCmd returns a parent transaction command handler, where all child
// commands can submit transactions on IBC-connected networks.
func transactionCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "transact",
Aliases: []string{"tx"},
Short: "IBC transaction commands",
Long: strings.TrimSpace(`Commands to create IBC transactions on pre-configured chains.
Most of these commands take a [path] argument. Make sure:
1. Chains are properly configured to relay over by using the 'rly chains list' command
2. Path is properly configured to relay over by using the 'rly paths list' command`,
),
}
cmd.AddCommand(
linkCmd(a),
linkThenStartCmd(a),
relayMsgsCmd(a),
relayMsgCmd(a),
relayAcksCmd(a),
xfersend(a),
lineBreakCommand(),
createClientsCmd(a),
createClientCmd(a),
updateClientsCmd(a),
upgradeClientsCmd(a),
//upgradeChainCmd(),
createConnectionCmd(a),
createChannelCmd(a),
closeChannelCmd(a),
lineBreakCommand(),
//sendCmd(),
)
return cmd
}
// TODO send needs revised still
//func sendCmd() *cobra.Command {
// cmd := &cobra.Command{
// Use: "send [chain-id] [from-key] [to-address] [amount]",
// Short: "send funds to a different address on the same chain",
// Args: cobra.ExactArgs(4),
// Example: strings.TrimSpace(fmt.Sprintf(`
//$ %s tx send testkey cosmos10yft4nc8tacpngwlpyq3u4t88y7qzc9xv0q4y8 10000uatom`,
// appName,
// )),
// RunE: func(cmd *cobra.Command, args []string) error {
// c, err := config.Chains.Get(args[0])
// if err != nil {
// return err
// }
//
// // ensure that keys exist
//
// key, err := c.Keybase.Key(args[1])
// if err != nil {
// return err
// }
//
// to, err := sdk.AccAddressFromBech32(args[2])
// if err != nil {
// return err
// }
//
// amt, err := sdk.ParseCoinsNormalized(args[3])
// if err != nil {
// return err
// }
//
// msg := banktypes.NewMsgSend(key.GetAddress(), to, amt)
// if err := msg.ValidateBasic(); err != nil {
// return err
// }
//
// res, _, err := c.ChainProvider.SendMessage(msg)
// if err != nil {
// return err
// }
//
// return c.Print(res, false, true)
// },
// }
//
// return cmd
//}
func createClientsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "clients [path-name]",
Short: "create a clients between two configured chains with a configured path",
Long: "Creates a working ibc client for chain configured on each end of the" +
" path by querying headers from each chain and then sending the corresponding create-client messages",
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`$ %s transact clients demo-path`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
allowUpdateAfterExpiry, err := cmd.Flags().GetBool(flagUpdateAfterExpiry)
if err != nil {
return err
}
allowUpdateAfterMisbehaviour, err := cmd.Flags().GetBool(flagUpdateAfterMisbehaviour)
if err != nil {
return err
}
override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
modified, err := c[src].CreateClients(cmd.Context(), c[dst], allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override)
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
return err
},
}
return overrideFlag(a.Viper, clientParameterFlags(a.Viper, cmd))
}
func createClientCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "client [src-chain-id] [dst-chain-id] [path-name]",
Short: "create a client between two configured chains with a configured path",
Long: "Creates a working ibc client for chain configured on each end of the" +
" path by querying headers from each chain and then sending the corresponding create-client messages",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`$ %s transact client demo-path`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
allowUpdateAfterExpiry, err := cmd.Flags().GetBool(flagUpdateAfterExpiry)
if err != nil {
return err
}
allowUpdateAfterMisbehaviour, err := cmd.Flags().GetBool(flagUpdateAfterMisbehaviour)
if err != nil {
return err
}
override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}
src := args[0]
dst := args[1]
c, err := a.Config.Chains.Gets(src, dst)
if err != nil {
return err
}
pathName := args[2]
path, err := a.Config.Paths.Get(pathName)
if err != nil {
return err
}
c[src].PathEnd = path.End(c[src].ChainID())
c[dst].PathEnd = path.End(c[dst].ChainID())
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
// Query the latest heights on src and dst and retry if the query fails
var srch, dsth int64
if err = retry.Do(func() error {
srch, dsth, err = relayer.QueryLatestHeights(cmd.Context(), c[src], c[dst])
if srch == 0 || dsth == 0 || err != nil {
return fmt.Errorf("failed to query latest heights: %w", err)
}
return err
}, retry.Context(cmd.Context()), relayer.RtyAtt, relayer.RtyDel, relayer.RtyErr); err != nil {
return err
}
// Query the light signed headers for src & dst at the heights srch & dsth, retry if the query fails
var srcUpdateHeader, dstUpdateHeader exported.Header
if err = retry.Do(func() error {
srcUpdateHeader, dstUpdateHeader, err = relayer.GetLightSignedHeadersAtHeights(cmd.Context(), c[src], c[dst], srch, dsth)
if err != nil {
return fmt.Errorf("failed to query light signed headers: %w", err)
}
return err
}, retry.Context(cmd.Context()), relayer.RtyAtt, relayer.RtyDel, relayer.RtyErr, retry.OnRetry(func(n uint, err error) {
a.Log.Debug(
"Failed to get light signed header",
zap.String("src_chain_id", c[src].ChainID()),
zap.Int64("src_height", srch),
zap.String("dst_chain_id", c[dst].ChainID()),
zap.Int64("dst_height", dsth),
zap.Uint("attempt", n+1),
zap.Uint("max_attempts", relayer.RtyAttNum),
zap.Error(err),
)
srch, dsth, _ = relayer.QueryLatestHeights(cmd.Context(), c[src], c[dst])
})); err != nil {
return err
}
modified, err := relayer.CreateClient(cmd.Context(), c[src], c[dst], srcUpdateHeader, dstUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override)
if modified {
if err = a.OverwriteConfig(a.Config); err != nil {
return err
}
}
return nil
},
}
return overrideFlag(a.Viper, clientParameterFlags(a.Viper, cmd))
}
func updateClientsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "update-clients [path-name]",
Short: "update IBC clients between two configured chains with a configured path",
Long: `Updates IBC client for chain configured on each end of the supplied path.
Clients are updated by querying headers from each chain and then sending the
corresponding update-client messages.`,
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`$ %s transact update-clients demo-path`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
return c[src].UpdateClients(cmd.Context(), c[dst])
},
}
return cmd
}
func upgradeClientsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade-clients [path-name] [chain-id]",
Short: "upgrades IBC clients between two configured chains with a configured path and chain-id",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
height, err := cmd.Flags().GetInt64(flags.FlagHeight)
if err != nil {
return err
}
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
targetChainID := args[1]
// send the upgrade message on the targetChainID
if src == targetChainID {
return c[src].UpgradeClients(cmd.Context(), c[dst], height)
}
return c[dst].UpgradeClients(cmd.Context(), c[src], height)
},
}
return heightFlag(a.Viper, cmd)
}
func createConnectionCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "connection [path-name]",
Aliases: []string{"conn"},
Short: "create a connection between two configured chains with a configured path",
Long: strings.TrimSpace(`Create or repair a connection between two IBC-connected networks
along a specific path.`,
),
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact connection demo-path
$ %s tx conn demo-path --timeout 5s`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
allowUpdateAfterExpiry, err := cmd.Flags().GetBool(flagUpdateAfterExpiry)
if err != nil {
return err
}
allowUpdateAfterMisbehaviour, err := cmd.Flags().GetBool(flagUpdateAfterMisbehaviour)
if err != nil {
return err
}
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
to, err := getTimeout(cmd)
if err != nil {
return err
}
retries, err := cmd.Flags().GetUint64(flagMaxRetries)
if err != nil {
return err
}
override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
// ensure that the clients exist
modified, err := c[src].CreateClients(cmd.Context(), c[dst], allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override)
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
if err != nil {
return err
}
modified, err = c[src].CreateOpenConnections(cmd.Context(), c[dst], retries, to)
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
return err
},
}
return overrideFlag(a.Viper, clientParameterFlags(a.Viper, retryFlag(a.Viper, timeoutFlag(a.Viper, cmd))))
}
func createChannelCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "channel [path-name]",
Aliases: []string{"chan"},
Short: "create a channel between two configured chains with a configured path using specified or default channel identifiers",
Long: strings.TrimSpace(`Create or repair a channel between two IBC-connected networks
along a specific path.`,
),
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact channel demo-path --src-port transfer --dst-port transfer --order unordered --version ics20-1
$ %s tx chan demo-path --timeout 5s --max-retries 10`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}
srcPort, err := cmd.Flags().GetString(flagSrcPort)
if err != nil {
return err
}
dstPort, err := cmd.Flags().GetString(flagDstPort)
if err != nil {
return err
}
order, err := cmd.Flags().GetString(flagOrder)
if err != nil {
return err
}
version, err := cmd.Flags().GetString(flagVersion)
if err != nil {
return err
}
to, err := getTimeout(cmd)
if err != nil {
return err
}
retries, err := cmd.Flags().GetUint64(flagMaxRetries)
if err != nil {
return err
}
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
// create channel if it isn't already created
modified, err := c[src].CreateOpenChannels(cmd.Context(), c[dst], retries, to, srcPort, dstPort, order, version, override)
if err != nil {
return fmt.Errorf("error creating channels: %w", err)
}
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
return nil
},
}
return channelParameterFlags(a.Viper, overrideFlag(a.Viper, retryFlag(a.Viper, timeoutFlag(a.Viper, cmd))))
}
func closeChannelCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "channel-close [path-name] [src-channel-id] [src-port-id]",
Short: "close a channel between two configured chains with a configured path",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact channel-close demo-path channel-0 transfer
$ %s tx channel-close demo-path channel-0 transfer --timeout 5s
$ %s tx channel-close demo-path channel-0 transfer
$ %s tx channel-close demo-path channel-0 transfer -o 3s`,
appName, appName, appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
to, err := getTimeout(cmd)
if err != nil {
return err
}
channelID := args[1]
portID := args[2]
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
srch, err := c[src].ChainProvider.QueryLatestHeight(cmd.Context())
if err != nil {
return err
}
channel, err := c[src].ChainProvider.QueryChannel(cmd.Context(), srch, channelID, portID)
if err != nil {
return err
}
return c[src].CloseChannel(cmd.Context(), c[dst], to, channelID, portID, channel)
},
}
return timeoutFlag(a.Viper, cmd)
}
func linkCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "link [path-name]",
Aliases: []string{"connect"},
Short: "create clients, connection, and channel between two configured chains with a configured path",
Long: strings.TrimSpace(`Create an IBC client between two IBC-enabled networks, in addition
to creating a connection and a channel between the two networks on a configured path.`,
),
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact link demo-path --src-port transfer --dst-port transfer
$ %s tx link demo-path
$ %s tx connect demo-path --src-port transfer --dst-port transfer --order unordered --version ics20-1`,
appName, appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
allowUpdateAfterExpiry, err := cmd.Flags().GetBool(flagUpdateAfterExpiry)
if err != nil {
return err
}
allowUpdateAfterMisbehaviour, err := cmd.Flags().GetBool(flagUpdateAfterMisbehaviour)
if err != nil {
return err
}
pth, err := a.Config.Paths.Get(args[0])
if err != nil {
return err
}
src, dst := pth.Src.ChainID, pth.Dst.ChainID
c, err := a.Config.Chains.Gets(src, dst)
if err != nil {
return err
}
c[src].PathEnd = pth.Src
c[dst].PathEnd = pth.Dst
srcPort, err := cmd.Flags().GetString(flagSrcPort)
if err != nil {
return err
}
dstPort, err := cmd.Flags().GetString(flagDstPort)
if err != nil {
return err
}
order, err := cmd.Flags().GetString(flagOrder)
if err != nil {
return err
}
version, err := cmd.Flags().GetString(flagVersion)
if err != nil {
return err
}
to, err := getTimeout(cmd)
if err != nil {
return err
}
retries, err := cmd.Flags().GetUint64(flagMaxRetries)
if err != nil {
return err
}
override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}
// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on src chain %s", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on dst chain %s", c[dst].ChainProvider.Key(), c[dst].ChainID())
}
// create clients if they aren't already created
modified, err := c[src].CreateClients(cmd.Context(), c[dst], allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override)
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
if err != nil {
return fmt.Errorf("error creating clients: %w", err)
}
// create connection if it isn't already created
modified, err = c[src].CreateOpenConnections(cmd.Context(), c[dst], retries, to)
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
if err != nil {
return fmt.Errorf("error creating connections: %w", err)
}
// create channel if it isn't already created
modified, err = c[src].CreateOpenChannels(cmd.Context(), c[dst], retries, to, srcPort, dstPort, order, version, override)
if modified {
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
if err != nil {
return fmt.Errorf("error creating channels: %w", err)
}
return nil
},
}
return overrideFlag(a.Viper, channelParameterFlags(a.Viper, clientParameterFlags(a.Viper, retryFlag(a.Viper, timeoutFlag(a.Viper, cmd)))))
}
func linkThenStartCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "link-then-start [path-name]",
Aliases: []string{"connect-then-start"},
Short: "a shorthand command to execute 'link' followed by 'start'",
Long: strings.TrimSpace(`Create IBC clients, connection, and channel between two configured IBC
networks with a configured path and then start the relayer on that path.`,
),
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact link-then-start demo-path
$ %s tx link-then-start demo-path --timeout 5s`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
lCmd := linkCmd(a)
for err := lCmd.RunE(cmd, args); err != nil; err = lCmd.RunE(cmd, args) {
fmt.Fprintf(cmd.ErrOrStderr(), "retrying link: %s\n", err)
time.Sleep(1 * time.Second)
}
sCmd := startCmd(a)
return sCmd.RunE(cmd, args)
},
}
return overrideFlag(a.Viper, channelParameterFlags(a.Viper, clientParameterFlags(a.Viper, strategyFlag(a.Viper, retryFlag(a.Viper, timeoutFlag(a.Viper, cmd))))))
}
func relayMsgCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "relay-packet [path-name] [src-channel-id] [seq-num]",
Aliases: []string{"relay-pkt"},
Short: "relay a non-relayed packet with a specific sequence number, in both directions",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact relay-packet demo-path channel-1 1
$ %s tx relay-pkt demo-path channel-1 1`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
if err = ensureKeysExist(c); err != nil {
return err
}
maxTxSize, maxMsgLength, err := GetStartOptions(cmd)
if err != nil {
return err
}
seqNum, err := strconv.Atoi(args[2])
if err != nil {
return err
}
channelID := args[1]
channel, err := relayer.QueryChannel(cmd.Context(), c[src], channelID)
if err != nil {
return err
}
sp, err := relayer.UnrelayedSequences(cmd.Context(), c[src], c[dst], channel)
if err != nil {
return err
}
return relayer.RelayPacket(cmd.Context(), c[src], c[dst], sp, maxTxSize, maxMsgLength, uint64(seqNum), channel)
},
}
return strategyFlag(a.Viper, cmd)
}
func relayMsgsCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "relay-packets [path-name] [src-channel-id]",
Aliases: []string{"relay-pkts"},
Short: "relay any remaining non-relayed packets on a given path, in both directions",
Args: cobra.ExactArgs(2),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact relay-packets demo-path channel-0
$ %s tx relay-pkts demo-path channel-0`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
if err = ensureKeysExist(c); err != nil {
return err
}
maxTxSize, maxMsgLength, err := GetStartOptions(cmd)
if err != nil {
return err
}
channelID := args[1]
channel, err := relayer.QueryChannel(cmd.Context(), c[src], channelID)
if err != nil {
return err
}
sp, err := relayer.UnrelayedSequences(cmd.Context(), c[src], c[dst], channel)
if err != nil {
return err
}
if err = relayer.RelayPackets(cmd.Context(), c[src], c[dst], sp, maxTxSize, maxMsgLength, channel); err != nil {
return err
}
return nil
},
}
return strategyFlag(a.Viper, cmd)
}
func relayAcksCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "relay-acknowledgements [path-name] [src-channel-id]",
Aliases: []string{"relay-acks"},
Short: "relay any remaining non-relayed acknowledgements on a given path, in both directions",
Args: cobra.ExactArgs(2),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact relay-acknowledgements demo-path channel-0
$ %s tx relay-acks demo-path channel-0 -l 3 -s 6`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := a.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
if err = ensureKeysExist(c); err != nil {
return err
}
maxTxSize, maxMsgLength, err := GetStartOptions(cmd)
if err != nil {
return err
}
channelID := args[1]
channel, err := relayer.QueryChannel(cmd.Context(), c[src], channelID)
if err != nil {
return err
}
// sp.Src contains all sequences acked on SRC but acknowledgement not processed on DST
// sp.Dst contains all sequences acked on DST but acknowledgement not processed on SRC
sp, err := relayer.UnrelayedAcknowledgements(cmd.Context(), c[src], c[dst], channel)
if err != nil {
return err
}
if err = relayer.RelayAcknowledgements(cmd.Context(), c[src], c[dst], sp, maxTxSize, maxMsgLength, channel); err != nil {
return err
}
return nil
},
}
return strategyFlag(a.Viper, cmd)
}
// TODO still needs a revisit
//func upgradeChainCmd() *cobra.Command {
// cmd := &cobra.Command{
// Use: "upgrade-chain [path-name] [chain-id] [new-unbonding-period] [deposit] [path/to/upgradePlan.json]",
// Short: "upgrade an IBC-enabled network with a given upgrade plan",
// Long: strings.TrimSpace(`Upgrade an IBC-enabled network by providing the chain-id of the
//network being upgraded, the new unbonding period, the proposal deposit and the JSON file of the
//upgrade plan without the upgrade client state.`,
// ),
// Args: cobra.ExactArgs(5),
// RunE: func(cmd *cobra.Command, args []string) error {
// c, src, dst, err := config.ChainsFromPath(args[0])
// if err != nil {
// return err
// }
//
// targetChainID := args[1]
//
// unbondingPeriod, err := time.ParseDuration(args[2])
// if err != nil {
// return err
// }
//
// // ensure that keys exist
// if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
// return fmt.Errorf("key %s not found on chain %s \n", c[src].ChainProvider.Key(), c[src].ChainID())
// }
// if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
// return fmt.Errorf("key %s not found on chain %s \n", c[dst].ChainProvider.Key(), c[dst].ChainID())
// }
//
// // parse deposit
// deposit, err := sdk.ParseCoinNormalized(args[3])
// if err != nil {
// return err
// }
//
// // parse plan
// plan := &upgradetypes.Plan{}
// path := args[4]
// if _, err := os.Stat(path); err != nil {
// return err
// }
//
// byt, err := os.ReadFile(path)
// if err != nil {
// return err
// }
//
// if err = json.Unmarshal(byt, plan); err != nil {
// return err
// }
//
// // send the upgrade message on the targetChainID
// if src == targetChainID {
// return c[src].UpgradeChain(c[dst], plan, deposit, unbondingPeriod)
// }
//
// return c[dst].UpgradeChain(c[src], plan, deposit, unbondingPeriod)
// },
// }
//
// return cmd
//}
func xfersend(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "transfer [src-chain-id] [dst-chain-id] [amount] [dst-addr] [src-channel-id]",
Short: "initiate a transfer from one network to another",
Long: `Initiate a token transfer via IBC between two networks. The created packet
must be relayed to the destination chain.`,
Args: cobra.ExactArgs(5),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s tx transfer ibc-0 ibc-1 100000stake cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk channel-0 --path demo-path
$ %s tx transfer ibc-0 ibc-1 100000stake cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk channel-0 --path demo -y 2 -c 10
$ %s tx transfer ibc-0 ibc-1 100000stake raw:non-bech32-address channel-0 --path demo
$ %s tx raw send ibc-0 ibc-1 100000stake cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk channel-0 --path demo -c 5
`, appName, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
src, dst := args[0], args[1]
c, err := a.Config.Chains.Gets(src, dst)
if err != nil {
return err
}
pathString, err := cmd.Flags().GetString(flagPath)
if err != nil {
return err
}
var path *relayer.Path
if path, err = setPathsFromArgs(a, c[src], c[dst], pathString); err != nil {
return err
}
amount, err := sdk.ParseCoinNormalized(args[2])
if err != nil {
return err
}
srch, err := c[src].ChainProvider.QueryLatestHeight(cmd.Context())
if err != nil {
return err
}
// Query all channels for the configured connection on the src chain
srcChannelID := args[4]
channels, err := c[src].ChainProvider.QueryConnectionChannels(cmd.Context(), srch, path.Src.ConnectionID)
if err != nil {
return err
}
// Ensure the specified channel exists for the given path
var srcChannel *chantypes.IdentifiedChannel
for _, channel := range channels {
if channel.ChannelId == srcChannelID {
srcChannel = channel
break
}
}
if srcChannel == nil {
return fmt.Errorf("could not find channel{%s} for chain{%s}@connection{%s}",
srcChannelID, c[src], path.Src.ConnectionID)
}
dts, err := c[src].ChainProvider.QueryDenomTraces(cmd.Context(), 0, 100, srch)
if err != nil {
return err
}
for _, d := range dts {
if amount.Denom == d.GetFullDenomPath() {
amount = sdk.NewCoin(d.IBCDenom(), amount.Amount)
}
}
toHeightOffset, err := cmd.Flags().GetUint64(flagTimeoutHeightOffset)
if err != nil {
return err
}
toTimeOffset, err := cmd.Flags().GetDuration(flagTimeoutTimeOffset)
if err != nil {
return err
}
// TODO this needs to be rewritten?
// If the argument begins with "raw:" then use the suffix directly.
rawDstAddr := strings.TrimPrefix(args[3], "raw:")
var dstAddr string
if rawDstAddr == args[3] {
// not "raw:", so we treat the dstAddr as bech32
//done := c[dst].UseSDKContext()
dst, err := sdk.AccAddressFromBech32(args[3])
if err != nil {
return err
}
dstAddr = dst.String()
//done()
} else {
// Don't parse the rest of the dstAddr... it's raw.
dstAddr = rawDstAddr
}
return c[src].SendTransferMsg(cmd.Context(), c[dst], amount, dstAddr, toHeightOffset, toTimeOffset, srcChannel)
},