Skip to content

Commit

Permalink
fix: restore runtime app API registered module discovery (#3785)
Browse files Browse the repository at this point in the history
* fix: restore runtime app API registered modules

* ci: fix lint issue with unused argument

* tests: fix broken unit tests

* ci: fix linting issue

* feat: add support for IBC modules discovery

This is a temporary feature that should be removed once IBC modules
are injected inteas of being wired using the definitions from the
scafolded app file `app/ibc.go`.

* fix: correct issue that cause modules not to be discovered

The removed snippet was not able to handle some Go import paths
which were removed, like the IBC ones, also causing that the TS
client generation was incomplete because of the missing modules.

* feat(pkg/xstrings): add `StringBetween` function

* fix: discover "cosmossdk.io/x" based packages proto files

The "cosmossdk.io/x" Go packages MUST search proto files in the Cosmos
SDK Go package because these packages don't have the proto files included
so they can't be discovered.

* feat(pkg/gocmd): add Go mod download support

* feat(pkg/cosmosanalysis): add find module support

* refactor(pkg/cosmosanalysis): change Cosmos SDK package path discovery

Discovery changed to use go mod download instead of using Go's module
path which might not be present in some context.

* refactor: add filter function to remove duplicated modules

* chore: TS template updates

* fix: correct TS imports for generated "cosmossdk.io" module packages

* tests: add unit tests for the new `pkg/gomodule` functions

* refactor: add options to `pkg/cosmosanalysis` Discover function

* fix: Update package.json template

---------

Co-authored-by: Danilo Pantani <[email protected]>
Co-authored-by: Clockwork <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2023
1 parent fb2b140 commit ab3f5c1
Show file tree
Hide file tree
Showing 28 changed files with 1,166 additions and 300 deletions.
364 changes: 284 additions & 80 deletions ignite/pkg/cosmosanalysis/app/app.go

Large diffs are not rendered by default.

142 changes: 24 additions & 118 deletions ignite/pkg/cosmosanalysis/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func TestFindRegisteredModules(t *testing.T) {
"github.com/cosmos/cosmos-sdk/x/staking",
"github.com/cosmos/cosmos-sdk/x/gov",
"github.com/username/test/x/foo",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
}

cases := []struct {
Expand All @@ -110,6 +113,8 @@ func TestFindRegisteredModules(t *testing.T) {
"cosmossdk.io/x/evidence",
"cosmossdk.io/x/feegrant/module",
"cosmossdk.io/x/upgrade",
"github.com/cosmos/cosmos-sdk/x/auth",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/x/auth/tx/config",
"github.com/cosmos/cosmos-sdk/x/auth/vesting",
"github.com/cosmos/cosmos-sdk/x/authz/module",
Expand All @@ -125,6 +130,8 @@ func TestFindRegisteredModules(t *testing.T) {
"github.com/ignite/mars/x/mars",
"github.com/cosmos/cosmos-sdk/x/gov",
"github.com/username/test/x/foo",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
},
},
{
Expand Down Expand Up @@ -191,6 +198,9 @@ func TestFindRegisteredModules(t *testing.T) {
path: "testdata/modules/spn",
expectedModules: []string{
"github.com/cosmos/cosmos-sdk/x/auth",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
"github.com/cosmos/cosmos-sdk/x/bank",
"github.com/cosmos/cosmos-sdk/x/capability",
"github.com/cosmos/cosmos-sdk/x/staking",
Expand Down Expand Up @@ -239,6 +249,9 @@ func TestFindRegisteredModules(t *testing.T) {
"github.com/cosmos/cosmos-sdk/x/authz",
"github.com/CosmWasm/wasmd/x/wasm",
"github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
},
},
}
Expand All @@ -252,13 +265,16 @@ func TestFindRegisteredModules(t *testing.T) {
}
}

func TestFindKeepersModules(t *testing.T) {
func TestDiscoverModules(t *testing.T) {
basicModules := []string{
"github.com/cosmos/cosmos-sdk/x/auth",
"github.com/cosmos/cosmos-sdk/x/bank",
"github.com/cosmos/cosmos-sdk/x/staking",
"github.com/cosmos/cosmos-sdk/x/gov",
"github.com/username/test/x/foo",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
}

cases := []struct {
Expand Down Expand Up @@ -343,6 +359,9 @@ func TestFindKeepersModules(t *testing.T) {
path: "testdata/modules/spn",
expectedModules: []string{
"github.com/cosmos/cosmos-sdk/x/auth",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
"github.com/cosmos/cosmos-sdk/x/bank",
"github.com/cosmos/cosmos-sdk/x/capability",
"github.com/cosmos/cosmos-sdk/x/staking",
Expand Down Expand Up @@ -391,6 +410,9 @@ func TestFindKeepersModules(t *testing.T) {
"github.com/cosmos/cosmos-sdk/x/authz",
"github.com/CosmWasm/wasmd/x/wasm",
"github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts",
"github.com/cosmos/cosmos-sdk/x/auth/tx",
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice",
"github.com/cosmos/cosmos-sdk/client/grpc/node",
},
},
}
Expand All @@ -403,7 +425,7 @@ func TestFindKeepersModules(t *testing.T) {
got := make([]string, 0)
for _, f := range appPkg.Files {
fileImports := goanalysis.FormatImports(f)
modules, err := FindKeepersModules(f, fileImports)
modules, err := DiscoverModules(f, tt.path, fileImports)
require.NoError(t, err)
if modules != nil {
got = append(got, modules...)
Expand All @@ -414,122 +436,6 @@ func TestFindKeepersModules(t *testing.T) {
}
}

func Test_mergeImports(t *testing.T) {
tests := []struct {
name string
blankImports []string
discovered []string
want []string
}{
{
name: "test nil imports",
blankImports: nil,
discovered: nil,
want: nil,
},
{
name: "test empty imports",
blankImports: []string{},
discovered: []string{},
want: []string{},
},
{
name: "test only one blank import",
blankImports: []string{"github.com/cosmos/cosmos-sdk/x/auth"},
discovered: []string{},
want: []string{"github.com/cosmos/cosmos-sdk/x/auth"},
},
{
name: "test only one discovered import",
blankImports: []string{},
discovered: []string{"github.com/cosmos/cosmos-sdk/x/auth"},
want: []string{"github.com/cosmos/cosmos-sdk/x/auth"},
},
{
name: "test only one import",
blankImports: []string{"github.com/cosmos/cosmos-sdk/x/auth"},
discovered: []string{"github.com/cosmos/cosmos-sdk/x/auth/keeper"},
want: []string{"github.com/cosmos/cosmos-sdk/x/auth"},
},
{
name: "test only one keeper import",
blankImports: []string{"github.com/cosmos/cosmos-sdk/x/auth/module"},
discovered: []string{"github.com/cosmos/cosmos-sdk/x/auth/keeper"},
want: []string{"github.com/cosmos/cosmos-sdk/x/auth/module"},
},
{
name: "test two keeper import",
blankImports: []string{
"github.com/cosmos/cosmos-sdk/x/auth/module",
"github.com/cosmos/cosmos-sdk/x/bank/module",
},
discovered: []string{
"github.com/cosmos/cosmos-sdk/x/auth/keeper",
"github.com/cosmos/cosmos-sdk/x/bank/keeper",
},
want: []string{
"github.com/cosmos/cosmos-sdk/x/auth/module",
"github.com/cosmos/cosmos-sdk/x/bank/module",
},
},
{
name: "test two keeper import",
blankImports: []string{
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts",
},
discovered: []string{
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper",
"github.com/cosmos/cosmos-sdk/x/bank/keeper",
},
want: []string{
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts",
"github.com/cosmos/cosmos-sdk/x/bank/keeper",
},
},
{
name: "test keeper imports",
blankImports: []string{
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts",
"cosmossdk.io/x/feegrant/module",
},
discovered: []string{
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper",
"github.com/cosmos/cosmos-sdk/x/bank/keeper",
"cosmossdk.io/x/feegrant/types",
"cosmossdk.io/x/feegrant",
"cosmossdk.io/x/foo",
},
want: []string{
"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts",
"github.com/cosmos/cosmos-sdk/x/bank/keeper",
"cosmossdk.io/x/feegrant/module",
"cosmossdk.io/x/foo",
},
},
{
name: "test three keeper import",
blankImports: []string{
"github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts",
"github.com/cosmos/ibc-go/modules/capability",
},
discovered: []string{
"github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts",
"github.com/cosmos/ibc-go/modules/capability",
},
want: []string{
"github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts",
"github.com/cosmos/ibc-go/modules/capability",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := mergeImports(tt.blankImports, tt.discovered)
require.ElementsMatch(t, tt.want, got)
})
}
}

func Test_removeKeeperPkgPath(t *testing.T) {
tests := []struct {
name string
Expand Down
30 changes: 30 additions & 0 deletions ignite/pkg/cosmosanalysis/app/testdata/modules/app_config/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module app

go 1.20

require (
cosmossdk.io/api v0.3.1
cosmossdk.io/core v0.5.1
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/bufbuild/buf v1.23.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.2.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.55.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.31.0
)
11 changes: 0 additions & 11 deletions ignite/pkg/cosmosanalysis/app/testdata/modules/crescent/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"path/filepath"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/server"

"cosmossdk.io/client/v2/autocli"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -1031,13 +1030,3 @@ func (App) GetSubspace(moduleName string) paramstypes.Subspace {
func (App) SimulationManager() *module.SimulationManager {
return app.sm
}

// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
app.App.RegisterAPIRoutes(apiSvr, apiConfig)
// register swagger API in app.go so that other applications can override easily
if err := server.RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger); err != nil {
panic(err)
}
}
29 changes: 29 additions & 0 deletions ignite/pkg/cosmosanalysis/app/testdata/modules/crescent/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/crescent-network/crescent/v5

go 1.18

require (
github.com/cosmos/cosmos-sdk v0.45.10
github.com/cosmos/ibc-go/v3 v3.4.0
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/golangci/golangci-lint v1.50.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.13.0
github.com/rakyll/statik v0.1.7
github.com/regen-network/cosmos-proto v0.3.1
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/tendermint/tendermint v0.34.22
github.com/tendermint/tm-db v0.6.7
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v2 v2.4.0
)

12 changes: 0 additions & 12 deletions ignite/pkg/cosmosanalysis/app/testdata/modules/gaia/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/server"

"cosmossdk.io/client/v2/autocli"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -1096,13 +1094,3 @@ func (GaiaApp) GetSubspace(moduleName string) paramstypes.Subspace {
func (GaiaApp) SimulationManager() *module.SimulationManager {
return app.sm
}

// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (GaiaApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
app.App.RegisterAPIRoutes(apiSvr, apiConfig)
// register swagger API in app.go so that other applications can override easily
if err := server.RegisterSwaggerAPI(apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger); err != nil {
panic(err)
}
}
28 changes: 28 additions & 0 deletions ignite/pkg/cosmosanalysis/app/testdata/modules/gaia/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module github.com/cosmos/gaia/v14

go 1.20

require (
cosmossdk.io/errors v1.0.0
cosmossdk.io/math v1.2.0
github.com/cosmos/cosmos-sdk v0.45.16
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v4 v4.1.1
github.com/cosmos/ibc-go/v4 v4.4.2
github.com/cosmos/interchain-security/v2 v2.0.0
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.3
github.com/google/gofuzz v1.2.0
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/ory/dockertest/v3 v3.10.0
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.7
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb
google.golang.org/grpc v1.58.2
)
33 changes: 33 additions & 0 deletions ignite/pkg/cosmosanalysis/app/testdata/modules/juno/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module github.com/CosmosContracts/juno/v18

go 1.21

require (
cosmossdk.io/api v0.3.1
cosmossdk.io/errors v1.0.0
cosmossdk.io/log v1.2.1
cosmossdk.io/math v1.1.2
cosmossdk.io/tools/rosetta v0.2.1
github.com/CosmWasm/wasmd v0.45.0
github.com/CosmWasm/wasmvm v1.5.0
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.0.1
github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.0.0
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79
github.com/cosmos/ibc-go/v7 v7.3.1
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/prometheus/client_golang v1.16.0
github.com/skip-mev/pob v1.0.4
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98
google.golang.org/grpc v1.58.3
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit ab3f5c1

Please sign in to comment.