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

create generic helper function for parsing event attributes #6792

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Changes from 3 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
22 changes: 12 additions & 10 deletions testing/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,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("proposal_id", event.Attributes); 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("packet_sequence", event.Attributes); found {
return strconv.ParseUint(attribute.Value, 10, 64)
}
}

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

Expand Down Expand Up @@ -257,3 +251,11 @@ func containsAttributeKey(attrs []abci.EventAttribute, key string) bool {
return attr.Key == key
})
}

func attributeByKey(key string, attributes []abci.EventAttribute) (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
}
Loading