Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: implement packet parsing from events in MsgSendPacketWithSender #8012

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions testing/endpoint_v2.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ibctesting

import (
"github.com/cosmos/gogoproto/proto"
"encoding/hex"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"

clientv2types "github.com/cosmos/ibc-go/v10/modules/core/02-client/v2/types"
channeltypesv2 "github.com/cosmos/ibc-go/v10/modules/core/04-channel/v2/types"
Expand Down Expand Up @@ -43,22 +44,26 @@ func (endpoint *Endpoint) MsgSendPacketWithSender(timeoutTimestamp uint64, paylo
return channeltypesv2.Packet{}, err
}

// TODO: parse the packet from events instead of from the response. https://github.com/cosmos/ibc-go/issues/7459
// get sequence from msg response
var msgData sdk.TxMsgData
err = proto.Unmarshal(res.Data, &msgData)
if err != nil {
return channeltypesv2.Packet{}, err
}
msgResponse := msgData.MsgResponses[0]
var sendResponse channeltypesv2.MsgSendPacketResponse
err = proto.Unmarshal(msgResponse.Value, &sendResponse)
if err != nil {
return channeltypesv2.Packet{}, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be implemented as a function as is specified in the original issue: #7856

// Parse the packet from events
for _, event := range res.Events {
if event.Type == channeltypesv2.EventTypeSendPacket {
var packet channeltypesv2.Packet
for _, attr := range event.Attributes {
if string(attr.Key) == channeltypesv2.AttributeKeyEncodedPacketHex {
packetBytes, err := hex.DecodeString(string(attr.Value))
if err != nil {
return channeltypesv2.Packet{}, err
}
if err := proto.Unmarshal(packetBytes, &packet); err != nil {
return channeltypesv2.Packet{}, err
}
return packet, nil
}
}
}
}
packet := channeltypesv2.NewPacket(sendResponse.Sequence, endpoint.ClientID, endpoint.Counterparty.ClientID, timeoutTimestamp, payload)

return packet, nil
return channeltypesv2.Packet{}, fmt.Errorf("packet not found in events")
}

// MsgRecvPacket sends a MsgRecvPacket on the associated endpoint with the provided packet.
Expand Down
64 changes: 64 additions & 0 deletions testing/endpoint_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ibctesting

import (
"testing"

"github.com/stretchr/testify/require"

channeltypesv2 "github.com/cosmos/ibc-go/v10/modules/core/04-channel/v2/types"
)

func TestMsgSendPacketWithSender(t *testing.T) {
coordinator := NewCoordinator(t, 2)
chainA := coordinator.GetChain(GetChainID(1))
chainB := coordinator.GetChain(GetChainID(2))
coordinator.CommitBlock(chainA, chainB)

path := NewPath(chainA, chainB)
coordinator.SetupConnections(path)

// Create a mock payload
payload := channeltypesv2.Payload{
SourcePort: "sourcePort",
DestinationPort: "destPort",
Version: "1.0",
Encoding: "proto3",
Value: []byte("test data"),
}

// Create sender account
senderAccount := SenderAccount{
SenderPrivKey: chainA.SenderPrivKey,
SenderAccount: chainA.SenderAccount,
}

t.Run("successful packet send", func(t *testing.T) {
// Send packet
timeoutTimestamp := chainA.GetTimeoutTimestamp()
packet, err := path.EndpointA.MsgSendPacketWithSender(timeoutTimestamp, payload, senderAccount)
require.NoError(t, err)

// Verify packet fields
require.Equal(t, path.EndpointA.ClientID, packet.SourceClient)
require.Equal(t, path.EndpointB.ClientID, packet.DestinationClient)
require.Equal(t, timeoutTimestamp, packet.TimeoutTimestamp)
require.Len(t, packet.Payloads, 1)
require.Equal(t, payload, packet.Payloads[0])
})

t.Run("packet not found in events", func(t *testing.T) {
// Mock a failed send by using an invalid payload that will cause the event to be missing
invalidPayload := channeltypesv2.Payload{
SourcePort: "", // Invalid empty source port
DestinationPort: "", // Invalid empty destination port
Version: "",
Encoding: "",
Value: nil,
}

timeoutTimestamp := chainA.GetTimeoutTimestamp()
_, err := path.EndpointA.MsgSendPacketWithSender(timeoutTimestamp, invalidPayload, senderAccount)
require.Error(t, err)
require.Contains(t, err.Error(), "packet not found in events")
})
}