Skip to content

Commit

Permalink
Remove ICACallbacks from Middleware Stack (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Feb 17, 2023
1 parent 0413e99 commit 59a872b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 183 deletions.
40 changes: 26 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ func NewStrideApp(
app.GetSubspace(icacallbacksmoduletypes.ModuleName),
scopedIcacallbacksKeeper,
*app.IBCKeeper,
app.ICAControllerKeeper,
)

app.InterchainqueryKeeper = interchainquerykeeper.NewKeeper(appCodec, keys[interchainquerytypes.StoreKey], app.IBCKeeper)
Expand Down Expand Up @@ -561,34 +560,47 @@ func NewStrideApp(
icaModule := ica.NewAppModule(&app.ICAControllerKeeper, &app.ICAHostKeeper)

// Create the middleware stacks
// Stack one (ICAHost Stack) contains:
// - IBC
// - ICAHost
// - base app
icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)

// Stack one contains
// Stack two (Stakeibc Stack) contains
// - IBC
// - ICA
// - icacallbacks
// - stakeibc
// - base app
var icamiddlewareStack porttypes.IBCModule
icamiddlewareStack = icacallbacksmodule.NewIBCModule(app.IcacallbacksKeeper, stakeibcIBCModule)
icamiddlewareStack = icacontroller.NewIBCMiddleware(icamiddlewareStack, app.ICAControllerKeeper)
icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)
var stakeibcStack porttypes.IBCModule = stakeibcIBCModule
stakeibcStack = icacontroller.NewIBCMiddleware(stakeibcStack, app.ICAControllerKeeper)

// Stack two contains
// Stack three contains
// - IBC
// - records
// - transfer
// - base app
recordsStack := recordsmodule.NewIBCModule(app.RecordsKeeper, transferIBCModule)
var transferStack porttypes.IBCModule = transferIBCModule
transferStack = recordsmodule.NewIBCModule(app.RecordsKeeper, transferStack)

// Create static IBC router, add transfer route, then set and seal it
// Two routes are included for the ICAController because of the following procedure when registering an ICA
// 1. RegisterInterchainAccount binds the new portId to the icacontroller module and initiates a channel opening
// 2. MsgChanOpenInit is invoked from the IBC message server. The message server identifies that the
// icacontroller module owns the portID and routes to the stakeibc stack (the "icacontroller" route below)
// 3. The stakeibc stack works top-down, first in the ICAController's OnChanOpenInit, and then in stakeibc's OnChanOpenInit
// 4. In stakeibc's OnChanOpenInit, the stakeibc module steals the portId from the icacontroller module
// 5. Now in OnChanOpenAck and any other subsequent IBC callback, the message server will identify
// the portID owner as stakeibc and route to the same stakeibcStack, this time using the "stakeibc" route instead
ibcRouter := ibcporttypes.NewRouter()
ibcRouter.
AddRoute(ibctransfertypes.ModuleName, recordsStack).
AddRoute(icacontrollertypes.SubModuleName, icamiddlewareStack).
// ICAHost Stack
AddRoute(icahosttypes.SubModuleName, icaHostIBCModule).
// Note, authentication module packets are routed to the top level of the middleware stack
AddRoute(stakeibcmoduletypes.ModuleName, icamiddlewareStack).
AddRoute(icacallbacksmoduletypes.ModuleName, icamiddlewareStack)
// Stakeibc Stack
AddRoute(icacontrollertypes.SubModuleName, stakeibcStack).
AddRoute(stakeibcmoduletypes.ModuleName, stakeibcStack).
// Transfer stack
AddRoute(ibctransfertypes.ModuleName, transferStack)

// this line is used by starport scaffolding # ibc/app/router
app.IBCKeeper.SetRouter(ibcRouter)

Expand Down
2 changes: 0 additions & 2 deletions x/icacallbacks/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
for _, elem := range genState.CallbackDataList {
k.SetCallbackData(ctx, elem)
}
// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
}

Expand All @@ -24,7 +23,6 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis.Params = k.GetParams(ctx)

genesis.CallbackDataList = k.GetAllCallbackData(ctx)
// this line is used by starport scaffolding # genesis/module/export

return genesis
}
38 changes: 14 additions & 24 deletions x/icacallbacks/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

errorsmod "cosmossdk.io/errors"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
Expand All @@ -21,19 +20,17 @@ import (
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
icacontrollerkeeper "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper"
)

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
scopedKeeper capabilitykeeper.ScopedKeeper
icacallbacks map[string]types.ICACallbackHandler
IBCKeeper ibckeeper.Keeper
ICAControllerKeeper icacontrollerkeeper.Keeper
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
scopedKeeper capabilitykeeper.ScopedKeeper
icacallbacks map[string]types.ICACallbackHandler
IBCKeeper ibckeeper.Keeper
}
)

Expand All @@ -44,22 +41,20 @@ func NewKeeper(
ps paramtypes.Subspace,
scopedKeeper capabilitykeeper.ScopedKeeper,
ibcKeeper ibckeeper.Keeper,
icacontrollerkeeper icacontrollerkeeper.Keeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
scopedKeeper: scopedKeeper,
icacallbacks: make(map[string]types.ICACallbackHandler),
IBCKeeper: ibcKeeper,
ICAControllerKeeper: icacontrollerkeeper,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
scopedKeeper: scopedKeeper,
icacallbacks: make(map[string]types.ICACallbackHandler),
IBCKeeper: ibcKeeper,
}
}

Expand All @@ -85,11 +80,6 @@ func (k *Keeper) GetICACallbackHandler(module string) (types.ICACallbackHandler,
return callback, nil
}

// ClaimCapability claims the channel capability passed via the OnOpenChanInit callback
func (k *Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}

func (k Keeper) GetCallbackDataFromPacket(ctx sdk.Context, modulePacket channeltypes.Packet, callbackDataKey string) (cbd *types.CallbackData, found bool) {
// get the relevant module from the channel and port
portID := modulePacket.GetSourcePort()
Expand Down
142 changes: 0 additions & 142 deletions x/icacallbacks/module_ibc.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/stakeibc/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (im IBCModule) OnChanOpenInit(
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnChanOpenAck: portID %s, channelID %s", portID, channelID))
im.keeper.Logger(ctx).Info(fmt.Sprintf("OnChanOpenInit: portID %s, channelID %s", portID, channelID))
// Note: The channel capability must be claimed by the authentication module in OnChanOpenInit otherwise the
// authentication module will not be able to send packets on the channel created for the associated interchain account.
if err := im.keeper.ClaimCapability(ctx, channelCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
Expand Down

0 comments on commit 59a872b

Please sign in to comment.