-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtx.go
628 lines (546 loc) · 17.6 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
package cmd
import (
"errors"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// transactionCmd represents the tx command
func transactionCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "tx",
Short: "IBC Transaction Commands",
Long: strings.TrimSpace(`Commands to create IBC transactions on 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`),
RunE: noCommand,
}
cmd.AddCommand(
xfersend(ctx),
relayMsgsCmd(ctx),
relayAcksCmd(ctx),
flags.LineBreak,
createClientsCmd(ctx),
updateClientsCmd(ctx),
createConnectionCmd(ctx),
createChannelCmd(ctx),
channelUpgradeCmd(ctx),
)
return cmd
}
func createClientsCmd(ctx *config.Context) *cobra.Command {
const (
flagSrcHeight = "src-height"
flagDstHeight = "dst-height"
)
const (
defaultSrcHeight = 0
defaultDstHeight = 0
)
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),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
// ensure that keys exist
if _, err = c[src].GetAddress(); err != nil {
return err
}
if _, err = c[dst].GetAddress(); err != nil {
return err
}
// if the option "src-height" is not set or is set zero, the latest finalized height is used.
var srcHeight exported.Height
if height, err := cmd.Flags().GetUint64(flagSrcHeight); err != nil {
return err
} else if height == 0 {
srcHeight = nil
} else if latestHeight, err := c[src].LatestHeight(cmd.Context()); err != nil {
return fmt.Errorf("failed to get the latest height of src chain: %v", err)
} else {
srcHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
}
// if the option "dst-height" is not set or is set zero, the latest finalized height is used.
var dstHeight exported.Height
if height, err := cmd.Flags().GetUint64(flagDstHeight); err != nil {
return err
} else if height == 0 {
dstHeight = nil
} else if latestHeight, err := c[dst].LatestHeight(cmd.Context()); err != nil {
return fmt.Errorf("failed to get the latest height of dst chain: %v", err)
} else {
dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
}
return core.CreateClients(cmd.Context(), pathName, c[src], c[dst], srcHeight, dstHeight)
},
}
cmd.Flags().Uint64(flagSrcHeight, defaultSrcHeight, "src header at this height is submitted to dst chain")
cmd.Flags().Uint64(flagDstHeight, defaultDstHeight, "dst header at this height is submitted to src chain")
return cmd
}
func updateClientsCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "update-clients [path-name]",
Short: "update the clients between two configured chains with a configured path",
Long: strings.TrimSpace(`This command is meant to be used to updates
the clients with a configured path in the config file`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
// ensure that keys exist
if _, err = c[src].GetAddress(); err != nil {
return err
}
if _, err = c[dst].GetAddress(); err != nil {
return err
}
return core.UpdateClients(cmd.Context(), c[src], c[dst])
},
}
return cmd
}
func createConnectionCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "connection [path-name]",
Short: "create a connection between two configured chains with a configured path",
Long: strings.TrimSpace(`This command is meant to be used to repair or create
a connection between two chains with a configured path in the config file`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
to, err := getTimeout(cmd)
if err != nil {
return err
}
// ensure that keys exist
if _, err = c[src].GetAddress(); err != nil {
return err
}
if _, err = c[dst].GetAddress(); err != nil {
return err
}
return core.CreateConnection(cmd.Context(), pathName, c[src], c[dst], to)
},
}
return timeoutFlag(cmd)
}
func createChannelCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "channel [path-name]",
Short: "create a channel between two configured chains with a configured path",
Long: strings.TrimSpace(`This command is meant to be used to repair or
create a channel between two chains with a configured path in the config file`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
to, err := getTimeout(cmd)
if err != nil {
return err
}
// ensure that keys exist
if _, err = c[src].GetAddress(); err != nil {
return err
}
if _, err = c[dst].GetAddress(); err != nil {
return err
}
return core.CreateChannel(cmd.Context(), pathName, c[src], c[dst], to)
},
}
return timeoutFlag(cmd)
}
func channelUpgradeCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "channel-upgrade",
Short: "execute operations related to IBC channel upgrade",
Long: "This command is meant to be used to upgrade a channel between two chains with a configured path in the config file",
RunE: noCommand,
}
cmd.AddCommand(
channelUpgradeInitCmd(ctx),
channelUpgradeExecuteCmd(ctx),
channelUpgradeCancelCmd(ctx),
)
return cmd
}
func channelUpgradeInitCmd(ctx *config.Context) *cobra.Command {
const (
flagOrdering = "ordering"
flagConnectionHops = "connection-hops"
flagVersion = "version"
flagUnsafe = "unsafe"
)
cmd := cobra.Command{
Use: "init [path-name] [chain-id]",
Short: "execute chanUpgradeInit",
Long: "This command is meant to be used to initialize an IBC channel upgrade on a configured chain",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
chainID := args[1]
chains, srcID, dstID, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
var chain, cp *core.ProvableChain
switch chainID {
case srcID:
chain = chains[srcID]
cp = chains[dstID]
case dstID:
chain = chains[dstID]
cp = chains[srcID]
default:
return fmt.Errorf("unknown chain ID: %s", chainID)
}
// check cp state
permitUnsafe, err := cmd.Flags().GetBool(flagUnsafe)
if err != nil {
return err
}
// get ordering from flags
ordering, err := getOrderFromFlags(cmd.Flags(), flagOrdering)
if err != nil {
return err
} else if ordering == chantypes.NONE {
return errors.New("NONE is unacceptable channel ordering")
}
// get connection hops from flags
connHops, err := cmd.Flags().GetStringSlice(flagConnectionHops)
if err != nil {
return err
}
// get version from flags
version, err := cmd.Flags().GetString(flagVersion)
if err != nil {
return err
}
return core.InitChannelUpgrade(
cmd.Context(),
chain,
cp,
chantypes.UpgradeFields{
Ordering: ordering,
ConnectionHops: connHops,
Version: version,
},
permitUnsafe,
)
},
}
cmd.Flags().String(flagOrdering, "", "channel ordering applied for the new channel")
cmd.Flags().StringSlice(flagConnectionHops, nil, "connection hops applied for the new channel")
cmd.Flags().String(flagVersion, "", "channel version applied for the new channel")
cmd.Flags().Bool(flagUnsafe, false, "set true if you want to allow for initializing a new channel upgrade even though the counterparty chain is still flushing packets.")
return &cmd
}
func channelUpgradeExecuteCmd(ctx *config.Context) *cobra.Command {
const (
flagInterval = "interval"
flagTargetSrcState = "target-src-state"
flagTargetDstState = "target-dst-state"
)
const (
defaultInterval = time.Second
defaultTargetState = "UNINIT"
)
cmd := cobra.Command{
Use: "execute [path-name]",
Short: "execute channel upgrade handshake",
Long: "This command is meant to be used to execute an IBC channel upgrade handshake between two chains with a configured path in the config file",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
chains, srcChainID, dstChainID, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
src, ok := chains[srcChainID]
if !ok {
panic("src chain not found")
}
dst, ok := chains[dstChainID]
if !ok {
panic("dst chain not found")
}
interval, err := cmd.Flags().GetDuration(flagInterval)
if err != nil {
return err
}
targetSrcState, err := getUpgradeStateFromFlags(cmd.Flags(), flagTargetSrcState)
if err != nil {
return err
}
targetDstState, err := getUpgradeStateFromFlags(cmd.Flags(), flagTargetDstState)
if err != nil {
return err
}
return core.ExecuteChannelUpgrade(cmd.Context(), pathName, src, dst, interval, targetSrcState, targetDstState)
},
}
cmd.Flags().Duration(flagInterval, defaultInterval, "the interval between attempts to proceed the upgrade handshake")
cmd.Flags().String(flagTargetSrcState, defaultTargetState, "the source channel's upgrade state to be reached")
cmd.Flags().String(flagTargetDstState, defaultTargetState, "the destination channel's upgrade state to be reached")
return &cmd
}
func channelUpgradeCancelCmd(ctx *config.Context) *cobra.Command {
const (
flagSettlementInterval = "settlement-interval"
)
cmd := cobra.Command{
Use: "cancel [path-name] [chain-id]",
Short: "execute chanUpgradeCancel",
Long: "This command is meant to be used to cancel an IBC channel upgrade on a configured chain",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
chainID := args[1]
settlementInterval, err := cmd.Flags().GetDuration(flagSettlementInterval)
if err != nil {
return err
}
_, srcChainID, dstChainID, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
var cpChainID string
switch chainID {
case srcChainID:
cpChainID = dstChainID
case dstChainID:
cpChainID = srcChainID
default:
return fmt.Errorf("invalid chain ID: %s or %s was expected, but %s was given", srcChainID, dstChainID, chainID)
}
chain, err := ctx.Config.GetChain(chainID)
if err != nil {
return err
}
cp, err := ctx.Config.GetChain(cpChainID)
if err != nil {
return err
}
return core.CancelChannelUpgrade(cmd.Context(), chain, cp, settlementInterval)
},
}
cmd.Flags().Duration(flagSettlementInterval, 10*time.Second, "time interval between attemts to query for settled channel/upgrade states")
return &cmd
}
func relayMsgsCmd(ctx *config.Context) *cobra.Command {
const (
flagDoRefresh = "do-refresh"
flagSrcSeqs = "src-seqs"
flagDstSeqs = "dst-seqs"
)
const (
defaultDoRefresh = false
)
cmd := &cobra.Command{
Use: "relay [path-name]",
Short: "relay any packets that remain to be relayed on a given path, in both directions",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
path, err := ctx.Config.Paths.Get(args[0])
if err != nil {
return err
}
sh, err := core.NewSyncHeaders(cmd.Context(), c[src], c[dst])
if err != nil {
return err
}
st, err := core.GetStrategy(*path.Strategy)
if err != nil {
return err
}
if err := st.SetupRelay(cmd.Context(), c[src], c[dst]); err != nil {
return err
}
sp, err := st.UnrelayedPackets(cmd.Context(), c[src], c[dst], sh, false)
if err != nil {
return err
}
srcSeq := getUint64Slice(flagSrcSeqs)
dstSeq := getUint64Slice(flagDstSeqs)
if err = tryFilterRelayPackets(sp, srcSeq, dstSeq); err != nil {
return err
}
sp, err = st.ProcessTimeoutPackets(cmd.Context(), c[src], c[dst], sh, sp)
if err != nil {
return err
}
msgs := core.NewRelayMsgs()
doExecuteRelaySrc := len(sp.Dst) > 0
doExecuteRelayDst := len(sp.Src) > 0
doExecuteAckSrc := false
doExecuteAckDst := false
if m, err := st.UpdateClients(cmd.Context(), c[src], c[dst], doExecuteRelaySrc, doExecuteRelayDst, doExecuteAckSrc, doExecuteAckDst, sh, viper.GetBool(flagDoRefresh)); err != nil {
return err
} else {
msgs.Merge(m)
}
if m, err := st.RelayPackets(cmd.Context(), c[src], c[dst], sp, sh, doExecuteRelaySrc, doExecuteRelayDst); err != nil {
return err
} else {
msgs.Merge(m)
}
st.Send(cmd.Context(), c[src], c[dst], msgs)
return nil
},
}
cmd.Flags().Bool(flagDoRefresh, defaultDoRefresh, "execute light client refresh (updateClient) if required")
cmd.Flags().IntSlice(flagSrcSeqs, nil, "packet filter for src chain")
cmd.Flags().IntSlice(flagDstSeqs, nil, "packet filter for dst chain")
// TODO add option support for strategy
return cmd
}
func relayAcksCmd(ctx *config.Context) *cobra.Command {
const (
flagDoRefresh = "do-refresh"
flagSrcSeqs = "src-seqs"
flagDstSeqs = "dst-seqs"
)
const (
defaultDoRefresh = false
)
cmd := &cobra.Command{
Use: "relay-acknowledgements [path-name]",
Aliases: []string{"acks"},
Short: "relay any acknowledgements that remain to be relayed on a given path, in both directions",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
path, err := ctx.Config.Paths.Get(args[0])
if err != nil {
return err
}
sh, err := core.NewSyncHeaders(cmd.Context(), c[src], c[dst])
if err != nil {
return err
}
st, err := core.GetStrategy(*path.Strategy)
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 := st.UnrelayedAcknowledgements(cmd.Context(), c[src], c[dst], sh, false)
if err != nil {
return err
}
srcSeq := getUint64Slice(flagSrcSeqs)
dstSeq := getUint64Slice(flagDstSeqs)
if err = tryFilterRelayPackets(sp, srcSeq, dstSeq); err != nil {
return err
}
msgs := core.NewRelayMsgs()
doExecuteRelaySrc := false
doExecuteRelayDst := false
doExecuteAckSrc := len(sp.Dst) > 0
doExecuteAckDst := len(sp.Src) > 0
if m, err := st.UpdateClients(cmd.Context(), c[src], c[dst], doExecuteRelaySrc, doExecuteRelayDst, doExecuteAckSrc, doExecuteAckDst, sh, viper.GetBool(flagDoRefresh)); err != nil {
return err
} else {
msgs.Merge(m)
}
if m, err := st.RelayAcknowledgements(cmd.Context(), c[src], c[dst], sp, sh, doExecuteAckSrc, doExecuteAckDst); err != nil {
return err
} else {
msgs.Merge(m)
}
st.Send(cmd.Context(), c[src], c[dst], msgs)
return nil
},
}
cmd.Flags().Bool(flagDoRefresh, defaultDoRefresh, "execute light client refresh (updateClient) if required")
cmd.Flags().IntSlice(flagSrcSeqs, nil, "packet filter for src chain")
cmd.Flags().IntSlice(flagDstSeqs, nil, "packet filter for dst chain")
return cmd
}
func tryFilterRelayPackets(sp *core.RelayPackets, srcSeq []uint64, dstSeq []uint64) error {
if len(srcSeq) > 0 {
sp.Src = sp.Src.Filter(srcSeq)
if len(sp.Src) != len(srcSeq) {
return fmt.Errorf("src packet not found packetLength=%d selectedLength=%d", len(sp.Src), len(srcSeq))
}
}
if len(dstSeq) > 0 {
sp.Dst = sp.Dst.Filter(dstSeq)
if len(sp.Dst) != len(dstSeq) {
return fmt.Errorf("dst packet not found packetLength=%d selectedLength=%d", len(sp.Dst), len(dstSeq))
}
}
return nil
}
func getUint64Slice(key string) []uint64 {
org := viper.GetIntSlice(key)
ret := make([]uint64, len(org))
for i, e := range org {
ret[i] = uint64(e)
}
return ret
}
func getUpgradeStateFromFlags(flags *pflag.FlagSet, flagName string) (core.UpgradeState, error) {
s, err := flags.GetString(flagName)
if err != nil {
return 0, err
}
switch strings.ToUpper(s) {
case "UNINIT":
return core.UPGRADE_STATE_UNINIT, nil
case "INIT":
return core.UPGRADE_STATE_INIT, nil
case "FLUSHING":
return core.UPGRADE_STATE_FLUSHING, nil
case "FLUSHCOMPLETE":
return core.UPGRADE_STATE_FLUSHCOMPLETE, nil
default:
return 0, fmt.Errorf("invalid upgrade state specified: %s", s)
}
}
func getOrderFromFlags(flags *pflag.FlagSet, flagName string) (chantypes.Order, error) {
s, err := flags.GetString(flagName)
if err != nil {
return 0, err
}
s = "ORDER_" + strings.ToUpper(s)
value, ok := chantypes.Order_value[s]
if !ok {
return 0, fmt.Errorf("invalid channel order specified: %s", s)
}
return chantypes.Order(value), nil
}