Skip to content

Commit

Permalink
add wasmKeeper and IBCFee, and wasm ante
Browse files Browse the repository at this point in the history
  • Loading branch information
lacsomot committed Mar 11, 2024
1 parent b3b0cc5 commit 675f162
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 30 deletions.
13 changes: 13 additions & 0 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
storetypes "github.com/cosmos/cosmos-sdk/store/types"

ibcante "github.com/cosmos/ibc-go/v6/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v6/modules/core/keeper"

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

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
ante.HandlerOptions

WasmConfig *wasmTypes.WasmConfig
IBCKeeper *ibckeeper.Keeper
TXCounterStoreKey storetypes.StoreKey
}

func GetAnteDecorators(options HandlerOptions) []sdk.AnteDecorator {
Expand All @@ -25,6 +31,9 @@ func GetAnteDecorators(options HandlerOptions) []sdk.AnteDecorator {
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),

ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),

ante.NewValidateBasicDecorator(),
Expand Down Expand Up @@ -54,6 +63,10 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
}

if options.WasmConfig == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}

if options.BankKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder")
}
Expand Down
110 changes: 105 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package app

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
Expand Down Expand Up @@ -78,6 +80,9 @@ import (
epochskeeper "github.com/dymensionxyz/dymension-rdk/x/epochs/keeper"
epochstypes "github.com/dymensionxyz/dymension-rdk/x/epochs/types"

ibcfee "github.com/cosmos/ibc-go/v6/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v6/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v6/modules/apps/29-fee/types"
ibctransfer "github.com/cosmos/ibc-go/v6/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v6/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
Expand All @@ -92,6 +97,10 @@ import (

rollappparams "github.com/dymensionxyz/rollapp/app/params"

"github.com/CosmWasm/wasmd/x/wasm"
wasmclient "github.com/CosmWasm/wasmd/x/wasm/client"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"

Expand Down Expand Up @@ -120,13 +129,15 @@ var (
ibchost.StoreKey, upgradetypes.StoreKey,
epochstypes.StoreKey,
ibctransfertypes.StoreKey, capabilitytypes.StoreKey,
wasm.StoreKey, ibcfeetypes.StoreKey,
}
)

func getGovProposalHandlers() []govclient.ProposalHandler {
var govProposalHandlers []govclient.ProposalHandler

govProposalHandlers = append(govProposalHandlers,
govProposalHandlers = append(
wasmclient.ProposalHandlers,
paramsclient.ProposalHandler,
distrclient.ProposalHandler,
upgradeclient.LegacyProposalHandler,
Expand All @@ -138,6 +149,33 @@ func getGovProposalHandlers() []govclient.ProposalHandler {
return govProposalHandlers
}

var (
// If EnabledSpecificProposals is "", and this is "true", then enable all x/wasm proposals.
// If EnabledSpecificProposals is "", and this is not "true", then disable all x/wasm proposals.
ProposalsEnabled = "false"
// If set to non-empty string it must be comma-separated list of values that are all a subset
// of "EnableAllProposals" (takes precedence over ProposalsEnabled)
// https://github.com/CosmWasm/wasmd/blob/02a54d33ff2c064f3539ae12d75d027d9c665f05/x/wasm/internal/types/proposal.go#L28-L34
EnableSpecificProposals = ""
)

// GetEnabledProposals parses the ProposalsEnabled / EnableSpecificProposals values to
// produce a list of enabled proposals to pass into wasmd app.
func GetEnabledProposals() []wasm.ProposalType {
if EnableSpecificProposals == "" {
if ProposalsEnabled == "true" {
return wasm.EnableAllProposals
}
return wasm.DisableAllProposals
}
chunks := strings.Split(EnableSpecificProposals, ",")
proposals, err := wasm.ConvertToProposals(chunks)
if err != nil {
panic(err)
}
return proposals
}

var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
Expand All @@ -161,6 +199,8 @@ var (
ibc.AppModuleBasic{},
ibctransfer.AppModuleBasic{},
vesting.AppModuleBasic{},
wasm.AppModuleBasic{},
ibcfee.AppModuleBasic{},
)

// module account permissions
Expand All @@ -172,6 +212,8 @@ var (
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
wasm.ModuleName: {authtypes.Burner},
ibcfeetypes.ModuleName: nil,
}
)

Expand Down Expand Up @@ -219,11 +261,15 @@ type App struct {
UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCFeeKeeper ibcfeekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
WasmKeeper wasm.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper
ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper

// mm is the module manager
mm *module.Manager
Expand All @@ -245,7 +291,9 @@ func NewRollapp(
homePath string,
invCheckPeriod uint,
encodingConfig rollappparams.EncodingConfig,
enabledProposals []wasm.ProposalType,
appOpts servertypes.AppOptions,
wasmOpts []wasm.Option,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {

Expand Down Expand Up @@ -301,6 +349,7 @@ func NewRollapp(
// their scoped modules in `NewApp` with `ScopeToModule`
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibchost.ModuleName)
scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasm.ModuleName)
app.CapabilityKeeper.Seal()

// add keepers
Expand Down Expand Up @@ -396,18 +445,62 @@ func NewRollapp(
),
)

// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec, keys[ibcfeetypes.StoreKey],
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper,
)

// Create Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper,
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
scopedTransferKeeper,
)

wasmDir := filepath.Join(homePath, "wasm")
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error while reading wasm config: %s", err))
}

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
availableCapabilities := "iterator,staking,stargate,cosmwasm_1_1"
app.WasmKeeper = wasm.NewKeeper(
appCodec,
keys[wasm.StoreKey],
app.GetSubspace(wasm.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
app.DistrKeeper,
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
scopedWasmKeeper,
app.TransferKeeper,
app.MsgServiceRouter(),
app.GRPCQueryRouter(),
wasmDir,
wasmConfig,
availableCapabilities,
wasmOpts...,
)

// The gov proposal types can be individually enabled
if len(enabledProposals) != 0 {
govRouter.AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.WasmKeeper, enabledProposals))
}

transferIBCModule := ibctransfer.NewIBCModule(app.TransferKeeper)

// Create static IBC router, add transfer route, then set and seal it
Expand All @@ -433,6 +526,7 @@ func NewRollapp(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
sequencers.NewAppModule(appCodec, app.SequencersKeeper, app.AccountKeeper, app.BankKeeper),
epochs.NewAppModule(appCodec, app.EpochsKeeper),
params.NewAppModule(app.ParamsKeeper),
Expand Down Expand Up @@ -464,6 +558,7 @@ func NewRollapp(
genutiltypes.ModuleName,
epochstypes.ModuleName,
paramstypes.ModuleName,
wasm.ModuleName,
}
app.mm.SetOrderBeginBlockers(beginBlockersList...)

Expand All @@ -483,6 +578,7 @@ func NewRollapp(
upgradetypes.ModuleName,
ibchost.ModuleName,
ibctransfertypes.ModuleName,
wasm.ModuleName,
}
app.mm.SetOrderEndBlockers(endBlockersList...)

Expand All @@ -508,6 +604,7 @@ func NewRollapp(
epochstypes.ModuleName,
upgradetypes.ModuleName,
ibctransfertypes.ModuleName,
wasm.ModuleName,
}
app.mm.SetOrderInitGenesis(initGenesisList...)

Expand Down Expand Up @@ -542,7 +639,7 @@ func NewRollapp(
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.setAnteHandler(encodingConfig.TxConfig)
app.setAnteHandler(encodingConfig.TxConfig, wasmConfig, keys)
// In v0.46, the SDK introduces _postHandlers_. PostHandlers are like
// antehandlers, but are run _after_ the `runMsgs` execution. They are also
// defined as a chain, and have the same signature as antehandlers.
Expand Down Expand Up @@ -570,7 +667,7 @@ func NewRollapp(
return app
}

func (app *App) setAnteHandler(txConfig client.TxConfig) {
func (app *App) setAnteHandler(txConfig client.TxConfig, wasmConfig wasmtypes.WasmConfig, keys map[string]*storetypes.KVStoreKey) {
anteHandler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
Expand All @@ -579,7 +676,9 @@ func (app *App) setAnteHandler(txConfig client.TxConfig) {
SignModeHandler: txConfig.SignModeHandler(),
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCKeeper: app.IBCKeeper,
IBCKeeper: app.IBCKeeper,
WasmConfig: &wasmConfig,
TXCounterStoreKey: keys[wasm.StoreKey],
},
)
if err != nil {
Expand Down Expand Up @@ -817,6 +916,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable())
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(wasm.ModuleName)

return paramsKeeper
}
14 changes: 14 additions & 0 deletions cmd/rollappd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
"github.com/spf13/cobra"
tmcfg "github.com/tendermint/tendermint/config"
tmcli "github.com/tendermint/tendermint/libs/cli"
tmlog "github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"

rdkserver "github.com/dymensionxyz/dymension-rdk/server"
"github.com/dymensionxyz/dymension-rdk/utils"
sequencercli "github.com/dymensionxyz/dymension-rdk/x/sequencers/client/cli"
Expand Down Expand Up @@ -242,6 +246,11 @@ func (ac appCreator) newApp(
skipUpgradeHeights[int64(h)] = true
}

var wasmOpts []wasm.Option
if cast.ToBool(appOpts.Get("telemetry.enabled")) {
wasmOpts = append(wasmOpts, wasmkeeper.WithVMCacheMetrics(prometheus.DefaultRegisterer))
}

return app.NewRollapp(
logger,
db,
Expand All @@ -251,7 +260,9 @@ func (ac appCreator) newApp(
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
ac.encCfg,
app.GetEnabledProposals(),
appOpts,
wasmOpts,
baseappOptions...)
}

Expand All @@ -271,6 +282,7 @@ func (ac appCreator) appExport(
}

loadLatest := height == -1
var emptyWasmOpts []wasm.Option
rollapp = app.NewRollapp(
logger,
db,
Expand All @@ -280,7 +292,9 @@ func (ac appCreator) appExport(
homePath,
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
ac.encCfg,
app.GetEnabledProposals(),
appOpts,
emptyWasmOpts,
)

if height != -1 {
Expand Down
Loading

0 comments on commit 675f162

Please sign in to comment.