Skip to content

Commit

Permalink
added wasm light client (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs committed Jun 24, 2024
1 parent 0849e93 commit 28046fa
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
35 changes: 34 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
wasmvm "github.com/CosmWasm/wasmvm"
"github.com/Stride-Labs/ibc-rate-limiting/ratelimit"
ratelimitkeeper "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/keeper"
ratelimittypes "github.com/Stride-Labs/ibc-rate-limiting/ratelimit/types"
Expand Down Expand Up @@ -88,6 +89,9 @@ import (
ibchooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7"
ibchookskeeper "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/keeper"
ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/types"
ibcwasm "github.com/cosmos/ibc-go/modules/light-clients/08-wasm"
ibcwasmkeeper "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper"
ibcwasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
icacontroller "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller"
icacontrollerkeeper "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/keeper"
Expand Down Expand Up @@ -227,6 +231,7 @@ var (
stakedym.AppModuleBasic{},
wasm.AppModuleBasic{},
ibchooks.AppModuleBasic{},
ibcwasm.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -311,6 +316,7 @@ type StrideApp struct {
WasmKeeper wasmkeeper.Keeper
ContractKeeper *wasmkeeper.PermissionedKeeper
IBCHooksKeeper ibchookskeeper.Keeper
WasmClientKeeper ibcwasmkeeper.Keeper

// Middleware for IBCHooks
Ics20WasmHooks *ibchooks.WasmHooks
Expand Down Expand Up @@ -391,6 +397,7 @@ func NewStrideApp(
stakedymtypes.StoreKey,
wasmtypes.StoreKey,
ibchookstypes.StoreKey,
ibcwasmtypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -542,14 +549,27 @@ func NewStrideApp(
// Set the TransferKeeper reference in the PacketForwardKeeper
app.PacketForwardKeeper.SetTransferKeeper(app.TransferKeeper)

// Add wasm keeper (must be after IBCKeeper and TransferKeeper)
// Add wasm keeper and wasm client keeper (must be after IBCKeeper and TransferKeeper)
wasmContractMemoryLimit := uint32(32)
wasmCapabilities := "iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,cosmwasm_1_4"
wasmDir := filepath.Join(homePath, "wasm")
wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error while reading wasm config: %s", err))
}

wasmer, err := wasmvm.NewVM(
wasmDir,
wasmCapabilities,
wasmContractMemoryLimit,
wasmConfig.ContractDebugMode,
wasmConfig.MemoryCacheSize,
)
if err != nil {
panic(err)
}
wasmOpts = append(wasmOpts, wasmkeeper.WithWasmEngine(wasmer))

scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName)
app.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
Expand All @@ -573,6 +593,15 @@ func NewStrideApp(
)
app.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper)

app.WasmClientKeeper = ibcwasmkeeper.NewKeeperWithVM(
appCodec,
keys[ibcwasmtypes.StoreKey],
app.IBCKeeper.ClientKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
wasmer,
app.GRPCQueryRouter(),
)

// Create evidence Keeper for to register the IBC light client misbehaviour evidence route
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], &app.ConsumerKeeper, app.SlashingKeeper,
Expand Down Expand Up @@ -874,6 +903,7 @@ func NewStrideApp(
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.BaseApp.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)),
ibchooks.NewAppModule(app.AccountKeeper),
transfer.NewAppModule(app.TransferKeeper),
ibcwasm.NewAppModule(app.WasmClientKeeper),
// monitoringModule,
stakeibcModule,
epochsModule,
Expand Down Expand Up @@ -930,6 +960,7 @@ func NewStrideApp(
stakedymtypes.ModuleName,
wasmtypes.ModuleName,
ibchookstypes.ModuleName,
ibcwasmtypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand Down Expand Up @@ -969,6 +1000,7 @@ func NewStrideApp(
stakedymtypes.ModuleName,
wasmtypes.ModuleName,
ibchookstypes.ModuleName,
ibcwasmtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -1013,6 +1045,7 @@ func NewStrideApp(
stakedymtypes.ModuleName,
wasmtypes.ModuleName,
ibchookstypes.ModuleName,
ibcwasmtypes.ModuleName,
)

app.mm.RegisterInvariants(app.CrisisKeeper)
Expand Down
6 changes: 6 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types"
ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/types"
ibcwasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
consumertypes "github.com/cosmos/interchain-security/v4/x/ccv/consumer/types"
evmosvestingtypes "github.com/evmos/vesting/x/vesting/types"

Expand Down Expand Up @@ -303,6 +304,7 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
v23.CreateUpgradeHandler(
app.mm,
app.configurator,
app.IBCKeeper.ClientKeeper,
app.StakeibcKeeper,
),
)
Expand Down Expand Up @@ -364,6 +366,10 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{ibchookstypes.StoreKey},
}
case "v23":
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{ibcwasmtypes.ModuleName},
}
}

if storeUpgrades != nil {
Expand Down
13 changes: 13 additions & 0 deletions app/upgrades/v23/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibcwasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
clientkeeper "github.com/cosmos/ibc-go/v7/modules/core/02-client/keeper"

stakeibckeeper "github.com/Stride-Labs/stride/v22/x/stakeibc/keeper"
)
Expand All @@ -16,11 +18,15 @@ var (
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
clientKeeper clientkeeper.Keeper,
stakeibcKeeper stakeibckeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade v23...")

ctx.Logger().Info("Adding wasm client...")
AddWasmAllowedClient(ctx, clientKeeper)

ctx.Logger().Info("Migrating trade routes...")
MigrateTradeRoutes(ctx, stakeibcKeeper)

Expand All @@ -29,6 +35,13 @@ func CreateUpgradeHandler(
}
}

// Add the wasm client to the IBC client's allowed clients
func AddWasmAllowedClient(ctx sdk.Context, k clientkeeper.Keeper) {
params := k.GetParams(ctx)
params.AllowedClients = append(params.AllowedClients, ibcwasmtypes.Wasm)
k.SetParams(ctx, params)
}

// Migration to deprecate the trade config
// The min transfer amount can be set from the min swap amount
func MigrateTradeRoutes(ctx sdk.Context, k stakeibckeeper.Keeper) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
github.com/CosmWasm/wasmd v0.45.0
github.com/CosmWasm/wasmvm v1.5.2
github.com/Stride-Labs/ibc-rate-limiting v1.0.0
github.com/cometbft/cometbft v0.37.4
github.com/cometbft/cometbft-db v0.8.0
Expand All @@ -16,6 +17,7 @@ require (
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.3-0.20240228213828-cce7f56d000b
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20240403143657-8e64543c87e0
github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.1.2-0.20240412103620-7ee2a2452b79
github.com/cosmos/ibc-go/v7 v7.4.0
github.com/cosmos/ics23/go v0.10.0
github.com/cosmos/interchain-security/v4 v4.0.0
Expand Down Expand Up @@ -49,7 +51,6 @@ require (
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/CosmWasm/wasmvm v1.5.2 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.3-0.2024
github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.3-0.20240228213828-cce7f56d000b/go.mod h1:UvDmcGIWJPIytq+Q78/ff5NTOsuX/7IrNgEugTW5i0s=
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20240403143657-8e64543c87e0 h1:obD+CSx+aXpMEUWHLyMkWEcxLuYAcxvQaIXTwjjh3qI=
github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20240403143657-8e64543c87e0/go.mod h1:JwHFbo1oX/ht4fPpnPvmhZr+dCkYK1Vihw+vZE9umR4=
github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.1.2-0.20240412103620-7ee2a2452b79 h1:Y7zi+3BksgH5kCMLFfC2vghihNoQVrt3Ft+sHtgywPQ=
github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.1.2-0.20240412103620-7ee2a2452b79/go.mod h1:VR2Hg2i/X1bafbmmNsV2Khwsg0PzNeuWoVKmSN5dAwo=
github.com/cosmos/ibc-go/v7 v7.4.0 h1:8FqYMptvksgMvlbN4UW9jFxTXzsPyfAzEZurujXac8M=
github.com/cosmos/ibc-go/v7 v7.4.0/go.mod h1:L/KaEhzV5TGUCTfGysVgMBQtl5Dm7hHitfpk+GIeoAo=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
Expand Down

0 comments on commit 28046fa

Please sign in to comment.