Merge PR #4847: Sim refactor 1 move store decoders to modules

* move store decoders to modules

* fix

* module pattern

* compile

* update Decoders params

* fix

* address @colin-axner comments

* Update cmd_test.go

* simulation manager

* mino fixes

* cleanup

* Apply suggestions from code review

Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com>

* address @rigelrozanski comments

* changelog

* Apply suggestions from code review

Co-Authored-By: colin axner <colinaxner@berkeley.edu>

* restructure modules simulation pkgs

* address @alexanderbez comments

* fix
This commit is contained in:
Federico Kunze
2019-08-13 18:16:03 -04:00
committed by frog power 4000
co-authored by frog power 4000 colin axner
parent 81f86fefe5
commit c441ce2fab
43 changed files with 1377 additions and 877 deletions
+44 -28
View File
@@ -3,6 +3,7 @@ Package module contains application module patterns and associated "manager" fun
The module pattern has been broken down by:
- independent module functionality (AppModuleBasic)
- inter-dependent module genesis functionality (AppModuleGenesis)
- inter-dependent module simulation functionality (AppModuleSimulation)
- inter-dependent module full functionality (AppModule)
inter-dependent module functionality is module functionality which somehow
@@ -41,6 +42,7 @@ import (
)
//__________________________________________________________________________________________
// AppModuleBasic is the standard form for basic non-dependant elements of an application module.
type AppModuleBasic interface {
Name() string
@@ -56,9 +58,10 @@ type AppModuleBasic interface {
GetQueryCmd(*codec.Codec) *cobra.Command
}
// collections of AppModuleBasic
// BasicManager is a collection of AppModuleBasic
type BasicManager map[string]AppModuleBasic
// NewBasicManager creates a new BasicManager object
func NewBasicManager(modules ...AppModuleBasic) BasicManager {
moduleMap := make(map[string]AppModuleBasic)
for _, module := range modules {
@@ -67,14 +70,14 @@ func NewBasicManager(modules ...AppModuleBasic) BasicManager {
return moduleMap
}
// RegisterCodecs registers all module codecs
// RegisterCodec registers all module codecs
func (bm BasicManager) RegisterCodec(cdc *codec.Codec) {
for _, b := range bm {
b.RegisterCodec(cdc)
}
}
// Provided default genesis information for all modules
// DefaultGenesis provides default genesis information for all modules
func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage {
genesis := make(map[string]json.RawMessage)
for _, b := range bm {
@@ -83,7 +86,7 @@ func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage {
return genesis
}
// Provided default genesis information for all modules
// ValidateGenesis performs genesis state validation for all modules
func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error {
for _, b := range bm {
if err := b.ValidateGenesis(genesis[b.Name()]); err != nil {
@@ -93,14 +96,14 @@ func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error
return nil
}
// RegisterRestRoutes registers all module rest routes
// RegisterRESTRoutes registers all module rest routes
func (bm BasicManager) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
for _, b := range bm {
b.RegisterRESTRoutes(ctx, rtr)
}
}
// add all tx commands to the rootTxCmd
// AddTxCommands adds all tx commands to the rootTxCmd
func (bm BasicManager) AddTxCommands(rootTxCmd *cobra.Command, cdc *codec.Codec) {
for _, b := range bm {
if cmd := b.GetTxCmd(cdc); cmd != nil {
@@ -109,7 +112,7 @@ func (bm BasicManager) AddTxCommands(rootTxCmd *cobra.Command, cdc *codec.Codec)
}
}
// add all query commands to the rootQueryCmd
// AddQueryCommands adds all query commands to the rootQueryCmd
func (bm BasicManager) AddQueryCommands(rootQueryCmd *cobra.Command, cdc *codec.Codec) {
for _, b := range bm {
if cmd := b.GetQueryCmd(cdc); cmd != nil {
@@ -119,6 +122,7 @@ func (bm BasicManager) AddQueryCommands(rootQueryCmd *cobra.Command, cdc *codec.
}
//_________________________________________________________
// AppModuleGenesis is the standard form for an application module genesis functions
type AppModuleGenesis interface {
AppModuleBasic
@@ -126,9 +130,17 @@ type AppModuleGenesis interface {
ExportGenesis(sdk.Context) json.RawMessage
}
// AppModuleSimulation defines the standard functions that every module should expose
// for the SDK blockchain simulator
type AppModuleSimulation interface {
// register a func to decode the each module's defined types from their corresponding store key
RegisterStoreDecoder(sdk.StoreDecoderRegistry)
}
// AppModule is the standard form for an application module
type AppModule interface {
AppModuleGenesis
AppModuleSimulation
// registers
RegisterInvariants(sdk.InvariantRegistry)
@@ -139,12 +151,14 @@ type AppModule interface {
QuerierRoute() string
NewQuerierHandler() sdk.Querier
// ABCI
BeginBlock(sdk.Context, abci.RequestBeginBlock)
EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate
}
//___________________________
// app module
// GenesisOnlyAppModule is an AppModule that only has import/export functionality
type GenesisOnlyAppModule struct {
AppModuleGenesis
}
@@ -156,31 +170,35 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule {
}
}
// register invariants
// RegisterInvariants is a placeholder function register no invariants
func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
// module message route ngame
// RegisterStoreDecoder empty store decoder registry
func (GenesisOnlyAppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}
// Route empty module message route
func (GenesisOnlyAppModule) Route() string { return "" }
// module handler
// NewHandler returns an empty module handler
func (GenesisOnlyAppModule) NewHandler() sdk.Handler { return nil }
// module querier route ngame
// QuerierRoute returns an empty module querier route
func (GenesisOnlyAppModule) QuerierRoute() string { return "" }
// module querier
// NewQuerierHandler returns an empty module querier
func (gam GenesisOnlyAppModule) NewQuerierHandler() sdk.Querier { return nil }
// module begin-block
// BeginBlock returns an empty module begin-block
func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {}
// module end-block
// EndBlock returns an empty module end-block
func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
//____________________________________________________________________________
// module manager provides the high level utility for managing and executing
// Manager defines a module manager that provides the high level utility for managing and executing
// operations for a group of modules
type Manager struct {
Modules map[string]AppModule
@@ -190,7 +208,7 @@ type Manager struct {
OrderEndBlockers []string
}
// NewModuleManager creates a new Manager object
// NewManager creates a new Manager object
func NewManager(modules ...AppModule) *Manager {
moduleMap := make(map[string]AppModule)
@@ -209,34 +227,34 @@ func NewManager(modules ...AppModule) *Manager {
}
}
// set the order of init genesis calls
// SetOrderInitGenesis sets the order of init genesis calls
func (m *Manager) SetOrderInitGenesis(moduleNames ...string) {
m.OrderInitGenesis = moduleNames
}
// set the order of export genesis calls
// SetOrderExportGenesis sets the order of export genesis calls
func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
m.OrderExportGenesis = moduleNames
}
// set the order of set begin-blocker calls
// SetOrderBeginBlockers sets the order of set begin-blocker calls
func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
m.OrderBeginBlockers = moduleNames
}
// set the order of set end-blocker calls
// SetOrderEndBlockers sets the order of set end-blocker calls
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
m.OrderEndBlockers = moduleNames
}
// register all module routes and module querier routes
// RegisterInvariants registers all module routes and module querier routes
func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) {
for _, module := range m.Modules {
module.RegisterInvariants(ir)
}
}
// register all module routes and module querier routes
// RegisterRoutes registers all module routes and module querier routes
func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) {
for _, module := range m.Modules {
if module.Route() != "" {
@@ -248,7 +266,7 @@ func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter)
}
}
// perform init genesis functionality for modules
// InitGenesis performs init genesis functionality for modules
func (m *Manager) InitGenesis(ctx sdk.Context, genesisData map[string]json.RawMessage) abci.ResponseInitChain {
var validatorUpdates []abci.ValidatorUpdate
for _, moduleName := range m.OrderInitGenesis {
@@ -271,7 +289,7 @@ func (m *Manager) InitGenesis(ctx sdk.Context, genesisData map[string]json.RawMe
}
}
// perform export genesis functionality for modules
// ExportGenesis performs export genesis functionality for modules
func (m *Manager) ExportGenesis(ctx sdk.Context) map[string]json.RawMessage {
genesisData := make(map[string]json.RawMessage)
for _, moduleName := range m.OrderExportGenesis {
@@ -320,6 +338,4 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo
ValidatorUpdates: validatorUpdates,
Events: ctx.EventManager().ABCIEvents(),
}
}
// DONTCOVER
}
+27
View File
@@ -0,0 +1,27 @@
package module
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// SimulationManager defines a simulation manager that provides the high level utility
// for managing and executing simulation functionalities for a group of modules
type SimulationManager struct {
Modules map[string]AppModule
StoreDecoders sdk.StoreDecoderRegistry
}
// NewSimulationManager creates a new SimulationManager object
func NewSimulationManager(moduleMap map[string]AppModule) *SimulationManager {
return &SimulationManager{
Modules: moduleMap,
StoreDecoders: make(sdk.StoreDecoderRegistry),
}
}
// RegisterStoreDecoders registers each of the modules' store decoders into a map
func (sm *SimulationManager) RegisterStoreDecoders() {
for _, module := range sm.Modules {
module.RegisterStoreDecoder(sm.StoreDecoders)
}
}