feat!: Comet v0.38 Integration (#15519)

Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
Aleksandr Bezobchuk
2023-05-24 16:09:19 +00:00
committed by GitHub
co-authored by marbar3778 cool-developer Aaron Craelius Matt Kocubinski Julien Robert
parent 166be3766b
commit 6cee22df52
148 changed files with 14747 additions and 15262 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ func Example_oneModule() {
Params: params,
},
// this allows to the begin and end blocker of the module before and after the message
integration.WithAutomaticBeginEndBlock(),
integration.WithAutomaticFinalizeBlock(),
// this allows to commit the state after the message
integration.WithAutomaticCommit(),
)
+4 -4
View File
@@ -2,17 +2,17 @@ package integration
// Config is the configuration for the integration app.
type Config struct {
AutomaticBeginEndBlock bool
AutomaticFinalizeBlock bool
AutomaticCommit bool
}
// Option is a function that can be used to configure the integration app.
type Option func(*Config)
// WithAutomaticBlockCreation enables begin/end block calls.
func WithAutomaticBeginEndBlock() Option {
// WithAutomaticFinalizeBlock calls ABCI finalize block.
func WithAutomaticFinalizeBlock() Option {
return func(cfg *Config) {
cfg.AutomaticBeginEndBlock = true
cfg.AutomaticFinalizeBlock = true
}
}
+45 -35
View File
@@ -4,13 +4,12 @@ import (
"context"
"fmt"
cmtabcitypes "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
cmtabcitypes "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
@@ -32,13 +31,21 @@ const appName = "integration-app"
type App struct {
*baseapp.BaseApp
ctx sdk.Context
logger log.Logger
queryHelper *baseapp.QueryServiceTestHelper
ctx sdk.Context
logger log.Logger
moduleManager module.Manager
queryHelper *baseapp.QueryServiceTestHelper
}
// NewIntegrationApp creates an application for testing purposes. This application is able to route messages to their respective handlers.
func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*storetypes.KVStoreKey, appCodec codec.Codec, modules ...module.AppModule) *App {
// NewIntegrationApp creates an application for testing purposes. This application
// is able to route messages to their respective handlers.
func NewIntegrationApp(
sdkCtx sdk.Context,
logger log.Logger,
keys map[string]*storetypes.KVStoreKey,
appCodec codec.Codec,
modules ...module.AppModule,
) *App {
db := dbm.NewMemDB()
interfaceRegistry := codectypes.NewInterfaceRegistry()
@@ -50,22 +57,22 @@ func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*s
bApp := baseapp.NewBaseApp(appName, logger, db, txConfig.TxDecoder(), baseapp.SetChainID(appName))
bApp.MountKVStores(keys)
bApp.SetInitChainer(func(ctx sdk.Context, req cmtabcitypes.RequestInitChain) (cmtabcitypes.ResponseInitChain, error) {
bApp.SetInitChainer(func(ctx sdk.Context, _ *cmtabcitypes.RequestInitChain) (*cmtabcitypes.ResponseInitChain, error) {
for _, mod := range modules {
if m, ok := mod.(module.HasGenesis); ok {
m.InitGenesis(ctx, appCodec, m.DefaultGenesis(appCodec))
}
}
return cmtabcitypes.ResponseInitChain{}, nil
return &cmtabcitypes.ResponseInitChain{}, nil
})
moduleManager := module.NewManager(modules...)
bApp.SetBeginBlocker(func(_ sdk.Context, req cmtabcitypes.RequestBeginBlock) (cmtabcitypes.ResponseBeginBlock, error) {
return moduleManager.BeginBlock(sdkCtx, req)
bApp.SetBeginBlocker(func(_ sdk.Context) (sdk.BeginBlock, error) {
return moduleManager.BeginBlock(sdkCtx)
})
bApp.SetEndBlocker(func(_ sdk.Context, req cmtabcitypes.RequestEndBlock) (cmtabcitypes.ResponseEndBlock, error) {
return moduleManager.EndBlock(sdkCtx, req)
bApp.SetEndBlocker(func(_ sdk.Context) (sdk.EndBlock, error) {
return moduleManager.EndBlock(sdkCtx)
})
router := baseapp.NewMsgServiceRouter()
@@ -73,7 +80,6 @@ func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*s
bApp.SetMsgServiceRouter(router)
if keys[consensusparamtypes.StoreKey] != nil {
// set baseApp param store
consensusParamsKeeper := consensusparamkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), authtypes.NewModuleAddress("gov").String(), runtime.EventService{})
bApp.SetParamStore(consensusParamsKeeper.ParamsStore)
@@ -81,13 +87,18 @@ func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*s
if err := bApp.LoadLatestVersion(); err != nil {
panic(fmt.Errorf("failed to load application version from store: %w", err))
}
bApp.InitChain(cmtabcitypes.RequestInitChain{ChainId: appName, ConsensusParams: simtestutil.DefaultConsensusParams})
if _, err := bApp.InitChain(&cmtabcitypes.RequestInitChain{ChainId: appName, ConsensusParams: simtestutil.DefaultConsensusParams}); err != nil {
panic(fmt.Errorf("failed to initialize application: %w", err))
}
} else {
if err := bApp.LoadLatestVersion(); err != nil {
panic(fmt.Errorf("failed to load application version from store: %w", err))
}
bApp.InitChain(cmtabcitypes.RequestInitChain{ChainId: appName})
if _, err := bApp.InitChain(&cmtabcitypes.RequestInitChain{ChainId: appName}); err != nil {
panic(fmt.Errorf("failed to initialize application: %w", err))
}
}
bApp.Commit()
@@ -95,39 +106,36 @@ func NewIntegrationApp(sdkCtx sdk.Context, logger log.Logger, keys map[string]*s
ctx := sdkCtx.WithBlockHeader(cmtproto.Header{ChainID: appName}).WithIsCheckTx(true)
return &App{
BaseApp: bApp,
logger: logger,
ctx: ctx,
queryHelper: baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry),
BaseApp: bApp,
logger: logger,
ctx: ctx,
moduleManager: *moduleManager,
queryHelper: baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry),
}
}
// RunMsg allows to run a message and return the response.
// RunMsg provides the ability to run a message and return the response.
// In order to run a message, the application must have a handler for it.
// These handlers are registered on the application message service router.
// The result of the message execution is returned as a Any type.
// The result of the message execution is returned as an Any type.
// That any type can be unmarshaled to the expected response type.
// If the message execution fails, an error is returned.
func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) {
// set options
cfg := Config{}
cfg := &Config{}
for _, opt := range option {
opt(&cfg)
opt(cfg)
}
if cfg.AutomaticCommit {
defer app.Commit()
}
if cfg.AutomaticBeginEndBlock {
if cfg.AutomaticFinalizeBlock {
height := app.LastBlockHeight() + 1
app.logger.Info("Running beging block", "height", height)
app.BeginBlock(cmtabcitypes.RequestBeginBlock{Header: cmtproto.Header{Height: height, ChainID: appName}})
defer func() {
app.logger.Info("Running end block", "height", height)
app.EndBlock(cmtabcitypes.RequestEndBlock{})
}()
if _, err := app.FinalizeBlock(&cmtabcitypes.RequestFinalizeBlock{Height: height}); err != nil {
return nil, fmt.Errorf("failed to run finalize block: %w", err)
}
}
app.logger.Info("Running msg", "msg", msg.String())
@@ -155,8 +163,8 @@ func (app *App) RunMsg(msg sdk.Msg, option ...Option) (*codectypes.Any, error) {
return response, nil
}
// Context returns the application context.
// It can be unwraped to a sdk.Context, with the sdk.UnwrapSDKContext function.
// Context returns the application context. It can be unwrapped to a sdk.Context,
// with the sdk.UnwrapSDKContext function.
func (app *App) Context() context.Context {
return app.ctx
}
@@ -171,9 +179,11 @@ func (app *App) QueryHelper() *baseapp.QueryServiceTestHelper {
func CreateMultiStore(keys map[string]*storetypes.KVStoreKey, logger log.Logger) storetypes.CommitMultiStore {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, logger, metrics.NewNoOpMetrics())
for key := range keys {
cms.MountStoreWithDB(keys[key], storetypes.StoreTypeIAVL, db)
}
_ = cms.LoadLatestVersion()
return cms
}