refactor: use appmodule.AppModule in integration helpers (backport #16755) (#16756)

Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot] 2023-06-29 11:01:43 +00:00 committed by GitHub
parent c452721442
commit d805e631e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 76 additions and 31 deletions

View File

@ -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

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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())

View File

@ -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()

View File

@ -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

View File

@ -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)
})

View File

@ -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 {