Skip to content

Commit

Permalink
allow client identifiers in ics20 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gjermundgaraba committed Nov 10, 2024
1 parent d620d31 commit 862a023
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion modules/apps/transfer/types/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cmttypes "github.com/cometbft/cometbft/types"

channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
)

// NewDenom creates a new Denom instance given the base denomination and a variable number of hops.
Expand Down Expand Up @@ -167,7 +168,7 @@ func ExtractDenomFromPath(fullPath string) Denom {
// will be incorrectly parsed, but the token will continue to be treated correctly
// as an IBC denomination. The hash used to store the token internally on our chain
// will be the same value as the base denomination being correctly parsed.
if i < length-1 && length > 2 && channeltypes.IsValidChannelID(denomSplit[i+1]) {
if i < length-1 && length > 2 && (channeltypes.IsValidChannelID(denomSplit[i+1]) || clienttypes.IsValidClientID(denomSplit[i+1])) {
trace = append(trace, NewHop(denomSplit[i], denomSplit[i+1]))
} else {
baseDenomSlice = denomSplit[i:]
Expand Down
30 changes: 30 additions & 0 deletions modules/apps/transfer/types/denom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func (suite *TypesTestSuite) TestDenomsValidate() {
types.Denoms{types.NewDenom("uatom", types.NewHop("transfer", "channel-1"), types.NewHop("transfer", "channel-2"))},
nil,
},
{
"valid trace with client id",
types.Denoms{types.NewDenom("uatom", types.NewHop("transfer", "07-tendermint-0"))},
nil,
},
{
"valid multiple trace info",
types.Denoms{
Expand Down Expand Up @@ -79,6 +84,16 @@ func (suite *TypesTestSuite) TestPath() {
types.NewDenom("uatom", types.NewHop("transfer", "channel-0")),
"transfer/channel-0/uatom",
},
{
"1 hop denom with client id",
types.NewDenom("uatom", types.NewHop("transfer", "07-tendermint-0")),
"transfer/07-tendermint-0/uatom",
},
{
"1 hop denom with client id and slashes",
types.NewDenom("gamm/pool/osmo", types.NewHop("transfer", "07-tendermint-0")),
"transfer/07-tendermint-0/gamm/pool/osmo",
},
{
"2 hop denom",
types.NewDenom("uatom", types.NewHop("transfer", "channel-0"), types.NewHop("transfer", "channel-52")),
Expand Down Expand Up @@ -202,6 +217,13 @@ func (suite *TypesTestSuite) TestDenomChainSource() {
"channel-0",
false,
},
{
"sender chain is source: single trace with client id",
types.NewDenom("ubtc", types.NewHop("transfer", "07-tendermint-0")),
"transfer",
"channel-0",
false,
},
{
"sender chain is source: swapped portID and channelID",
types.NewDenom("uatom", types.NewHop("transfer", "channel-0")),
Expand All @@ -226,6 +248,13 @@ func (suite *TypesTestSuite) TestDenomChainSource() {
"channel-0",
true,
},
{
"receiver chain is source: single trace with client id",
types.NewDenom("ubtc", types.NewHop("transfer", "07-tendermint-0")),
"transfer",
"07-tendermint-0",
true,
},
{
"receiver chain is source: multi-trace",
types.NewDenom("uatom", types.NewHop("transfer", "channel-0"), types.NewHop("transfer", "channel-52")),
Expand Down Expand Up @@ -284,6 +313,7 @@ func TestExtractDenomFromPath(t *testing.T) {
{"base denom with trailing slash", "atom/", types.NewDenom("atom/")},
{"base denom multiple trailing slash", "foo///bar//baz/atom/", types.NewDenom("foo///bar//baz/atom/")},
{"ibc denom one hop", "transfer/channel-0/atom", types.NewDenom("atom", types.NewHop("transfer", "channel-0"))},
{"ibc denom one hop with client id", "transfer/07-tendermint-0/atom", types.NewDenom("atom", types.NewHop("transfer", "07-tendermint-0"))},
{"ibc denom one hop trailing slash", "transfer/channel-0/atom/", types.NewDenom("atom/", types.NewHop("transfer", "channel-0"))},
{"ibc denom one hop multiple slashes", "transfer/channel-0//at/om/", types.NewDenom("/at/om/", types.NewHop("transfer", "channel-0"))},
{"ibc denom two hops", "transfer/channel-0/transfer/channel-60/atom", types.NewDenom("atom", types.NewHop("transfer", "channel-0"), types.NewHop("transfer", "channel-60"))},
Expand Down
39 changes: 39 additions & 0 deletions modules/core/04-channel/v2/types/commitment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types_test

import (
"encoding/hex"
"encoding/json"
"testing"

transfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
"github.com/stretchr/testify/require"
)

func TestCommitPacket(t *testing.T) {
transferData, err := json.Marshal(transfertypes.FungibleTokenPacketData{
Denom: "uatom",
Amount: "1000000",
Sender: "sender",
Receiver: "receiver",
Memo: "memo",
})
require.NoError(t, err)
packet := types.Packet{
Sequence: 1,
SourceChannel: "channel-0",
DestinationChannel: "channel-1",
TimeoutTimestamp: 100,
Payloads: []types.Payload{
{
SourcePort: transfertypes.PortID,
DestinationPort: transfertypes.PortID,
Version: transfertypes.V1,
Encoding: "application/json",
Value: transferData,
},
},
}
commitment := types.CommitPacket(packet)
require.Equal(t, "c75fb6745b83fe67fb01d11cc01de73f9203386cb20f5ae6102080ae07e28a24", hex.EncodeToString(commitment))
}

0 comments on commit 862a023

Please sign in to comment.