Skip to content

Commit

Permalink
feat(x/bank): app wiring migration (cosmos#12032)
Browse files Browse the repository at this point in the history
* Initial work on bank module app wiring

* Fix import in bank module

* integrating bank module into simapp DI

* Integrate usages of ModuleAccountAddrs into DI container, remove from SimApp

* Remove dependency on authkeeper from bank module

* Integrate with keyed resolvers feature of depinject

* Refactoring to remove direct dependency from bank -> auth

* Clean up app.yaml usage in test

* Clean up comments, keys, and testing fns.

* Remove commented example in bank module config

* Regenerate code from proto files

* Fix usage of BlockedModuleAccountsOverride
  • Loading branch information
kocubinski authored Jun 1, 2022
1 parent a963b54 commit ca7b3a1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
25 changes: 7 additions & 18 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func init() {
//go:embed app.yaml
var appConfigYaml []byte

var appConfig = appconfig.LoadYAML(appConfigYaml)
var AppConfig = appconfig.LoadYAML(appConfigYaml)

// NewSimApp returns a reference to an initialized SimApp.
func NewSimApp(
Expand All @@ -211,16 +211,18 @@ func NewSimApp(
var appBuilder *runtime.AppBuilder
var paramsKeeper paramskeeper.Keeper
var accountKeeper authkeeper.AccountKeeper
var bankKeeper bankkeeper.Keeper
var appCodec codec.Codec
var legacyAmino *codec.LegacyAmino
var interfaceRegistry codectypes.InterfaceRegistry
err := depinject.Inject(appConfig,
err := depinject.Inject(AppConfig,
&appBuilder,
&paramsKeeper,
&appCodec,
&legacyAmino,
&interfaceRegistry,
&accountKeeper,
&bankKeeper,
)
if err != nil {
panic(err)
Expand All @@ -229,7 +231,7 @@ func NewSimApp(
runtimeApp := appBuilder.Build(logger, db, traceStore, baseAppOptions...)

keys := sdk.NewKVStoreKeys(
banktypes.StoreKey, stakingtypes.StoreKey,
stakingtypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, capabilitytypes.StoreKey,
Expand Down Expand Up @@ -266,9 +268,8 @@ func NewSimApp(
app.CapabilityKeeper.Seal()

// add keepers
app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(),
)
app.BankKeeper = bankKeeper

stakingKeeper := stakingkeeper.NewKeeper(
appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName),
)
Expand Down Expand Up @@ -351,7 +352,6 @@ func NewSimApp(
encodingConfig.TxConfig,
),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
Expand Down Expand Up @@ -498,16 +498,6 @@ func (app *SimApp) LoadHeight(height int64) error {
return app.LoadVersion(height)
}

// ModuleAccountAddrs returns all the app's module account addresses.
func (app *SimApp) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[authtypes.NewModuleAddress(acc).String()] = true
}

return modAccAddrs
}

// LegacyAmino returns SimApp's amino codec.
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
Expand Down Expand Up @@ -606,7 +596,6 @@ func GetMaccPerms() map[string][]string {

// initParamsKeeper init params keeper and its subspaces
func initParamsKeeper(paramsKeeper paramskeeper.Keeper) {
paramsKeeper.Subspace(banktypes.ModuleName)
paramsKeeper.Subspace(stakingtypes.ModuleName)
paramsKeeper.Subspace(minttypes.ModuleName)
paramsKeeper.Subspace(distrtypes.ModuleName)
Expand Down
4 changes: 4 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ modules:
- name: params
config:
"@type": cosmos.params.module.v1.Module

- name: bank
config:
"@type": cosmos.bank.module.v1.Module
4 changes: 2 additions & 2 deletions sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down Expand Up @@ -87,7 +87,7 @@ func BenchmarkInvariants(b *testing.B) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down
10 changes: 5 additions & 5 deletions sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestFullAppSimulation(t *testing.T) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestAppImportExport(t *testing.T) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(newApp, newApp.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestAppStateDeterminism(t *testing.T) {
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
ModuleAccountAddrs(),
config,
app.AppCodec(),
)
Expand Down
14 changes: 14 additions & 0 deletions test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/depinject"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -505,3 +507,15 @@ type EmptyAppOptions struct{}
func (ao EmptyAppOptions) Get(o string) interface{} {
return nil
}

// ModuleAccountAddrs provides a list of blocked module accounts from configuration in app.yaml
//
// Ported from SimApp
func ModuleAccountAddrs() map[string]bool {
var bk bankkeeper.Keeper
err := depinject.Inject(AppConfig, &bk)
if err != nil {
panic("unable to load DI container")
}
return bk.GetBlockedAddresses()
}
3 changes: 0 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ type App interface {
forZeroHeight bool, jailAllowedAddrs []string,
) (types.ExportedApp, error)

// All the registered module account addreses.
ModuleAccountAddrs() map[string]bool

// Helper for the simulation framework.
SimulationManager() *module.SimulationManager
}

0 comments on commit ca7b3a1

Please sign in to comment.