-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathevents.go
164 lines (140 loc) · 4.94 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ibctesting
import (
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v6/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
)
type EventsMap map[string]map[string]string
// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the
// client identifier.
func ParseClientIDFromEvents(events sdk.Events) (string, error) {
for _, ev := range events {
if ev.Type == clienttypes.EventTypeCreateClient {
for _, attr := range ev.Attributes {
if string(attr.Key) == clienttypes.AttributeKeyClientID {
return string(attr.Value), nil
}
}
}
}
return "", fmt.Errorf("client identifier event attribute not found")
}
// ParseConnectionIDFromEvents parses events emitted from a MsgConnectionOpenInit or
// MsgConnectionOpenTry and returns the connection identifier.
func ParseConnectionIDFromEvents(events sdk.Events) (string, error) {
for _, ev := range events {
if ev.Type == connectiontypes.EventTypeConnectionOpenInit ||
ev.Type == connectiontypes.EventTypeConnectionOpenTry {
for _, attr := range ev.Attributes {
if string(attr.Key) == connectiontypes.AttributeKeyConnectionID {
return string(attr.Value), nil
}
}
}
}
return "", fmt.Errorf("connection identifier event attribute not found")
}
// ParseChannelIDFromEvents parses events emitted from a MsgChannelOpenInit or
// MsgChannelOpenTry and returns the channel identifier.
func ParseChannelIDFromEvents(events sdk.Events) (string, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry {
for _, attr := range ev.Attributes {
if string(attr.Key) == channeltypes.AttributeKeyChannelID {
return string(attr.Value), nil
}
}
}
}
return "", fmt.Errorf("channel identifier event attribute not found")
}
// ParsePacketFromEvents parses events emitted from a MsgRecvPacket and returns the
// acknowledgement.
func ParsePacketFromEvents(events sdk.Events) (channeltypes.Packet, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeSendPacket {
packet := channeltypes.Packet{}
for _, attr := range ev.Attributes {
switch string(attr.Key) {
case channeltypes.AttributeKeyData: //nolint:staticcheck // DEPRECATED
packet.Data = attr.Value
case channeltypes.AttributeKeySequence:
seq, err := strconv.ParseUint(string(attr.Value), 10, 64)
if err != nil {
return channeltypes.Packet{}, err
}
packet.Sequence = seq
case channeltypes.AttributeKeySrcPort:
packet.SourcePort = string(attr.Value)
case channeltypes.AttributeKeySrcChannel:
packet.SourceChannel = string(attr.Value)
case channeltypes.AttributeKeyDstPort:
packet.DestinationPort = string(attr.Value)
case channeltypes.AttributeKeyDstChannel:
packet.DestinationChannel = string(attr.Value)
case channeltypes.AttributeKeyTimeoutHeight:
height, err := clienttypes.ParseHeight(string(attr.Value))
if err != nil {
return channeltypes.Packet{}, err
}
packet.TimeoutHeight = height
case channeltypes.AttributeKeyTimeoutTimestamp:
timestamp, err := strconv.ParseUint(string(attr.Value), 10, 64)
if err != nil {
return channeltypes.Packet{}, err
}
packet.TimeoutTimestamp = timestamp
default:
continue
}
}
return packet, nil
}
}
return channeltypes.Packet{}, fmt.Errorf("acknowledgement event attribute not found")
}
// ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the
// acknowledgement.
func ParseAckFromEvents(events sdk.Events) ([]byte, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeWriteAck {
for _, attr := range ev.Attributes {
if string(attr.Key) == channeltypes.AttributeKeyAck { //nolint:staticcheck // DEPRECATED
return attr.Value, nil
}
}
}
}
return nil, fmt.Errorf("acknowledgement event attribute not found")
}
// AssertEvents asserts that expected events are present in the actual events.
// Expected map needs to be a subset of actual events to pass.
func AssertEvents(
suite *suite.Suite,
expected EventsMap,
actual sdk.Events,
) {
hasEvents := make(map[string]bool)
for eventType := range expected {
hasEvents[eventType] = false
}
for _, event := range actual {
expEvent, eventFound := expected[event.Type]
if eventFound {
hasEvents[event.Type] = true
suite.Require().Len(event.Attributes, len(expEvent))
for _, attr := range event.Attributes {
expValue, found := expEvent[string(attr.Key)]
suite.Require().True(found)
suite.Require().Equal(expValue, string(attr.Value))
}
}
}
for eventName, hasEvent := range hasEvents {
suite.Require().True(hasEvent, "event: %s was not found in events", eventName)
}
}