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

fix: add tibc route #132

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion modules/tibc/apps/mt_transfer/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +47,7 @@ type Outputs struct {
depinject.Out

MtTransferKeeper keeper.Keeper
Route routingtypes.Route
Module appmodule.AppModule
}

Expand All @@ -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,
}
}
10 changes: 9 additions & 1 deletion modules/tibc/apps/nft_transfer/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +47,7 @@ type Outputs struct {
depinject.Out

NftTransferKeeper keeper.Keeper
Route routingtypes.Route
Module appmodule.AppModule
}

Expand All @@ -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,
}
}
26 changes: 25 additions & 1 deletion modules/tibc/core/26-routing/types/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)]
Expand Down
26 changes: 26 additions & 0 deletions modules/tibc/core/depinject.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tibc

import (
"slices"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"

Expand All @@ -14,13 +16,15 @@ 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"
)

// App Wiring Setup
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideModule),
appmodule.Invoke(InvokeAddRoutes),
)
}

Expand Down Expand Up @@ -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)
}
Loading