-
Notifications
You must be signed in to change notification settings - Fork 8
/
event_utils.go
118 lines (105 loc) · 3.17 KB
/
event_utils.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
package ibctesting
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/cometbft/cometbft/abci/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
)
func getSendPackets(evts []abci.Event) []channeltypes.Packet {
var res []channeltypes.Packet
for _, evt := range evts {
if evt.Type == channeltypes.EventTypeSendPacket {
packet := parsePacketFromEvent(evt)
res = append(res, packet)
}
}
return res
}
// Used for various debug statements above when needed... do not remove
// func showEvent(evt abci.Event) {
// fmt.Printf("evt.Type: %s\n", evt.Type)
// for _, attr := range evt.Attributes {
// fmt.Printf(" %s = %s\n", string(attr.Key), string(attr.Value))
// }
//}
func parsePacketFromEvent(evt abci.Event) channeltypes.Packet {
return channeltypes.Packet{
Sequence: getUintField(evt, channeltypes.AttributeKeySequence),
SourcePort: getField(evt, channeltypes.AttributeKeySrcPort),
SourceChannel: getField(evt, channeltypes.AttributeKeySrcChannel),
DestinationPort: getField(evt, channeltypes.AttributeKeyDstPort),
DestinationChannel: getField(evt, channeltypes.AttributeKeyDstChannel),
Data: getHexField(evt, channeltypes.AttributeKeyDataHex),
TimeoutHeight: parseTimeoutHeight(getField(evt, channeltypes.AttributeKeyTimeoutHeight)),
TimeoutTimestamp: getUintField(evt, channeltypes.AttributeKeyTimeoutTimestamp),
}
}
func getHexField(evt abci.Event, key string) []byte {
got := getField(evt, key)
if got == "" {
return nil
}
bz, err := hex.DecodeString(got)
if err != nil {
panic(err)
}
return bz
}
// return the value for the attribute with the given name
func getField(evt abci.Event, key string) string {
for _, attr := range evt.Attributes {
if attr.Key == key {
return attr.Value
}
}
return ""
}
func getUintField(evt abci.Event, key string) uint64 {
raw := getField(evt, key)
return toUint64(raw)
}
func toUint64(raw string) uint64 {
if raw == "" {
return 0
}
i, err := strconv.ParseUint(raw, 10, 64)
if err != nil {
panic(err)
}
return i
}
func parseTimeoutHeight(raw string) clienttypes.Height {
chunks := strings.Split(raw, "-")
return clienttypes.Height{
RevisionNumber: toUint64(chunks[0]),
RevisionHeight: toUint64(chunks[1]),
}
}
func ParsePortIDFromEvents(events sdk.Events) (string, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry {
for _, attr := range ev.Attributes {
if attr.Key == channeltypes.AttributeKeyPortID {
return attr.Value, nil
}
}
}
}
return "", fmt.Errorf("port id event attribute not found")
}
func ParseChannelVersionFromEvents(events sdk.Events) (string, error) {
for _, ev := range events {
if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry {
for _, attr := range ev.Attributes {
if attr.Key == channeltypes.AttributeVersion {
return attr.Value, nil
}
}
}
}
return "", fmt.Errorf("version event attribute not found")
}