Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamer committed Jan 20, 2023
1 parent a64d5dc commit d2dd106
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
3 changes: 0 additions & 3 deletions keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ func (k Keeper) createOutgoingPacket(ctx sdk.Context,
return channeltypes.Packet{}, err
}

// shape content
packetData.ShapeContent()

return channeltypes.NewPacket(
packetData.GetBytes(),
sequence,
Expand Down
34 changes: 15 additions & 19 deletions types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,27 @@ func (nftpd NonFungibleTokenPacketData) ValidateBasic() error {
return nil
}

// ShapeContent will reshape tokenUris and tokenData in NonFungibleTokenPacketData:
// 1. if tokenUris/tokenData is ["","",""] or [], then set it to nil.
// 2. if tokenUris/tokenData is ["a","b","c"] or ["a", "", "c"], then keep it.
// NOTE: Only use this before sending pkg.
func (nftpd *NonFungibleTokenPacketData) ShapeContent() {

if shape := requireShape(nftpd.TokenUris); shape {
// GetBytes is a helper for serializing
func (nftpd NonFungibleTokenPacketData) GetBytes() []byte {
// Format will reshape tokenUris and tokenData in NonFungibleTokenPacketData:
// 1. if tokenUris/tokenData is ["","",""] or [], then set it to nil.
// 2. if tokenUris/tokenData is ["a","b","c"] or ["a", "", "c"], then keep it.
// NOTE: Only use this before sending pkg.
if requireShape(nftpd.TokenUris) {
nftpd.TokenUris = nil
}

if shape := requireShape(nftpd.TokenData); shape {
if requireShape(nftpd.TokenData) {
nftpd.TokenData = nil
}
return sdk.MustSortJSON(MustProtoMarshalJSON(&nftpd))
}

// GetBytes is a helper for serializing
func (nftpd NonFungibleTokenPacketData) GetBytes() []byte {
return sdk.MustSortJSON(MustProtoMarshalJSON(&nftpd))
func GetIfExist(i int, data []string) string {
if i < 0 || i >= len(data) {
return ""
}
return data[i]
}

// requireShape checks if TokenUris/TokenData needs to be set as nil
Expand All @@ -111,7 +114,7 @@ func requireShape(contents []string) bool {

emptyStringCount := 0
for _, v := range contents {
if v == "" {
if len(v) == 0 {
emptyStringCount++
}
}
Expand All @@ -122,10 +125,3 @@ func requireShape(contents []string) bool {

return false
}

func GetIfExist(i int, data []string) string {
if i < 0 || i >= len(data) {
return ""
}
return data[i]
}
75 changes: 75 additions & 0 deletions types/packet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"testing"
)

Expand Down Expand Up @@ -95,3 +96,77 @@ func TestNonFungibleTokenPacketData_ValidateBasic(t *testing.T) {
})
}
}

func TestNonFungibleTokenPacketData_GetBytes(t *testing.T) {
type fields struct {
ClassId string
ClassUri string
ClassData string
TokenIds []string
TokenUris []string
TokenData []string
Sender string
Receiver string
Memo string
}
tests := []struct {
name string
fields fields
want []byte
}{
{
"success",
fields{"classId", "classUri", "classData", []string{"id1", "id2"}, []string{"uri1", "uri2"}, []string{"data1"}, sender, receiver, "memo"},
[]byte(`{"classData":"classData","classId":"classId","classUri":"classUri","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"],"tokenUris":["uri1","uri2"]}`),
},
{
"success with missing classUri",
fields{"classId", "", "classData", []string{"id1", "id2"}, []string{"uri1", "uri2"}, []string{"data1"}, sender, receiver, "memo"},
[]byte(`{"classData":"classData","classId":"classId","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"],"tokenUris":["uri1","uri2"]}`),
},
{
"success with missing classData",
fields{"classId", "classUri", "", []string{"id1", "id2"}, []string{"uri1", "uri2"}, []string{"data1"}, sender, receiver, "memo"},
[]byte(`{"classId":"classId","classUri":"classUri","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"],"tokenUris":["uri1","uri2"]}`),
},
{
"success with empty tokenUris",
fields{"classId", "classUri", "classData", []string{"id1", "id2"}, []string{"", ""}, []string{"data1"}, sender, receiver, "memo"},
[]byte(`{"classData":"classData","classId":"classId","classUri":"classUri","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"`),
},
{
"success with nil tokenUris",
fields{"classId", "classUri", "classData", []string{"id1", "id2"}, nil, []string{"data1"}, sender, receiver, "memo"},
[]byte(`{"classData":"classData","classId":"classId","classUri":"classUri","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"`),
},
{
"success with empty tokenData",
fields{"classId", "classUri", "classData", []string{"id1", "id2"}, []string{"uri1", "uri2"}, []string{"", ""}, sender, receiver, "memo"},
[]byte(`{"classData":"classData","classId":"classId","classUri":"classUri","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"`),
},
{
"success with nil tokenData",
fields{"classId", "classUri", "classData", []string{"id1", "id2"}, []string{"uri1", "uri2"}, nil, sender, receiver, "memo"},
[]byte(`{"classData":"classData","classId":"classId","classUri":"classUri","memo":"memo","receiver":"cosmos15mn87gny58ptfpzq0du6t398gle50xphkw2pkt","sender":"cosmos1eshqg3adwvnuqng0eqfr6ppj35j9hh6zyd9qss","tokenData":["data1"],"tokenIds":["id1","id2"`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nftpd := NonFungibleTokenPacketData{
ClassId: tt.fields.ClassId,
ClassUri: tt.fields.ClassUri,
ClassData: tt.fields.ClassData,
TokenIds: tt.fields.TokenIds,
TokenUris: tt.fields.TokenUris,
TokenData: tt.fields.TokenData,
Sender: tt.fields.Sender,
Receiver: tt.fields.Receiver,
Memo: tt.fields.Memo,
}
t.Logf("%s\n", nftpd.GetBytes())
if got := nftpd.GetBytes(); bytes.Equal(got, tt.want) {
t.Errorf("NonFungibleTokenPacketData.GetBytes() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d2dd106

Please sign in to comment.