Skip to content

Commit

Permalink
Added AppRouterV2
Browse files Browse the repository at this point in the history
  • Loading branch information
bznein committed Sep 18, 2024
1 parent e0cce22 commit 373028a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
33 changes: 33 additions & 0 deletions modules/core/05-port/types/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,39 @@ type IBCModule interface {
) error
}

type IBCModuleV2 interface {
OnSendPacketV2(
ctx context.Context,
sourceID string,
sequence uint64,
timeoutTimestamp uint64,
payload channeltypes.Payload,
signer sdk.AccAddress,
) error

OnRecvPacketV2(
ctx context.Context,
packet channeltypes.PacketV2,
payload channeltypes.Payload,
relayer sdk.AccAddress,
) channeltypes.RecvPacketResult

OnAcknowledgementPacketV2(
ctx context.Context,
packet channeltypes.PacketV2,
payload channeltypes.Payload,
recvPacketResult channeltypes.RecvPacketResult,
relayer sdk.AccAddress,
) error

OnTimeoutPacketV2(
ctx context.Context,
packet channeltypes.PacketV2,
payload channeltypes.Payload,
relayer sdk.AccAddress,
) error
}

// UpgradableModule defines the callbacks required to perform a channel upgrade.
// Note: applications must ensure that state related to packet processing remains unmodified until the OnChanUpgradeOpen callback is executed.
// This guarantees that in-flight packets are correctly flushed using the existing channel parameters.
Expand Down
55 changes: 55 additions & 0 deletions modules/core/05-port/types/router_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package types

import (
"errors"
"fmt"
"strings"

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

// AppRouterV2 contains all the module-defined callbacks required by ICS-26
type AppRouterV2 struct {
routes map[string]IBCModuleV2
}

func NewAppRouter() *AppRouterV2 {
return &AppRouterV2{
routes: make(map[string]IBCModuleV2),
}
}

func (rtr *AppRouterV2) AddRoute(module string, cbs IBCModuleV2) *AppRouterV2 {
if !sdk.IsAlphaNumeric(module) {
panic(errors.New("route expressions can only contain alphanumeric characters"))
}

rtr.routes[module] = cbs

return rtr
}

func (rtr *AppRouterV2) Route(appName string) IBCModuleV2 {
route, ok := rtr.route(appName)
if !ok {
panic(fmt.Sprintf("no route for %s", appName))
}

return route
}

func (rtr *AppRouterV2) route(appName string) (IBCModuleV2, bool) {
route, ok := rtr.routes[appName]
if ok {
return route, 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.HasPrefix(appName, prefix) {
return rtr.routes[prefix], true
}
}
return nil, false
}

0 comments on commit 373028a

Please sign in to comment.