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

chore: add ParseEurekaPacketFromEvents and TestParseEurekaPacketsFromEvents #7906

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions modules/core/04-channel/types/channel.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions modules/core/04-channel/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ func NewPacket(
}
}

func NewEurekaPacket(
data []byte,
sequence uint64, sourcePort, sourceChannel,
destinationPort, destinationChannel string,
timeoutHeight clienttypes.Height, timeoutTimestamp uint64,
) EurekaPacket {
return EurekaPacket{
Sequence: sequence,
SourcePort: sourcePort,
SourceChannel: sourceChannel,
DestinationPort: destinationPort,
DestinationChannel: destinationChannel,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
}
}

// GetSequence implements PacketI interface
func (p Packet) GetSequence() uint64 { return p.Sequence }

Expand Down
68 changes: 68 additions & 0 deletions testing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ func ParsePacketFromEvents(events []abci.Event) (channeltypes.Packet, error) {
return packets[0], nil
}

func ParseEurekaPacketFromEvents(events []abci.Event) (channeltypes.EurekaPacket, error) {
packets, err := ParseEurekaPacketsFromEvents(channeltypes.EventTypeSendPacket, events)
if err != nil {
return channeltypes.EurekaPacket{}, err
}
return packets[0], nil
}

// ParseRecvPacketFromEvents parses events emitted from a MsgRecvPacket and returns
// the first EventTypeRecvPacket packet found.
// Returns an error if no packet is found.
Expand Down Expand Up @@ -147,6 +155,66 @@ func ParsePacketsFromEvents(eventType string, events []abci.Event) ([]channeltyp
return packets, nil
}

func ParseEurekaPacketsFromEvents(eventType string, events []abci.Event) ([]channeltypes.EurekaPacket, error) {
ferr := func(err error) ([]channeltypes.EurekaPacket, error) {
return nil, fmt.Errorf("ibctesting.ParseEurekaPacketsFromEvents: %w", err)
}
var packets []channeltypes.EurekaPacket
for _, ev := range events {
if ev.Type == eventType {
var packet channeltypes.EurekaPacket
for _, attr := range ev.Attributes {
switch attr.Key {
case channeltypes.AttributeKeySequence:
seq, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return ferr(err)
}

packet.Sequence = seq

case channeltypes.AttributeKeySrcPort:
packet.SourcePort = attr.Value

case channeltypes.AttributeKeySrcChannel:
packet.SourceChannel = attr.Value

case channeltypes.AttributeKeyDstPort:
packet.DestinationPort = attr.Value

case channeltypes.AttributeKeyDstChannel:
packet.DestinationChannel = attr.Value

case channeltypes.AttributeKeyTimeoutHeight:
height, err := clienttypes.ParseHeight(attr.Value)
if err != nil {
return ferr(err)
}

packet.TimeoutHeight = height

case channeltypes.AttributeKeyTimeoutTimestamp:
timestamp, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return ferr(err)
}

packet.TimeoutTimestamp = timestamp

default:
continue
}
}

packets = append(packets, packet)
}
}
if len(packets) == 0 {
return ferr(errors.New("acknowledgement event attribute not found"))
}
return packets, nil
}

// ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the
// acknowledgement.
func ParseAckFromEvents(events []abci.Event) ([]byte, error) {
Expand Down
197 changes: 197 additions & 0 deletions testing/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,200 @@ func TestParsePacketsFromEvents(t *testing.T) {
})
}
}

func TestParseEurekaPacketsFromEvents(t *testing.T) {
testCases := []struct {
name string
events []abci.Event
expectedPackets []channeltypes.EurekaPacket
expectedError string
}{
{
name: "success",
events: []abci.Event{
{
Type: "xxx",
},
{
Type: channeltypes.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypes.AttributeKeySequence,
Value: "42",
},
{
Key: channeltypes.AttributeKeySrcPort,
Value: "srcPort",
},
{
Key: channeltypes.AttributeKeySrcChannel,
Value: "srcChannel",
},
{
Key: channeltypes.AttributeKeyDstPort,
Value: "dstPort",
},
{
Key: channeltypes.AttributeKeyDstChannel,
Value: "dstChannel",
},
{
Key: channeltypes.AttributeKeyTimeoutHeight,
Value: "1-2",
},
{
Key: channeltypes.AttributeKeyTimeoutTimestamp,
Value: "1000",
},
},
},
{
Type: "yyy",
},
{
Type: channeltypes.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypes.AttributeKeySequence,
Value: "43",
},
{
Key: channeltypes.AttributeKeySrcPort,
Value: "srcPort",
},
{
Key: channeltypes.AttributeKeySrcChannel,
Value: "srcChannel",
},
{
Key: channeltypes.AttributeKeyDstPort,
Value: "dstPort",
},
{
Key: channeltypes.AttributeKeyDstChannel,
Value: "dstChannel",
},
{
Key: channeltypes.AttributeKeyTimeoutHeight,
Value: "1-3",
},
{
Key: channeltypes.AttributeKeyTimeoutTimestamp,
Value: "1001",
},
},
},
},
expectedPackets: []channeltypes.EurekaPacket{
{
Sequence: 42,
SourcePort: "srcPort",
SourceChannel: "srcChannel",
DestinationPort: "dstPort",
DestinationChannel: "dstChannel",
TimeoutHeight: types.Height{
RevisionNumber: 1,
RevisionHeight: 2,
},
TimeoutTimestamp: 1000,
},
{
Sequence: 43,
SourcePort: "srcPort",
SourceChannel: "srcChannel",
DestinationPort: "dstPort",
DestinationChannel: "dstChannel",
TimeoutHeight: types.Height{
RevisionNumber: 1,
RevisionHeight: 3,
},
TimeoutTimestamp: 1001,
},
},
},

{
name: "fail: no events",
events: []abci.Event{},
expectedError: "acknowledgement event attribute not found",
},
{
name: "fail: events without packet",
events: []abci.Event{
{
Type: "xxx",
},
{
Type: "yyy",
},
},
expectedError: "acknowledgement event attribute not found",
},
{
name: "fail: event packet with invalid AttributeKeySequence",
events: []abci.Event{
{
Type: channeltypes.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypes.AttributeKeySequence,
Value: "x",
},
},
},
},
expectedError: "strconv.ParseUint: parsing \"x\": invalid syntax",
},
{
name: "fail: event packet with invalid AttributeKeyTimeoutHeight",
events: []abci.Event{
{
Type: channeltypes.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypes.AttributeKeyTimeoutHeight,
Value: "x",
},
},
},
},
expectedError: "expected height string format: {revision}-{height}. Got: x: invalid height",
},
{
name: "fail: event packet with invalid AttributeKeyTimeoutTimestamp",
events: []abci.Event{
{
Type: channeltypes.EventTypeSendPacket,
Attributes: []abci.EventAttribute{
{
Key: channeltypes.AttributeKeyTimeoutTimestamp,
Value: "x",
},
},
},
},
expectedError: "strconv.ParseUint: parsing \"x\": invalid syntax",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
allPackets, err := ibctesting.ParseEurekaPacketsFromEvents(channeltypes.EventTypeSendPacket, tc.events)

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

firstPacket, err := ibctesting.ParseEurekaPacketFromEvents(tc.events)

if tc.expectedError == "" {
require.NoError(t, err)
require.Equal(t, tc.expectedPackets[0], firstPacket)
} else {
require.ErrorContains(t, err, tc.expectedError)
}
})
}
}
Loading