Skip to content

Commit 85c5ddc

Browse files
committed
add tibc route
1 parent 2100991 commit 85c5ddc

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

modules/tibc/apps/mt_transfer/depinject.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
modulev1 "github.com/bianjieai/tibc-go/api/tibc/apps/mt_transfer/module/v1"
1010
"github.com/bianjieai/tibc-go/modules/tibc/apps/mt_transfer/keeper"
1111
"github.com/bianjieai/tibc-go/modules/tibc/apps/mt_transfer/types"
12+
routingtypes "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/types"
1213
)
1314

1415
// App Wiring Setup
@@ -46,6 +47,7 @@ type Outputs struct {
4647
depinject.Out
4748

4849
MtTransferKeeper keeper.Keeper
50+
Route routingtypes.Route
4951
Module appmodule.AppModule
5052
}
5153

@@ -62,8 +64,14 @@ func ProvideModule(in Inputs) Outputs {
6264
in.PacketKeeper,
6365
in.ClientKeeper,
6466
)
67+
m := NewAppModule(keeper)
68+
route := routingtypes.Route{
69+
Port: string(routingtypes.MT),
70+
Module: m,
71+
}
6572
return Outputs{
6673
MtTransferKeeper: keeper,
67-
Module: NewAppModule(keeper),
74+
Route: route,
75+
Module: m,
6876
}
6977
}

modules/tibc/apps/nft_transfer/depinject.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
modulev1 "github.com/bianjieai/tibc-go/api/tibc/apps/nft_transfer/module/v1"
1010
"github.com/bianjieai/tibc-go/modules/tibc/apps/nft_transfer/keeper"
1111
"github.com/bianjieai/tibc-go/modules/tibc/apps/nft_transfer/types"
12+
routingtypes "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/types"
1213
)
1314

1415
// App Wiring Setup
@@ -46,6 +47,7 @@ type Outputs struct {
4647
depinject.Out
4748

4849
NftTransferKeeper keeper.Keeper
50+
Route routingtypes.Route
4951
Module appmodule.AppModule
5052
}
5153

@@ -62,8 +64,14 @@ func ProvideModule(in Inputs) Outputs {
6264
in.PacketKeeper,
6365
in.ClientKeeper,
6466
)
67+
m := NewAppModule(keeper)
68+
route := routingtypes.Route{
69+
Port: string(routingtypes.NFT),
70+
Module: m,
71+
}
6572
return Outputs{
6673
NftTransferKeeper: keeper,
67-
Module: NewAppModule(keeper),
74+
Route: route,
75+
Module: m,
6876
}
6977
}

modules/tibc/core/26-routing/types/router.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ import (
44
"fmt"
55
)
66

7-
// The router is a map from module name to the TIBCModule
7+
// Route is a TIBCModule and a Port
8+
type Route struct {
9+
Module TIBCModule
10+
Port string
11+
}
12+
13+
// IsManyPerContainerType implements the depinject.ManyPerContainerType interface.
14+
func (Route) IsManyPerContainerType() {}
15+
16+
// Router is a map from module name to the TIBCModule
817
// which contains all the module-defined callbacks required by TICS-26
918
type Router struct {
1019
routes map[string]TIBCModule
1120
sealed bool
1221
}
1322

23+
// NewRouter creates a new TIBC Router
1424
func NewRouter() *Router {
1525
return &Router{
1626
routes: make(map[string]TIBCModule),
@@ -45,6 +55,20 @@ func (rtr *Router) AddRoute(port Port, cbs TIBCModule) *Router {
4555
return rtr
4656
}
4757

58+
// Add adds TIBCModule for a given module name. It returns the Router
59+
// so Add calls can be linked. It will panic if the Router is sealed.
60+
func (rtr *Router) Add(r Route) *Router {
61+
if rtr.sealed {
62+
panic(fmt.Sprintf("router sealed; cannot register %s route callbacks", r.Port))
63+
}
64+
if rtr.HasRoute(Port(r.Port)) {
65+
panic(fmt.Sprintf("route %s has already been registered", r.Port))
66+
}
67+
68+
rtr.routes[r.Port] = r.Module
69+
return rtr
70+
}
71+
4872
// HasRoute returns true if the Router has a module registered or false otherwise.
4973
func (rtr *Router) HasRoute(port Port) bool {
5074
_, ok := rtr.routes[string(port)]

modules/tibc/core/depinject.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tibc
22

33
import (
4+
"slices"
5+
46
"cosmossdk.io/core/appmodule"
57
"cosmossdk.io/depinject"
68

@@ -14,13 +16,15 @@ import (
1416
clienttypes "github.com/bianjieai/tibc-go/modules/tibc/core/02-client/types"
1517
packetkeeper "github.com/bianjieai/tibc-go/modules/tibc/core/04-packet/keeper"
1618
routingkeeper "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/keeper"
19+
routingtypes "github.com/bianjieai/tibc-go/modules/tibc/core/26-routing/types"
1720
"github.com/bianjieai/tibc-go/modules/tibc/core/keeper"
1821
)
1922

2023
// App Wiring Setup
2124
func init() {
2225
appmodule.Register(&modulev1.Module{},
2326
appmodule.Provide(ProvideModule),
27+
appmodule.Invoke(InvokeAddRoutes),
2428
)
2529
}
2630

@@ -81,3 +85,25 @@ func ProvideModule(in Inputs) Outputs {
8185
RoutingKeeper: keeper.RoutingKeeper,
8286
}
8387
}
88+
89+
// InvokeAddRoutes adds routes to the TIBC router
90+
func InvokeAddRoutes(keeper *keeper.Keeper, routes []routingtypes.Route) {
91+
if keeper == nil || routes == nil {
92+
return
93+
}
94+
95+
// Default route order is a lexical sort by RouteKey.
96+
// Explicit ordering can be added to the module config if required.
97+
slices.SortFunc(routes, func(a, b routingtypes.Route) int {
98+
if a.Port < b.Port {
99+
return -1
100+
}
101+
return 1
102+
})
103+
104+
router := routingtypes.NewRouter()
105+
for _, r := range routes {
106+
router.Add(r)
107+
}
108+
keeper.SetRouter(router)
109+
}

0 commit comments

Comments
 (0)