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

Adds Generic Middleware helper functions #383

Merged
merged 7 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* [\#373](https://github.com/cosmos/ibc-go/pull/375) Added optional field `PacketCommitmentSequences` to `QueryPacketAcknowledgementsRequest` to provide filtering of packet acknowledgements.
* [\#383](https://github.com/cosmos/ibc-go/pull/383) Adds helper functions for merging and splitting middleware versions from the underlying app version.
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved

### Features

Expand Down
27 changes: 27 additions & 0 deletions modules/core/04-channel/types/version.go
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)
}
72 changes: 72 additions & 0 deletions modules/core/04-channel/types/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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
}{
{
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
"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",
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
[]string{"ics20-1"},
"ics20-1",
},
{
"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")
}
}