Skip to content

Commit

Permalink
create generic helper function for parsing event attributes (#6792)
Browse files Browse the repository at this point in the history
* create generic helper function for parsing event attributes

* review:

Align sig with other attr helpers (attrs, key)
Slap docusting
Amend contains to re-use it.
Use in ParseAckFromEvents

---------

Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
chandiniv1 and DimitrisJim authored Jul 11, 2024
1 parent 99f21bf commit bd2fe00
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions testing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,13 @@ func ParsePacketsFromEvents(eventType string, events []abci.Event) ([]channeltyp
func ParseAckFromEvents(events []abci.Event) ([]byte, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeWriteAck {
for _, attr := range ev.Attributes {
if attr.Key == channeltypes.AttributeKeyAckHex {
value, err := hex.DecodeString(attr.Value)
if err != nil {
return nil, err
}

return value, nil
if attribute, found := attributeByKey(ev.Attributes, channeltypes.AttributeKeyAckHex); found {
value, err := hex.DecodeString(attribute.Value)
if err != nil {
return nil, err
}

return value, nil
}
}
}
Expand All @@ -175,26 +173,20 @@ func ParseAckFromEvents(events []abci.Event) ([]byte, error) {
// ParseProposalIDFromEvents parses events emitted from MsgSubmitProposal and returns proposalID
func ParseProposalIDFromEvents(events []abci.Event) (uint64, error) {
for _, event := range events {
for _, attribute := range event.Attributes {
if attribute.Key == "proposal_id" {
return strconv.ParseUint(attribute.Value, 10, 64)
}
if attribute, found := attributeByKey(event.Attributes, "proposal_id"); found {
return strconv.ParseUint(attribute.Value, 10, 64)
}
}

return 0, fmt.Errorf("proposalID event attribute not found")
}

// ParsePacketSequenceFromEvents parses events emitted from MsgRecvPacket and returns the packet sequence
func ParsePacketSequenceFromEvents(events []abci.Event) (uint64, error) {
for _, event := range events {
for _, attribute := range event.Attributes {
if attribute.Key == "packet_sequence" {
return strconv.ParseUint(attribute.Value, 10, 64)
}
if attribute, found := attributeByKey(event.Attributes, "packet_sequence"); found {
return strconv.ParseUint(attribute.Value, 10, 64)
}
}

return 0, fmt.Errorf("packet sequence event attribute not found")
}

Expand Down Expand Up @@ -253,7 +245,15 @@ func containsAttribute(attrs []abci.EventAttribute, key, value string) bool {

// containsAttributeKey returns true if the given key is contained in the given attributes.
func containsAttributeKey(attrs []abci.EventAttribute, key string) bool {
return slices.ContainsFunc(attrs, func(attr abci.EventAttribute) bool {
return attr.Key == key
})
_, found := attributeByKey(attrs, key)
return found
}

// attributeByKey returns the event attribute's value keyed by the given key and a boolean indicating its presence in the given attributes.
func attributeByKey(attributes []abci.EventAttribute, key string) (abci.EventAttribute, bool) {
idx := slices.IndexFunc(attributes, func(a abci.EventAttribute) bool { return a.Key == key })
if idx == -1 {
return abci.EventAttribute{}, false
}
return attributes[idx], true
}

0 comments on commit bd2fe00

Please sign in to comment.