Skip to content

Commit

Permalink
chore: loosen assertions in SetOrderBeginBlockers() and SetOrderEndBl…
Browse files Browse the repository at this point in the history
…ockers() (#14415)
  • Loading branch information
0Tech authored Dec 29, 2022
1 parent cc573b8 commit e18a093
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (module)[#14415](https://github.com/cosmos/cosmos-sdk/pull/14415) Loosen assertions in SetOrderBeginBlockers() and SetOrderEndBlockers()
* (context)[#14384](https://github.com/cosmos/cosmos-sdk/pull/14384) refactor(context): Pass EventManager to the context as an interface.
* (types) [#14354](https://github.com/cosmos/cosmos-sdk/pull/14354) - improve performance on Context.KVStore and Context.TransientStore by 40%
* (crypto/keyring) [#14151](https://github.com/cosmos/cosmos-sdk/pull/14151) Move keys presentation from `crypto/keyring` to `client/keys`
Expand Down
26 changes: 15 additions & 11 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,23 @@ func NewSimApp(
// NOTE: staking module is required if HistoricalEntries param > 0
// NOTE: capability module's beginblocker must come before any modules using capabilities (e.g. IBC)
app.ModuleManager.SetOrderBeginBlockers(
upgradetypes.ModuleName, capabilitytypes.ModuleName, minttypes.ModuleName, distrtypes.ModuleName, slashingtypes.ModuleName,
evidencetypes.ModuleName, stakingtypes.ModuleName,
authtypes.ModuleName, banktypes.ModuleName, govtypes.ModuleName, crisistypes.ModuleName, genutiltypes.ModuleName,
authz.ModuleName, feegrant.ModuleName, nft.ModuleName, group.ModuleName,
paramstypes.ModuleName, vestingtypes.ModuleName, consensusparamtypes.ModuleName,
upgradetypes.ModuleName,
capabilitytypes.ModuleName,
minttypes.ModuleName,
distrtypes.ModuleName,
slashingtypes.ModuleName,
evidencetypes.ModuleName,
stakingtypes.ModuleName,
genutiltypes.ModuleName,
authz.ModuleName,
)
app.ModuleManager.SetOrderEndBlockers(
crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName,
capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName,
slashingtypes.ModuleName, minttypes.ModuleName,
genutiltypes.ModuleName, evidencetypes.ModuleName, authz.ModuleName,
feegrant.ModuleName, nft.ModuleName, group.ModuleName,
paramstypes.ModuleName, upgradetypes.ModuleName, vestingtypes.ModuleName, consensusparamtypes.ModuleName,
crisistypes.ModuleName,
govtypes.ModuleName,
stakingtypes.ModuleName,
genutiltypes.ModuleName,
feegrant.ModuleName,
group.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down
23 changes: 0 additions & 23 deletions simapp/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,16 @@ var (
slashingtypes.ModuleName,
evidencetypes.ModuleName,
stakingtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
govtypes.ModuleName,
crisistypes.ModuleName,
genutiltypes.ModuleName,
authz.ModuleName,
feegrant.ModuleName,
nft.ModuleName,
group.ModuleName,
paramstypes.ModuleName,
vestingtypes.ModuleName,
consensustypes.ModuleName,
},
EndBlockers: []string{
crisistypes.ModuleName,
govtypes.ModuleName,
stakingtypes.ModuleName,
capabilitytypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
distrtypes.ModuleName,
slashingtypes.ModuleName,
minttypes.ModuleName,
genutiltypes.ModuleName,
evidencetypes.ModuleName,
authz.ModuleName,
feegrant.ModuleName,
nft.ModuleName,
group.ModuleName,
paramstypes.ModuleName,
consensustypes.ModuleName,
upgradetypes.ModuleName,
vestingtypes.ModuleName,
},
OverrideStoreKeys: []*runtimev1alpha1.StoreKeyConfig{
{
Expand Down
30 changes: 23 additions & 7 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,32 +293,42 @@ func NewManagerFromMap(moduleMap map[string]appmodule.AppModule) *Manager {

// SetOrderInitGenesis sets the order of init genesis calls
func (m *Manager) SetOrderInitGenesis(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderInitGenesis", moduleNames)
m.assertNoForgottenModules("SetOrderInitGenesis", moduleNames, nil)
m.OrderInitGenesis = moduleNames
}

// SetOrderExportGenesis sets the order of export genesis calls
func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderExportGenesis", moduleNames)
m.assertNoForgottenModules("SetOrderExportGenesis", moduleNames, nil)
m.OrderExportGenesis = moduleNames
}

// SetOrderBeginBlockers sets the order of set begin-blocker calls
func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderBeginBlockers", moduleNames)
m.assertNoForgottenModules("SetOrderBeginBlockers", moduleNames,
func(moduleName string) bool {
module := m.Modules[moduleName]
_, hasBeginBlock := module.(BeginBlockAppModule)
return !hasBeginBlock
})
m.OrderBeginBlockers = moduleNames
}

// SetOrderEndBlockers sets the order of set end-blocker calls
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames)
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames,
func(moduleName string) bool {
module := m.Modules[moduleName]
_, hasEndBlock := module.(EndBlockAppModule)
return !hasEndBlock
})
m.OrderEndBlockers = moduleNames
}

// SetOrderMigrations sets the order of migrations to be run. If not set
// then migrations will be run with an order defined in `DefaultMigrationsOrder`.
func (m *Manager) SetOrderMigrations(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderMigrations", moduleNames)
m.assertNoForgottenModules("SetOrderMigrations", moduleNames, nil)
m.OrderMigrations = moduleNames
}

Expand Down Expand Up @@ -425,21 +435,27 @@ func (m *Manager) checkModulesExists(moduleName []string) error {

// assertNoForgottenModules checks that we didn't forget any modules in the
// SetOrder* functions.
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string) {
// `pass` is a closure which allows one to omit modules from `moduleNames`. If you provide non-nil `pass` and it returns true, the module would not be subject of the assertion.
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string, pass func(moduleName string) bool) {
ms := make(map[string]bool)
for _, m := range moduleNames {
ms[m] = true
}
var missing []string
for m := range m.Modules {
m := m
if pass != nil && pass(m) {
continue
}

if !ms[m] {
missing = append(missing, m)
}
}
if len(missing) != 0 {
sort.Strings(missing)
panic(fmt.Sprintf(
"%s: all modules must be defined when setting %s, missing: %v", setOrderFnName, setOrderFnName, missing))
"all modules must be defined when setting %s, missing: %v", setOrderFnName, missing))
}
}

Expand Down
11 changes: 7 additions & 4 deletions types/module/module_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ func (s TestSuite) TestAssertNoForgottenModules() { //nolint:govet
name string
positive bool
modules []string
pass func(string) bool
}{
{"same modules", true, []string{"a", "b"}},
{"more modules", true, []string{"a", "b", "c"}},
{"less modules", false, []string{"a"}, nil},
{"same modules", true, []string{"a", "b"}, nil},
{"more modules", true, []string{"a", "b", "c"}, nil},
{"pass module b", true, []string{"a"}, func(moduleName string) bool { return moduleName == "b" }},
}

for _, tc := range tcs {
if tc.positive {
m.assertNoForgottenModules("x", tc.modules)
m.assertNoForgottenModules("x", tc.modules, tc.pass)
} else {
s.Panics(func() { m.assertNoForgottenModules("x", tc.modules) })
s.Panics(func() { m.assertNoForgottenModules("x", tc.modules, tc.pass) })
}
}
}
Expand Down

0 comments on commit e18a093

Please sign in to comment.