-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package api_test | ||
|
||
import ( | ||
testifysuite "github.com/stretchr/testify/suite" | ||
"testing" | ||
) | ||
|
||
type ApiTestSuite struct { | ||
Check failure on line 8 in modules/core/api/api_test.go GitHub Actions / lint
|
||
testifysuite.Suite | ||
} | ||
|
||
func TestApiTestSuite(t *testing.T) { | ||
testifysuite.Run(t, new(ApiTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"strings" | ||
) | ||
|
||
// Router contains all the module-defined callbacks required by IBC Protocol V2. | ||
type Router struct { | ||
routes map[string]IBCModule | ||
} | ||
|
||
// NewRouter creates a new Router instance. | ||
func NewRouter() *Router { | ||
return &Router{ | ||
routes: make(map[string]IBCModule), | ||
} | ||
} | ||
|
||
// AddRoute registers a route for a given module name. | ||
func (rtr *Router) AddRoute(module string, cbs IBCModule) *Router { | ||
if !sdk.IsAlphaNumeric(module) { | ||
panic(errors.New("route expressions can only contain alphanumeric characters")) | ||
} | ||
|
||
if rtr.HasRoute(module) { | ||
panic(errors.New(fmt.Sprintf("route %s has already been registered", module))) | ||
Check failure on line 29 in modules/core/api/router.go GitHub Actions / lint
|
||
} | ||
|
||
rtr.routes[module] = cbs | ||
|
||
return rtr | ||
} | ||
|
||
// Route returns the IBCModule for a given module name. | ||
func (rtr *Router) Route(module string) IBCModule { | ||
route, ok := rtr.routeOrPrefixedRoute(module) | ||
if !ok { | ||
panic(fmt.Sprintf("no route for %s", module)) | ||
} | ||
return route | ||
} | ||
|
||
// HasRoute returns true if the Router has a module registered or false otherwise. | ||
func (rtr *Router) HasRoute(module string) bool { | ||
_, ok := rtr.routeOrPrefixedRoute(module) | ||
return ok | ||
} | ||
|
||
// routeOrPrefixedRoute returns the IBCModule for a given module name. | ||
// if an exact match is not found, a route with the provided prefix is searched for instead. | ||
func (rtr *Router) routeOrPrefixedRoute(module string) (IBCModule, bool) { | ||
route, ok := rtr.routes[module] | ||
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(module, prefix) { | ||
return rtr.routes[prefix], true | ||
} | ||
} | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package api_test | ||
|
||
import ( | ||
"github.com/cosmos/ibc-go/v9/modules/core/api" | ||
mockv2 "github.com/cosmos/ibc-go/v9/testing/mock/v2" | ||
) | ||
|
||
func (suite *ApiTestSuite) TestRouteer() { | ||
var router *api.Router | ||
|
||
var testCases = []struct { | ||
name string | ||
malleate func() | ||
assertionFn func() | ||
}{ | ||
{ | ||
name: "success", | ||
malleate: func() { | ||
router.AddRoute("module01", &mockv2.IBCModule{}) | ||
}, | ||
assertionFn: func() { | ||
suite.Require().True(router.HasRoute("module01")) | ||
}, | ||
}, | ||
{ | ||
name: "success: multiple modules", | ||
malleate: func() { | ||
router.AddRoute("module01", &mockv2.IBCModule{}) | ||
router.AddRoute("module02", &mockv2.IBCModule{}) | ||
router.AddRoute("module03", &mockv2.IBCModule{}) | ||
}, | ||
assertionFn: func() { | ||
suite.Require().True(router.HasRoute("module01")) | ||
suite.Require().True(router.HasRoute("module02")) | ||
suite.Require().True(router.HasRoute("module03")) | ||
}, | ||
}, | ||
{ | ||
name: "success: find by prefix", | ||
malleate: func() { | ||
router.AddRoute("module01", &mockv2.IBCModule{}) | ||
}, | ||
assertionFn: func() { | ||
suite.Require().True(router.HasRoute("module01-foo")) | ||
}, | ||
}, | ||
{ | ||
name: "failure: panics on duplicate module", | ||
malleate: func() { | ||
router.AddRoute("module01", &mockv2.IBCModule{}) | ||
}, | ||
assertionFn: func() { | ||
suite.Require().PanicsWithError("route module01 has already been registered", func() { | ||
router.AddRoute("module01", &mockv2.IBCModule{}) | ||
}) | ||
}, | ||
}, | ||
{ | ||
name: "failure: panics invalid-name", | ||
malleate: func() { | ||
router.AddRoute("module01", &mockv2.IBCModule{}) | ||
}, | ||
assertionFn: func() { | ||
suite.Require().PanicsWithError("route expressions can only contain alphanumeric characters", func() { | ||
router.AddRoute("module-02", &mockv2.IBCModule{}) | ||
}) | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
suite.Run(tc.name, func() { | ||
router = api.NewRouter() | ||
|
||
tc.malleate() | ||
|
||
tc.assertionFn() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
channeltypesv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types" | ||
"github.com/cosmos/ibc-go/v9/modules/core/api" | ||
) | ||
|
||
var _ api.IBCModule = (*IBCModule)(nil) | ||
|
||
type IBCModule struct{} | ||
|
||
func (I IBCModule) OnSendPacket(ctx context.Context, sourceID string, destinationID string, sequence uint64, data channeltypesv2.PacketData, signer sdk.AccAddress) error { | ||
Check failure on line 14 in testing/mock/v2/ibc_module.go GitHub Actions / lint
|
||
panic("implement me") | ||
} |