From 7b69b55286e45298995e1213e3cee819ce2fdc5e Mon Sep 17 00:00:00 2001 From: Marri Harish Date: Wed, 6 Mar 2024 17:08:08 +0530 Subject: [PATCH] feat: add ibc-hooks middleware (#159) --- app/app.go | 3 +- app/keepers/keepers.go | 50 ++++++++++++++++++++++++++++------ app/keepers/keys.go | 2 ++ app/modules.go | 8 ++++++ app/upgrades/v3.2/constants.go | 17 ++++++++++++ app/upgrades/v3.2/upgrades.go | 28 +++++++++++++++++++ go.mod | 5 ++-- go.sum | 6 ++-- 8 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 app/upgrades/v3.2/constants.go create mode 100644 app/upgrades/v3.2/upgrades.go diff --git a/app/app.go b/app/app.go index 9e06fdfb..1af377f8 100644 --- a/app/app.go +++ b/app/app.go @@ -56,6 +56,7 @@ import ( v2 "github.com/OmniFlix/omniflixhub/v3/app/upgrades/v2" v2_1 "github.com/OmniFlix/omniflixhub/v3/app/upgrades/v2.1" v3 "github.com/OmniFlix/omniflixhub/v3/app/upgrades/v3" + v3_2 "github.com/OmniFlix/omniflixhub/v3/app/upgrades/v3.2" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) @@ -89,7 +90,7 @@ func getGovProposalHandlers() []govclient.ProposalHandler { var ( // DefaultNodeHome default home directories for the application daemon DefaultNodeHome string - Upgrades = []upgrades.Upgrade{v012.Upgrade, v2.Upgrade, v2_1.Upgrade, v3.Upgrade} + Upgrades = []upgrades.Upgrade{v012.Upgrade, v2.Upgrade, v2_1.Upgrade, v3.Upgrade, v3_2.Upgrade} Forks []upgrades.Fork ) diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index dc4398be..a3efb4eb 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -16,6 +16,7 @@ import ( servertypes "github.com/cosmos/cosmos-sdk/server/types" "github.com/cosmos/cosmos-sdk/store/streaming" storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" icq "github.com/cosmos/ibc-apps/modules/async-icq/v7" icqkeeper "github.com/cosmos/ibc-apps/modules/async-icq/v7/keeper" icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v7/types" @@ -95,6 +96,10 @@ import ( packetforwardkeeper "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/keeper" packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types" + 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" + allockeeper "github.com/OmniFlix/omniflixhub/v3/x/alloc/keeper" alloctypes "github.com/OmniFlix/omniflixhub/v3/x/alloc/types" @@ -146,6 +151,7 @@ type AppKeepers struct { EvidenceKeeper evidencekeeper.Keeper TransferKeeper ibctransferkeeper.Keeper PacketForwardKeeper *packetforwardkeeper.Keeper + IBCHooksKeeper ibchookskeeper.Keeper FeeGrantKeeper feegrantkeeper.Keeper AuthzKeeper authzkeeper.Keeper ConsensusParamsKeeper consensusparamkeeper.Keeper @@ -154,6 +160,7 @@ type AppKeepers struct { TokenFactoryKeeper tokenfactorykeeper.Keeper IBCNFTTransferKeeper ibcnfttransferkeeper.Keeper WasmKeeper wasmkeeper.Keeper + ContractKeeper *wasmkeeper.PermissionedKeeper // make scoped keepers public for test purposes ScopedIBCKeeper capabilitykeeper.ScopedKeeper @@ -163,6 +170,10 @@ type AppKeepers struct { ScopedNFTTransferKeeper capabilitykeeper.ScopedKeeper ScopedWasmKeeper capabilitykeeper.ScopedKeeper + // Middleware wrapper + Ics20WasmHooks *ibchooks.WasmHooks + HooksICS4Wrapper ibchooks.ICS4Middleware + AllocKeeper allockeeper.Keeper ONFTKeeper onftkeeper.Keeper MarketplaceKeeper marketplacekeeper.Keeper @@ -372,6 +383,19 @@ func NewAppKeeper( groupConfig, ) + hooksKeeper := ibchookskeeper.NewKeeper( + keys[ibchookstypes.StoreKey], + ) + appKeepers.IBCHooksKeeper = hooksKeeper + + omniflixPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix() + wasmHooks := ibchooks.NewWasmHooks(&appKeepers.IBCHooksKeeper, nil, omniflixPrefix) // The contract keeper needs to be set later + appKeepers.Ics20WasmHooks = &wasmHooks + appKeepers.HooksICS4Wrapper = ibchooks.NewICS4Middleware( + appKeepers.IBCKeeper.ChannelKeeper, + appKeepers.Ics20WasmHooks, + ) + // initialize ibc packet forwarding middleware router appKeepers.PacketForwardKeeper = packetforwardkeeper.NewKeeper( appCodec, @@ -380,7 +404,8 @@ func NewAppKeeper( appKeepers.IBCKeeper.ChannelKeeper, appKeepers.DistrKeeper, appKeepers.BankKeeper, - appKeepers.IBCKeeper.ChannelKeeper, + // The ICS4Wrapper is replaced by the HooksICS4Wrapper instead of the channel so that sending can be overridden by the middleware + appKeepers.HooksICS4Wrapper, govModAddress, ) @@ -389,7 +414,7 @@ func NewAppKeeper( appCodec, appKeepers.keys[ibctransfertypes.StoreKey], appKeepers.GetSubspace(ibctransfertypes.ModuleName), - appKeepers.PacketForwardKeeper, + &appKeepers.HooksICS4Wrapper, appKeepers.IBCKeeper.ChannelKeeper, &appKeepers.IBCKeeper.PortKeeper, appKeepers.AccountKeeper, @@ -467,7 +492,7 @@ func NewAppKeeper( appCodec, keys[ibcnfttransfertypes.StoreKey], govModAddress, - appKeepers.IBCKeeper.ChannelKeeper, + appKeepers.HooksICS4Wrapper, appKeepers.IBCKeeper.ChannelKeeper, &appKeepers.IBCKeeper.PortKeeper, appKeepers.AccountKeeper, @@ -509,20 +534,21 @@ func NewAppKeeper( appKeepers.GovKeeper.SetLegacyRouter(govRouter) - var ibcStack porttypes.IBCModule - ibcStack = transfer.NewIBCModule(appKeepers.TransferKeeper) - ibcStack = packetforward.NewIBCMiddleware( - ibcStack, + var ibcTransferStack porttypes.IBCModule + ibcTransferStack = transfer.NewIBCModule(appKeepers.TransferKeeper) + ibcTransferStack = packetforward.NewIBCMiddleware( + ibcTransferStack, appKeepers.PacketForwardKeeper, 0, packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp, packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, ) + ibcTransferStack = ibchooks.NewIBCMiddleware(ibcTransferStack, &appKeepers.HooksICS4Wrapper) // Create static IBC router, add transfer route, then set and seal it ibcRouter := porttypes.NewRouter() ibcRouter. - AddRoute(ibctransfertypes.ModuleName, ibcStack). + AddRoute(ibctransfertypes.ModuleName, ibcTransferStack). AddRoute(icahosttypes.SubModuleName, icaHostIBCModule). AddRoute(icqtypes.ModuleName, icqModule). AddRoute(ibcnfttransfertypes.ModuleName, nfttransferIBCModule) @@ -559,7 +585,7 @@ func NewAppKeeper( appKeepers.BankKeeper, appKeepers.StakingKeeper, distrkeeper.NewQuerier(appKeepers.DistrKeeper), - appKeepers.IBCKeeper.ChannelKeeper, + &appKeepers.HooksICS4Wrapper, appKeepers.IBCKeeper.ChannelKeeper, &appKeepers.IBCKeeper.PortKeeper, appKeepers.ScopedWasmKeeper, @@ -572,6 +598,12 @@ func NewAppKeeper( govModAddress, wasmOpts..., ) + // set the contract keeper for the Ics20WasmHooks + appKeepers.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(&appKeepers.WasmKeeper) + appKeepers.Ics20WasmHooks.ContractKeeper = &appKeepers.WasmKeeper + + ibcRouter.AddRoute(wasmtypes.ModuleName, wasm.NewIBCHandler(appKeepers.WasmKeeper, appKeepers.IBCKeeper.ChannelKeeper, appKeepers.IBCKeeper.ChannelKeeper)) + appKeepers.IBCKeeper.SetRouter(ibcRouter) return appKeepers } diff --git a/app/keepers/keys.go b/app/keepers/keys.go index a59b2b4e..77bbcbe4 100644 --- a/app/keepers/keys.go +++ b/app/keepers/keys.go @@ -30,6 +30,7 @@ import ( upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types" icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v7/types" + ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/types" icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" @@ -53,6 +54,7 @@ func (appKeepers *AppKeepers) GenerateKeys() { icahosttypes.StoreKey, icqtypes.StoreKey, packetforwardtypes.StoreKey, + ibchookstypes.StoreKey, ibcnfttransfertypes.StoreKey, capabilitytypes.StoreKey, crisistypes.StoreKey, diff --git a/app/modules.go b/app/modules.go index 39a6801d..e72ac7e0 100644 --- a/app/modules.go +++ b/app/modules.go @@ -81,6 +81,9 @@ import ( "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward" packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types" + ibchooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7" + ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/types" + "github.com/OmniFlix/omniflixhub/v3/x/alloc" alloctypes "github.com/OmniFlix/omniflixhub/v3/x/alloc/types" @@ -121,6 +124,7 @@ var ( ica.AppModuleBasic{}, icq.AppModuleBasic{}, packetforward.AppModuleBasic{}, + ibchooks.AppModuleBasic{}, feegrantmodule.AppModuleBasic{}, authzmodule.AppModuleBasic{}, upgrade.AppModuleBasic{}, @@ -243,6 +247,7 @@ func appModules( icq.NewAppModule(app.ICQKeeper, app.GetSubspace(icqtypes.ModuleName)), nfttransfer.NewAppModule(app.IBCNFTTransferKeeper), packetforward.NewAppModule(app.PacketForwardKeeper, app.GetSubspace(packetforwardtypes.ModuleName)), + ibchooks.NewAppModule(app.AccountKeeper), globalfee.NewAppModule(appCodec, app.GlobalFeeKeeper, bondDenom), alloc.NewAppModule(appCodec, app.AllocKeeper, app.GetSubspace(alloctypes.ModuleName)), onft.NewAppModule( @@ -317,6 +322,7 @@ func orderBeginBlockers() []string { govtypes.ModuleName, paramstypes.ModuleName, consensusparamtypes.ModuleName, + ibchookstypes.ModuleName, wasmtypes.ModuleName, ibctransfertypes.ModuleName, icatypes.ModuleName, @@ -352,6 +358,7 @@ func orderEndBlockers() []string { vestingtypes.ModuleName, paramstypes.ModuleName, consensusparamtypes.ModuleName, + ibchookstypes.ModuleName, wasmtypes.ModuleName, ibctransfertypes.ModuleName, icatypes.ModuleName, @@ -403,6 +410,7 @@ func orderInitGenesis() []string { upgradetypes.ModuleName, vestingtypes.ModuleName, feegrant.ModuleName, + ibchookstypes.ModuleName, wasmtypes.ModuleName, globalfee.ModuleName, group.ModuleName, diff --git a/app/upgrades/v3.2/constants.go b/app/upgrades/v3.2/constants.go new file mode 100644 index 00000000..0b6f34f9 --- /dev/null +++ b/app/upgrades/v3.2/constants.go @@ -0,0 +1,17 @@ +package v3_2 + +import ( + "github.com/OmniFlix/omniflixhub/v3/app/upgrades" + store "github.com/cosmos/cosmos-sdk/store/types" + ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/types" +) + +const UpgradeName = "v3.2-test" + +var Upgrade = upgrades.Upgrade{ + UpgradeName: UpgradeName, + CreateUpgradeHandler: CreateV3_2UpgradeHandler, + StoreUpgrades: store.StoreUpgrades{ + Added: []string{ibchookstypes.ModuleName}, + }, +} diff --git a/app/upgrades/v3.2/upgrades.go b/app/upgrades/v3.2/upgrades.go new file mode 100644 index 00000000..f1079dcf --- /dev/null +++ b/app/upgrades/v3.2/upgrades.go @@ -0,0 +1,28 @@ +package v3_2 + +import ( + "github.com/OmniFlix/omniflixhub/v3/app/keepers" + "github.com/OmniFlix/omniflixhub/v3/app/upgrades" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" +) + +func CreateV3_2UpgradeHandler( + mm *module.Manager, + cfg module.Configurator, + _ upgrades.BaseAppParamManager, + keepers *keepers.AppKeepers, +) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { + ctx.Logger().Info("running migrations ...") + // Run migrations before applying any other state changes. + // NOTE: DO NOT PUT ANY STATE CHANGES BEFORE RunMigrations(). + versionMap, err := mm.RunMigrations(ctx, cfg, fromVM) + if err != nil { + return nil, err + } + ctx.Logger().Info("Upgrade complete") + return versionMap, nil + } +} diff --git a/go.mod b/go.mod index 0940f675..947e5744 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/cosmos/gogoproto v1.4.10 github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.1 github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.1.1 + github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79 github.com/cosmos/ibc-go/v7 v7.3.1 github.com/golang/protobuf v1.5.3 github.com/google/uuid v1.4.0 @@ -36,7 +37,7 @@ require ( cloud.google.com/go/iam v1.1.5 // indirect cloud.google.com/go/storage v1.30.1 // indirect cosmossdk.io/api v0.3.1 - cosmossdk.io/core v0.5.1 // indirect + cosmossdk.io/core v0.6.1 // indirect cosmossdk.io/depinject v1.0.0-alpha.4 // indirect cosmossdk.io/errors v1.0.1 cosmossdk.io/log v1.3.1 // indirect @@ -44,7 +45,7 @@ require ( cosmossdk.io/tools/rosetta v0.2.1 // indirect filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect - github.com/99designs/keyring v1.2.1 // indirect + github.com/99designs/keyring v1.2.2 // indirect github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect github.com/armon/go-metrics v0.4.1 // indirect github.com/aws/aws-sdk-go v1.44.203 // indirect diff --git a/go.sum b/go.sum index 10e11618..a6f40a45 100644 --- a/go.sum +++ b/go.sum @@ -193,8 +193,8 @@ cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoIS collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= cosmossdk.io/api v0.3.1 h1:NNiOclKRR0AOlO4KIqeaG6PS6kswOMhHD0ir0SscNXE= cosmossdk.io/api v0.3.1/go.mod h1:DfHfMkiNA2Uhy8fj0JJlOCYOBp4eWUUJ1te5zBGNyIw= -cosmossdk.io/core v0.5.1 h1:vQVtFrIYOQJDV3f7rw4pjjVqc1id4+mE0L9hHP66pyI= -cosmossdk.io/core v0.5.1/go.mod h1:KZtwHCLjcFuo0nmDc24Xy6CRNEL9Vl/MeimQ2aC7NLE= +cosmossdk.io/core v0.6.1 h1:OBy7TI2W+/gyn2z40vVvruK3di+cAluinA6cybFbE7s= +cosmossdk.io/core v0.6.1/go.mod h1:g3MMBCBXtxbDWBURDVnJE7XML4BG5qENhs0gzkcpuFA= cosmossdk.io/depinject v1.0.0-alpha.4 h1:PLNp8ZYAMPTUKyG9IK2hsbciDWqna2z1Wsl98okJopc= cosmossdk.io/depinject v1.0.0-alpha.4/go.mod h1:HeDk7IkR5ckZ3lMGs/o91AVUc7E596vMaOmslGFM3yU= cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0= @@ -419,6 +419,8 @@ github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.1 h1:PqI github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.1.1/go.mod h1:UvDmcGIWJPIytq+Q78/ff5NTOsuX/7IrNgEugTW5i0s= github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.1.1 h1:02RCbih5lQ8aGdDMSvxhTnk5JDLEDitn17ytEE1Qhko= github.com/cosmos/ibc-apps/modules/async-icq/v7 v7.1.1/go.mod h1:LvVkEXTORVgd87W2Yu7ZY3acKKeTMq/txdTworn8EZI= +github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79 h1:pCxyhIxgWTabAQC5UerkITraHG3SwajdLKKMCFDWCv4= +github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79/go.mod h1:JwHFbo1oX/ht4fPpnPvmhZr+dCkYK1Vihw+vZE9umR4= github.com/cosmos/ibc-go/v7 v7.3.1 h1:bil1IjnHdyWDASFYKfwdRiNtFP6WK3osW7QFEAgU4I8= github.com/cosmos/ibc-go/v7 v7.3.1/go.mod h1:wvx4pPBofe5ZdMNV3OFRxSI4auEP5Qfqf8JXLLNV04g= github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=