-
Notifications
You must be signed in to change notification settings - Fork 586
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
Adding GetChannelsWithPortPrefix function to 04-channel #2304
Changes from all commits
74dbd34
ebbcd36
b5e1e9f
b8bbe7d
9001e64
691748b
137439f
2ffd5e4
fc0c1a4
b44777e
5bd25fd
019afc6
1b663db
2707071
78697b5
e3e4fc5
afe7941
f404c27
ba22f69
d287003
0e61f33
d7ee861
b085b89
bd9ded4
4f695dc
c8364f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" | ||
"github.com/cosmos/ibc-go/v6/modules/core/04-channel/types" | ||
ibctesting "github.com/cosmos/ibc-go/v6/testing" | ||
ibcmock "github.com/cosmos/ibc-go/v6/testing/mock" | ||
|
@@ -81,6 +83,87 @@ func (suite *KeeperTestSuite) TestGetAppVersion() { | |
suite.Require().Equal(ibcmock.Version, channelVersion) | ||
} | ||
|
||
// TestGetAllChannelsWithPortPrefix verifies ports are filtered correctly using a port prefix. | ||
func (suite *KeeperTestSuite) TestGetAllChannelsWithPortPrefix() { | ||
|
||
const ( | ||
secondChannelID = "channel-1" | ||
differentChannelPortID = "different-portid" | ||
) | ||
|
||
allChannels := []types.IdentifiedChannel{ | ||
types.NewIdentifiedChannel(transfertypes.PortID, ibctesting.FirstChannelID, types.Channel{}), | ||
types.NewIdentifiedChannel(differentChannelPortID, secondChannelID, types.Channel{}), | ||
} | ||
|
||
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great suggestion @colin-axner , this makes things much cleaner. |
||
tests := []struct { | ||
name string | ||
prefix string | ||
allChannels []types.IdentifiedChannel | ||
expectedChannels []types.IdentifiedChannel | ||
}{ | ||
{ | ||
name: "transfer channel is retrieved with prefix", | ||
prefix: "tra", | ||
allChannels: allChannels, | ||
expectedChannels: []types.IdentifiedChannel{types.NewIdentifiedChannel(transfertypes.TypeMsgTransfer, ibctesting.FirstChannelID, types.Channel{})}, | ||
}, | ||
{ | ||
name: "matches port with full name as prefix", | ||
prefix: transfertypes.TypeMsgTransfer, | ||
allChannels: allChannels, | ||
expectedChannels: []types.IdentifiedChannel{types.NewIdentifiedChannel(transfertypes.TypeMsgTransfer, ibctesting.FirstChannelID, types.Channel{})}, | ||
}, | ||
{ | ||
name: "no ports match prefix", | ||
prefix: "wont-match-anything", | ||
allChannels: allChannels, | ||
expectedChannels: nil, | ||
}, | ||
{ | ||
name: "empty prefix matches everything", | ||
prefix: "", | ||
allChannels: allChannels, | ||
expectedChannels: allChannels, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
for _, ch := range tc.allChannels { | ||
suite.chainA.GetSimApp().GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), ch.PortId, ch.ChannelId, types.Channel{}) | ||
} | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ctxA := suite.chainA.GetContext() | ||
|
||
actualChannels := suite.chainA.GetSimApp().GetIBCKeeper().ChannelKeeper.GetAllChannelsWithPortPrefix(ctxA, tc.prefix) | ||
|
||
suite.Require().True(containsAll(tc.expectedChannels, actualChannels)) | ||
}) | ||
} | ||
} | ||
|
||
// containsAll verifies if all elements in the expected slice exist in the actual slice | ||
// independent of order. | ||
func containsAll(expected, actual []types.IdentifiedChannel) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this function as I saw that the order of the values returned in |
||
for _, expectedChannel := range expected { | ||
foundMatch := false | ||
for _, actualChannel := range actual { | ||
if reflect.DeepEqual(actualChannel, expectedChannel) { | ||
foundMatch = true | ||
break | ||
} | ||
} | ||
if !foundMatch { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// TestGetAllChannels creates multiple channels on chain A through various connections | ||
// and tests their retrieval. 2 channels are on connA0 and 1 channel is on connA1 | ||
func (suite KeeperTestSuite) TestGetAllChannels() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missed this condition before the tests!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This is a good addition