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
53 changes: 37 additions & 16 deletions testing/endpoint_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@ import (

"github.com/cosmos/gogoproto/proto"

abci "github.com/cometbft/cometbft/abci/types"
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"
hostv2 "github.com/cosmos/ibc-go/v10/modules/core/24-host/v2"
)

// ParsePacketFromEventsV2 parses a packet from events using the encoded packet hex attribute.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please take a look at testing/events.go. There is a ParsePacketFromEvents there. This one should be moved there.

// It returns the parsed packet and an error if the packet cannot be found or decoded.
func ParsePacketFromEventsV2(events []abci.Event) (channeltypesv2.Packet, error) {
for _, event := range events {
if event.Type == channeltypesv2.EventTypeSendPacket {
for _, attr := range event.Attributes {
if string(attr.Key) == channeltypesv2.AttributeKeyEncodedPacketHex {
packetBytes, err := hex.DecodeString(string(attr.Value))
if err != nil {
return channeltypesv2.Packet{}, fmt.Errorf("failed to decode packet bytes: %w", err)
}
var packet channeltypesv2.Packet
if err := proto.Unmarshal(packetBytes, &packet); err != nil {
return channeltypesv2.Packet{}, fmt.Errorf("failed to unmarshal packet: %w", err)
}
return packet, nil
}
}
}
}
return channeltypesv2.Packet{}, fmt.Errorf("packet not found in events")
}

// RegisterCounterparty will construct and execute a MsgRegisterCounterparty on the associated endpoint.
func (endpoint *Endpoint) RegisterCounterparty() (err error) {
msg := clientv2types.NewMsgRegisterCounterparty(endpoint.ClientID, endpoint.Counterparty.MerklePathPrefix.KeyPath, endpoint.Counterparty.ClientID, endpoint.Chain.SenderAccount.GetAddress().String())
Expand Down Expand Up @@ -44,26 +68,23 @@ func (endpoint *Endpoint) MsgSendPacketWithSender(timeoutTimestamp uint64, paylo
return channeltypesv2.Packet{}, err
}

// 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
}
// Convert sdk.Events to abci.Events
abciEvents := make([]abci.Event, len(res.Events))
for i, event := range res.Events {
attributes := make([]abci.EventAttribute, len(event.Attributes))
for j, attr := range event.Attributes {
attributes[j] = abci.EventAttribute{
Key: attr.Key,
Value: attr.Value,
}
}
abciEvents[i] = abci.Event{
Type: event.Type,
Attributes: attributes,
}
}

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

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

import (
"encoding/hex"
"testing"

"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"

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

func TestParsePacketFromEventsV2(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check the tests we have for the previous version of this in testing/events_test.go and make sure we cover the same scenarios. Right off the bat, I see you are not testing what will happen if there are multiple packets in the events.

testCases := []struct {
name string
events []abci.Event
expectedError string
}{
{
name: "successful packet parsing",
events: []abci.Event{
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypesv2.AttributeKeyEncodedPacketHex,
Value: hex.EncodeToString(func() []byte {
packet := channeltypesv2.Packet{
SourceClient: "client-0",
DestinationClient: "client-1",
Sequence: 1,
TimeoutTimestamp: 100,
Payloads: []channeltypesv2.Payload{
{
SourcePort: "transfer",
DestinationPort: "transfer",
Version: "1.0",
Encoding: "proto3",
Value: []byte("test data"),
},
},
}
bz, err := proto.Marshal(&packet)
require.NoError(t, err)
return bz
}()),
},
},
},
},
expectedError: "",
},
{
name: "empty events",
events: []abci.Event{},
expectedError: "packet not found in events",
},
{
name: "invalid hex encoding",
events: []abci.Event{
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypesv2.AttributeKeyEncodedPacketHex,
Value: "invalid hex",
},
},
},
},
expectedError: "failed to decode packet bytes",
},
{
name: "invalid proto encoding",
events: []abci.Event{
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypesv2.AttributeKeyEncodedPacketHex,
Value: hex.EncodeToString([]byte("invalid proto")),
},
},
},
},
expectedError: "failed to unmarshal packet",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
packet, err := ParsePacketFromEventsV2(tc.events)
if tc.expectedError == "" {
require.NoError(t, err)
require.NotEmpty(t, packet)
require.Equal(t, "client-0", packet.SourceClient)
require.Equal(t, "client-1", packet.DestinationClient)
require.Equal(t, uint64(1), packet.Sequence)
require.Equal(t, uint64(100), packet.TimeoutTimestamp)
require.Len(t, packet.Payloads, 1)
require.Equal(t, "transfer", packet.Payloads[0].SourcePort)
require.Equal(t, "transfer", packet.Payloads[0].DestinationPort)
} else {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedError)
}
})
}
}

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