forked from Finschia/wasmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_utils.go
92 lines (81 loc) · 2.33 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
package ibctesting
import (
"strconv"
"strings"
abci "github.com/tendermint/tendermint/abci/types"
clienttypes "github.com/line/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/line/ibc-go/v3/modules/core/04-channel/types"
)
func getSendPackets(evts []abci.Event) []channeltypes.Packet {
var res []channeltypes.Packet
for _, evt := range evts {
if evt.Type == "send_packet" {
packet := parsePacketFromEvent(evt)
res = append(res, packet)
}
}
return res
}
func getAckPackets(evts []abci.Event) []PacketAck {
var res []PacketAck
for _, evt := range evts {
if evt.Type == "write_acknowledgement" {
packet := parsePacketFromEvent(evt)
ack := PacketAck{
Packet: packet,
Ack: []byte(getField(evt, "packet_ack")),
}
res = append(res, ack)
}
}
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, "packet_sequence"),
SourcePort: getField(evt, "packet_src_port"),
SourceChannel: getField(evt, "packet_src_channel"),
DestinationPort: getField(evt, "packet_dst_port"),
DestinationChannel: getField(evt, "packet_dst_channel"),
Data: []byte(getField(evt, "packet_data")),
TimeoutHeight: parseTimeoutHeight(getField(evt, "packet_timeout_height")),
TimeoutTimestamp: getUintField(evt, "packet_timeout_timestamp"),
}
}
// return the value for the attribute with the given name
func getField(evt abci.Event, key string) string {
for _, attr := range evt.Attributes {
if string(attr.Key) == key {
return string(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]),
}
}