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

fix to correctly parse denoms with slashes in the base denom #1451

Merged
25 changes: 23 additions & 2 deletions modules/apps/transfer/types/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"regexp"
"sort"
"strings"

Expand All @@ -12,6 +13,7 @@ import (
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmtypes "github.com/tendermint/tendermint/types"

channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
)

Expand All @@ -21,6 +23,9 @@ import (
// Examples:
//
// - "portidone/channelidone/uatom" => DenomTrace{Path: "portidone/channelidone", BaseDenom: "uatom"}
// - "portidone/channelidone/portidtwo/channelidtwo/uatom" => DenomTrace{Path: "portidone/channelidone/portidtwo/channelidtwo", BaseDenom: "uatom"}
// - "portidone/channelidone/gamm/pool/1" => DenomTrace{Path: "portidone/channelidone", BaseDenom: "gamm/pool/1"}
// - "gamm/pool/1" => DenomTrace{Path: "", BaseDenom: "gamm/pool/1"}
// - "uatom" => DenomTrace{Path: "", BaseDenom: "uatom"}
func ParseDenomTrace(rawDenom string) DenomTrace {
denomSplit := strings.Split(rawDenom, "/")
Expand All @@ -32,9 +37,25 @@ func ParseDenomTrace(rawDenom string) DenomTrace {
}
}

path := []string{}
baseDenom := []string{}
length := len(denomSplit)
r, err := regexp.Compile(fmt.Sprintf("%s[0-9]+", channeltypes.ChannelPrefix))
if err != nil {
panic("could not compile regexp 'channel-[0-9]+'")
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
}
for i := 0; i < length; i = i + 2 {
if i < length-1 && denomSplit[i] == PortID && r.MatchString(denomSplit[i+1]) {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
path = append(path, denomSplit[i], denomSplit[i+1])
} else {
baseDenom = denomSplit[i:]
break
}
}

return DenomTrace{
Path: strings.Join(denomSplit[:len(denomSplit)-1], "/"),
BaseDenom: denomSplit[len(denomSplit)-1],
Path: strings.Join(path, "/"),
BaseDenom: strings.Join(baseDenom, "/"),
}
}

Expand Down
25 changes: 21 additions & 4 deletions modules/apps/transfer/types/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@ func TestParseDenomTrace(t *testing.T) {
}{
{"empty denom", "", DenomTrace{}},
{"base denom", "uatom", DenomTrace{BaseDenom: "uatom"}},
{"trace info", "transfer/channelToA/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/channelToA"}},
{"incomplete path", "transfer/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer"}},
{"invalid path (1)", "transfer//uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/"}},
{"invalid path (2)", "transfer/channelToA/uatom/", DenomTrace{BaseDenom: "", Path: "transfer/channelToA/uatom"}},
{"base denom ending with '/'", "uatom/", DenomTrace{BaseDenom: "uatom/"}},
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
{"base denom with single '/'s", "gamm/pool/1", DenomTrace{BaseDenom: "gamm/pool/1"}},
{"base denom with double '/'s", "gamm//pool//1", DenomTrace{BaseDenom: "gamm//pool//1"}},
{"trace info", "transfer/channel-0/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/channel-0"}},
{"trace info with base denom ending in '/'", "transfer/channel-0/uatom/", DenomTrace{BaseDenom: "uatom/", Path: "transfer/channel-0"}},
{"trace info with single '/' in base denom", "transfer/channel-0/erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA", DenomTrace{BaseDenom: "erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA", Path: "transfer/channel-0"}},
{"trace info with multiple '/'s in base denom", "transfer/channel-0/gamm/pool/1", DenomTrace{BaseDenom: "gamm/pool/1", Path: "transfer/channel-0"}},
{"trace info with multiple double '/'s in base denom", "transfer/channel-0/gamm//pool//1", DenomTrace{BaseDenom: "gamm//pool//1", Path: "transfer/channel-0"}},
{"trace info with multiple port/channel pairs", "transfer/channel-0/transfer/channel-1/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/channel-0/transfer/channel-1"}},
{"incomplete path", "transfer/uatom", DenomTrace{BaseDenom: "transfer/uatom"}},
{"invalid path (1)", "transfer//uatom", DenomTrace{BaseDenom: "transfer//uatom"}},
{"invalid path (2)", "transfer/channelToA/uatom", DenomTrace{BaseDenom: "transfer/channelToA/uatom"}},
{"invalid path (3)", "channel-0/transfer/uatom", DenomTrace{BaseDenom: "channel-0/transfer/uatom"}},
{"invalid path (4)", "uatom/transfer", DenomTrace{BaseDenom: "uatom/transfer"}},
{"invalid path (5)", "transfer/channel-0", DenomTrace{Path: "transfer/channel-0"}},
{"invalid path (6)", "transfer/channel-0/", DenomTrace{Path: "transfer/channel-0"}},
{"invalid path (7)", "transfer/channelToA", DenomTrace{BaseDenom: "transfer/channelToA"}},
{"invalid path (8)", "transfer/channel-0/transfer", DenomTrace{BaseDenom: "transfer", Path: "transfer/channel-0"}},
{"invalid path (9)", "transfer/channel-0/transfer/channel-1", DenomTrace{Path: "transfer/channel-0/transfer/channel-1"}},
{"invalid path (10)", "transfer/channel-0/transfer/channelToB", DenomTrace{BaseDenom: "transfer/channelToB", Path: "transfer/channel-0"}},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -131,6 +147,7 @@ func TestValidateIBCDenom(t *testing.T) {
}{
{"denom with trace hash", "ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", false},
{"base denom", "uatom", false},
{"base denom ending with '/'", "uatom/", false},
{"base denom with single '/'s", "gamm/pool/1", false},
{"base denom with double '/'s", "gamm//pool//1", false},
{"non-ibc prefix with hash", "notibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", false},
Expand Down