From 56f5b59ac46b05208cebad25db0433429e527abf Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 12:36:27 +0000 Subject: [PATCH] fix(runtime): allow to properly register non app wiring modules (backport #17284) (#17305) Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + docs/docs/building-apps/01-app-go-v2.md | 25 +++++++++++++++++++++++++ runtime/app.go | 15 +++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b885e5f4c80..b10e12036db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (runtime) [#17284](https://github.com/cosmos/cosmos-sdk/pull/17284) Properly allow to combine depinject-enabled modules and non-depinject-enabled modules in app v2. * (baseapp) [#17159](https://github.com/cosmos/cosmos-sdk/pull/17159) Validators can propose blocks that exceed the gas limit. * (baseapp) [#16547](https://github.com/cosmos/cosmos-sdk/pull/16547) Ensure a transaction's gas limit cannot exceed the block gas limit. * (x/gov,x/group) [#17220](https://github.com/cosmos/cosmos-sdk/pull/17220) Do not try validate `msgURL` as web URL in `draft-proposal` command. diff --git a/docs/docs/building-apps/01-app-go-v2.md b/docs/docs/building-apps/01-app-go-v2.md index a30a202a462..aee53ce89da 100644 --- a/docs/docs/building-apps/01-app-go-v2.md +++ b/docs/docs/building-apps/01-app-go-v2.md @@ -117,6 +117,31 @@ More information on how work `depinject.Configs` and `depinject.Supply` can be f https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_v2.go#L186-L216 ``` +### Registering non app wiring modules + +It is possible to combine app wiring / depinject enabled modules with non app wiring modules. +To do so, use the `app.RegisterModules` method to register the modules on your app, as well as `app.RegisterStores` for registering the extra stores needed. + +```go +// .... +app.App = appBuilder.Build(db, traceStore, baseAppOptions...) + +// register module manually +app.RegisterStores(storetypes.NewKVStoreKey(example.ModuleName)) +app.ExampleKeeper = examplekeeper.NewKeeper(app.appCodec, app.AccountKeeper.AddressCodec(), runtime.NewKVStoreService(app.GetKey(example.ModuleName)), authtypes.NewModuleAddress(govtypes.ModuleName).String()) +exampleAppModule := examplemodule.NewAppModule(app.ExampleKeeper) +if err := app.RegisterModules(&exampleAppModule); err != nil { + panic(err) +} + +// .... +``` + +:::warning +When using AutoCLI and combining app wiring and non app wiring modules. The AutoCLI options should be manually constructed instead of injected. +Otherwise it will miss the non depinject modules and not register their CLI. +::: + ### Complete `app_v2.go` :::tip diff --git a/runtime/app.go b/runtime/app.go index 0c81c6bbcd2..b5cfcc54cec 100644 --- a/runtime/app.go +++ b/runtime/app.go @@ -72,7 +72,22 @@ func (a *App) RegisterModules(modules ...module.AppModule) error { appModule.RegisterInterfaces(a.interfaceRegistry) appModule.RegisterLegacyAminoCodec(a.amino) + if module, ok := appModule.(module.HasServices); ok { + module.RegisterServices(a.configurator) + } } + + return nil +} + +// RegisterStores registers the provided store keys. +// This method should only be used for registering extra stores +// wiich is necessary for modules that not registered using the app config. +// To be used in combination of RegisterModules. +func (a *App) RegisterStores(keys ...storetypes.StoreKey) error { + a.storeKeys = append(a.storeKeys, keys...) + a.MountStores(keys...) + return nil }