Skip to content

Commit

Permalink
Adding updates to composite router and scaffolding for LegacyIBCModule (
Browse files Browse the repository at this point in the history
#7010)

* chore: adding updates to composite router and scaffolding for IBCLegacyModule

* chore: support construction of LegacyIBCModules over consecutive calls to AddRoute

* chore: removed ack handhsake impl

* chore: fix linter

* chore: lint

* Linter

* chore: addressing PR feedback

* chore: routeToLegacyModule returns a single item

* Update modules/core/05-port/types/router_v2.go

* Update modules/core/05-port/types/router_v2.go

* Update modules/core/05-port/types/router_v2.go

* chore: fix errors from merge conflict

---------

Co-authored-by: bznein <nikolas.degiorgis@interchain.io>
Co-authored-by: Colin Axnér <25233464+colin-axner@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent a4a7bdd commit 832a0fc
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 72 deletions.
9 changes: 8 additions & 1 deletion modules/apps/callbacks/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ func (s *CallbacksTestSuite) TestSendPacket() {
cbs, ok := GetSimApp(s.chainA).IBCKeeper.PortKeeper.AppRoute(transfertypes.ModuleName)
s.Require().True(ok)

callbacksModule, ok := cbs[1].(ibccallbacks.IBCMiddleware) // callbacks module is routed second
s.Require().Len(cbs, 1, "expected 1 legacy module")

legacyModule, ok := cbs[0].(porttypes.LegacyIBCModule)
s.Require().True(ok, "expected there to be a single legacy ibc module")

legacyModuleCbs := legacyModule.GetCallbacks()

callbacksModule, ok := legacyModuleCbs[1].(ibccallbacks.IBCMiddleware) // callbacks module is routed second
s.Require().True(ok)

packetData = transfertypes.NewFungibleTokenPacketDataV2(
Expand Down
32 changes: 2 additions & 30 deletions modules/core/05-port/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"strings"

"cosmossdk.io/log"

Expand Down Expand Up @@ -87,38 +86,11 @@ func (k *Keeper) LookupModuleByPort(ctx sdk.Context, portID string) (string, *ca
// Route returns a IBCModule for a given module, and a boolean indicating
// whether or not the route is present.
func (k *Keeper) Route(module string) (types.IBCModule, bool) {
routes, ok := k.Router.GetRoute(module)

if ok {
return routes, true
}

for _, prefix := range k.Keys() {
if strings.Contains(module, prefix) {
return k.Router.GetRoute(prefix)
}
}

return nil, false
return k.Router.GetRoute(module)
}

// AppRoute returns an ordered list of IBCModule callbacks for a given module name, and a boolean indicating
// whether or not the callbacks are present.
func (k *Keeper) AppRoute(module string) ([]types.IBCModule, bool) {
routes, ok := k.AppRouter.GetRoute(module)
if ok {
return routes, true
}

for _, prefix := range k.Keys() {
if strings.Contains(module, prefix) {
return k.AppRouter.GetRoute(prefix)
}
}

return nil, false
}

func (k *Keeper) Keys() []string {
return k.AppRouter.Keys()
return k.AppRouter.PacketRoute(module)
}
172 changes: 172 additions & 0 deletions modules/core/05-port/types/ibc_legacy_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package types

import (
errorsmod "cosmossdk.io/errors"

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

clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
)

// TODO: IBCModule will be renamed to ClassicIBCModule in a follow up.
type ClassicIBCModule IBCModule

// LegacyIBCModule implements the ICS26 interface for transfer given the transfer keeper.
type LegacyIBCModule struct {
cbs []ClassicIBCModule
}

// TODO: added this for testing purposes, we can remove later if tests are refactored.
func (im *LegacyIBCModule) GetCallbacks() []ClassicIBCModule {
return im.cbs
}

// NewLegacyIBCModule creates a new IBCModule given the keeper
func NewLegacyIBCModule(cbs ...ClassicIBCModule) ClassicIBCModule {
return LegacyIBCModule{
cbs: cbs,
}
}

// OnChanOpenInit implements the IBCModule interface
func (LegacyIBCModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
return "", nil
}

// OnChanOpenTry implements the IBCModule interface.
func (LegacyIBCModule) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID,
channelID string,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
return "", nil
}

// OnChanOpenAck implements the IBCModule interface
func (LegacyIBCModule) OnChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
counterpartyChannelID string,
counterpartyVersion string,
) error {
return nil
}

// OnChanOpenConfirm implements the IBCModule interface
func (LegacyIBCModule) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return nil
}

// OnChanCloseInit implements the IBCModule interface
func (LegacyIBCModule) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
) error {
return nil
}

// OnChanCloseConfirm implements the IBCModule interface
func (LegacyIBCModule) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return nil
}

// OnSendPacket implements the IBCModule interface.
func (im LegacyIBCModule) OnSendPacket(
ctx sdk.Context,
portID string,
channelID string,
sequence uint64,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
dataBz []byte,
signer sdk.AccAddress,
) error {
// to maintain backwards compatibility, OnSendPacket iterates over the callbacks in order, as they are wired from bottom to top of the stack.
for _, cb := range im.cbs {
if err := cb.OnSendPacket(ctx, portID, channelID, sequence, timeoutHeight, timeoutTimestamp, dataBz, signer); err != nil {
return errorsmod.Wrapf(err, "send packet callback failed for portID %s channelID %s", portID, channelID)
}
}
return nil
}

// OnRecvPacket implements the IBCModule interface. A successful acknowledgement
// is returned if the packet data is successfully decoded and the receive application
// logic returns without error.
// A nil acknowledgement may be returned when using the packet forwarding feature. This signals to core IBC that the acknowledgement will be written asynchronously.
func (LegacyIBCModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
return nil
}

// OnAcknowledgementPacket implements the IBCModule interface
func (LegacyIBCModule) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
return nil
}

// OnTimeoutPacket implements the IBCModule interface
func (LegacyIBCModule) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
return nil
}

// OnChanUpgradeInit implements the IBCModule interface
func (LegacyIBCModule) OnChanUpgradeInit(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) (string, error) {
return "", nil
}

// OnChanUpgradeTry implements the IBCModule interface
func (LegacyIBCModule) OnChanUpgradeTry(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, counterpartyVersion string) (string, error) {
return "", nil
}

// OnChanUpgradeAck implements the IBCModule interface
func (LegacyIBCModule) OnChanUpgradeAck(ctx sdk.Context, portID, channelID, counterpartyVersion string) error {
return nil
}

// OnChanUpgradeOpen implements the IBCModule interface
func (LegacyIBCModule) OnChanUpgradeOpen(ctx sdk.Context, portID, channelID string, proposedOrder channeltypes.Order, proposedConnectionHops []string, proposedVersion string) {
}

// UnmarshalPacketData attempts to unmarshal the provided packet data bytes
// into a FungibleTokenPacketData. This function implements the optional
// PacketDataUnmarshaler interface required for ADR 008 support.
func (LegacyIBCModule) UnmarshalPacketData(ctx sdk.Context, portID, channelID string, bz []byte) (interface{}, error) {
return nil, nil
}
16 changes: 13 additions & 3 deletions modules/core/05-port/types/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"errors"
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -59,8 +60,17 @@ func (rtr *Router) HasRoute(module string) bool {

// GetRoute returns a IBCModule for a given module.
func (rtr *Router) GetRoute(module string) (IBCModule, bool) {
if !rtr.HasRoute(module) {
return nil, false
route, ok := rtr.routes[module]
if ok {
return route, true
}
return rtr.routes[module], true

// it's possible that some routes have been dynamically added e.g. with interchain accounts.
// in this case, we need to check if the module has the specified prefix.
for prefix := range rtr.routes {
if strings.Contains(module, prefix) {
return rtr.routes[prefix], true
}
}
return nil, false
}
Loading

0 comments on commit 832a0fc

Please sign in to comment.