-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
297 lines (243 loc) · 6.7 KB
/
Copy pathconfig.go
File metadata and controls
297 lines (243 loc) · 6.7 KB
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
package config
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"cosmossdk.io/math"
"github.com/caarlos0/env/v9"
"github.com/go-playground/validator/v10"
"github.com/google/go-github/v55/github"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
log "github.com/archway-network/relayer_exporter/pkg/logger"
)
const ibcPathSuffix = ".json"
var (
ErrGitHubClient = errors.New("GitHub client not provided")
ErrMissingRPCConfigMsg = "missing RPC config for chain: %s"
)
type Account struct {
Address string `yaml:"address" validate:"required"`
Denom []string `yaml:"denom" validate:"required"`
ChainName string `yaml:"chainName" validate:"required"`
Balance []math.Int
Tags []string `yaml:"tags,omitempty"`
}
type RPC struct {
ChainName string `yaml:"chainName" validate:"required"`
ChainID string `yaml:"chainId" validate:"required"`
URL string `yaml:"url" validate:"required,http_url,has_port"`
Timeout string `yaml:"timeout"`
}
type GitHub struct {
Org string `yaml:"org" validate:"required"`
Repo string `yaml:"repo" validate:"required"`
IBCDir string `yaml:"dir" validate:"required"`
TestnetsIBCDir string `yaml:"testnetsDir"`
Token string `env:"GITHUB_TOKEN"`
}
type Config struct {
Accounts []*Account `yaml:"accounts"`
GlobalRPCTimeout string `env:"GLOBAL_RPC_TIMEOUT" envDefault:"5s"`
RPCs []*RPC `yaml:"rpc"`
GitHub *GitHub `yaml:"github"`
}
type IBCChainMeta struct {
ChainName string `json:"chain_name"`
ClientID string `json:"client_id"`
ConnectionID string `json:"connection_id"`
}
type Channel struct {
Chain1 struct {
ChannelID string `json:"channel_id"`
PortID string `json:"port_id"`
} `json:"chain_1"`
Chain2 struct {
ChannelID string `json:"channel_id"`
PortID string `json:"port_id"`
} `json:"chain_2"`
Ordering string `json:"ordering"`
Version string `json:"version"`
Tags struct {
Status string `json:"status"`
Preferred bool `json:"preferred"`
Dex string `json:"dex"`
Properties string `json:"properties"`
} `json:"tags,omitempty"`
}
type Operator struct {
Chain1 struct {
Address string `json:"address"`
} `json:"chain_1"`
Chain2 struct {
Address string `json:"address"`
} `json:"chain_2"`
Memo string `json:"memo"`
Name string `json:"name"`
Discord Discord `json:"discord"`
}
type IBCData struct {
Schema string `json:"$schema"`
Chain1 IBCChainMeta `json:"chain_1"`
Chain2 IBCChainMeta `json:"chain_2"`
Channels []Channel `json:"channels"`
Operators []Operator `json:"operators"`
}
type Discord struct {
Handle string `json:"handle"`
ID string `json:"id"`
}
// GetRPCsMap uses the provided config file to return a map of chain
// chain_names to RPCs. It uses IBCData already extracted from
// github IBC registry to validate config for missing RPCs and raises
// an error if any are missing.
func (c *Config) GetRPCsMap() *map[string]RPC {
rpcs := map[string]RPC{}
for _, rpc := range c.RPCs {
if rpc.Timeout == "" {
rpc.Timeout = c.GlobalRPCTimeout
}
rpcs[rpc.ChainName] = *rpc
}
return &rpcs
}
func (c *Config) IBCPaths(ctx context.Context) ([]*IBCData, error) {
client := github.NewClient(nil)
if c.GitHub == nil {
return nil, fmt.Errorf("GitHub configuration is required")
}
log.Info(
fmt.Sprintf(
"Github IBC registry: %s/%s",
c.GitHub.Org,
c.GitHub.Repo,
),
zap.String("Mainnet Directory", c.GitHub.IBCDir),
zap.String("Testnet Directory", c.GitHub.TestnetsIBCDir),
)
if c.GitHub.Token != "" {
log.Debug("Using provided GITHUB_TOKEN env var for GitHub client")
client = github.NewClient(nil).WithAuthToken(c.GitHub.Token)
}
paths, err := c.getPaths(ctx, c.GitHub.IBCDir, client)
if err != nil {
return nil, err
}
testnetsPaths := []*IBCData{}
if c.GitHub.TestnetsIBCDir != "" {
testnetsPaths, err = c.getPaths(ctx, c.GitHub.TestnetsIBCDir, client)
if err != nil {
return nil, err
}
}
paths = append(paths, testnetsPaths...)
return paths, nil
}
func (c *Config) getPaths(ctx context.Context, dir string, client *github.Client) ([]*IBCData, error) {
if client == nil {
return nil, ErrGitHubClient
}
_, ibcDir, _, err := client.Repositories.GetContents(ctx, c.GitHub.Org, c.GitHub.Repo, dir, nil)
if err != nil {
return nil, err
}
ibcs := []*IBCData{}
for _, file := range ibcDir {
if strings.HasSuffix(*file.Path, ibcPathSuffix) {
log.Debug(fmt.Sprintf("Fetching IBC data for %s/%s/%s", c.GitHub.Org, c.GitHub.Repo, *file.Path))
content, _, _, err := client.Repositories.GetContents(
ctx,
c.GitHub.Org,
c.GitHub.Repo,
*file.Path,
nil,
)
if err != nil {
return nil, err
}
ibc := &IBCData{}
c, err := content.GetContent()
if err != nil {
return nil, err
}
if err = json.Unmarshal([]byte(c), &ibc); err != nil {
return nil, err
}
ibcs = append(ibcs, ibc)
}
}
return ibcs, nil
}
func (c *Config) Validate() error {
validate := validator.New(validator.WithRequiredStructEnabled())
// register custom validation for http url as expected by go relayer i.e.
// http_url must have port defined.
// https://github.com/cosmos/relayer/blob/259b1278264180a2aefc2085f1b55753849c4815/cregistry/chain_info.go#L115
err := validate.RegisterValidation("has_port", func(fl validator.FieldLevel) bool {
val := fl.Field().String()
urlParsed, err := url.Parse(val)
if err != nil {
return false
}
port := urlParsed.Port()
// Port must be a iny <= 65535.
if portNum, err := strconv.ParseInt(
port, 10, 32,
); err != nil || portNum > 65535 || portNum < 1 {
return false
}
return true
})
if err != nil {
return err
}
// validate top level fields
if err := validate.Struct(c); err != nil {
return err
}
// validate RPCs
for _, rpc := range c.RPCs {
if err := validate.Struct(rpc); err != nil {
return fmt.Errorf("%v for RPC config: %+v", err, rpc)
}
}
// validate accounts
for _, account := range c.Accounts {
if err := validate.Struct(account); err != nil {
return fmt.Errorf("%v for accounts config: %+v", err, account)
}
rpcMap := c.GetRPCsMap()
if rpcMap != nil {
_, ok := (*rpcMap)[account.ChainName]
if !ok {
return fmt.Errorf(ErrMissingRPCConfigMsg, account.ChainName)
}
}
}
return nil
}
func NewConfig(configPath string) (*Config, error) {
config := &Config{}
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(config); err != nil {
return nil, err
}
if err := env.Parse(config); err != nil {
return nil, err
}
err = config.Validate()
if err != nil {
return nil, err
}
return config, nil
}