Skip to content

feat: Prefix-based routing in IBCv2 Router #8303

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/DataDog/datadog-go v4.8.3+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
Expand Down
26 changes: 18 additions & 8 deletions modules/core/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import (
"errors"
"fmt"

radix "github.com/armon/go-radix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Router contains all the module-defined callbacks required by IBC Protocol V2.
type Router struct {
// routes is a map from portID to IBCModule
routes map[string]IBCModule
// routes is a radix trie that provides a prefix-based
// look-up structure. It maps portIDs and their prefixes to
// IBCModules.
routes radix.Tree
}

// NewRouter creates a new Router instance.
func NewRouter() *Router {
return &Router{
routes: make(map[string]IBCModule),
routes: *radix.New(),
}
}

// AddRoute registers a route for a given portID to a given IBCModule.
// AddRoute registers a route for a given port ID prefix to a given IBCModule.
func (rtr *Router) AddRoute(portID string, cbs IBCModule) *Router {
if !sdk.IsAlphaNumeric(portID) {
panic(errors.New("route expressions can only contain alphanumeric characters"))
Expand All @@ -30,22 +33,29 @@ func (rtr *Router) AddRoute(portID string, cbs IBCModule) *Router {
panic(fmt.Errorf("route %s has already been registered", portID))
}

rtr.routes[portID] = cbs
rtr.routes.Insert(portID, cbs)

return rtr
}

// Route returns the IBCModule for a given portID.
func (rtr *Router) Route(portID string) IBCModule {
route, ok := rtr.routes[portID]
_, route, ok := rtr.routes.LongestPrefix(portID)
if !ok {
panic(fmt.Sprintf("no route for %s", portID))
}
return route
return route.(IBCModule)
}

// HasRoute returns true if the Router has a module registered for the portID or false otherwise.
func (rtr *Router) HasRoute(portID string) bool {
_, ok := rtr.routes[portID]
_, ok := rtr.routes.Get(portID)
return ok
}

// HasPrefixRoute returns true if the Router has a module registered for the given portID or its prefix.
// Returns false otherwise.
func (rtr *Router) HasPrefixRoute(portIDPrefix string) bool {
_, _, ok := rtr.routes.LongestPrefix(portIDPrefix)
return ok
}
14 changes: 14 additions & 0 deletions modules/core/api/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ func (suite *APITestSuite) TestRouter() {
suite.Require().True(router.HasRoute("port03"))
},
},
{
name: "success: prefix based routing works",
malleate: func() {
router.AddRoute("somemodule", &mockv2.IBCModule{})
router.AddRoute("port01", &mockv2.IBCModule{})
},
assertionFn: func() {
suite.Require().True(router.HasRoute("somemodule"))
suite.Require().False(router.HasRoute("somemoduleport01"))
suite.Require().True(router.HasPrefixRoute("somemoduleport01"))
suite.Require().NotNil(router.Route("somemoduleport01"))
suite.Require().True(router.HasRoute("port01"))
},
},
{
name: "failure: panics on duplicate module",
malleate: func() {
Expand Down