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

[Main] Wasm Module #465

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
29 changes: 20 additions & 9 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package band

import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

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

corestoretypes "cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -23,15 +28,17 @@ import (
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions
Cdc codec.Codec
AuthzKeeper *authzkeeper.Keeper
OracleKeeper *oraclekeeper.Keeper
IBCKeeper *ibckeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
GlobalfeeKeeper *globalfeekeeper.Keeper
TSSKeeper *tsskeeper.Keeper
BandtssKeeper *bandtsskeeper.Keeper
FeedsKeeper *feedskeeper.Keeper
Cdc codec.Codec
AuthzKeeper *authzkeeper.Keeper
OracleKeeper *oraclekeeper.Keeper
IBCKeeper *ibckeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
GlobalfeeKeeper *globalfeekeeper.Keeper
TSSKeeper *tsskeeper.Keeper
BandtssKeeper *bandtsskeeper.Keeper
FeedsKeeper *feedskeeper.Keeper
TXCounterStoreService corestoretypes.KVStoreService
WasmConfig *wasmtypes.WasmConfig
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
Expand Down Expand Up @@ -93,6 +100,10 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

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.TXCounterStoreService),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
Expand Down
44 changes: 35 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"os"
"path/filepath"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/spf13/cast"

abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtos "github.com/cometbft/cometbft/libs/os"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -112,6 +116,7 @@ func NewBandApp(
skipUpgradeHeights map[int64]bool,
homePath string,
appOpts servertypes.AppOptions,
wasmOpts []wasmkeeper.Option,
owasmCacheSize uint32,
baseAppOptions ...func(*baseapp.BaseApp),
) *BandApp {
Expand Down Expand Up @@ -176,6 +181,7 @@ func NewBandApp(
invCheckPeriod,
logger,
appOpts,
wasmOpts,
owasmCacheSize,
)

Expand Down Expand Up @@ -258,6 +264,11 @@ func NewBandApp(
app.MountTransientStores(app.GetTransientStoreKey())
app.MountMemoryStores(app.GetMemoryStoreKey())

wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic("error while reading wasm config: " + err.Error())
}

anteHandler, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
Expand All @@ -267,15 +278,17 @@ func NewBandApp(
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
Cdc: app.appCodec,
AuthzKeeper: &app.AuthzKeeper,
OracleKeeper: &app.OracleKeeper,
TSSKeeper: app.TSSKeeper,
BandtssKeeper: &app.BandtssKeeper,
FeedsKeeper: &app.FeedsKeeper,
IBCKeeper: app.IBCKeeper,
StakingKeeper: app.StakingKeeper,
GlobalfeeKeeper: &app.GlobalFeeKeeper,
Cdc: app.appCodec,
AuthzKeeper: &app.AuthzKeeper,
OracleKeeper: &app.OracleKeeper,
TSSKeeper: app.TSSKeeper,
BandtssKeeper: &app.BandtssKeeper,
FeedsKeeper: &app.FeedsKeeper,
IBCKeeper: app.IBCKeeper,
StakingKeeper: app.StakingKeeper,
GlobalfeeKeeper: &app.GlobalFeeKeeper,
WasmConfig: &wasmConfig,
TXCounterStoreService: runtime.NewKVStoreService(app.AppKeepers.GetKey(wasmtypes.StoreKey)),
},
)
if err != nil {
Expand Down Expand Up @@ -306,6 +319,13 @@ func NewBandApp(
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
}

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

app.setupUpgradeHandlers()
Expand All @@ -328,6 +348,12 @@ func NewBandApp(
if err := app.LoadLatestVersion(); err != nil {
cmtos.Exit(fmt.Sprintf("failed to load latest version: %s", err))
}

ctx := app.BaseApp.NewUncachedContext(true, cmtproto.Header{})

if err := app.AppKeepers.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
cmtos.Exit(fmt.Sprintf("WasmKeeper failed initialize pinned codes %s", err))
}
}

return app
Expand Down
4 changes: 4 additions & 0 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"time"

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

"github.com/cosmos/ibc-go/modules/capability"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
icagenesistypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/genesis/types"
Expand Down Expand Up @@ -136,5 +139,6 @@ func NewDefaultGenesisState(cdc codec.Codec) GenesisState {
tunneltypes.ModuleName: tunnel.AppModuleBasic{}.DefaultGenesis(cdc),
globalfeetypes.ModuleName: cdc.MustMarshalJSON(globalfeeGenesis),
restaketypes.ModuleName: restake.AppModuleBasic{}.DefaultGenesis(cdc),
wasmtypes.ModuleName: wasm.AppModuleBasic{}.DefaultGenesis(cdc),
}
}
46 changes: 44 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"os"
"path/filepath"

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

capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
Expand Down Expand Up @@ -122,6 +126,7 @@ type AppKeepers struct {
ConsensusParamsKeeper consensusparamkeeper.Keeper
GlobalFeeKeeper globalfeekeeper.Keeper
RestakeKeeper restakekeeper.Keeper
WasmKeeper wasmkeeper.Keeper
// IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCKeeper *ibckeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper
Expand All @@ -137,6 +142,7 @@ type AppKeepers struct {
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper
ScopedOracleKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper
}

func NewAppKeeper(
Expand All @@ -151,6 +157,7 @@ func NewAppKeeper(
invCheckPeriod uint,
logger log.Logger,
appOpts servertypes.AppOptions,
wasmOpts []wasmkeeper.Option,
owasmCacheSize uint32,
) AppKeepers {
appKeepers := AppKeepers{}
Expand Down Expand Up @@ -197,6 +204,7 @@ func NewAppKeeper(
appKeepers.ScopedICAHostKeeper = appKeepers.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName)
appKeepers.ScopedTransferKeeper = appKeepers.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
appKeepers.ScopedOracleKeeper = appKeepers.CapabilityKeeper.ScopeToModule(oracletypes.ModuleName)
appKeepers.ScopedWasmKeeper = appKeepers.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName)

// Applications that wish to enforce statically created ScopedKeepers should call `Seal` after creating
// their scoped modules in `NewApp` with `ScopeToModule`
Expand Down Expand Up @@ -430,6 +438,33 @@ func NewAppKeeper(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

wasmDir := homePath
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic("error while reading wasm config: " + err.Error())
}

appKeepers.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(appKeepers.keys[wasmtypes.StoreKey]),
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.StakingKeeper,
distrkeeper.NewQuerier(appKeepers.DistrKeeper),
appKeepers.IBCFeeKeeper,
appKeepers.IBCKeeper.ChannelKeeper,
appKeepers.IBCKeeper.PortKeeper,
appKeepers.ScopedWasmKeeper,
appKeepers.TransferKeeper,
bApp.MsgServiceRouter(),
bApp.GRPCQueryRouter(),
wasmDir,
wasmConfig,
wasmkeeper.BuiltInCapabilities(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
wasmOpts...,
)

owasmVM, err := owasm.NewVm(owasmCacheSize)
if err != nil {
panic(err)
Expand Down Expand Up @@ -532,12 +567,18 @@ func NewAppKeeper(
icaHostStack = icahost.NewIBCModule(appKeepers.ICAHostKeeper)
icaHostStack = ibcfee.NewIBCMiddleware(icaHostStack, appKeepers.IBCFeeKeeper)

var wasmStack porttypes.IBCModule
wasmStack = wasm.NewIBCHandler(appKeepers.WasmKeeper, appKeepers.IBCKeeper.ChannelKeeper, appKeepers.IBCFeeKeeper)
wasmStack = ibcfee.NewIBCMiddleware(wasmStack, appKeepers.IBCFeeKeeper)

// Create Oracle Stack
var oracleStack porttypes.IBCModule = oracle.NewIBCModule(appKeepers.OracleKeeper)

ibcRouter := porttypes.NewRouter().AddRoute(icahosttypes.SubModuleName, icaHostStack).
ibcRouter := porttypes.NewRouter().
AddRoute(icahosttypes.SubModuleName, icaHostStack).
AddRoute(ibctransfertypes.ModuleName, transferStack).
AddRoute(oracletypes.ModuleName, oracleStack)
AddRoute(oracletypes.ModuleName, oracleStack).
AddRoute(wasmtypes.ModuleName, wasmStack)

appKeepers.IBCKeeper.SetRouter(ibcRouter)

Expand Down Expand Up @@ -583,6 +624,7 @@ func initParamsKeeper(
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(oracletypes.ModuleName)
paramsKeeper.Subspace(wasmtypes.ModuleName)

return paramsKeeper
}
3 changes: 3 additions & 0 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keepers

import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
Expand Down Expand Up @@ -67,6 +69,7 @@ func (appKeepers *AppKeepers) GenerateKeys() {
tsstypes.StoreKey,
rollingseedtypes.StoreKey,
tunneltypes.StoreKey,
wasmtypes.StoreKey,
)

// Define transient store keys
Expand Down
29 changes: 28 additions & 1 deletion app/modules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package band

import (
wasm "github.com/CosmWasm/wasmd/x/wasm"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

"github.com/cosmos/ibc-go/modules/capability"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
Expand Down Expand Up @@ -84,6 +87,7 @@ var maccPerms = map[string][]string{
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
ibcfeetypes.ModuleName: nil,
bandtsstypes.ModuleName: nil,
wasmtypes.ModuleName: {authtypes.Burner},
restaketypes.ModuleName: nil,
tunneltypes.ModuleName: nil,
}
Expand Down Expand Up @@ -164,6 +168,15 @@ func appModules(
app.TransferModule,
app.ICAModule,
globalfee.NewAppModule(app.GlobalFeeKeeper),
wasm.NewAppModule(
appCodec,
&app.AppKeepers.WasmKeeper,
app.AppKeepers.StakingKeeper,
app.AppKeepers.AccountKeeper,
app.AppKeepers.BankKeeper,
app.MsgServiceRouter(),
app.GetSubspace(wasmtypes.ModuleName),
),
ibcfee.NewAppModule(app.IBCFeeKeeper),
restake.NewAppModule(appCodec, app.RestakeKeeper),
feeds.NewAppModule(appCodec, app.FeedsKeeper),
Expand Down Expand Up @@ -265,6 +278,15 @@ func simulationModules(
app.StakingKeeper,
app.GetSubspace(oracletypes.ModuleName),
),
wasm.NewAppModule(
appCodec,
&app.AppKeepers.WasmKeeper,
app.AppKeepers.StakingKeeper,
app.AppKeepers.AccountKeeper,
app.AppKeepers.BankKeeper,
app.MsgServiceRouter(),
app.GetSubspace(wasmtypes.ModuleName),
),
ibc.NewAppModule(app.IBCKeeper),
app.TransferModule,
}
Expand Down Expand Up @@ -301,6 +323,7 @@ func orderBeginBlockers() []string {
vestingtypes.ModuleName,
consensusparamtypes.ModuleName,
globalfeetypes.ModuleName,
wasmtypes.ModuleName,
}
}

Expand Down Expand Up @@ -344,6 +367,7 @@ func orderEndBlockers() []string {
vestingtypes.ModuleName,
consensusparamtypes.ModuleName,
globalfeetypes.ModuleName,
wasmtypes.ModuleName,
}
}

Expand All @@ -365,7 +389,6 @@ func orderInitBlockers() []string {
stakingtypes.ModuleName,
slashingtypes.ModuleName,
minttypes.ModuleName,
crisistypes.ModuleName,
genutiltypes.ModuleName,
ibctransfertypes.ModuleName,
ibcexported.ModuleName,
Expand All @@ -387,5 +410,9 @@ func orderInitBlockers() []string {
restaketypes.ModuleName,
feedstypes.ModuleName,
tunneltypes.ModuleName,
wasmtypes.ModuleName,
// crisis needs to be last so that the genesis state is consistent
// when it checks invariants
crisistypes.ModuleName,
}
}
Loading
Loading