Skip to content

Commit 8757361

Browse files
refactor(cosmosanalysis): proper message filtering (backport #4686) (#4693)
* refactor(cosmosanalysis): proper message filtering (#4686) * refactor(cosmosanalysis): keep only messages * updates * simplify * updates * fix * harden logic (cherry picked from commit 20a54d4) * fixes --------- Co-authored-by: julienrbrt <julien@rbrt.fr>
1 parent f785e63 commit 8757361

File tree

7 files changed

+151
-77
lines changed

7 files changed

+151
-77
lines changed

.github/CODEOWNERS

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
# CODEOWNERS: https://help.github.com/articles/about-codeowners/
1+
# CODEOWNERS: <https://help.github.com/articles/about-codeowners/>
22

33
# Primary repo maintainers
4-
* @ilgooz @jeronimoalbi @Pantani @julienrbrt @Ehsan-saradar
4+
5+
* @ilgooz @Pantani @julienrbrt
6+
7+
# Ts templates
8+
9+
*.js @ilgooz @Pantani @julienrbrt @clockworkgr
10+
ignite/pkg/cosmosgen/templates/* @ilgooz @Pantani @julienrbrt @clockworkgr
511

612
# Docs
7-
*.md @salmad3 @toschdev @ilgooz
8-
docs/* @salmad3 @toschdev @ilgooz
9-
changelog.md @ilgooz @salmad3 @toschdev @jeronimoalbi @Pantani @julienrbrt @Ehsan-saradar
13+
14+
*.md @ilgooz @Pantani @julienrbrt @toschdev
15+
docs/* @ilgooz @Pantani @julienrbrt @toschdev

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Bug Fixes
6+
7+
- [#4686](https://github.com/ignite/cli/pull/4686) Filter discovered protos to only messages.
8+
59
## [`v28.10.0`](https://github.com/ignite/cli/releases/tag/v28.10.0)
610

711
### Features

ignite/pkg/cosmosanalysis/module/message.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

ignite/pkg/cosmosanalysis/module/module.go

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,6 @@ func RootGoImportPath(importPath string) string {
208208
return importPath
209209
}
210210

211-
func extractRelPath(pkgGoImportPath, baseGoPath string) (string, error) {
212-
// Remove the import prefix to get the relative path
213-
if strings.HasPrefix(pkgGoImportPath, baseGoPath) {
214-
return strings.TrimPrefix(pkgGoImportPath, baseGoPath), nil
215-
}
216-
217-
// When the import path prefix defined by the proto package
218-
// doesn't match the base Go import path it means that the
219-
// latter might have a version suffix and the former doesn't.
220-
if p, v := path.Split(baseGoPath); semver.IsValid(v) {
221-
// Use the import path without the version as prefix
222-
p = strings.TrimRight(p, "/")
223-
224-
return strings.TrimPrefix(pkgGoImportPath, p), nil
225-
}
226-
227-
return "", errors.Errorf("proto go import %s is not relative to %s", pkgGoImportPath, baseGoPath)
228-
}
229-
230211
// discover discovers and sdk module by a proto pkg.
231212
func (d *moduleDiscoverer) discover(pkg protoanalysis.Package) (Module, error) {
232213
// Check if the proto package services are implemented
@@ -235,19 +216,7 @@ func (d *moduleDiscoverer) discover(pkg protoanalysis.Package) (Module, error) {
235216
return Module{}, err
236217
}
237218

238-
pkgRelPath, err := extractRelPath(pkg.GoImportPath(), d.basegopath)
239-
if err != nil {
240-
return Module{}, err
241-
}
242-
243-
// Find the `sdk.Msg` interface implementation
244-
pkgPath := filepath.Join(d.sourcePath, pkgRelPath)
245-
msgs, err := cosmosanalysis.FindImplementation(pkgPath, messageImplementation)
246-
if err != nil {
247-
return Module{}, err
248-
}
249-
250-
if len(pkg.Services)+len(msgs) == 0 {
219+
if len(pkg.Services) == 0 {
251220
return Module{}, nil
252221
}
253222

@@ -257,22 +226,6 @@ func (d *moduleDiscoverer) discover(pkg protoanalysis.Package) (Module, error) {
257226
Pkg: pkg,
258227
}
259228

260-
for _, msg := range msgs {
261-
pkgmsg, err := pkg.MessageByName(msg)
262-
if err != nil {
263-
// no msg found in the proto defs corresponds to discovered sdk message.
264-
// if it cannot be found, nothing to worry about, this means that it is used
265-
// only internally and not open for actual use.
266-
continue
267-
}
268-
269-
m.Msgs = append(m.Msgs, Msg{
270-
Name: msg,
271-
URI: fmt.Sprintf("%s.%s", pkg.Name, msg),
272-
FilePath: pkgmsg.Path,
273-
})
274-
}
275-
276229
// isType whether if protomsg can be added as an any Type to Module.
277230
isType := func(protomsg protoanalysis.Message) bool {
278231
// do not use GenesisState type.
@@ -315,19 +268,37 @@ func (d *moduleDiscoverer) discover(pkg protoanalysis.Package) (Module, error) {
315268
})
316269
}
317270

318-
// fill queries.
271+
// fill queries & messages.
319272
for _, s := range pkg.Services {
320273
for _, q := range s.RPCFuncs {
321-
if len(q.HTTPRules) == 0 {
322-
continue
323-
}
274+
switch s.Name {
275+
case "Msg":
276+
pkgmsg, err := pkg.MessageByName(q.RequestType)
277+
if err != nil {
278+
// no msg found in the proto defs corresponds to discovered sdk message.
279+
// if it cannot be found, nothing to worry about, this means that it is used
280+
// only internally and not open for actual use.
281+
continue
282+
}
324283

325-
m.HTTPQueries = append(m.HTTPQueries, HTTPQuery{
326-
Name: q.Name,
327-
FullName: s.Name + q.Name,
328-
Rules: q.HTTPRules,
329-
Paginated: q.Paginated,
330-
})
284+
m.Msgs = append(m.Msgs, Msg{
285+
Name: q.RequestType,
286+
URI: fmt.Sprintf("%s.%s", pkg.Name, q.RequestType),
287+
FilePath: pkgmsg.Path,
288+
})
289+
case "Query":
290+
// no http rules means this query is not exposed as a REST endpoint.
291+
if len(q.HTTPRules) == 0 {
292+
continue
293+
}
294+
295+
m.HTTPQueries = append(m.HTTPQueries, HTTPQuery{
296+
Name: q.Name,
297+
FullName: s.Name + q.Name,
298+
Rules: q.HTTPRules,
299+
Paginated: q.Paginated,
300+
})
301+
}
331302
}
332303
}
333304

ignite/pkg/cosmosanalysis/module/module_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@ func newModule(relChainPath, goImportPath string) module.Module {
2929
},
3030
GoImportName: "github.com/tendermint/planet/x/mars/types",
3131
Messages: []protoanalysis.Message{
32+
{
33+
Name: "MsgMyMessageRequest",
34+
Path: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
35+
HighestFieldNumber: 1,
36+
Fields: map[string]string{
37+
"mytypefield": "string",
38+
},
39+
},
40+
{
41+
Name: "MsgMyMessageResponse",
42+
Path: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
43+
HighestFieldNumber: 1,
44+
Fields: map[string]string{
45+
"mytypefield": "string",
46+
},
47+
},
48+
{
49+
Name: "MsgBarRequest",
50+
Path: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
51+
HighestFieldNumber: 1,
52+
Fields: map[string]string{
53+
"mytypefield": "string",
54+
},
55+
},
56+
{
57+
Name: "MsgBarResponse",
58+
Path: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
59+
HighestFieldNumber: 1,
60+
Fields: map[string]string{
61+
"mytypefield": "string",
62+
},
63+
},
3264
{
3365
Name: "QueryMyQueryRequest",
3466
Path: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
@@ -58,6 +90,21 @@ func newModule(relChainPath, goImportPath string) module.Module {
5890
},
5991
},
6092
Services: []protoanalysis.Service{
93+
{
94+
Name: "Msg",
95+
RPCFuncs: []protoanalysis.RPCFunc{
96+
{
97+
Name: "MyMessage",
98+
RequestType: "MsgMyMessageRequest",
99+
ReturnsType: "MsgMyMessageResponse",
100+
},
101+
{
102+
Name: "Bar",
103+
RequestType: "MsgBarRequest",
104+
ReturnsType: "MsgBarResponse",
105+
},
106+
},
107+
},
61108
{
62109
Name: "Query",
63110
RPCFuncs: []protoanalysis.RPCFunc{
@@ -89,7 +136,18 @@ func newModule(relChainPath, goImportPath string) module.Module {
89136
},
90137
},
91138
},
92-
Msgs: []module.Msg(nil),
139+
Msgs: []module.Msg{
140+
{
141+
Name: "MsgMyMessageRequest",
142+
URI: "tendermint.planet.mars.MsgMyMessageRequest",
143+
FilePath: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
144+
},
145+
{
146+
Name: "MsgBarRequest",
147+
URI: "tendermint.planet.mars.MsgBarRequest",
148+
FilePath: filepath.Join(relChainPath, "proto/planet/mars/mars.proto"),
149+
},
150+
},
93151
HTTPQueries: []module.HTTPQuery{
94152
{
95153
Name: "MyQuery",

ignite/pkg/cosmosanalysis/module/testdata/earth/proto/planet/mars/mars.proto

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@ import "cosmos/base/query/v1beta1/pagination.proto";
44
import "google/api/annotations.proto";
55
option go_package = "github.com/tendermint/planet/x/mars/types";
66

7+
service Msg {
8+
rpc MyMessage(MsgMyMessageRequest) returns (MsgMyMessageResponse);
9+
10+
rpc Bar(MsgBarRequest) returns (MsgBarResponse);
11+
}
12+
13+
message MsgMyMessageRequest {
14+
string mytypefield = 1;
15+
}
16+
17+
message MsgMyMessageResponse {
18+
string mytypefield = 1;
19+
}
20+
21+
message MsgBarRequest {
22+
string mytypefield = 1;
23+
}
24+
25+
message MsgBarResponse {
26+
string mytypefield = 1;
27+
}
28+
729
service Query {
830
rpc MyQuery(QueryMyQueryRequest) returns (QueryMyQueryResponse) {
9-
option (google.api.http).get = "/tendermint/mars/withoutmsg/my_query/{mytypefield}";
31+
option (google.api.http).get = "/tendermint/mars/my_query/{mytypefield}";
1032
}
1133

1234
rpc Foo(QueryFooRequest) returns (QueryFooResponse) {
13-
option (google.api.http).get = "/tendermint/mars/withoutmsg/foo/";
35+
option (google.api.http).get = "/tendermint/mars/foo/";
1436
}
1537
}
1638

ignite/pkg/cosmosanalysis/module/testdata/planet/proto/planet/mars/mars.proto

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@ import "cosmos/base/query/v1beta1/pagination.proto";
44
import "google/api/annotations.proto";
55
option go_package = "github.com/tendermint/planet/x/mars/types";
66

7+
service Msg {
8+
rpc MyMessage(MsgMyMessageRequest) returns (MsgMyMessageResponse);
9+
10+
rpc Bar(MsgBarRequest) returns (MsgBarResponse);
11+
}
12+
13+
message MsgMyMessageRequest {
14+
string mytypefield = 1;
15+
}
16+
17+
message MsgMyMessageResponse {
18+
string mytypefield = 1;
19+
}
20+
21+
message MsgBarRequest {
22+
string mytypefield = 1;
23+
}
24+
25+
message MsgBarResponse {
26+
string mytypefield = 1;
27+
}
28+
729
service Query {
830
rpc MyQuery(QueryMyQueryRequest) returns (QueryMyQueryResponse) {
9-
option (google.api.http).get = "/tendermint/mars/withoutmsg/my_query/{mytypefield}";
31+
option (google.api.http).get = "/tendermint/mars/my_query/{mytypefield}";
1032
}
1133

1234
rpc Foo(QueryFooRequest) returns (QueryFooResponse) {
13-
option (google.api.http).get = "/tendermint/mars/withoutmsg/foo/";
35+
option (google.api.http).get = "/tendermint/mars/foo/";
1436
}
1537
}
1638

0 commit comments

Comments
 (0)