Skip to content

Commit

Permalink
fix to encode intermediate sender changed packet with counterparty po…
Browse files Browse the repository at this point in the history
…rt consideration (#246)

put source port

add testcase
  • Loading branch information
beer-1 authored Aug 13, 2024
1 parent d488e00 commit b5195f4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
13 changes: 2 additions & 11 deletions x/ibc-hooks/move-hooks/receive.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package move_hooks

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -51,11 +50,7 @@ func (h MoveHooks) onRecvIcs20Packet(
//
// If that succeeds, we make the contract call
data.Receiver = intermediateSender
bz, err := json.Marshal(data)
if err != nil {
return newEmitErrorAcknowledgement(err)
}
packet.Data = bz
packet.Data = data.GetBytes()

ack := im.App.OnRecvPacket(ctx, packet, relayer)
if !ack.Success() {
Expand Down Expand Up @@ -107,11 +102,7 @@ func (h MoveHooks) onRecvIcs721Packet(
//
// If that succeeds, we make the contract call
data.Receiver = intermediateSender
bz, err := json.Marshal(data)
if err != nil {
return newEmitErrorAcknowledgement(err)
}
packet.Data = bz
packet.Data = data.GetBytes(packet.GetSourcePort())

ack := im.App.OnRecvPacket(ctx, packet, relayer)
if !ack.Success() {
Expand Down
59 changes: 58 additions & 1 deletion x/ibc-hooks/move-hooks/receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_OnReceivePacket_ICS721(t *testing.T) {
require.True(t, ack.Success())
}

func Test_onReceiveIcs20Packet_memo_ICS721(t *testing.T) {
func Test_onReceivePacket_memo_ICS721(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()

Expand Down Expand Up @@ -169,3 +169,60 @@ func Test_onReceiveIcs20Packet_memo_ICS721(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "\"1\"", queryRes.Ret)
}

func Test_onReceivePacket_memo_ICS721_Wasm(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()

data := nfttransfertypes.NonFungibleTokenPacketDataWasm{
ClassId: "classId",
ClassUri: "classUri",
ClassData: "classData",
TokenIds: []string{"tokenId"},
TokenUris: []string{"tokenUri"},
TokenData: []string{"tokenData"},
Sender: addr.String(),
Receiver: "0x1::Counter::increase",
Memo: `{
"move": {
"message": {
"module_address": "0x1",
"module_name": "Counter",
"function_name": "increase"
}
}
}`,
}

dataBz, err := json.Marshal(&data)
require.NoError(t, err)

// failed to due to acl
ack := input.IBCHooksMiddleware.OnRecvPacket(ctx, channeltypes.Packet{
SourcePort: "wasm.contract_address",
Data: dataBz,
}, addr)
require.False(t, ack.Success())

// set acl
require.NoError(t, input.IBCHooksKeeper.SetAllowed(ctx, movetypes.ConvertVMAddressToSDKAddress(vmtypes.StdAddress), true))

// success
ack = input.IBCHooksMiddleware.OnRecvPacket(ctx, channeltypes.Packet{
SourcePort: "wasm.contract_address",
Data: dataBz,
}, addr)
require.True(t, ack.Success())

// check the contract state
queryRes, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
"get",
[]vmtypes.TypeTag{},
[][]byte{},
)
require.NoError(t, err)
require.Equal(t, "\"1\"", queryRes.Ret)
}
4 changes: 2 additions & 2 deletions x/ibc-hooks/move-hooks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func isIcs20Packet(packetData []byte) (isIcs20 bool, ics20data transfertypes.Fun
return true, data
}

func isIcs721Packet(packetData []byte, counterPartyPort string) (isIcs721 bool, ics721data nfttransfertypes.NonFungibleTokenPacketData) {
if data, err := nfttransfertypes.DecodePacketData(packetData, counterPartyPort); err != nil {
func isIcs721Packet(packetData []byte, counterpartyPort string) (isIcs721 bool, ics721data nfttransfertypes.NonFungibleTokenPacketData) {
if data, err := nfttransfertypes.DecodePacketData(packetData, counterpartyPort); err != nil {
return false, data
} else {
return true, data
Expand Down
8 changes: 4 additions & 4 deletions x/ibc/nft-transfer/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (nftpd NonFungibleTokenPacketData) ValidateBasic() error {
}

// GetBytes is a helper for serializing
func (nftpd NonFungibleTokenPacketData) GetBytes(counterPartyPort string) []byte {
func (nftpd NonFungibleTokenPacketData) GetBytes(counterpartyPort string) []byte {
var bz []byte
var err error
if isWasmPacket(counterPartyPort) {
if isWasmPacket(counterpartyPort) {
bz, err = json.Marshal(nftpd.ToWasmData())
} else {
bz, err = json.Marshal(nftpd)
Expand All @@ -87,11 +87,11 @@ func (nftpd NonFungibleTokenPacketData) GetBytes(counterPartyPort string) []byte
}

// decode packet data to NonFungibleTokenPacketData
func DecodePacketData(packetData []byte, counterPartyPort string) (NonFungibleTokenPacketData, error) {
func DecodePacketData(packetData []byte, counterpartyPort string) (NonFungibleTokenPacketData, error) {
decoder := json.NewDecoder(strings.NewReader(string(packetData)))
decoder.DisallowUnknownFields()

if isWasmPacket(counterPartyPort) {
if isWasmPacket(counterpartyPort) {
var wasmData NonFungibleTokenPacketDataWasm
if err := decoder.Decode(&wasmData); err != nil {
return NonFungibleTokenPacketData{}, sdkerrors.ErrInvalidRequest.Wrap(err.Error())
Expand Down

0 comments on commit b5195f4

Please sign in to comment.