Skip to content

Commit

Permalink
Update events_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
crStiv authored Mar 1, 2025
1 parent f67a79c commit 44b8155
Showing 1 changed file with 182 additions and 2 deletions.
184 changes: 182 additions & 2 deletions testing/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/hex"
"testing"

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

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/ibc-go/v10/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v10/modules/core/04-channel/types"
channeltypesv2 "github.com/cosmos/ibc-go/v10/modules/core/04-channel/v2/types"
ibctesting "github.com/cosmos/ibc-go/v10/testing"
)

Expand Down Expand Up @@ -137,7 +139,7 @@ func TestParsePacketsFromEvents(t *testing.T) {
{
name: "fail: no events",
events: []abci.Event{},
expectedError: "acknowledgement event attribute not found",
expectedError: "packet not found in events",
},
{
name: "fail: events without packet",
Expand All @@ -149,7 +151,7 @@ func TestParsePacketsFromEvents(t *testing.T) {
Type: "yyy",
},
},
expectedError: "acknowledgement event attribute not found",
expectedError: "packet not found in events",
},
{
name: "fail: event packet with invalid AttributeKeySequence",
Expand Down Expand Up @@ -219,3 +221,181 @@ func TestParsePacketsFromEvents(t *testing.T) {
})
}
}

func TestParsePacketsFromEventsV2(t *testing.T) {
testCases := []struct {
name string
events []abci.Event
expectedPackets []channeltypesv2.Packet
expectedError string
}{
{
name: "success with multiple packets",
events: []abci.Event{
{
Type: "xxx",
},
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: "packet_hex",
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("data1"),
},
},
}
bz, err := proto.Marshal(&packet)
require.NoError(t, err)
return bz
}()),
},
},
},
{
Type: "yyy",
},
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: "packet_hex",
Value: hex.EncodeToString(func() []byte {
packet := channeltypesv2.Packet{
SourceClient: "client-0",
DestinationClient: "client-1",
Sequence: 2,
TimeoutTimestamp: 200,
Payloads: []channeltypesv2.Payload{
{
SourcePort: "transfer",
DestinationPort: "transfer",
Version: "1.0",
Encoding: "proto3",
Value: []byte("data2"),
},
},
}
bz, err := proto.Marshal(&packet)
require.NoError(t, err)
return bz
}()),
},
},
},
},
expectedPackets: []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("data1"),
},
},
},
{
SourceClient: "client-0",
DestinationClient: "client-1",
Sequence: 2,
TimeoutTimestamp: 200,
Payloads: []channeltypesv2.Payload{
{
SourcePort: "transfer",
DestinationPort: "transfer",
Version: "1.0",
Encoding: "proto3",
Value: []byte("data2"),
},
},
},
},
},
{
name: "fail: no events",
events: []abci.Event{},
expectedError: "packet not found in events",
},
{
name: "fail: events without packet",
events: []abci.Event{
{
Type: "xxx",
},
{
Type: "yyy",
},
},
expectedError: "packet not found in events",
},
{
name: "fail: invalid hex encoding",
events: []abci.Event{
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: "packet_hex",
Value: "invalid hex",
},
},
},
},
expectedError: "failed to decode packet bytes",
},
{
name: "fail: invalid proto encoding",
events: []abci.Event{
{
Type: channeltypesv2.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: "packet_hex",
Value: hex.EncodeToString([]byte("invalid proto")),
},
},
},
},
expectedError: "failed to unmarshal packet",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
allPackets, err := ibctesting.ParsePacketsFromEventsV2(channeltypesv2.EventTypeSendPacket, tc.events)

if tc.expectedError == "" {
require.NoError(t, err)
require.Equal(t, tc.expectedPackets, allPackets)

// Test ParsePacketFromEventsV2 as well
firstPacket, err := ibctesting.ParsePacketFromEventsV2(tc.events)
require.NoError(t, err)
require.Equal(t, tc.expectedPackets[0], firstPacket)
} else {
require.ErrorContains(t, err, tc.expectedError)

// Test ParsePacketFromEventsV2 as well
_, err = ibctesting.ParsePacketFromEventsV2(tc.events)
require.ErrorContains(t, err, tc.expectedError)
}
})
}
}

0 comments on commit 44b8155

Please sign in to comment.