Skip to content

Commit fa133cf

Browse files
colin-axnermergify[bot]
authored andcommitted
fix: skip emission of unpopulated memo field in ics20 (#2651)
## Description By setting `EmitDefaults` to false, we will not include an empty memo field in the marshaled json bytes closes: #2645 --- Before we can merge this PR, please make sure that all the following items have been checked off. If any of the checklist items are not applicable, please leave them but write a little note why. - [x] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules/10-structure.md). - [ ] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing) - [ ] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`) - [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code). - [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md` - [ ] Re-reviewed `Files changed` in the Github PR explorer - [ ] Review `Codecov Report` in the comment section below once CI passes (cherry picked from commit 393247b)
1 parent 625c02f commit fa133cf

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4242

4343
### State Machine Breaking
4444

45+
* (apps/transfer) [\#2651](https://github.com/cosmos/ibc-go/pull/2651) Introduce `mustProtoMarshalJSON` for ics20 packet data marshalling which will skip emission (marshalling) of the memo field if unpopulated (empty).
4546
* (27-interchain-accounts) [\#2580](https://github.com/cosmos/ibc-go/issues/2580) Removing port prefix requirement from the ICA host channel handshake
4647
* (transfer) [\#2377](https://github.com/cosmos/ibc-go/pull/2377) Adding `sequence` to `MsgTransferResponse`.
4748

modules/apps/transfer/types/codec.go

+33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package types
22

33
import (
4+
"bytes"
5+
46
"github.com/cosmos/cosmos-sdk/codec"
57
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
68
sdk "github.com/cosmos/cosmos-sdk/types"
79
"github.com/cosmos/cosmos-sdk/types/msgservice"
10+
"github.com/gogo/protobuf/jsonpb"
11+
"github.com/gogo/protobuf/proto"
812
)
913

1014
// RegisterLegacyAminoCodec registers the necessary x/ibc transfer interfaces and concrete types
@@ -39,3 +43,32 @@ func init() {
3943
RegisterLegacyAminoCodec(amino)
4044
amino.Seal()
4145
}
46+
47+
// mustProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded
48+
// bytes of a message.
49+
// NOTE: Copied from https://github.com/cosmos/cosmos-sdk/blob/971c542453e0972ef1dfc5a80159ad5049c7211c/codec/json.go
50+
// and modified in order to allow `EmitDefaults` to be set to false for ics20 packet marshalling.
51+
// This allows for the introduction of the memo field to be backwards compatible.
52+
func mustProtoMarshalJSON(msg proto.Message) []byte {
53+
anyResolver := codectypes.NewInterfaceRegistry()
54+
55+
// EmitDefaults is set to false to prevent marshalling of unpopulated fields (memo)
56+
// OrigName and the anyResovler match the fields the original SDK function would expect
57+
// in order to minimize changes.
58+
59+
// OrigName is true since there is no particular reason to use camel case
60+
// The any resolver is empty, but provided anyways.
61+
jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: false, AnyResolver: anyResolver}
62+
63+
err := codectypes.UnpackInterfaces(msg, codectypes.ProtoJSONPacker{JSONPBMarshaler: jm})
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
buf := new(bytes.Buffer)
69+
if err := jm.Marshal(buf, msg); err != nil {
70+
panic(err)
71+
}
72+
73+
return buf.Bytes()
74+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package types_test
2+
3+
import (
4+
"strings"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
8+
"github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
9+
)
10+
11+
// TestMustMarshalProtoJSON tests that the memo field is only emitted (marshalled) if it is populated
12+
func (suite *TypesTestSuite) TestMustMarshalProtoJSON() {
13+
memo := "memo"
14+
packetData := types.NewFungibleTokenPacketData(sdk.DefaultBondDenom, "1", suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), memo)
15+
16+
bz := packetData.GetBytes()
17+
exists := strings.Contains(string(bz), memo)
18+
suite.Require().True(exists)
19+
20+
packetData.Memo = ""
21+
22+
bz = packetData.GetBytes()
23+
exists = strings.Contains(string(bz), memo)
24+
suite.Require().False(exists)
25+
}

modules/apps/transfer/types/packet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ func (ftpd FungibleTokenPacketData) ValidateBasic() error {
5656

5757
// GetBytes is a helper for serialising
5858
func (ftpd FungibleTokenPacketData) GetBytes() []byte {
59-
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&ftpd))
59+
return sdk.MustSortJSON(mustProtoMarshalJSON(&ftpd))
6060
}

0 commit comments

Comments
 (0)