Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasmd): integrate wasmd #2223

Open
wants to merge 4 commits into
base: robert/cosmos-sdk-047
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package app

import (
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
"github.com/cosmos/ibc-go/v7/modules/core/keeper"

errorsmod "cosmossdk.io/errors"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions

IBCKeeper *keeper.Keeper
WasmKeeper *wasmkeeper.Keeper
WasmConfig *wasmTypes.WasmConfig
TXCounterStoreKey storetypes.StoreKey
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
}
if options.BankKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if options.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}
if options.WasmConfig == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}
if options.TXCounterStoreKey == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit), // after setup context to enforce limits early
wasmkeeper.NewCountTXDecorator(options.TXCounterStoreKey),
wasmkeeper.NewGasRegisterDecorator(options.WasmKeeper.GetGasRegister()),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
}
122 changes: 104 additions & 18 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"math/big"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/CosmWasm/wasmd/x/wasm"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
Expand All @@ -19,6 +23,8 @@

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -189,15 +195,14 @@
ica.AppModuleBasic{},
ibcfee.AppModuleBasic{},
intertxmodule.AppModule{},
wasm.AppModuleBasic{},
)

// module account permissions
maccPerms = func() map[string][]string {
perms := map[string][]string{
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
icatypes.ModuleName: nil,
ibcfeetypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
Expand All @@ -206,6 +211,11 @@
ecocredit.ModuleName: {authtypes.Burner},
baskettypes.BasketSubModuleName: {authtypes.Burner, authtypes.Minter},
marketplace.FeePoolName: {authtypes.Burner},

// non sdk
icatypes.ModuleName: nil,
ibcfeetypes.ModuleName: nil,
wasmtypes.ModuleName: {authtypes.Burner},
}

return perms
Expand Down Expand Up @@ -250,25 +260,30 @@
FeeGrantKeeper feegrantkeeper.Keeper
GovKeeper *govkeeper.Keeper
GroupKeeper groupkeeper.Keeper
IBCFeeKeeper ibcfeekeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCTransferKeeper ibctransferkeeper.Keeper
ICAControllerKeeper icacontrollerkeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper
InterTxKeeper intertxkeeper.Keeper
MintKeeper mintkeeper.Keeper
ParamsKeeper paramskeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper

IBCFeeKeeper ibcfeekeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCTransferKeeper ibctransferkeeper.Keeper
ICAControllerKeeper icacontrollerkeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper

WasmKeeper wasmkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedIBCTransferKeeper capabilitykeeper.ScopedKeeper
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper
ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper
ScopedInterTxKeeper capabilitykeeper.ScopedKeeper

ScopedWasmKeeper capabilitykeeper.ScopedKeeper

// the module manager
ModuleManager *module.Manager

Expand All @@ -283,6 +298,7 @@
func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
invCheckPeriod uint,
appOpts servertypes.AppOptions,
wasmOpts []wasmkeeper.Option,
baseAppOptions ...func(*baseapp.BaseApp)) *RegenApp {

encCfg := MakeEncodingConfig()
Expand All @@ -306,6 +322,7 @@
ibcexported.StoreKey, ibctransfertypes.StoreKey,
icahosttypes.StoreKey, ibcfeetypes.StoreKey, icacontrollertypes.StoreKey,
ecocredit.ModuleName, data.ModuleName, intertx.ModuleName,
wasmtypes.StoreKey,
)

tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -352,6 +369,9 @@
app.ScopedICAControllerKeeper = app.CapabilityKeeper.ScopeToModule(icacontrollertypes.SubModuleName)
app.ScopedInterTxKeeper = app.CapabilityKeeper.ScopeToModule(intertx.ModuleName)

// grant capabilities for wasm modules
app.ScopedWasmKeeper = app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName)

// Applications that wish to enforce statically created ScopedKeepers should call `Seal` after creating
// their scoped modules in `NewApp` with `ScopeToModule`
app.CapabilityKeeper.Seal()
Expand Down Expand Up @@ -514,6 +534,40 @@
app.ScopedInterTxKeeper,
)

// Wasm Keepr
wasmDir := filepath.Join(homePath, "wasm")
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error while reading wasm config: %s", err))
}
availableCapabilities := strings.Join(AllCapabilities(), ",")
// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
app.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
keys[wasmtypes.StoreKey],
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
distrkeeper.NewQuerier(app.DistrKeeper),
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.ScopedWasmKeeper,
app.IBCTransferKeeper,
app.MsgServiceRouter(),
app.GRPCQueryRouter(),
wasmDir,
wasmConfig,
availableCapabilities,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
wasmOpts...,
)
// Create fee enabled wasm ibc Stack
var wasmStack porttypes.IBCModule
wasmStack = wasm.NewIBCHandler(app.WasmKeeper, app.IBCKeeper.ChannelKeeper, app.IBCFeeKeeper)
wasmStack = ibcfee.NewIBCMiddleware(wasmStack, app.IBCFeeKeeper)

// Create IBC stacks to add to IBC router

interTxModule := intertxmodule.NewModule(app.InterTxKeeper)
Expand All @@ -534,7 +588,8 @@
AddRoute(ibctransfertypes.ModuleName, ibcTransferStack).
AddRoute(intertx.ModuleName, icaControllerStack).
AddRoute(icacontrollertypes.SubModuleName, icaControllerStack).
AddRoute(icahosttypes.SubModuleName, icaHostStack)
AddRoute(icahosttypes.SubModuleName, icaHostStack).
AddRoute(wasmtypes.ModuleName, wasmStack)
app.IBCKeeper.SetRouter(ibcRouter)

// Register the proposal types
Expand Down Expand Up @@ -595,6 +650,8 @@
params.NewAppModule(app.ParamsKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
//
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
ibctransfer.NewAppModule(app.IBCTransferKeeper),
ecocreditMod,
dataMod,
Expand Down Expand Up @@ -635,6 +692,9 @@
icatypes.ModuleName,
ibcfeetypes.ModuleName,
intertx.ModuleName,

// wasm module
wasmtypes.ModuleName,
)
app.ModuleManager.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand Down Expand Up @@ -663,6 +723,9 @@
icatypes.ModuleName,
ibcfeetypes.ModuleName,
intertx.ModuleName,

// wasm module
wasmtypes.ModuleName,
)
// NOTE: The genutils module must occur after staking so that pools are
// properly initialized with tokens from genesis accounts.
Expand Down Expand Up @@ -696,6 +759,9 @@
icatypes.ModuleName,
ibcfeetypes.ModuleName,
intertx.ModuleName,

// wasm module
wasmtypes.ModuleName,
)

app.ModuleManager.RegisterInvariants(app.CrisisKeeper)
Expand Down Expand Up @@ -742,30 +808,49 @@
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.setAnteHandler(txConfig)
app.setAnteHandler(txConfig, wasmConfig, keys[wasmtypes.StoreKey])
app.setPostHandler()

//
if manager := app.SnapshotManager(); manager != nil {
err = manager.RegisterExtensions(wasmkeeper.NewWasmSnapshotter(app.CommitMultiStore(), &app.WasmKeeper))
if err != nil {
panic("failed to register snapshot extension: " + err.Error())
}
}

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}

ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})
if err := app.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
tmos.Exit(fmt.Sprintf("WasmKeeper failed initialize pinned codes %s", err))
}
}

return app
}

func (app *RegenApp) setAnteHandler(txConfig client.TxConfig) {
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
func (app *RegenApp) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, txCounterStoreKey storetypes.StoreKey) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
SignModeHandler: txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCKeeper: app.IBCKeeper,
WasmConfig: &wasmConfig,
TXCounterStoreKey: txCounterStoreKey,
WasmKeeper: &app.WasmKeeper,
},
)
if err != nil {
panic(err)
panic(fmt.Errorf("failed to create AnteHandler: %s", err))
}
app.SetAnteHandler(anteHandler)
}
Expand Down Expand Up @@ -848,7 +933,7 @@
}

// DefaultGenesis returns a default genesis from the registered AppModuleBasic's.
func (a *RegenApp) DefaultGenesis() map[string]json.RawMessage {

Check warning on line 936 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci (app)

receiver-naming: receiver name a should be consistent with previous receiver name app for RegenApp (revive)
return ModuleBasics.DefaultGenesis(a.appCodec)
}

Expand Down Expand Up @@ -986,6 +1071,7 @@
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(ibcfeetypes.ModuleName)
paramsKeeper.Subspace(icacontrollertypes.SubModuleName)
paramsKeeper.Subspace(wasmtypes.ModuleName)

return paramsKeeper
}
Loading
Loading