-
Notifications
You must be signed in to change notification settings - Fork 28
/
msg.go
188 lines (171 loc) · 6.26 KB
/
msg.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package tendermint
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/hyperledger-labs/yui-relayer/core"
)
var (
_ core.MsgID = (*MsgID)(nil)
_ core.MsgResult = (*MsgResult)(nil)
)
func (*MsgID) Is_MsgID() {}
type MsgResult struct {
height clienttypes.Height
// These show the status of the tx that contains the message.
// It should be noted that the cause of the failure can be a different message.
txStatus bool
txFailureReason string
events []core.MsgEventLog
}
func (r *MsgResult) BlockHeight() clienttypes.Height {
return r.height
}
func (r *MsgResult) Status() (bool, string) {
return r.txStatus, r.txFailureReason
}
func (r *MsgResult) Events() []core.MsgEventLog {
return r.events
}
func parseMsgEventLogs(logs sdk.ABCIMessageLogs, msgIndex uint32) ([]core.MsgEventLog, error) {
var msgEventLogs []core.MsgEventLog
for _, log := range logs {
if msgIndex == log.MsgIndex {
for _, ev := range log.Events {
event, err := parseMsgEventLog(ev)
if err != nil {
return nil, fmt.Errorf("failed to parse msg event log: %v", err)
}
msgEventLogs = append(msgEventLogs, event)
}
}
}
return msgEventLogs, nil
}
func parseMsgEventLog(ev sdk.StringEvent) (core.MsgEventLog, error) {
switch ev.Type {
case clienttypes.EventTypeCreateClient:
clientID, err := getAttributeString(ev, clienttypes.AttributeKeyClientID)
if err != nil {
return nil, err
}
return &core.EventGenerateClientIdentifier{ID: clientID}, nil
case conntypes.EventTypeConnectionOpenInit, conntypes.EventTypeConnectionOpenTry:
connectionID, err := getAttributeString(ev, conntypes.AttributeKeyConnectionID)
if err != nil {
return nil, err
}
return &core.EventGenerateConnectionIdentifier{ID: connectionID}, nil
case chantypes.EventTypeChannelOpenInit, chantypes.EventTypeChannelOpenTry:
channelID, err := getAttributeString(ev, chantypes.AttributeKeyChannelID)
if err != nil {
return nil, err
}
return &core.EventGenerateChannelIdentifier{ID: channelID}, nil
case chantypes.EventTypeSendPacket:
var event core.EventSendPacket
var err0, err1, err2, err3, err4, err5 error
event.Sequence, err0 = getAttributeUint64(ev, chantypes.AttributeKeySequence)
event.SrcPort, err1 = getAttributeString(ev, chantypes.AttributeKeySrcPort)
event.SrcChannel, err2 = getAttributeString(ev, chantypes.AttributeKeySrcChannel)
event.TimeoutHeight, err3 = getAttributeHeight(ev, chantypes.AttributeKeyTimeoutHeight)
event.TimeoutTimestamp, err4 = getAttributeTimestamp(ev, chantypes.AttributeKeyTimeoutTimestamp)
event.Data, err5 = getAttributeBytes(ev, chantypes.AttributeKeyDataHex)
if err := errors.Join(err0, err1, err2, err3, err4, err5); err != nil {
return nil, err
}
return &event, nil
case chantypes.EventTypeRecvPacket:
var event core.EventRecvPacket
var err0, err1, err2, err3, err4, err5 error
event.Sequence, err0 = getAttributeUint64(ev, chantypes.AttributeKeySequence)
event.DstPort, err1 = getAttributeString(ev, chantypes.AttributeKeyDstPort)
event.DstChannel, err2 = getAttributeString(ev, chantypes.AttributeKeyDstChannel)
event.TimeoutHeight, err3 = getAttributeHeight(ev, chantypes.AttributeKeyTimeoutHeight)
event.TimeoutTimestamp, err4 = getAttributeTimestamp(ev, chantypes.AttributeKeyTimeoutTimestamp)
event.Data, err5 = getAttributeBytes(ev, chantypes.AttributeKeyDataHex)
if err := errors.Join(err0, err1, err2, err3, err4, err5); err != nil {
return nil, err
}
return &event, nil
case chantypes.EventTypeWriteAck:
var event core.EventWriteAcknowledgement
var err0, err1, err2, err3 error
event.Sequence, err0 = getAttributeUint64(ev, chantypes.AttributeKeySequence)
event.DstPort, err1 = getAttributeString(ev, chantypes.AttributeKeyDstPort)
event.DstChannel, err2 = getAttributeString(ev, chantypes.AttributeKeyDstChannel)
event.Acknowledgement, err3 = getAttributeBytes(ev, chantypes.AttributeKeyAckHex)
if err := errors.Join(err0, err1, err2, err3); err != nil {
return nil, err
}
return &event, nil
case chantypes.EventTypeAcknowledgePacket:
var event core.EventAcknowledgePacket
var err0, err1, err2, err3, err4 error
event.Sequence, err0 = getAttributeUint64(ev, chantypes.AttributeKeySequence)
event.SrcPort, err1 = getAttributeString(ev, chantypes.AttributeKeySrcPort)
event.SrcChannel, err2 = getAttributeString(ev, chantypes.AttributeKeySrcChannel)
event.TimeoutHeight, err3 = getAttributeHeight(ev, chantypes.AttributeKeyTimeoutHeight)
event.TimeoutTimestamp, err4 = getAttributeTimestamp(ev, chantypes.AttributeKeyTimeoutTimestamp)
if err := errors.Join(err0, err1, err2, err3, err4); err != nil {
return nil, err
}
return &event, nil
default:
return &core.EventUnknown{Value: ev}, nil
}
}
func getAttributeString(ev sdk.StringEvent, key string) (string, error) {
for _, attr := range ev.Attributes {
if attr.Key == key {
return attr.Value, nil
}
}
return "", fmt.Errorf("failed to find attribute of key %q", key)
}
func getAttributeBytes(ev sdk.StringEvent, key string) ([]byte, error) {
v, err := getAttributeString(ev, key)
if err != nil {
return nil, err
}
bz, err := hex.DecodeString(v)
if err != nil {
return nil, fmt.Errorf("failed to decode hex string: %v", err)
}
return bz, nil
}
func getAttributeHeight(ev sdk.StringEvent, key string) (clienttypes.Height, error) {
v, err := getAttributeString(ev, key)
if err != nil {
return clienttypes.Height{}, err
}
height, err := clienttypes.ParseHeight(v)
if err != nil {
return clienttypes.Height{}, fmt.Errorf("failed to parse height: %v", err)
}
return height, nil
}
func getAttributeUint64(ev sdk.StringEvent, key string) (uint64, error) {
v, err := getAttributeString(ev, key)
if err != nil {
return 0, err
}
d, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse uint: %v", err)
}
return d, nil
}
func getAttributeTimestamp(ev sdk.StringEvent, key string) (time.Time, error) {
d, err := getAttributeUint64(ev, key)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, int64(d)), nil
}