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

refactor: ibc port router for app callbacks #6314

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
845e45f
feat: add intial SendPacket msgs and rpc definition to ibc core
damiannolan May 15, 2024
db5cddf
feat: add OnSendPacket callback to IBCModule interface
damiannolan May 15, 2024
385674a
chore: invoke OnSendPacket callback from msg server rpc
damiannolan May 15, 2024
5c861f4
chore: add comment
damiannolan May 15, 2024
9d4d2ea
chore: rename proto fields
damiannolan May 15, 2024
90fe8ae
chore: add OnSendPacket to ibccallbacks
damiannolan May 15, 2024
fc309f6
feat: add router_v2 while leaving current router impl in place
damiannolan May 15, 2024
d0f85db
chore: add getter and setters for AppRouter to core ibc
damiannolan May 15, 2024
588ff66
imp: implement callbacks OnSendPacket
colin-axner May 15, 2024
8bc82f6
imp: implement OnSendPacket for ics27
colin-axner May 16, 2024
f883d68
imp: implement OnSendPacket in transfer
damiannolan May 16, 2024
c5eef93
Merge branch 'main' into feat/port-router
damiannolan May 16, 2024
e626c7b
chore: start wiring up router_v2 in app.go
damiannolan May 16, 2024
45896aa
imp: add msg server handler to transfer
colin-axner May 16, 2024
20326d4
Merge branch 'feat/port-router' of github.com:cosmos/ibc-go into feat…
colin-axner May 16, 2024
3714960
refactor: enable core IBC SendPacket API and switch transfer to redir…
colin-axner May 16, 2024
b2df2f3
refactor: rewire controller to use SendPacket API
colin-axner May 16, 2024
b295949
refactor: rewire 29-fee to use SendPacket API
colin-axner May 16, 2024
2f5116e
refactor: use SendPacket API for callbacks
colin-axner May 16, 2024
16a88b4
imp: remove capability from SendPacket and add prefix routing
damiannolan May 16, 2024
67e4b5f
refactor: adapt signer to sdk.AccAddress
damiannolan May 16, 2024
3db1613
chore: rm capabilities from recv, ack, timeout packet handler
damiannolan May 16, 2024
07e8b1c
lint: make lint-fix and address
damiannolan May 16, 2024
4214201
refactor: rm SendPacket from ics4wrapper
damiannolan May 16, 2024
9610871
chore: rm callbacks var in SendPacket handler
damiannolan May 16, 2024
ef6e063
Merge branch 'main' into feat/port-router
damiannolan May 22, 2024
cf339f2
fix: rm capability arg in ibc ante handler
damiannolan May 22, 2024
4f8d319
Merge branch 'main' into feat/port-router
damiannolan Jul 11, 2024
a4a7bdd
(chore): remove capabilities from channel handsake (#7009)
bznein Aug 5, 2024
832a0fc
Adding updates to composite router and scaffolding for LegacyIBCModul…
chatton Aug 5, 2024
d701aa2
Rename IBCModule to ClassicIBCModule (#7015)
chatton Aug 5, 2024
9565e36
Merge branch 'main' of github.com:cosmos/ibc-go into feat/port-router
colin-axner Aug 6, 2024
3eaf2ce
fix: tests
colin-axner Aug 6, 2024
41c6656
Add Wrap/Unwrap Interfaces and implement OnChanOpenInit (#7059)
chatton Aug 7, 2024
9460400
Use new port router for onchanupgradetry (#7067)
chatton Aug 7, 2024
6ec1efa
Use new port router for OnChanOpenAck (#7084)
colin-axner Aug 7, 2024
dc6e072
Use new port router for OnChanUpgradeInit (#7082)
chatton Aug 8, 2024
ae43f7a
refactor: implement OnChanOpenConfirm using new port router (#7088)
colin-axner Aug 8, 2024
63dd289
chore: implement OnChanUpgradeTry (#7092)
chatton Aug 8, 2024
0c47e45
chore(api)!: move checks from Transfer to OnSendPacket (#7068)
bznein Aug 8, 2024
467819d
refactor: implement OnChanCloseInit using new port router. (#7095)
bznein Aug 8, 2024
d779697
Use new port router for OnChanUpgradeAck (#7094)
chatton Aug 8, 2024
179025d
refactor: implement onchancloseconfirm using new port router (#7096)
bznein Aug 8, 2024
62db721
Use new port router for OnChanUpgradeOpen (#7102)
chatton Aug 8, 2024
48b6a3f
refactor: replace ack interface with result type for OnRecvPacket (#7…
damiannolan Aug 8, 2024
d9c4e96
imp (api)!: convert coins to token only once in MsgTransfer (#7110)
bznein Aug 8, 2024
0ea612a
use new port router with OnAcknowledgementPacket (#7108)
colin-axner Aug 8, 2024
a9d34ba
chore: docs (#7111)
colin-axner Aug 12, 2024
ec593cb
Bznein/7023/on timeout packet (#7144)
bznein Aug 13, 2024
f5ab15b
Add convenience func to reverse callbacks and refactor other methods …
chatton Aug 13, 2024
6327b58
chore: use app router in OnTimeoutPacket handler (#7164)
chatton Aug 14, 2024
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
196 changes: 73 additions & 123 deletions modules/apps/27-interchain-accounts/controller/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/controller/keeper"
"github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported"
)
Expand All @@ -23,12 +21,13 @@ var (
_ porttypes.Middleware = (*IBCMiddleware)(nil)
_ porttypes.PacketDataUnmarshaler = (*IBCMiddleware)(nil)
_ porttypes.UpgradableModule = (*IBCMiddleware)(nil)
_ porttypes.VersionWrapper = (*IBCMiddleware)(nil)
)

// IBCMiddleware implements the ICS26 callbacks for the fee middleware given the
// ICA controller keeper and the underlying application.
type IBCMiddleware struct {
app porttypes.IBCModule
app porttypes.ClassicIBCModule
keeper keeper.Keeper
}

Expand All @@ -43,7 +42,7 @@ func NewIBCMiddleware(k keeper.Keeper) IBCMiddleware {
}

// NewIBCMiddlewareWithAuth creates a new IBCMiddleware given the associated keeper and underlying application
func NewIBCMiddlewareWithAuth(app porttypes.IBCModule, k keeper.Keeper) IBCMiddleware {
func NewIBCMiddlewareWithAuth(app porttypes.ClassicIBCModule, k keeper.Keeper) IBCMiddleware {
return IBCMiddleware{
app: app,
keeper: k,
Expand All @@ -62,32 +61,17 @@ func (im IBCMiddleware) OnChanOpenInit(
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
if !im.keeper.GetParams(ctx).ControllerEnabled {
return "", types.ErrControllerSubModuleDisabled
}

version, err := im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version)
version, err := im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, counterparty, version)
if err != nil {
return "", err
}

if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return "", err
}

// call underlying app's OnChanOpenInit callback with the passed in version
// the version returned is discarded as the ica-auth module does not have permission to edit the version string.
// ics27 will always return the version string containing the Metadata struct which is created during the `RegisterInterchainAccount` call.
if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionHops[0]) {
if _, err := im.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, nil, counterparty, version); err != nil {
return "", err
}
}

return version, nil
}

Expand All @@ -98,7 +82,6 @@ func (IBCMiddleware) OnChanOpenTry(
connectionHops []string,
portID,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
Expand Down Expand Up @@ -126,16 +109,6 @@ func (im IBCMiddleware) OnChanOpenAck(
return err
}

connectionID, err := im.keeper.GetConnectionID(ctx, portID, channelID)
if err != nil {
return err
}

// call underlying app's OnChanOpenAck callback with the counterparty app version.
if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
return im.app.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion)
}

return nil
}

Expand Down Expand Up @@ -164,17 +137,54 @@ func (im IBCMiddleware) OnChanCloseConfirm(
portID,
channelID string,
) error {
if err := im.keeper.OnChanCloseConfirm(ctx, portID, channelID); err != nil {
return im.keeper.OnChanCloseConfirm(ctx, portID, channelID)
}

// OnSendPacket implements the IBCModule interface.
func (im IBCMiddleware) OnSendPacket(
ctx sdk.Context,
portID string,
channelID string,
sequence uint64,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
signer sdk.AccAddress,
) error {
if !im.keeper.GetParams(ctx).ControllerEnabled {
return types.ErrControllerSubModuleDisabled
}

controllerPortID, err := icatypes.NewControllerPortID(signer.String())
if err != nil {
return err
}

if controllerPortID != portID {
return errorsmod.Wrap(ibcerrors.ErrUnauthorized, "signer is not owner of interchain account channel")
}

connectionID, err := im.keeper.GetConnectionID(ctx, portID, channelID)
if err != nil {
return err
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
return im.app.OnChanCloseConfirm(ctx, portID, channelID)
activeChannelID, found := im.keeper.GetOpenActiveChannel(ctx, connectionID, portID)
if !found {
return errorsmod.Wrapf(icatypes.ErrActiveChannelNotFound, "failed to retrieve active channel on connection %s for port %s", connectionID, portID)
}

if activeChannelID != channelID {
return errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "active channel ID does not match provided channelID. expected %s, got %s", activeChannelID, channelID)
}

var icaPacketData icatypes.InterchainAccountPacketData
if err := icaPacketData.UnmarshalJSON(data); err != nil {
return err
}

if err := icaPacketData.ValidateBasic(); err != nil {
return errorsmod.Wrap(err, "invalid interchain account packet data")
}

return nil
Expand All @@ -186,11 +196,14 @@ func (IBCMiddleware) OnRecvPacket(
_ string,
packet channeltypes.Packet,
_ sdk.AccAddress,
) ibcexported.Acknowledgement {
) ibcexported.RecvPacketResult {
err := errorsmod.Wrapf(icatypes.ErrInvalidChannelFlow, "cannot receive packet on controller chain")
ack := channeltypes.NewErrorAcknowledgement(err)
keeper.EmitAcknowledgementEvent(ctx, packet, ack, err)
return ack
return ibcexported.RecvPacketResult{
Status: ibcexported.Failure,
Acknowledgement: ack.Acknowledgement(),
}
}

// OnAcknowledgementPacket implements the IBCMiddleware interface
Expand All @@ -205,16 +218,6 @@ func (im IBCMiddleware) OnAcknowledgementPacket(
return types.ErrControllerSubModuleDisabled
}

connectionID, err := im.keeper.GetConnectionID(ctx, packet.GetSourcePort(), packet.GetSourceChannel())
if err != nil {
return err
}

// call underlying app's OnAcknowledgementPacket callback.
if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, packet.GetSourcePort(), connectionID) {
return im.app.OnAcknowledgementPacket(ctx, channelVersion, packet, acknowledgement, relayer)
}

return nil
}

Expand All @@ -233,15 +236,6 @@ func (im IBCMiddleware) OnTimeoutPacket(
return err
}

connectionID, err := im.keeper.GetConnectionID(ctx, packet.GetSourcePort(), packet.GetSourceChannel())
if err != nil {
return err
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, packet.GetSourcePort(), connectionID) {
return im.app.OnTimeoutPacket(ctx, channelVersion, packet, relayer)
}

return nil
}

Expand All @@ -251,26 +245,7 @@ func (im IBCMiddleware) OnChanUpgradeInit(ctx sdk.Context, portID, channelID str
return "", types.ErrControllerSubModuleDisabled
}

proposedVersion, err := im.keeper.OnChanUpgradeInit(ctx, portID, channelID, proposedOrder, proposedConnectionHops, proposedVersion)
if err != nil {
return "", err
}

connectionID, err := im.keeper.GetConnectionID(ctx, portID, channelID)
if err != nil {
return "", err
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
// Only cast to UpgradableModule if the application is set.
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
return "", errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack")
}
return cbs.OnChanUpgradeInit(ctx, portID, channelID, proposedOrder, proposedConnectionHops, proposedVersion)
}

return proposedVersion, nil
return im.keeper.OnChanUpgradeInit(ctx, portID, channelID, proposedOrder, proposedConnectionHops, proposedVersion)
}

// OnChanUpgradeTry implements the IBCModule interface
Expand All @@ -284,63 +259,18 @@ func (im IBCMiddleware) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, cou
return types.ErrControllerSubModuleDisabled
}

if err := im.keeper.OnChanUpgradeAck(ctx, portID, channelID, counterpartyVersion); err != nil {
return err
}

connectionID, err := im.keeper.GetConnectionID(ctx, portID, channelID)
if err != nil {
return err
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
// Only cast to UpgradableModule if the application is set.
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
return errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack")
}
return cbs.OnChanUpgradeAck(ctx, portID, channelID, counterpartyVersion)
}

return nil
return im.keeper.OnChanUpgradeAck(ctx, portID, channelID, counterpartyVersion)
}

// OnChanUpgradeOpen implements the IBCModule interface
func (im IBCMiddleware) OnChanUpgradeOpen(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) {
connectionID, err := im.keeper.GetConnectionID(ctx, portID, channelID)
if err != nil {
panic(err)
}

if im.app != nil && im.keeper.IsMiddlewareEnabled(ctx, portID, connectionID) {
// Only cast to UpgradableModule if the application is set.
cbs, ok := im.app.(porttypes.UpgradableModule)
if !ok {
panic(errorsmod.Wrap(porttypes.ErrInvalidRoute, "upgrade route not found to module in application callstack"))
}
cbs.OnChanUpgradeOpen(ctx, portID, channelID, proposedOrder, proposedConnectionHops, proposedVersion)
}
}

// SendPacket implements the ICS4 Wrapper interface
func (IBCMiddleware) SendPacket(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (uint64, error) {
panic(errors.New("SendPacket not supported for ICA controller module. Please use SendTx"))
func (IBCMiddleware) OnChanUpgradeOpen(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) {
}

// WriteAcknowledgement implements the ICS4 Wrapper interface
func (IBCMiddleware) WriteAcknowledgement(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
packet ibcexported.PacketI,
ack ibcexported.Acknowledgement,
ack []byte,
) error {
panic(errors.New("WriteAcknowledgement not supported for ICA controller module"))
}
Expand All @@ -367,3 +297,23 @@ func (im IBCMiddleware) UnmarshalPacketData(ctx sdk.Context, portID string, chan

return data, version, nil
}

// WrapVersion returns the wrapped version based on the provided version string and underlying application version.
// TODO: decide how we want to handle the underlying app. For now I made it backwards compatible.
// https://github.com/cosmos/ibc-go/issues/7063
func (IBCMiddleware) WrapVersion(cbVersion, underlyingAppVersion string) string {
// ignore underlying app version
return cbVersion
}

// UnwrapVersionUnsafe returns the version. Interchain accounts does not wrap versions.
func (IBCMiddleware) UnwrapVersionUnsafe(version string) (string, string, error) {
// ignore underlying app version
return version, "", nil
}

// UnwrapVersionSafe returns the version. Interchain accounts does not wrap versions.
func (IBCMiddleware) UnwrapVersionSafe(ctx sdk.Context, portID, channelID, version string) (string, string) {
// ignore underlying app version
return version, ""
}
Loading