Skip to content

Commit

Permalink
refactor: use appmodule.AppModule in integration helpers (#16755)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jun 29, 2023
1 parent 2bbc3a1 commit f8c8de4
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/docs/building-modules/16-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gotest.tools/v3/assert"
"pgregory.net/rapid"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

Expand Down Expand Up @@ -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())

Expand Down
8 changes: 7 additions & 1 deletion tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gotest.tools/v3/assert"

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -124,7 +125,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())

Expand Down
9 changes: 8 additions & 1 deletion tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down
9 changes: 8 additions & 1 deletion tests/integration/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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())

Expand Down
7 changes: 6 additions & 1 deletion tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down
7 changes: 6 additions & 1 deletion tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down
7 changes: 6 additions & 1 deletion tests/integration/staking/keeper/determinstic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
10 changes: 8 additions & 2 deletions testutil/integration/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions testutil/integration/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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)
})
Expand Down
32 changes: 16 additions & 16 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit f8c8de4

Please sign in to comment.