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
Show file tree
Hide file tree
Changes from 5 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
38 changes: 33 additions & 5 deletions modules/apps/transfer/types/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
//
// Examples:
//
// - "portidone/channelidone/uatom" => DenomTrace{Path: "portidone/channelidone", BaseDenom: "uatom"}
// - "transfer/channelidone/uatom" => DenomTrace{Path: "transfer/channelidone", BaseDenom: "uatom"}
// - "transfer/channelidone/transfer/channelidtwo/uatom" => DenomTrace{Path: "transfer/channelidone/transfer/channelidtwo", BaseDenom: "uatom"}
// - "transfer/channelidone/gamm/pool/1" => DenomTrace{Path: "transfer/channelidone", BaseDenom: "gamm/pool/1"}
// - "gamm/pool/1" => DenomTrace{Path: "", BaseDenom: "gamm/pool/1"}
// - "uatom" => DenomTrace{Path: "", BaseDenom: "uatom"}
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
func ParseDenomTrace(rawDenom string) DenomTrace {
denomSplit := strings.Split(rawDenom, "/")
Expand All @@ -32,9 +35,10 @@ func ParseDenomTrace(rawDenom string) DenomTrace {
}
}

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

Expand Down Expand Up @@ -70,6 +74,22 @@ func (dt DenomTrace) GetFullDenomPath() string {
return dt.GetPrefix() + dt.BaseDenom
}

func extractPathAndBaseDenomFromDenomTrace(denomTraceItems []string) (string, string) {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
path := []string{}
baseDenom := []string{}
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
length := len(denomTraceItems)
for i := 0; i < length; i = i + 2 {
if i < length-1 && length > 2 && denomTraceItems[i] == PortID {
path = append(path, denomTraceItems[i], denomTraceItems[i+1])
} else {
baseDenom = denomTraceItems[i:]
break
}
}

return strings.Join(path, "/"), strings.Join(baseDenom, "/")
}

func validateTraceIdentifiers(identifiers []string) error {
if len(identifiers) == 0 || len(identifiers)%2 != 0 {
return fmt.Errorf("trace info must come in pairs of port and channel identifiers '{portID}/{channelID}', got the identifiers: %s", identifiers)
Expand Down Expand Up @@ -143,8 +163,10 @@ func (t Traces) Sort() Traces {
// ValidatePrefixedDenom checks that the denomination for an IBC fungible token packet denom is correctly prefixed.
// The function will return no error if the given string follows one of the two formats:
//
// - Prefixed denomination: '{portIDN}/{channelIDN}/.../{portID0}/{channelID0}/baseDenom'
// - Prefixed denomination: 'transfer/{channelIDN}/.../transfer/{channelID0}/baseDenom'
// - Unprefixed denomination: 'baseDenom'
//
// 'baseDenom' may or may not contain '/'s
func ValidatePrefixedDenom(denom string) error {
denomSplit := strings.Split(denom, "/")
if denomSplit[0] == denom && strings.TrimSpace(denom) != "" {
Expand All @@ -156,7 +178,13 @@ func ValidatePrefixedDenom(denom string) error {
return sdkerrors.Wrap(ErrInvalidDenomForTransfer, "base denomination cannot be blank")
}

identifiers := denomSplit[:len(denomSplit)-1]
path, _ := extractPathAndBaseDenomFromDenomTrace(denomSplit)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if path == "" {
// NOTE: base denom contains slashes, so no base denomination validation
return nil
}

identifiers := strings.Split(path, "/")
return validateTraceIdentifiers(identifiers)
}

Expand Down
29 changes: 24 additions & 5 deletions modules/apps/transfer/types/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@ func TestParseDenomTrace(t *testing.T) {
}{
{"empty denom", "", DenomTrace{}},
{"base denom", "uatom", DenomTrace{BaseDenom: "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/channelToA/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/channelToA"}},
{"incomplete path", "transfer/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer"}},
{"trace info with base denom ending in '/'", "transfer/channelToA/uatom/", DenomTrace{BaseDenom: "uatom/", Path: "transfer/channelToA"}},
{"trace info with single '/' in base denom", "transfer/channelToA/erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA", DenomTrace{BaseDenom: "erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA", Path: "transfer/channelToA"}},
{"trace info with multiple '/'s in base denom", "transfer/channelToA/gamm/pool/1", DenomTrace{BaseDenom: "gamm/pool/1", Path: "transfer/channelToA"}},
{"trace info with multiple double '/'s in base denom", "transfer/channelToA/gamm//pool//1", DenomTrace{BaseDenom: "gamm//pool//1", Path: "transfer/channelToA"}},
{"trace info with multiple port/channel pairs", "transfer/channelToA/transfer/channelToB/uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/channelToA/transfer/channelToB"}},
{"incomplete path", "transfer/uatom", DenomTrace{BaseDenom: "transfer/uatom"}},
{"invalid path (1)", "transfer//uatom", DenomTrace{BaseDenom: "uatom", Path: "transfer/"}},
{"invalid path (2)", "transfer/channelToA/uatom/", DenomTrace{BaseDenom: "", Path: "transfer/channelToA/uatom"}},
{"invalid path (2)", "channelToA/transfer/uatom", DenomTrace{BaseDenom: "channelToA/transfer/uatom"}},
{"invalid path (3)", "uatom/transfer", DenomTrace{BaseDenom: "uatom/transfer"}},
{"invalid path (4)", "transfer/channelToA", DenomTrace{BaseDenom: "transfer/channelToA"}},
{"invalid path (5)", "transfer/channelToA/", DenomTrace{Path: "transfer/channelToA"}},
{"invalid path (6)", "transfer/channelToA/transfer", DenomTrace{BaseDenom: "transfer", Path: "transfer/channelToA"}},
{"invalid path (7)", "transfer/channelToA/transfer/channelToB", DenomTrace{Path: "transfer/channelToA/transfer/channelToB"}},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -49,6 +62,8 @@ func TestDenomTrace_Validate(t *testing.T) {
expError bool
}{
{"base denom only", DenomTrace{BaseDenom: "uatom"}, false},
{"base denom only with single '/'", DenomTrace{BaseDenom: "erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA"}, false},
{"base denom only with multiple '/'s", DenomTrace{BaseDenom: "gamm/pool/1"}, false},
{"empty DenomTrace", DenomTrace{}, true},
{"valid single trace info", DenomTrace{BaseDenom: "uatom", Path: "transfer/channelToA"}, false},
{"valid multiple trace info", DenomTrace{BaseDenom: "uatom", Path: "transfer/channelToA/transfer/channelToB"}, false},
Expand Down Expand Up @@ -104,12 +119,15 @@ func TestValidatePrefixedDenom(t *testing.T) {
expError bool
}{
{"prefixed denom", "transfer/channelToA/uatom", false},
{"prefixed denom with '/'", "transfer/channelToA/gamm/pool/1", false},
{"empty prefix", "/uatom", false},
{"empty identifiers", "//uatom", false},
{"base denom", "uatom", false},
{"base denom with single '/'", "erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA", false},
{"base denom with multiple '/'s", "gamm/pool/1", false},
{"invalid port ID", "(transfer)/channelToA/uatom", false},
{"empty denom", "", true},
{"empty prefix", "/uatom", true},
{"empty identifiers", "//uatom", true},
{"single trace identifier", "transfer/", true},
{"invalid port ID", "(transfer)/channelToA/uatom", true},
{"invalid channel ID", "transfer/(channelToA)/uatom", true},
}

Expand All @@ -131,6 +149,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