Merge PR #4451: Client and Module Modularization

This commit is contained in:
frog power 4000
2019-06-05 19:26:16 -04:00
committed by Alexander Bezobchuk
parent d80a980c30
commit 5f9c3fdf88
184 changed files with 3108 additions and 2396 deletions
+4
View File
@@ -85,11 +85,13 @@ func (config *Config) SetAddressVerifier(addressVerifier func([]byte) error) {
config.addressVerifier = addressVerifier
}
// Set the BIP-0044 CoinType code on the config
func (config *Config) SetCoinType(coinType uint32) {
config.assertNotSealed()
config.coinType = coinType
}
// Set the FullFundraiserPath (BIP44Prefix) on the config
func (config *Config) SetFullFundraiserPath(fullFundraiserPath string) {
config.assertNotSealed()
config.fullFundraiserPath = fullFundraiserPath
@@ -144,10 +146,12 @@ func (config *Config) GetAddressVerifier() func([]byte) error {
return config.addressVerifier
}
// Get the BIP-0044 CoinType code on the config
func (config *Config) GetCoinType() uint32 {
return config.coinType
}
// Get the FullFundraiserPath (BIP44Prefix) on the config
func (config *Config) GetFullFundraiserPath() string {
return config.fullFundraiserPath
}
+1 -1
View File
@@ -257,7 +257,7 @@ type cloner interface {
Clone() interface{} // deep copy
}
// XXX add description
// TODO add description
type Op struct {
// type is always 'with'
gen int
+111 -68
View File
@@ -1,8 +1,9 @@
/*
Package types contains application module patterns and associated "manager" functionality.
Package module contains application module patterns and associated "manager" functionality.
The module pattern has been broken down by:
- independent module functionality (AppModuleBasic)
- inter-dependent module functionality (AppModule)
- inter-dependent module genesis functionality (AppModuleGenesis)
- inter-dependent module full functionality (AppModule)
inter-dependent module functionality is module functionality which somehow
depends on other modules, typically through the module keeper. Many of the
@@ -17,72 +18,114 @@ process. This separation is necessary, however we still want to allow for a
high level pattern for modules to follow - for instance, such that we don't
have to manually register all of the codecs for all the modules. This basic
procedure as well as other basic patterns are handled through the use of
ModuleBasicManager.
BasicManager.
Lastly the interface for genesis functionality (AppModuleGenesis) has been
separated out from full module functionality (AppModule) so that modules which
are only used for genesis can take advantage of the Module patterns without
needlessly defining many placeholder functions
*/
package types
package module
import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
)
// ModuleClient helps modules provide a standard interface for exporting client functionality
type ModuleClient interface {
GetQueryCmd() *cobra.Command
GetTxCmd() *cobra.Command
}
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
//__________________________________________________________________________________________
// AppModuleBasic is the standard form for basic non-dependant elements of an application module.
type AppModuleBasic interface {
Name() string
RegisterCodec(*codec.Codec)
// genesis
DefaultGenesis() json.RawMessage
ValidateGenesis(json.RawMessage) error
// client functionality
RegisterRESTRoutes(context.CLIContext, *mux.Router, *codec.Codec)
GetTxCmd(*codec.Codec) *cobra.Command
GetQueryCmd(*codec.Codec) *cobra.Command
}
// collections of AppModuleBasic
type ModuleBasicManager []AppModuleBasic
type BasicManager map[string]AppModuleBasic
func NewModuleBasicManager(modules ...AppModuleBasic) ModuleBasicManager {
return modules
func NewBasicManager(modules ...AppModuleBasic) BasicManager {
moduleMap := make(map[string]AppModuleBasic)
for _, module := range modules {
moduleMap[module.Name()] = module
}
return moduleMap
}
// RegisterCodecs registers all module codecs
func (mbm ModuleBasicManager) RegisterCodec(cdc *codec.Codec) {
for _, mb := range mbm {
mb.RegisterCodec(cdc)
func (bm BasicManager) RegisterCodec(cdc *codec.Codec) {
for _, b := range bm {
b.RegisterCodec(cdc)
}
}
// Provided default genesis information for all modules
func (mbm ModuleBasicManager) DefaultGenesis() map[string]json.RawMessage {
func (bm BasicManager) DefaultGenesis() map[string]json.RawMessage {
genesis := make(map[string]json.RawMessage)
for _, mb := range mbm {
genesis[mb.Name()] = mb.DefaultGenesis()
for _, b := range bm {
genesis[b.Name()] = b.DefaultGenesis()
}
return genesis
}
// Provided default genesis information for all modules
func (mbm ModuleBasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error {
for _, mb := range mbm {
if err := mb.ValidateGenesis(genesis[mb.Name()]); err != nil {
func (bm BasicManager) ValidateGenesis(genesis map[string]json.RawMessage) error {
for _, b := range bm {
if err := b.ValidateGenesis(genesis[b.Name()]); err != nil {
return err
}
}
return nil
}
// RegisterRestRoutes registers all module rest routes
func (bm BasicManager) RegisterRESTRoutes(
ctx context.CLIContext, rtr *mux.Router, cdc *codec.Codec) {
for _, b := range bm {
b.RegisterRESTRoutes(ctx, rtr, cdc)
}
}
// add 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 {
rootTxCmd.AddCommand(cmd)
}
}
}
// add 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 {
rootQueryCmd.AddCommand(cmd)
}
}
}
//_________________________________________________________
// AppModuleGenesis is the standard form for an application module genesis functions
type AppModuleGenesis interface {
AppModuleBasic
InitGenesis(Context, json.RawMessage) []abci.ValidatorUpdate
ExportGenesis(Context) json.RawMessage
InitGenesis(sdk.Context, json.RawMessage) []abci.ValidatorUpdate
ExportGenesis(sdk.Context) json.RawMessage
}
// AppModule is the standard form for an application module
@@ -90,16 +133,16 @@ type AppModule interface {
AppModuleGenesis
// registers
RegisterInvariants(InvariantRouter)
RegisterInvariants(sdk.InvariantRouter)
// routes
Route() string
NewHandler() Handler
NewHandler() sdk.Handler
QuerierRoute() string
NewQuerierHandler() Querier
NewQuerierHandler() sdk.Querier
BeginBlock(Context, abci.RequestBeginBlock) Tags
EndBlock(Context, abci.RequestEndBlock) ([]abci.ValidatorUpdate, Tags)
BeginBlock(sdk.Context, abci.RequestBeginBlock) sdk.Tags
EndBlock(sdk.Context, abci.RequestEndBlock) ([]abci.ValidatorUpdate, sdk.Tags)
}
//___________________________
@@ -116,34 +159,34 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule {
}
// register invariants
func (GenesisOnlyAppModule) RegisterInvariants(_ InvariantRouter) {}
func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRouter) {}
// module message route ngame
func (GenesisOnlyAppModule) Route() string { return "" }
// module handler
func (GenesisOnlyAppModule) NewHandler() Handler { return nil }
func (GenesisOnlyAppModule) NewHandler() sdk.Handler { return nil }
// module querier route ngame
func (GenesisOnlyAppModule) QuerierRoute() string { return "" }
// module querier
func (gam GenesisOnlyAppModule) NewQuerierHandler() Querier { return nil }
func (gam GenesisOnlyAppModule) NewQuerierHandler() sdk.Querier { return nil }
// module begin-block
func (gam GenesisOnlyAppModule) BeginBlock(ctx Context, req abci.RequestBeginBlock) Tags {
return EmptyTags()
func (gam GenesisOnlyAppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) sdk.Tags {
return sdk.EmptyTags()
}
// module end-block
func (GenesisOnlyAppModule) EndBlock(_ Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, Tags) {
return []abci.ValidatorUpdate{}, EmptyTags()
func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) ([]abci.ValidatorUpdate, sdk.Tags) {
return []abci.ValidatorUpdate{}, sdk.EmptyTags()
}
//____________________________________________________________________________
// module manager provides the high level utility for managing and executing
// operations for a group of modules
type ModuleManager struct {
type Manager struct {
Modules map[string]AppModule
OrderInitGenesis []string
OrderExportGenesis []string
@@ -151,8 +194,8 @@ type ModuleManager struct {
OrderEndBlockers []string
}
// NewModuleManager creates a new ModuleManager object
func NewModuleManager(modules ...AppModule) *ModuleManager {
// NewModuleManager creates a new Manager object
func NewManager(modules ...AppModule) *Manager {
moduleMap := make(map[string]AppModule)
var modulesStr []string
@@ -161,7 +204,7 @@ func NewModuleManager(modules ...AppModule) *ModuleManager {
modulesStr = append(modulesStr, module.Name())
}
return &ModuleManager{
return &Manager{
Modules: moduleMap,
OrderInitGenesis: modulesStr,
OrderExportGenesis: modulesStr,
@@ -171,35 +214,35 @@ func NewModuleManager(modules ...AppModule) *ModuleManager {
}
// set the order of init genesis calls
func (mm *ModuleManager) SetOrderInitGenesis(moduleNames ...string) {
mm.OrderInitGenesis = moduleNames
func (m *Manager) SetOrderInitGenesis(moduleNames ...string) {
m.OrderInitGenesis = moduleNames
}
// set the order of export genesis calls
func (mm *ModuleManager) SetOrderExportGenesis(moduleNames ...string) {
mm.OrderExportGenesis = moduleNames
func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
m.OrderExportGenesis = moduleNames
}
// set the order of set begin-blocker calls
func (mm *ModuleManager) SetOrderBeginBlockers(moduleNames ...string) {
mm.OrderBeginBlockers = moduleNames
func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
m.OrderBeginBlockers = moduleNames
}
// set the order of set end-blocker calls
func (mm *ModuleManager) SetOrderEndBlockers(moduleNames ...string) {
mm.OrderEndBlockers = moduleNames
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
m.OrderEndBlockers = moduleNames
}
// register all module routes and module querier routes
func (mm *ModuleManager) RegisterInvariants(invarRouter InvariantRouter) {
for _, module := range mm.Modules {
func (m *Manager) RegisterInvariants(invarRouter sdk.InvariantRouter) {
for _, module := range m.Modules {
module.RegisterInvariants(invarRouter)
}
}
// register all module routes and module querier routes
func (mm *ModuleManager) RegisterRoutes(router Router, queryRouter QueryRouter) {
for _, module := range mm.Modules {
func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) {
for _, module := range m.Modules {
if module.Route() != "" {
router.AddRoute(module.Route(), module.NewHandler())
}
@@ -210,13 +253,13 @@ func (mm *ModuleManager) RegisterRoutes(router Router, queryRouter QueryRouter)
}
// perform init genesis functionality for modules
func (mm *ModuleManager) InitGenesis(ctx Context, genesisData map[string]json.RawMessage) abci.ResponseInitChain {
func (m *Manager) InitGenesis(ctx sdk.Context, genesisData map[string]json.RawMessage) abci.ResponseInitChain {
var validatorUpdates []abci.ValidatorUpdate
for _, moduleName := range mm.OrderInitGenesis {
for _, moduleName := range m.OrderInitGenesis {
if genesisData[moduleName] == nil {
continue
}
moduleValUpdates := mm.Modules[moduleName].InitGenesis(ctx, genesisData[moduleName])
moduleValUpdates := m.Modules[moduleName].InitGenesis(ctx, genesisData[moduleName])
// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
@@ -233,19 +276,19 @@ func (mm *ModuleManager) InitGenesis(ctx Context, genesisData map[string]json.Ra
}
// perform export genesis functionality for modules
func (mm *ModuleManager) ExportGenesis(ctx Context) map[string]json.RawMessage {
func (m *Manager) ExportGenesis(ctx sdk.Context) map[string]json.RawMessage {
genesisData := make(map[string]json.RawMessage)
for _, moduleName := range mm.OrderExportGenesis {
genesisData[moduleName] = mm.Modules[moduleName].ExportGenesis(ctx)
for _, moduleName := range m.OrderExportGenesis {
genesisData[moduleName] = m.Modules[moduleName].ExportGenesis(ctx)
}
return genesisData
}
// perform begin block functionality for modules
func (mm *ModuleManager) BeginBlock(ctx Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
tags := EmptyTags()
for _, moduleName := range mm.OrderBeginBlockers {
moduleTags := mm.Modules[moduleName].BeginBlock(ctx, req)
func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
tags := sdk.EmptyTags()
for _, moduleName := range m.OrderBeginBlockers {
moduleTags := m.Modules[moduleName].BeginBlock(ctx, req)
tags = tags.AppendTags(moduleTags)
}
@@ -255,11 +298,11 @@ func (mm *ModuleManager) BeginBlock(ctx Context, req abci.RequestBeginBlock) abc
}
// perform end block functionality for modules
func (mm *ModuleManager) EndBlock(ctx Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
validatorUpdates := []abci.ValidatorUpdate{}
tags := EmptyTags()
for _, moduleName := range mm.OrderEndBlockers {
moduleValUpdates, moduleTags := mm.Modules[moduleName].EndBlock(ctx, req)
tags := sdk.EmptyTags()
for _, moduleName := range m.OrderEndBlockers {
moduleValUpdates, moduleTags := m.Modules[moduleName].EndBlock(ctx, req)
tags = tags.AppendTags(moduleTags)
// use these validator updates if provided, the module manager assumes
@@ -1,4 +1,4 @@
package types
package module
import (
"testing"
@@ -8,7 +8,7 @@ import (
)
func TestSetOrderBeginBlockers(t *testing.T) {
mm := NewModuleManager()
mm := NewManager()
mm.SetOrderBeginBlockers("a", "b", "c")
obb := mm.OrderBeginBlockers
require.Equal(t, 3, len(obb))