diff --git a/docs/docs/building-modules/16-testing.md b/docs/docs/building-modules/16-testing.md index b8bf14dd67..fee66b0dc4 100644 --- a/docs/docs/building-modules/16-testing.md +++ b/docs/docs/building-modules/16-testing.md @@ -66,7 +66,7 @@ The SDK provides small helpers for quickly setting up an integration tests. Thes ### Example ```go reference -https://github.com/cosmos/cosmos-sdk/blob/29e22b3bdb05353555c8e0b269311bbff7b8deca/testutil/integration/example_test.go#L22-L89 +https://github.com/cosmos/cosmos-sdk/blob/a2f73a7dd37bea0ab303792c55fa1e4e1db3b898/testutil/integration/example_test.go#L30-L116 ``` ## Deterministic and Regression tests diff --git a/tests/integration/bank/keeper/deterministic_test.go b/tests/integration/bank/keeper/deterministic_test.go index 2a6fc11b00..9dd5c7d94f 100644 --- a/tests/integration/bank/keeper/deterministic_test.go +++ b/tests/integration/bank/keeper/deterministic_test.go @@ -7,6 +7,7 @@ import ( "gotest.tools/v3/assert" "pgregory.net/rapid" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" @@ -100,7 +101,10 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil) bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil) - integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule) + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + }) sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index a3a38a0a7e..00520a3b7f 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" "gotest.tools/v3/assert" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -123,7 +124,12 @@ func initFixture(t testing.TB) *fixture { }, }) - integrationApp := integration.NewIntegrationApp(ctx, logger, keys, cdc, authModule, bankModule, stakingModule, distrModule) + integrationApp := integration.NewIntegrationApp(ctx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + stakingtypes.ModuleName: stakingModule, + distrtypes.ModuleName: distrModule, + }) sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index e11127dfcd..590be3863b 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -11,6 +11,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "gotest.tools/v3/assert" + "cosmossdk.io/core/appmodule" "cosmossdk.io/core/comet" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" @@ -134,7 +135,13 @@ func initFixture(t testing.TB) *fixture { slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, nil, cdc.InterfaceRegistry()) evidenceModule := evidence.NewAppModule(*evidenceKeeper) - integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule, stakingModule, slashingModule, evidenceModule) + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + stakingtypes.ModuleName: stakingModule, + slashingtypes.ModuleName: slashingModule, + evidencetypes.ModuleName: evidenceModule, + }) sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index 23204b633c..4421d890ac 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -6,6 +6,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "gotest.tools/v3/assert" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" @@ -129,7 +130,13 @@ func initFixture(t testing.TB) *fixture { distrModule := distribution.NewAppModule(cdc, distrKeeper, accountKeeper, bankKeeper, stakingKeeper, nil) govModule := gov.NewAppModule(cdc, govKeeper, accountKeeper, bankKeeper, nil) - integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule, stakingModule, distrModule, govModule) + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + distrtypes.ModuleName: distrModule, + stakingtypes.ModuleName: stakingModule, + types.ModuleName: govModule, + }) sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index a496ed5fc8..d59830ca46 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/require" "gotest.tools/v3/assert" + "cosmossdk.io/core/appmodule" "cosmossdk.io/core/comet" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" @@ -98,7 +99,11 @@ func initFixture(t testing.TB) *fixture { stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil) slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, nil, cdc.InterfaceRegistry()) - integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, bankModule, stakingModule, slashingModule) + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + banktypes.ModuleName: bankModule, + stakingtypes.ModuleName: stakingModule, + slashingtypes.ModuleName: slashingModule, + }) sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index 7eb68f1eca..56ccf1a5f8 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -7,6 +7,7 @@ import ( cmtprototypes "github.com/cometbft/cometbft/proto/tendermint/types" "gotest.tools/v3/assert" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -138,7 +139,11 @@ func initFixture(t testing.TB) *fixture { bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil) stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil) - integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule, stakingModule) + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + types.ModuleName: stakingModule, + }) sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) diff --git a/tests/integration/staking/keeper/determinstic_test.go b/tests/integration/staking/keeper/determinstic_test.go index acca2b0cf9..8963da00f0 100644 --- a/tests/integration/staking/keeper/determinstic_test.go +++ b/tests/integration/staking/keeper/determinstic_test.go @@ -8,6 +8,7 @@ import ( "gotest.tools/v3/assert" "pgregory.net/rapid" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -111,7 +112,11 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil) stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil) - integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule, stakingModule) + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + stakingtypes.ModuleName: stakingModule, + }) ctx := integrationApp.Context() diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index 532efe452c..b395324c19 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -7,6 +7,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/google/go-cmp/cmp" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" @@ -63,7 +64,10 @@ func Example() { logger, keys, encodingCfg.Codec, - authModule, mintModule, + map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + minttypes.ModuleName: mintModule, + }, ) // register the message and query servers @@ -144,7 +148,9 @@ func Example_oneModule() { logger, keys, encodingCfg.Codec, - authModule, + map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + }, ) // register the message and query servers diff --git a/testutil/integration/router.go b/testutil/integration/router.go index 8b8040455f..4270d493ad 100644 --- a/testutil/integration/router.go +++ b/testutil/integration/router.go @@ -8,6 +8,7 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" dbm "github.com/cosmos/cosmos-db" + "cosmossdk.io/core/appmodule" "cosmossdk.io/log" "cosmossdk.io/store" "cosmossdk.io/store/metrics" @@ -45,14 +46,14 @@ func NewIntegrationApp( logger log.Logger, keys map[string]*storetypes.KVStoreKey, appCodec codec.Codec, - modules ...module.AppModule, + modules map[string]appmodule.AppModule, ) *App { db := dbm.NewMemDB() interfaceRegistry := codectypes.NewInterfaceRegistry() - for _, module := range modules { - module.RegisterInterfaces(interfaceRegistry) - } + moduleManager := module.NewManagerFromMap(modules) + basicModuleManager := module.NewBasicManagerFromManager(moduleManager, nil) + basicModuleManager.RegisterInterfaces(interfaceRegistry) txConfig := authtx.NewTxConfig(codec.NewProtoCodec(interfaceRegistry), authtx.DefaultSignModes) bApp := baseapp.NewBaseApp(appName, logger, db, txConfig.TxDecoder(), baseapp.SetChainID(appName)) @@ -68,7 +69,6 @@ func NewIntegrationApp( return &cmtabcitypes.ResponseInitChain{}, nil }) - moduleManager := module.NewManager(modules...) bApp.SetBeginBlocker(func(_ sdk.Context) (sdk.BeginBlock, error) { return moduleManager.BeginBlock(sdkCtx) }) diff --git a/types/module/core_module.go b/types/module/core_module.go index 561645d0e5..3f8fa88af9 100644 --- a/types/module/core_module.go +++ b/types/module/core_module.go @@ -18,27 +18,27 @@ import ( ) var ( - _ AppModuleBasic = coreAppModuleBasicAdapator{} - _ HasGenesis = coreAppModuleBasicAdapator{} - _ HasServices = coreAppModuleBasicAdapator{} + _ AppModuleBasic = coreAppModuleBasicAdaptor{} + _ HasGenesis = coreAppModuleBasicAdaptor{} + _ HasServices = coreAppModuleBasicAdaptor{} ) // CoreAppModuleBasicAdaptor wraps the core API module as an AppModule that this version // of the SDK can use. func CoreAppModuleBasicAdaptor(name string, module appmodule.AppModule) AppModuleBasic { - return coreAppModuleBasicAdapator{ + return coreAppModuleBasicAdaptor{ name: name, module: module, } } -type coreAppModuleBasicAdapator struct { +type coreAppModuleBasicAdaptor struct { name string module appmodule.AppModule } // DefaultGenesis implements HasGenesis -func (c coreAppModuleBasicAdapator) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { +func (c coreAppModuleBasicAdaptor) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { if mod, ok := c.module.(appmodule.HasGenesis); ok { target := genesis.RawJSONTarget{} err := mod.DefaultGenesis(target.Target()) @@ -62,7 +62,7 @@ func (c coreAppModuleBasicAdapator) DefaultGenesis(cdc codec.JSONCodec) json.Raw } // ValidateGenesis implements HasGenesis -func (c coreAppModuleBasicAdapator) ValidateGenesis(cdc codec.JSONCodec, txConfig client.TxEncodingConfig, bz json.RawMessage) error { +func (c coreAppModuleBasicAdaptor) ValidateGenesis(cdc codec.JSONCodec, txConfig client.TxEncodingConfig, bz json.RawMessage) error { if mod, ok := c.module.(appmodule.HasGenesis); ok { source, err := genesis.SourceFromRawJSON(bz) if err != nil { @@ -82,7 +82,7 @@ func (c coreAppModuleBasicAdapator) ValidateGenesis(cdc codec.JSONCodec, txConfi } // ExportGenesis implements HasGenesis -func (c coreAppModuleBasicAdapator) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { +func (c coreAppModuleBasicAdaptor) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { if module, ok := c.module.(appmodule.HasGenesis); ok { ctx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) // avoid race conditions target := genesis.RawJSONTarget{} @@ -107,7 +107,7 @@ func (c coreAppModuleBasicAdapator) ExportGenesis(ctx sdk.Context, cdc codec.JSO } // InitGenesis implements HasGenesis -func (c coreAppModuleBasicAdapator) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate { +func (c coreAppModuleBasicAdaptor) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate { if module, ok := c.module.(appmodule.HasGenesis); ok { // core API genesis source, err := genesis.SourceFromRawJSON(bz) @@ -129,12 +129,12 @@ func (c coreAppModuleBasicAdapator) InitGenesis(ctx sdk.Context, cdc codec.JSONC } // Name implements AppModuleBasic -func (c coreAppModuleBasicAdapator) Name() string { +func (c coreAppModuleBasicAdaptor) Name() string { return c.name } // GetQueryCmd implements AppModuleBasic -func (c coreAppModuleBasicAdapator) GetQueryCmd() *cobra.Command { +func (c coreAppModuleBasicAdaptor) GetQueryCmd() *cobra.Command { if mod, ok := c.module.(interface { GetQueryCmd() *cobra.Command }); ok { @@ -145,7 +145,7 @@ func (c coreAppModuleBasicAdapator) GetQueryCmd() *cobra.Command { } // GetTxCmd implements AppModuleBasic -func (c coreAppModuleBasicAdapator) GetTxCmd() *cobra.Command { +func (c coreAppModuleBasicAdaptor) GetTxCmd() *cobra.Command { if mod, ok := c.module.(interface { GetTxCmd() *cobra.Command }); ok { @@ -156,7 +156,7 @@ func (c coreAppModuleBasicAdapator) GetTxCmd() *cobra.Command { } // RegisterGRPCGatewayRoutes implements AppModuleBasic -func (c coreAppModuleBasicAdapator) RegisterGRPCGatewayRoutes(ctx client.Context, mux *runtime.ServeMux) { +func (c coreAppModuleBasicAdaptor) RegisterGRPCGatewayRoutes(ctx client.Context, mux *runtime.ServeMux) { if mod, ok := c.module.(interface { RegisterGRPCGatewayRoutes(context client.Context, mux *runtime.ServeMux) }); ok { @@ -165,7 +165,7 @@ func (c coreAppModuleBasicAdapator) RegisterGRPCGatewayRoutes(ctx client.Context } // RegisterInterfaces implements AppModuleBasic -func (c coreAppModuleBasicAdapator) RegisterInterfaces(registry codectypes.InterfaceRegistry) { +func (c coreAppModuleBasicAdaptor) RegisterInterfaces(registry codectypes.InterfaceRegistry) { if mod, ok := c.module.(interface { RegisterInterfaces(registry codectypes.InterfaceRegistry) }); ok { @@ -174,7 +174,7 @@ func (c coreAppModuleBasicAdapator) RegisterInterfaces(registry codectypes.Inter } // RegisterLegacyAminoCodec implements AppModuleBasic -func (c coreAppModuleBasicAdapator) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { +func (c coreAppModuleBasicAdaptor) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { if mod, ok := c.module.(interface { RegisterLegacyAminoCodec(amino *codec.LegacyAmino) }); ok { @@ -183,7 +183,7 @@ func (c coreAppModuleBasicAdapator) RegisterLegacyAminoCodec(amino *codec.Legacy } // RegisterServices implements HasServices -func (c coreAppModuleBasicAdapator) RegisterServices(cfg Configurator) { +func (c coreAppModuleBasicAdaptor) RegisterServices(cfg Configurator) { if module, ok := c.module.(appmodule.HasServices); ok { err := module.RegisterServices(cfg) if err != nil {