-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Generic Middleware helper functions (#383)
* add generic middleware helper functions * Update modules/core/04-channel/types/version.go Co-authored-by: colin axnér <[email protected]> * remove app capability, add merge fn, changelog * add back newline Co-authored-by: colin axnér <[email protected]>
- Loading branch information
1 parent
2d3181b
commit 4f63be0
Showing
3 changed files
with
106 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
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,27 @@ | ||
package types | ||
|
||
import "strings" | ||
|
||
const ChannelVersionDelimiter = ":" | ||
|
||
// SplitChannelVersion splits the channel version string | ||
// into the outermost middleware version and the underlying app version. | ||
// It will use the default delimiter `:` for middleware versions. | ||
// In case there's no delimeter, this function returns an empty string for the middleware version (first return argument), | ||
// and the full input as the second underlying app version. | ||
func SplitChannelVersion(version string) (middlewareVersion, appVersion string) { | ||
// only split out the first middleware version | ||
splitVersions := strings.Split(version, ChannelVersionDelimiter) | ||
if len(splitVersions) == 1 { | ||
return "", version | ||
} | ||
middlewareVersion = splitVersions[0] | ||
appVersion = strings.Join(splitVersions[1:], ChannelVersionDelimiter) | ||
return | ||
} | ||
|
||
// MergeChannelVersions merges the provided versions together with the channel version delimiter | ||
// the versions should be passed in from the highest-level middleware to the base application | ||
func MergeChannelVersions(versions ...string) string { | ||
return strings.Join(versions, ChannelVersionDelimiter) | ||
} |
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,77 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" | ||
) | ||
|
||
func TestSplitVersions(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
version string | ||
mwVersion string | ||
appVersion string | ||
}{ | ||
{ | ||
"single wrapped middleware", | ||
"fee29-1:ics20-1", | ||
"fee29-1", | ||
"ics20-1", | ||
}, | ||
{ | ||
"multiple wrapped middleware", | ||
"fee29-1:whitelist:ics20-1", | ||
"fee29-1", | ||
"whitelist:ics20-1", | ||
}, | ||
{ | ||
"no middleware", | ||
"ics20-1", | ||
"", | ||
"ics20-1", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
mwVersion, appVersion := types.SplitChannelVersion(tc.version) | ||
require.Equal(t, tc.mwVersion, mwVersion, "middleware version is unexpected for case: %s", tc.name) | ||
require.Equal(t, tc.appVersion, appVersion, "app version is unexpected for case: %s", tc.name) | ||
} | ||
} | ||
|
||
func TestMergeVersions(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
versions []string | ||
merged string | ||
}{ | ||
{ | ||
"single version", | ||
[]string{"ics20-1"}, | ||
"ics20-1", | ||
}, | ||
{ | ||
"empty version", | ||
[]string{}, | ||
"", | ||
}, | ||
{ | ||
"two versions", | ||
[]string{"fee29-1", "ics20-1"}, | ||
"fee29-1:ics20-1", | ||
}, | ||
{ | ||
"multiple versions", | ||
[]string{"fee29-1", "whitelist", "ics20-1"}, | ||
"fee29-1:whitelist:ics20-1", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
actual := types.MergeChannelVersions(tc.versions...) | ||
require.Equal(t, tc.merged, actual, "merged versions string does not equal expected value") | ||
} | ||
} |