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

feat: add ibc-hooks middleware #159

Merged
merged 1 commit into from
Mar 6, 2024
Merged
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
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
)

Expand Down
50 changes: 41 additions & 9 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)

Expand All @@ -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,
Expand Down Expand Up @@ -467,7 +492,7 @@ func NewAppKeeper(
appCodec,
keys[ibcnfttransfertypes.StoreKey],
govModAddress,
appKeepers.IBCKeeper.ChannelKeeper,
appKeepers.HooksICS4Wrapper,
appKeepers.IBCKeeper.ChannelKeeper,
&appKeepers.IBCKeeper.PortKeeper,
appKeepers.AccountKeeper,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -53,6 +54,7 @@ func (appKeepers *AppKeepers) GenerateKeys() {
icahosttypes.StoreKey,
icqtypes.StoreKey,
packetforwardtypes.StoreKey,
ibchookstypes.StoreKey,
ibcnfttransfertypes.StoreKey,
capabilitytypes.StoreKey,
crisistypes.StoreKey,
Expand Down
8 changes: 8 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -121,6 +124,7 @@ var (
ica.AppModuleBasic{},
icq.AppModuleBasic{},
packetforward.AppModuleBasic{},
ibchooks.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
authzmodule.AppModuleBasic{},
upgrade.AppModuleBasic{},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -317,6 +322,7 @@ func orderBeginBlockers() []string {
govtypes.ModuleName,
paramstypes.ModuleName,
consensusparamtypes.ModuleName,
ibchookstypes.ModuleName,
wasmtypes.ModuleName,
ibctransfertypes.ModuleName,
icatypes.ModuleName,
Expand Down Expand Up @@ -352,6 +358,7 @@ func orderEndBlockers() []string {
vestingtypes.ModuleName,
paramstypes.ModuleName,
consensusparamtypes.ModuleName,
ibchookstypes.ModuleName,
wasmtypes.ModuleName,
ibctransfertypes.ModuleName,
icatypes.ModuleName,
Expand Down Expand Up @@ -403,6 +410,7 @@ func orderInitGenesis() []string {
upgradetypes.ModuleName,
vestingtypes.ModuleName,
feegrant.ModuleName,
ibchookstypes.ModuleName,
wasmtypes.ModuleName,
globalfee.ModuleName,
group.ModuleName,
Expand Down
17 changes: 17 additions & 0 deletions app/upgrades/v3.2/constants.go
Original file line number Diff line number Diff line change
@@ -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},
},
}
28 changes: 28 additions & 0 deletions app/upgrades/v3.2/upgrades.go
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,15 +37,15 @@ 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
cosmossdk.io/math v1.2.0
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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
Loading