From 28046faf0ffb583c74eaaf5efc888f294dc56f82 Mon Sep 17 00:00:00 2001 From: sampocs Date: Mon, 24 Jun 2024 15:42:48 -0500 Subject: [PATCH] added wasm light client (#1224) --- app/app.go | 35 ++++++++++++++++++++++++++++++++++- app/upgrades.go | 6 ++++++ app/upgrades/v23/upgrades.go | 13 +++++++++++++ go.mod | 3 ++- go.sum | 2 ++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/app/app.go b/app/app.go index e88dd86a1e..51c7fa7b9d 100644 --- a/app/app.go +++ b/app/app.go @@ -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" @@ -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" @@ -227,6 +231,7 @@ var ( stakedym.AppModuleBasic{}, wasm.AppModuleBasic{}, ibchooks.AppModuleBasic{}, + ibcwasm.AppModuleBasic{}, ) // module account permissions @@ -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 @@ -391,6 +397,7 @@ func NewStrideApp( stakedymtypes.StoreKey, wasmtypes.StoreKey, ibchookstypes.StoreKey, + ibcwasmtypes.StoreKey, ) tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey) @@ -542,7 +549,8 @@ 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) @@ -550,6 +558,18 @@ func NewStrideApp( 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, @@ -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, @@ -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, @@ -930,6 +960,7 @@ func NewStrideApp( stakedymtypes.ModuleName, wasmtypes.ModuleName, ibchookstypes.ModuleName, + ibcwasmtypes.ModuleName, ) app.mm.SetOrderEndBlockers( @@ -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 @@ -1013,6 +1045,7 @@ func NewStrideApp( stakedymtypes.ModuleName, wasmtypes.ModuleName, ibchookstypes.ModuleName, + ibcwasmtypes.ModuleName, ) app.mm.RegisterInvariants(app.CrisisKeeper) diff --git a/app/upgrades.go b/app/upgrades.go index 0be2a76e7c..dea4436e07 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -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" @@ -303,6 +304,7 @@ func (app *StrideApp) setupUpgradeHandlers(appOpts servertypes.AppOptions) { v23.CreateUpgradeHandler( app.mm, app.configurator, + app.IBCKeeper.ClientKeeper, app.StakeibcKeeper, ), ) @@ -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 { diff --git a/app/upgrades/v23/upgrades.go b/app/upgrades/v23/upgrades.go index 09cd1cb815..77583a514b 100644 --- a/app/upgrades/v23/upgrades.go +++ b/app/upgrades/v23/upgrades.go @@ -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" ) @@ -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) @@ -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) { diff --git a/go.mod b/go.mod index 99a6ea2f1f..7bd6a8a824 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 37c2e51634..57467631e8 100644 --- a/go.sum +++ b/go.sum @@ -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=