-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config.go
561 lines (485 loc) · 13.8 KB
/
config.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
/*
Package cmd includes relayer commands
Copyright © 2020 Jack Zampolin [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/relayer/relayer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
const (
// ORDERED is exported channel type constant
ORDERED = "ORDERED"
// UNORDERED is exported channel type constant
UNORDERED = "UNORDERED"
defaultOrder = ORDERED
defaultVersion = "ics20-1"
)
func configCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Aliases: []string{"cfg"},
Short: "manage configuration file",
}
cmd.AddCommand(
configShowCmd(),
configInitCmd(),
configAddDirCmd(),
)
return cmd
}
// Command for printing current configuration
func configShowCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Aliases: []string{"s", "list", "l"},
Short: "Prints current configuration",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s config show --home %s
$ %s cfg list`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}
cfgPath := path.Join(home, "config", "config.yaml")
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
if _, err := os.Stat(home); os.IsNotExist(err) {
return fmt.Errorf("home path does not exist: %s", home)
}
return fmt.Errorf("config does not exist: %s", cfgPath)
}
jsn, err := cmd.Flags().GetBool(flagJSON)
if err != nil {
return err
}
yml, err := cmd.Flags().GetBool(flagYAML)
if err != nil {
return err
}
switch {
case yml && jsn:
return fmt.Errorf("can't pass both --json and --yaml, must pick one")
case jsn:
out, err := json.Marshal(config)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
default:
out, err := yaml.Marshal(config)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
},
}
return yamlFlag(jsonFlag(cmd))
}
// Command for inititalizing an empty config at the --home location
func configInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Creates a default home directory at path defined by --home",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s config init --home %s
$ %s cfg i`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}
cfgDir := path.Join(home, "config")
cfgPath := path.Join(cfgDir, "config.yaml")
// If the config doesn't exist...
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// And the config folder doesn't exist...
if _, err := os.Stat(cfgDir); os.IsNotExist(err) {
// And the home folder doesn't exist
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the home folder
if err = os.Mkdir(home, os.ModePerm); err != nil {
return err
}
}
// Create the home config folder
if err = os.Mkdir(cfgDir, os.ModePerm); err != nil {
return err
}
}
// Then create the file...
f, err := os.Create(cfgPath)
if err != nil {
return err
}
defer f.Close()
// And write the default config to that location...
if _, err = f.Write(defaultConfig()); err != nil {
return err
}
// And return no error...
return nil
}
// Otherwise, the config file exists, and an error is returned...
return fmt.Errorf("config already exists: %s", cfgPath)
},
}
return cmd
}
func configAddDirCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add-dir [dir]",
Aliases: []string{"ad"},
Args: cobra.ExactArgs(1),
Short: `Add new chains and paths to the configuration file from a
directory full of chain and path configuration, useful for adding testnet configurations`,
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s config add-dir configs/
$ %s cfg ad configs/`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) (err error) {
var out *Config
if out, err = cfgFilesAdd(args[0]); err != nil {
return err
}
return overWriteConfig(cmd, out)
},
}
return cmd
}
func cfgFilesAdd(dir string) (cfg *Config, err error) {
dir = path.Clean(dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
cfg = config
for _, f := range files {
c := &relayer.Chain{}
pth := fmt.Sprintf("%s/%s", dir, f.Name())
if f.IsDir() {
fmt.Printf("directory at %s, skipping...\n", pth)
continue
}
byt, err := ioutil.ReadFile(pth)
if err != nil {
fmt.Printf("failed to read file %s, skipping...\n", pth)
continue
}
if err = json.Unmarshal(byt, c); err != nil {
fmt.Printf("failed to unmarshal file %s, skipping...\n", pth)
continue
}
if c.ChainID == "" && c.Key == "" && c.RPCAddr == "" {
p := &relayer.Path{}
if err = json.Unmarshal(byt, p); err != nil {
fmt.Printf("failed to unmarshal file %s, skipping...\n", pth)
continue
}
// In the case that order isn't added to the path, add it manually
if p.Src.Order == "" || p.Dst.Order == "" {
p.Src.Order = defaultOrder
p.Dst.Order = defaultOrder
}
// If the version isn't added to the path, add it manually
if p.Src.Version == "" {
p.Src.Version = defaultVersion
}
if p.Dst.Version == "" {
p.Dst.Version = defaultVersion
}
pthName := strings.Split(f.Name(), ".")[0]
if err = config.ValidatePath(p); err != nil {
fmt.Printf("%s: %s\n", pth, err.Error())
continue
}
if err = cfg.AddPath(pthName, p); err != nil {
fmt.Printf("%s: %s\n", pth, err.Error())
continue
}
// For now, we assume that all chain files must have same filename as chain-id
// this is to ensure non-chain files (global config) does not get parsed into chain struct.
if c.ChainID != pthName {
fmt.Printf("Skipping non chain file: %s\n", f.Name())
continue
}
}
if err = cfg.AddChain(c); err != nil {
fmt.Printf("%s: %s\n", pth, err.Error())
continue
}
fmt.Printf("added chain %s...\n", c.ChainID)
}
return cfg, nil
}
// Config represents the config file for the relayer
type Config struct {
Global GlobalConfig `yaml:"global" json:"global"`
Chains relayer.Chains `yaml:"chains" json:"chains"`
Paths relayer.Paths `yaml:"paths" json:"paths"`
}
// ChainsFromPath takes the path name and returns the properly configured chains
func (c *Config) ChainsFromPath(path string) (map[string]*relayer.Chain, string, string, error) {
pth, err := c.Paths.Get(path)
if err != nil {
return nil, "", "", err
}
src, dst := pth.Src.ChainID, pth.Dst.ChainID
chains, err := config.Chains.Gets(src, dst)
if err != nil {
return nil, "", "", err
}
if err = chains[src].SetPath(pth.Src); err != nil {
return nil, "", "", err
}
if err = chains[dst].SetPath(pth.Dst); err != nil {
return nil, "", "", err
}
return chains, src, dst, nil
}
// MustYAML returns the yaml string representation of the Paths
func (c Config) MustYAML() []byte {
out, err := yaml.Marshal(c)
if err != nil {
panic(err)
}
return out
}
func defaultConfig() []byte {
return Config{
Global: newDefaultGlobalConfig(),
Chains: relayer.Chains{},
Paths: relayer.Paths{},
}.MustYAML()
}
// GlobalConfig describes any global relayer settings
type GlobalConfig struct {
Timeout string `yaml:"timeout" json:"timeout"`
LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"`
}
// newDefaultGlobalConfig returns a global config with defaults set
func newDefaultGlobalConfig() GlobalConfig {
return GlobalConfig{
Timeout: "10s",
LightCacheSize: 20,
}
}
// AddChain adds an additional chain to the config
func (c *Config) AddChain(chain *relayer.Chain) (err error) {
chn, err := c.Chains.Get(chain.ChainID)
if chn == nil || err == nil {
return fmt.Errorf("chain with ID %s already exists in config", chain.ChainID)
}
c.Chains = append(c.Chains, chain)
return nil
}
// AddPath adds an additional path to the config
func (c *Config) AddPath(name string, path *relayer.Path) (err error) {
return c.Paths.Add(name, path)
}
// DeleteChain removes a chain from the config
func (c *Config) DeleteChain(chain string) *Config {
var set relayer.Chains
for _, ch := range c.Chains {
if ch.ChainID != chain {
set = append(set, ch)
}
}
c.Chains = set
return c
}
// Called to initialize the relayer.Chain types on Config
func validateConfig(c *Config) error {
to, err := time.ParseDuration(config.Global.Timeout)
if err != nil {
return fmt.Errorf("did you remember to run 'rly config init' error:%w", err)
}
for _, i := range c.Chains {
if err := i.Init(homePath, to, debug); err != nil {
return fmt.Errorf("did you remember to run 'rly config init' error:%w", err)
}
}
return nil
}
// initConfig reads in config file and ENV variables if set.
func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
if err != nil {
return err
}
config = &Config{}
cfgPath := path.Join(home, "config", "config.yaml")
if _, err := os.Stat(cfgPath); err == nil {
viper.SetConfigFile(cfgPath)
if err := viper.ReadInConfig(); err == nil {
// read the config file bytes
file, err := ioutil.ReadFile(viper.ConfigFileUsed())
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
// unmarshall them into the struct
err = yaml.Unmarshal(file, config)
if err != nil {
fmt.Println("Error unmarshalling config:", err)
os.Exit(1)
}
// ensure config has []*relayer.Chain used for all chain operations
err = validateConfig(config)
if err != nil {
fmt.Println("Error parsing chain config:", err)
os.Exit(1)
}
}
}
return nil
}
func overWriteConfig(cmd *cobra.Command, cfg *Config) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}
cfgPath := path.Join(home, "config", "config.yaml")
if _, err = os.Stat(cfgPath); err == nil {
viper.SetConfigFile(cfgPath)
if err = viper.ReadInConfig(); err == nil {
// ensure validateConfig runs properly
err = validateConfig(config)
if err != nil {
return err
}
// marshal the new config
out, err := yaml.Marshal(cfg)
if err != nil {
return err
}
// overwrite the config file
err = ioutil.WriteFile(viper.ConfigFileUsed(), out, 0600)
if err != nil {
return err
}
// set the global variable
config = cfg
}
}
return err
}
// ValidatePath checks that a path is valid
func (c *Config) ValidatePath(p *relayer.Path) (err error) {
if p.Src.Version == "" {
return fmt.Errorf("source must specify a version")
}
if err = c.ValidatePathEnd(p.Src); err != nil {
return err
}
if err = c.ValidatePathEnd(p.Dst); err != nil {
return err
}
if _, err = p.GetStrategy(); err != nil {
return err
}
if p.Src.Order != p.Dst.Order {
return fmt.Errorf("both sides must have same order ('ORDERED' or 'UNORDERED'), got src(%s) and dst(%s)",
p.Src.Order, p.Dst.Order)
}
return nil
}
// ValidatePathEnd validates provided pathend and returns error for invalid identifiers
func (c *Config) ValidatePathEnd(pe *relayer.PathEnd) error {
if err := pe.ValidateBasic(); err != nil {
return err
}
chain, err := c.Chains.Get(pe.ChainID)
if err != nil {
return err
}
height, err := chain.QueryLatestHeight()
if err != nil {
return err
}
if pe.ClientID != "" {
if err := c.ValidateClient(chain, height, pe); err != nil {
return err
}
if pe.ConnectionID != "" {
if err := c.ValidateConnection(chain, height, pe); err != nil {
return err
}
if pe.ChannelID != "" {
if err := c.ValidateChannel(chain, height, pe); err != nil {
return err
}
}
}
if pe.ConnectionID == "" && pe.ChannelID != "" {
return fmt.Errorf("connectionID is not configured for the channel: %s", pe.ChannelID)
}
}
if pe.ClientID == "" && pe.ConnectionID != "" {
return fmt.Errorf("clientID is not configured for the connection: %s", pe.ConnectionID)
}
return nil
}
// ValidateClient validates client id in provided pathend
func (c *Config) ValidateClient(chain *relayer.Chain, height int64, pe *relayer.PathEnd) error {
if err := pe.Vclient(); err != nil {
return err
}
_, err := chain.QueryClientState(height)
if err != nil {
return err
}
return nil
}
// ValidateConnection validates connection id in provided pathend
func (c *Config) ValidateConnection(chain *relayer.Chain, height int64, pe *relayer.PathEnd) error {
if err := pe.Vconn(); err != nil {
return err
}
connection, err := chain.QueryConnection(height)
if err != nil {
return err
}
if connection.Connection.ClientId != pe.ClientID {
return fmt.Errorf("clientID of connection: %s didn't match with provided ClientID", pe.ConnectionID)
}
return nil
}
// ValidateChannel validates channel id in provided pathend
func (c *Config) ValidateChannel(chain *relayer.Chain, height int64, pe *relayer.PathEnd) error {
if err := pe.Vchan(); err != nil {
return err
}
channel, err := chain.QueryChannel(height)
if err != nil {
return err
}
for _, connection := range channel.Channel.ConnectionHops {
if connection == pe.ConnectionID {
return nil
}
}
return fmt.Errorf("connectionID of channel: %s didn't match with provided ConnectionID", pe.ChannelID)
}