-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
paths.go
330 lines (295 loc) · 9.18 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
package cmd
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/cosmos/relayer/v2/relayer"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
const (
REPOURL = "https://github.com/cosmos/relayer"
PATHSURL = "https://github.com/cosmos/relayer/tree/main/interchain"
)
func pathsCmd(a *appState) *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(a),
pathsShowCmd(a),
pathsAddCmd(a),
pathsNewCmd(a),
pathsFetchCmd(a),
pathsDeleteCmd(a),
)
return cmd
}
func pathsDeleteCmd(a *appState) *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 := a.Config.Paths.Get(args[0]); err != nil {
return err
}
delete(a.Config.Paths, args[0])
return a.OverwriteConfig(a.Config)
},
}
return cmd
}
func pathsListCmd(a *appState) *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(a.Config.Paths)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
case jsn:
out, err := json.Marshal(a.Config.Paths)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
default:
i := 0
for k, pth := range a.Config.Paths {
chains, err := a.Config.Chains.Gets(pth.Src.ChainID, pth.Dst.ChainID)
if err != nil {
return err
}
stat := pth.QueryPathStatus(cmd.Context(), chains[pth.Src.ChainID], chains[pth.Dst.ChainID]).Status
printPath(cmd.OutOrStdout(), i, k, pth, checkmark(stat.Chains), checkmark(stat.Clients),
checkmark(stat.Connection))
i++
}
return nil
}
},
}
return yamlFlag(a.Viper, jsonFlag(a.Viper, cmd))
}
func printPath(stdout io.Writer, i int, k string, pth *relayer.Path, chains, clients, connection string) {
fmt.Fprintf(stdout, "%2d: %-20s -> chns(%s) clnts(%s) conn(%s) (%s<>%s)\n",
i, k, chains, clients, connection, pth.Src.ChainID, pth.Dst.ChainID)
}
func checkmark(status bool) string {
if status {
return check
}
return xIcon
}
func pathsShowCmd(a *appState) *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 {
p, err := a.Config.Paths.Get(args[0])
if err != nil {
return err
}
chains, err := a.Config.Chains.Gets(p.Src.ChainID, p.Dst.ChainID)
if err != nil {
return err
}
jsn, _ := cmd.Flags().GetBool(flagJSON)
yml, _ := cmd.Flags().GetBool(flagYAML)
pathWithStatus := p.QueryPathStatus(cmd.Context(), chains[p.Src.ChainID], chains[p.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.Fprintln(cmd.OutOrStdout(), string(out))
return nil
case jsn:
out, err := json.Marshal(pathWithStatus)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
default:
fmt.Fprintln(cmd.OutOrStdout(), pathWithStatus.PrintString(args[0]))
}
return nil
},
}
return yamlFlag(a.Viper, jsonFlag(a.Viper, cmd))
}
func pathsAddCmd(a *appState) *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 := a.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)
}
file, err := cmd.Flags().GetString(flagFile)
if err != nil {
return err
}
if file != "" {
if err := a.AddPathFromFile(cmd.Context(), cmd.ErrOrStderr(), file, args[2]); err != nil {
return err
}
} else {
if err := a.AddPathFromUserInput(cmd.Context(), cmd.InOrStdin(), cmd.ErrOrStderr(), src, dst, args[2]); err != nil {
return err
}
}
return a.OverwriteConfig(a.Config)
},
}
return fileFlag(a.Viper, cmd)
}
func pathsNewCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "new [src-chain-id] [dst-chain-id] [path-name]",
Aliases: []string{"n"},
Short: "Create a new blank path to be used in generating a new path (connection & client) between two chains",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths new ibc-0 ibc-1 demo-path
$ %s pth n ibc-0 ibc-1 demo-path`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
src, dst := args[0], args[1]
_, err := a.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)
}
p := &relayer.Path{
Src: &relayer.PathEnd{ChainID: src},
Dst: &relayer.PathEnd{ChainID: dst},
}
name := args[2]
if err = a.Config.Paths.Add(name, p); err != nil {
return err
}
return a.OverwriteConfig(a.Config)
},
}
return channelParameterFlags(a.Viper, cmd)
}
// pathsFetchCmd attempts to fetch the json files containing the path metadata, for each configured chain, from GitHub
func pathsFetchCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "fetch",
Aliases: []string{"fch"},
Short: "Fetches the json files necessary to setup the paths for the configured chains",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s paths fetch --home %s
$ %s pth fch`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
// Clone the GH repo to tmp dir, we will extract the path files from here
localRepo, err := os.MkdirTemp("", "")
if err != nil {
return err
}
if _, err = git.PlainClone(localRepo, false, &git.CloneOptions{
URL: REPOURL,
Progress: io.Discard,
ReferenceName: "refs/heads/main",
}); err != nil {
return err
}
// Try to fetch path info for each configured chain that has canonical chain/path info in the GH repo
for _, srcChain := range a.Config.Chains {
for _, dstChain := range a.Config.Chains {
// Add paths to rly config from {localRepo}/interchain/chaind-id/
localPathsDir := path.Join(localRepo, "interchain", srcChain.ChainID())
dir := path.Clean(localPathsDir)
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "path info does not exist for chain: %s. Consider adding its info to %s. Error: %v\n", srcChain.ChainID(), path.Join(PATHSURL, "interchain"), err)
break
}
// For each path file, check that the dst is also a configured chain in the relayers config
for _, f := range files {
pth := filepath.Join(dir, f.Name())
if f.IsDir() {
fmt.Fprintf(cmd.ErrOrStderr(), "directory at %s, skipping...\n", pth)
continue
}
byt, err := os.ReadFile(pth)
if err != nil {
cleanupDir(localRepo)
return fmt.Errorf("failed to read file %s: %w", pth, err)
}
p := &relayer.Path{}
if err = json.Unmarshal(byt, p); err != nil {
cleanupDir(localRepo)
return fmt.Errorf("failed to unmarshal file %s: %w", pth, err)
}
if p.Dst.ChainID == dstChain.ChainID() {
pthName := strings.Split(f.Name(), ".")[0]
if err = a.Config.AddPath(pthName, p); err != nil {
return fmt.Errorf("failed to add path %s: %w", pth, err)
}
fmt.Fprintf(cmd.ErrOrStderr(), "added path %s...\n", pthName)
}
}
if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
}
}
cleanupDir(localRepo)
return nil
},
}
return cmd
}
func cleanupDir(dir string) {
_ = os.RemoveAll(dir)
}