Skip to content

Commit

Permalink
Merge branch 'main' of github.com:cosmos/ibc-go into colin/6362-typed…
Browse files Browse the repository at this point in the history
…-trace
  • Loading branch information
colin-axner committed May 30, 2024
2 parents 369e9f3 + 80b0c9a commit 0f872d9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
36 changes: 18 additions & 18 deletions modules/apps/transfer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,15 +1096,6 @@ func (suite *KeeperTestSuite) TestPacketForwardsCompatibility() {
expError error
expAckError error
}{
{
"success: new field v2",
func() {
jsonString := fmt.Sprintf(`{"tokens":[{"denom": {"base": "atom", "trace": []},"amount":"100"}],"sender":"%s","receiver":"%s", "new_field":"value"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
nil,
nil,
},
{
"success: no new field with memo v2",
func() {
Expand Down Expand Up @@ -1132,24 +1123,22 @@ func (suite *KeeperTestSuite) TestPacketForwardsCompatibility() {
ibcerrors.ErrInvalidType,
},
{
"failure: missing field v2",
"failure: new field v2",
func() {
jsonString := fmt.Sprintf(`{"tokens":[{"denom": {"trace": []},"amount":"100"}],"sender":"%s","receiver":"%s"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
jsonString := fmt.Sprintf(`{"tokens":[{"denom": {"base": "atom", "trace": []},"amount":"100"}],"sender":"%s","receiver":"%s", "new_field":"value"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
types.ErrInvalidDenomForTransfer,
ibcerrors.ErrInvalidType,
ibcerrors.ErrInvalidType,
},
{
"success: new field",
"failure: missing field v2",
func() {
path.EndpointA.ChannelConfig.Version = types.V1
path.EndpointB.ChannelConfig.Version = types.V1
jsonString := fmt.Sprintf(`{"denom":"denom","amount":"100","sender":"%s","receiver":"%s","memo":"memo","new_field":"value"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
jsonString := fmt.Sprintf(`{"tokens":[{"denom": {"trace": []},"amount":"100"}],"sender":"%s","receiver":"%s"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
nil,
nil,
types.ErrInvalidDenomForTransfer,
ibcerrors.ErrInvalidType,
},
{
"success: no new field with memo",
Expand Down Expand Up @@ -1183,6 +1172,17 @@ func (suite *KeeperTestSuite) TestPacketForwardsCompatibility() {
ibcerrors.ErrInvalidType,
ibcerrors.ErrInvalidType,
},
{
"failure: new field",
func() {
path.EndpointA.ChannelConfig.Version = types.V1
path.EndpointB.ChannelConfig.Version = types.V1
jsonString := fmt.Sprintf(`{"denom":"denom","amount":"100","sender":"%s","receiver":"%s","memo":"memo","new_field":"value"}`, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String())
packetData = []byte(jsonString)
},
ibcerrors.ErrInvalidType,
ibcerrors.ErrInvalidType,
},
{
"failure: missing field",
func() {
Expand Down
46 changes: 46 additions & 0 deletions modules/apps/transfer/types/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

import (
"bytes"
"encoding/json"
)

// UnmarshalJSON implements the Unmarshaller interface for FungibleTokenPacketDataV2.
func (ftpd *FungibleTokenPacketDataV2) UnmarshalJSON(bz []byte) error {
// Recursion protection. We cannot unmarshal into FungibleTokenPacketData directly
// else UnmarshalJSON is going to get invoked again, ad infinum. Create an alias
// and unmarshal into that, instead.
type ftpdAlias FungibleTokenPacketDataV2

d := json.NewDecoder(bytes.NewReader(bz))
// Raise errors during decoding if unknown fields are encountered.
d.DisallowUnknownFields()

var alias ftpdAlias
if err := d.Decode(&alias); err != nil {
return err
}

*ftpd = FungibleTokenPacketDataV2(alias)
return nil
}

// UnmarshalJSON implements the Unmarshaller interface for FungibleTokenPacketData.
func (ftpd *FungibleTokenPacketData) UnmarshalJSON(bz []byte) error {
// Recursion protection. We cannot unmarshal into FungibleTokenPacketData directly
// else UnmarshalJSON is going to get invoked again, ad infinum. Create an alias
// and unmarshal into that, instead.
type ftpdAlias FungibleTokenPacketData

d := json.NewDecoder(bytes.NewReader(bz))
// Raise errors during decoding if unknown fields are encountered.
d.DisallowUnknownFields()

var alias ftpdAlias
if err := d.Decode(&alias); err != nil {
return err
}

*ftpd = FungibleTokenPacketData(alias)
return nil
}
27 changes: 27 additions & 0 deletions modules/apps/transfer/types/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,30 @@ func TestValidateIBCDenom(t *testing.T) {
require.NoError(t, err, tc.name)
}
}

func TestExtractDenomFromFullPath(t *testing.T) {
testCases := []struct {
name string
fullPath string
expDenom types.Denom
}{
{"base denom no slashes", "atom", types.Denom{Base: "atom"}},
{"base denom with trailing slash", "atom/", types.Denom{Base: "atom/"}},
{"base denom multiple trailing slash", "foo///bar//baz/atom/", types.Denom{Base: "foo///bar//baz/atom/"}},
{"ibc denom one hop", "transfer/channel-0/atom", types.Denom{Base: "atom", Trace: []string{"transfer/channel-0"}}},

Check failure on line 136 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal

Check failure on line 136 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal
{"ibc denom one hop trailing slash", "transfer/channel-0/atom/", types.Denom{Base: "atom/", Trace: []string{"transfer/channel-0"}}},

Check failure on line 137 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal

Check failure on line 137 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal
{"ibc denom two hops", "transfer/channel-0/transfer/channel-60/atom", types.Denom{Base: "atom", Trace: []string{"transfer/channel-0", "transfer/channel-60"}}},

Check failure on line 138 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal

Check failure on line 138 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal
{"ibc denom two hops trailing slash", "transfer/channel-0/transfer/channel-60/atom/", types.Denom{Base: "atom/", Trace: []string{"transfer/channel-0", "transfer/channel-60"}}},

Check failure on line 139 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal (typecheck)

Check failure on line 139 in modules/apps/transfer/types/trace_test.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use []string{…} (value of type []string) as []"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types".Trace value in struct literal
{"empty prefix", "/uatom", types.Denom{Base: "/uatom"}},
{"empty identifiers", "//uatom", types.Denom{Base: "//uatom"}},
{"base denom with single '/'", "erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA", types.Denom{Base: "erc20/0x85bcBCd7e79Ec36f4fBBDc54F90C643d921151AA"}},
{"single trace identifier", "transfer/", types.Denom{Base: "transfer/"}},
}

for _, tc := range testCases {
tc := tc

denom := types.ExtractDenomFromFullPath(tc.fullPath)
require.Equal(t, tc.expDenom, denom, tc.name)
}
}

0 comments on commit 0f872d9

Please sign in to comment.