/
app.go
413 lines (361 loc) · 9.31 KB
/
app.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
package app
import (
"context"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"github.com/ignite/cli/v29/ignite/pkg/cosmosanalysis"
"github.com/ignite/cli/v29/ignite/pkg/cosmosver"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/goanalysis"
"github.com/ignite/cli/v29/ignite/pkg/gomodule"
"github.com/ignite/cli/v29/ignite/pkg/xast"
)
const registerRoutesMethod = "RegisterAPIRoutes"
// CheckKeeper checks for the existence of the keeper with the provided name in the app structure.
func CheckKeeper(path, keeperName string) error {
// find app type
appImpl, err := cosmosanalysis.FindImplementation(path, cosmosanalysis.AppImplementation)
if err != nil {
return err
}
if len(appImpl) != 1 {
return errors.Errorf("app.go should contain a single app (got %d)", len(appImpl))
}
appTypeName := appImpl[0]
// Inspect the module for app struct
var found bool
fileSet := token.NewFileSet()
pkgs, err := parser.ParseDir(fileSet, path, nil, 0)
if err != nil {
return err
}
for _, pkg := range pkgs {
for _, f := range pkg.Files {
ast.Inspect(f, func(n ast.Node) bool {
// look for struct methods.
appType, ok := n.(*ast.TypeSpec)
if !ok || appType.Name.Name != appTypeName {
return true
}
appStruct, ok := appType.Type.(*ast.StructType)
if !ok {
return true
}
// Search for the keeper specific field
for _, field := range appStruct.Fields.List {
for _, fieldName := range field.Names {
if fieldName.Name == keeperName {
found = true
return false
}
}
}
return false
})
}
}
if !found {
return errors.Errorf("app doesn't contain %s", keeperName)
}
return nil
}
// FindRegisteredModules returns all registered modules into the chain root.
func FindRegisteredModules(chainRoot string) ([]string, error) {
// Assumption: modules are registered in the app package
appFilePath, err := cosmosanalysis.FindAppFilePath(chainRoot)
if err != nil {
return nil, err
}
// The directory where the app file is located.
// This is required to resolve references within the app package.
appDir := filepath.Dir(appFilePath)
appPkg, _, err := xast.ParseDir(appDir)
if err != nil {
return nil, err
}
// Search the app for the imported SDK modules
var discovered []string
for _, f := range appPkg.Files {
discovered = append(discovered, goanalysis.FindBlankImports(f)...)
fileImports := goanalysis.FormatImports(f)
d, err := DiscoverModules(f, chainRoot, fileImports)
if err != nil {
return nil, err
}
discovered = append(discovered, d...)
}
// Discover IBC wired modules
// TODO: This can be removed once IBC modules use dependency injection
ibcPath := filepath.Join(chainRoot, "app", "ibc.go")
if _, err := os.Stat(ibcPath); err == nil {
m, err := discoverIBCModules(ibcPath)
if err != nil {
return nil, err
}
discovered = append(discovered, m...)
}
return removeDuplicateEntries(discovered), nil
}
// DiscoverModules find a map of import modules based on the configured app.
func DiscoverModules(file *ast.File, chainRoot string, fileImports map[string]string) ([]string, error) {
// find app type
appImpl := cosmosanalysis.FindImplementationInFile(file, cosmosanalysis.AppImplementation)
appTypeName := "App"
switch {
case len(appImpl) > 1:
return nil, errors.Errorf("app.go should contain only a single app (got %d)", len(appImpl))
case len(appImpl) == 1:
appTypeName = appImpl[0]
}
var discovered []string
for _, decl := range file.Decls {
switch x := decl.(type) {
case *ast.GenDecl:
discovered = append(discovered, discoverKeeperModules(x, appTypeName, fileImports)...)
case *ast.FuncDecl:
// The modules registered by Cosmos SDK `rumtime.App` are included
// when the app registers API modules though the `App` instance.
if isRuntimeAppCalled(x) {
m, err := discoverRuntimeAppModules(chainRoot)
if err != nil {
return nil, err
}
discovered = append(discovered, m...)
}
}
}
return removeDuplicateEntries(discovered), nil
}
func removeDuplicateEntries(entries []string) (res []string) {
seen := make(map[string]struct{})
for _, e := range entries {
if _, ok := seen[e]; ok {
continue
}
seen[e] = struct{}{}
res = append(res, e)
}
return
}
func discoverKeeperModules(d *ast.GenDecl, appTypeName string, imports map[string]string) []string {
var modules []string
for _, spec := range d.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if typeSpec.Name.Name != appTypeName {
continue
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
for _, field := range structType.Fields.List {
f := field.Type
CheckSpec:
switch spec := f.(type) {
case *ast.StarExpr:
f, ok = spec.X.(*ast.SelectorExpr)
if !ok {
continue
}
goto CheckSpec
case *ast.SelectorExpr:
if !strings.HasSuffix(spec.Sel.Name, "Keeper") {
continue
}
ident, ok := spec.X.(*ast.Ident)
if !ok {
continue
}
fileImport, ok := imports[ident.Name]
if !ok {
continue
}
modules = append(modules, removeKeeperPkgPath(fileImport))
}
}
}
return modules
}
func discoverRuntimeAppModules(chainRoot string) ([]string, error) {
// Resolve the absolute path to the Cosmos SDK module
cosmosPath, err := resolveCosmosPackagePath(chainRoot)
if err != nil {
return nil, err
}
var modules []string
// When runtime package doesn't exists it means is an older Cosmos SDK version,
// so all the module API registrations are defined within user's app.
path := filepath.Join(cosmosPath, "runtime", "app.go")
if _, err := os.Stat(path); os.IsNotExist(err) {
return modules, nil
}
f, _, err := xast.ParseFile(path)
if err != nil {
return nil, err
}
imports := goanalysis.FormatImports(f)
err = xast.Inspect(f, func(n ast.Node) error {
if pkgs := findRegisterAPIRoutesRegistrations(n); pkgs != nil {
for _, p := range pkgs {
if m := imports[p]; m != "" {
modules = append(modules, m)
}
}
return xast.ErrStop
}
return nil
})
if err != nil {
return nil, err
}
return modules, nil
}
func discoverIBCModules(ibcPath string) ([]string, error) {
f, _, err := xast.ParseFile(ibcPath)
if err != nil {
return nil, err
}
var (
names []string
imports = goanalysis.FormatImports(f)
)
err = xast.Inspect(f, func(n ast.Node) error {
fn, _ := n.(*ast.FuncDecl)
if fn == nil {
return nil
}
if fn.Name.Name != "RegisterIBC" && fn.Name.Name != "AddIBCModuleManager" {
return nil
}
for _, stmt := range fn.Body.List {
x, _ := stmt.(*ast.AssignStmt)
if x == nil {
continue
}
if len(x.Rhs) == 0 {
continue
}
c, _ := x.Rhs[0].(*ast.CompositeLit)
if c == nil {
continue
}
s, _ := c.Type.(*ast.SelectorExpr)
if s == nil || s.Sel.Name != "AppModule" {
continue
}
if m, _ := s.X.(*ast.Ident); m != nil {
names = append(names, m.Name)
}
}
return xast.ErrStop
})
if err != nil {
return nil, err
}
var modules []string
for _, n := range names {
modules = append(modules, imports[n])
}
return modules, nil
}
func resolveCosmosPackagePath(chainRoot string) (string, error) {
modFile, err := gomodule.ParseAt(chainRoot)
if err != nil {
return "", err
}
deps, err := gomodule.ResolveDependencies(modFile, false)
if err != nil {
return "", err
}
var pkg string
for _, dep := range deps {
// dependencies are resolved, so we need to check for possible SDK forks
if cosmosver.CosmosSDKModulePathPattern.MatchString(dep.Path) {
pkg = dep.String()
break
}
}
if pkg == "" {
return "", errors.New("cosmos-sdk package version not found")
}
m, err := gomodule.FindModule(context.Background(), chainRoot, pkg)
if err != nil {
return "", err
}
return m.Dir, nil
}
func findRegisterAPIRoutesRegistrations(n ast.Node) []string {
funcLitType, ok := n.(*ast.FuncDecl)
if !ok {
return nil
}
if funcLitType.Name.Name != registerRoutesMethod {
return nil
}
var packagesRegistered []string
for _, stmt := range funcLitType.Body.List {
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
}
exprCall, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
continue
}
exprFun, ok := exprCall.Fun.(*ast.SelectorExpr)
if !ok || exprFun.Sel.Name != "RegisterGRPCGatewayRoutes" {
continue
}
identType, ok := exprFun.X.(*ast.Ident)
if !ok {
continue
}
pkgName := identType.Name
if pkgName == "" {
continue
}
packagesRegistered = append(packagesRegistered, identType.Name)
}
return packagesRegistered
}
func removeKeeperPkgPath(pkg string) string {
path := strings.TrimSuffix(pkg, "/keeper")
path = strings.TrimSuffix(path, "/controller")
return strings.TrimSuffix(path, "/host")
}
func isRuntimeAppCalled(fn *ast.FuncDecl) bool {
if fn.Name.Name != registerRoutesMethod {
return false
}
for _, stmt := range fn.Body.List {
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
}
exprCall, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
continue
}
exprFun, ok := exprCall.Fun.(*ast.SelectorExpr)
if !ok || exprFun.Sel.Name != registerRoutesMethod {
continue
}
exprSel, ok := exprFun.X.(*ast.SelectorExpr)
if !ok || exprSel.Sel.Name != "App" {
continue
}
identType, ok := exprSel.X.(*ast.Ident)
if !ok || identType.Name != "app" {
continue
}
return true
}
return false
}