From 85c5ddc635f2de71e6d44d758d9488a5d89db550 Mon Sep 17 00:00:00 2001 From: Dreamer Date: Fri, 26 Jul 2024 11:07:59 +0800 Subject: [PATCH] add tibc route --- modules/tibc/apps/mt_transfer/depinject.go | 10 +++++++- modules/tibc/apps/nft_transfer/depinject.go | 10 +++++++- modules/tibc/core/26-routing/types/router.go | 26 +++++++++++++++++++- modules/tibc/core/depinject.go | 26 ++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/modules/tibc/apps/mt_transfer/depinject.go b/modules/tibc/apps/mt_transfer/depinject.go index a6d613b8..3ca7b223 100644 --- a/modules/tibc/apps/mt_transfer/depinject.go +++ b/modules/tibc/apps/mt_transfer/depinject.go @@ -9,6 +9,7 @@ import ( modulev1 "github.com/bianjieai/tibc-go/api/tibc/apps/mt_transfer/module/v1" "github.com/bianjieai/tibc-go/modules/tibc/apps/mt_transfer/keeper" "github.com/bianjieai/tibc-go/modules/tibc/apps/mt_transfer/types" + routingtypes "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/types" ) // App Wiring Setup @@ -46,6 +47,7 @@ type Outputs struct { depinject.Out MtTransferKeeper keeper.Keeper + Route routingtypes.Route Module appmodule.AppModule } @@ -62,8 +64,14 @@ func ProvideModule(in Inputs) Outputs { in.PacketKeeper, in.ClientKeeper, ) + m := NewAppModule(keeper) + route := routingtypes.Route{ + Port: string(routingtypes.MT), + Module: m, + } return Outputs{ MtTransferKeeper: keeper, - Module: NewAppModule(keeper), + Route: route, + Module: m, } } diff --git a/modules/tibc/apps/nft_transfer/depinject.go b/modules/tibc/apps/nft_transfer/depinject.go index b87c1ad3..e0983dd0 100644 --- a/modules/tibc/apps/nft_transfer/depinject.go +++ b/modules/tibc/apps/nft_transfer/depinject.go @@ -9,6 +9,7 @@ import ( modulev1 "github.com/bianjieai/tibc-go/api/tibc/apps/nft_transfer/module/v1" "github.com/bianjieai/tibc-go/modules/tibc/apps/nft_transfer/keeper" "github.com/bianjieai/tibc-go/modules/tibc/apps/nft_transfer/types" + routingtypes "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/types" ) // App Wiring Setup @@ -46,6 +47,7 @@ type Outputs struct { depinject.Out NftTransferKeeper keeper.Keeper + Route routingtypes.Route Module appmodule.AppModule } @@ -62,8 +64,14 @@ func ProvideModule(in Inputs) Outputs { in.PacketKeeper, in.ClientKeeper, ) + m := NewAppModule(keeper) + route := routingtypes.Route{ + Port: string(routingtypes.NFT), + Module: m, + } return Outputs{ NftTransferKeeper: keeper, - Module: NewAppModule(keeper), + Route: route, + Module: m, } } diff --git a/modules/tibc/core/26-routing/types/router.go b/modules/tibc/core/26-routing/types/router.go index 67f1b0b8..dba9d532 100644 --- a/modules/tibc/core/26-routing/types/router.go +++ b/modules/tibc/core/26-routing/types/router.go @@ -4,13 +4,23 @@ import ( "fmt" ) -// The router is a map from module name to the TIBCModule +// Route is a TIBCModule and a Port +type Route struct { + Module TIBCModule + Port string +} + +// IsManyPerContainerType implements the depinject.ManyPerContainerType interface. +func (Route) IsManyPerContainerType() {} + +// Router is a map from module name to the TIBCModule // which contains all the module-defined callbacks required by TICS-26 type Router struct { routes map[string]TIBCModule sealed bool } +// NewRouter creates a new TIBC Router func NewRouter() *Router { return &Router{ routes: make(map[string]TIBCModule), @@ -45,6 +55,20 @@ func (rtr *Router) AddRoute(port Port, cbs TIBCModule) *Router { return rtr } +// Add adds TIBCModule for a given module name. It returns the Router +// so Add calls can be linked. It will panic if the Router is sealed. +func (rtr *Router) Add(r Route) *Router { + if rtr.sealed { + panic(fmt.Sprintf("router sealed; cannot register %s route callbacks", r.Port)) + } + if rtr.HasRoute(Port(r.Port)) { + panic(fmt.Sprintf("route %s has already been registered", r.Port)) + } + + rtr.routes[r.Port] = r.Module + return rtr +} + // HasRoute returns true if the Router has a module registered or false otherwise. func (rtr *Router) HasRoute(port Port) bool { _, ok := rtr.routes[string(port)] diff --git a/modules/tibc/core/depinject.go b/modules/tibc/core/depinject.go index c0f0fce8..c55aa1da 100644 --- a/modules/tibc/core/depinject.go +++ b/modules/tibc/core/depinject.go @@ -1,6 +1,8 @@ package tibc import ( + "slices" + "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" @@ -14,6 +16,7 @@ import ( clienttypes "github.com/bianjieai/tibc-go/modules/tibc/core/02-client/types" packetkeeper "github.com/bianjieai/tibc-go/modules/tibc/core/04-packet/keeper" routingkeeper "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/keeper" + routingtypes "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/types" "github.com/bianjieai/tibc-go/modules/tibc/core/keeper" ) @@ -21,6 +24,7 @@ import ( func init() { appmodule.Register(&modulev1.Module{}, appmodule.Provide(ProvideModule), + appmodule.Invoke(InvokeAddRoutes), ) } @@ -81,3 +85,25 @@ func ProvideModule(in Inputs) Outputs { RoutingKeeper: keeper.RoutingKeeper, } } + +// InvokeAddRoutes adds routes to the TIBC router +func InvokeAddRoutes(keeper *keeper.Keeper, routes []routingtypes.Route) { + if keeper == nil || routes == nil { + return + } + + // Default route order is a lexical sort by RouteKey. + // Explicit ordering can be added to the module config if required. + slices.SortFunc(routes, func(a, b routingtypes.Route) int { + if a.Port < b.Port { + return -1 + } + return 1 + }) + + router := routingtypes.NewRouter() + for _, r := range routes { + router.Add(r) + } + keeper.SetRouter(router) +}