diff --git a/app/app.go b/app/app.go index 42df2cdf1..1ac1367c4 100644 --- a/app/app.go +++ b/app/app.go @@ -191,7 +191,7 @@ type AxelarApp struct { interfaceRegistry types.InterfaceRegistry - mm *module.Manager + mm *FilteredModuleManager } // NewAxelarApp is a constructor function for axelar @@ -308,7 +308,7 @@ func NewAxelarApp( ), ) - mm := module.NewManager(appModules...) + mm := NewFilteredModuleManager(appModules, []string{vestingtypes.ModuleName}) mm.SetOrderMigrations(orderMigrations()...) mm.SetOrderBeginBlockers(orderBeginBlockers()...) mm.SetOrderEndBlockers(orderEndBlockers()...) diff --git a/app/module_manager.go b/app/module_manager.go new file mode 100644 index 000000000..1426acedd --- /dev/null +++ b/app/module_manager.go @@ -0,0 +1,57 @@ +package app + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/axelarnetwork/utils/slices" +) + +type FilteredModuleManager struct { + *module.Manager + filteredModules []string +} + +func NewFilteredModuleManager(appModules []module.AppModule, filteredModules []string) *FilteredModuleManager { + manager := module.NewManager(appModules...) + + return &FilteredModuleManager{ + manager, + filteredModules, + } +} + +// RegisterRoutes registers all module routes and module querier routes +func (m *FilteredModuleManager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter, legacyQuerierCdc *codec.LegacyAmino) { + for _, module := range m.Modules { + if m.isModuleFiltered(module.Name()) { + continue + } + + if r := module.Route(); !r.Empty() { + router.AddRoute(r) + } + if r := module.QuerierRoute(); r != "" { + queryRouter.AddRoute(r, module.LegacyQuerierHandler(legacyQuerierCdc)) + } + + } +} + +// RegisterServices registers all module services +func (m *FilteredModuleManager) RegisterServices(cfg module.Configurator) { + for _, module := range m.Modules { + if m.isModuleFiltered(module.Name()) { + continue + } + + module.RegisterServices(cfg) + } +} + +func (m *FilteredModuleManager) isModuleFiltered(moduleName string) bool { + return slices.Any(m.filteredModules, func(s string) bool { + return s == moduleName + }) +} diff --git a/app/module_manager_test.go b/app/module_manager_test.go new file mode 100644 index 000000000..129edc713 --- /dev/null +++ b/app/module_manager_test.go @@ -0,0 +1,57 @@ +package app_test + +import ( + "testing" + + bam "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/tests/mocks" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + + "github.com/axelarnetwork/axelar-core/app" +) + +func TestFilteredModuleManager_RegisterServices(t *testing.T) { + encodingConfig := app.MakeEncodingConfig() + configurator := module.NewConfigurator(encodingConfig.Codec, bam.NewMsgServiceRouter(), bam.NewGRPCQueryRouter()) + mockCtrl := gomock.NewController(t) + + mockAppModule1 := mocks.NewMockAppModule(mockCtrl) + mockAppModule2 := mocks.NewMockAppModule(mockCtrl) + + mockAppModule1.EXPECT().Name().Times(3).Return("module1") + mockAppModule2.EXPECT().Name().Times(3).Return("module2") + + mockAppModule1.EXPECT().RegisterServices(configurator).Times(1) + mockAppModule2.EXPECT().RegisterServices(configurator).Times(0) + + mm := app.NewFilteredModuleManager([]module.AppModule{mockAppModule1, mockAppModule2}, []string{"module2"}) + mm.RegisterServices(configurator) + +} + +func TestFilteredModuleManager_RegisterRoutes(t *testing.T) { + encodingConfig := app.MakeEncodingConfig() + mockCtrl := gomock.NewController(t) + + mockAppModule1 := mocks.NewMockAppModule(mockCtrl) + mockAppModule2 := mocks.NewMockAppModule(mockCtrl) + + mockAppModule1.EXPECT().Name().Times(3).Return("module1") + mockAppModule2.EXPECT().Name().Times(3).Return("module2") + + mm := app.NewFilteredModuleManager([]module.AppModule{mockAppModule1, mockAppModule2}, []string{"module2"}) + + router := bam.NewRouter() + queryRouter := bam.NewQueryRouter() + noopHandler := sdk.Handler(func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { return nil, nil }) + + mockAppModule1.EXPECT().QuerierRoute().Times(1) + mockAppModule1.EXPECT().Route().Times(1).Return(sdk.NewRoute("route1", noopHandler)) + mockAppModule2.EXPECT().Route().Times(0) + + mm.RegisterRoutes(router, queryRouter, encodingConfig.Amino) + assert.Nil(t, router.Route(sdk.Context{}, "route2")) +} diff --git a/go.mod b/go.mod index 664e57526..4b159d71d 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/ethereum/go-ethereum v1.10.26 github.com/go-errors/errors v1.5.1 github.com/gogo/protobuf v1.3.3 + github.com/golang/mock v1.6.0 github.com/golang/protobuf v1.5.3 github.com/gorilla/mux v1.8.1 github.com/grpc-ecosystem/grpc-gateway v1.16.0