Overview of app_v2.go
Pre-requisite Readings
This section is intended to provide an overview of the SimApp
app_v2.go
file with App Wiring.
app_config.go
The app_config.go
file is the single place to configure all modules parameters.
Create the
AppConfig
variable:https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_config.go#L91-L93
Configure the
runtime
module:https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_config.go#L94-L158
Configure the modules defined in the
BeginBlocker
andEndBlocker
and thetx
module:https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_config.go#L159-L177
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_config.go#L192-L194
Complete app_config.go
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_config.go#L52-L254
Alternative formats
The example above shows how to create an AppConfig
using Go. However, it is also possible to create an AppConfig
using YAML, or JSON.
The configuration can then be embed with go:embed
and read with appconfig.LoadYAML
, or appconfig.LoadJSON
, in app_v2.go
.
//go:embed app_config.yaml
var (
appConfigYaml []byte
appConfig = appconfig.LoadYAML(appConfigYaml)
)
modules:
- name: runtime
config:
"@type": cosmos.app.runtime.v1alpha1.Module
app_name: SimApp
begin_blockers: [staking, auth, bank]
end_blockers: [bank, auth, staking]
init_genesis: [bank, auth, staking]
- name: auth
config:
"@type": cosmos.auth.module.v1.Module
bech32_prefix: cosmos
- name: bank
config:
"@type": cosmos.bank.module.v1.Module
- name: staking
config:
"@type": cosmos.staking.module.v1.Module
- name: tx
config:
"@type": cosmos.tx.module.v1.Module
A more complete example of app.yaml
can be found here.
app_v2.go
app_v2.go
is the place where SimApp
is constructed. depinject.Inject
facilitates that by automatically wiring the app modules and keepers, provided an application configuration AppConfig
is provided. SimApp
is constructed, when calling the injected *runtime.AppBuilder
, with appBuilder.Build(...)
.
In short depinject
and the runtime
package abstract the wiring of the app, and the AppBuilder
is the place where the app is constructed. runtime
takes care of registering the codecs, KV store, subspaces and instantiating baseapp
.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_v2.go#L158-L291
When using depinject.Inject
, the injected types must be pointers.
Advanced Configuration
In advanced cases, it is possible to inject extra (module) configuration in a way that is not (yet) supported by AppConfig
.
In this case, use depinject.Configs
for combining the extra configuration and AppConfig
, and depinject.Supply
to providing that extra configuration.
More information on how work depinject.Configs
and depinject.Supply
can be found in the depinject
documentation.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_v2.go#L186-L216
Complete app_v2.go
Note that in the complete SimApp
app_v2.go
file, testing utilities are also defined, but they could as well be defined in a separate file.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/simapp/app_v2.go#L75-L395