-
Notifications
You must be signed in to change notification settings - Fork 179
/
events.go
290 lines (255 loc) · 9.34 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package convert
import (
"encoding/json"
"fmt"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/onflow/cadence/encoding/ccf"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-go/model/flow"
accessproto "github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"
)
// EventToMessage converts a flow.Event to a protobuf message
// Note: this function does not convert the payload encoding
func EventToMessage(e flow.Event) *entities.Event {
return &entities.Event{
Type: string(e.Type),
TransactionId: e.TransactionID[:],
TransactionIndex: e.TransactionIndex,
EventIndex: e.EventIndex,
Payload: e.Payload,
}
}
// MessageToEvent converts a protobuf message to a flow.Event
// Note: this function does not convert the payload encoding
func MessageToEvent(m *entities.Event) flow.Event {
return flow.Event{
Type: flow.EventType(m.GetType()),
TransactionID: flow.HashToID(m.GetTransactionId()),
TransactionIndex: m.GetTransactionIndex(),
EventIndex: m.GetEventIndex(),
Payload: m.GetPayload(),
}
}
// EventsToMessages converts a slice of flow.Events to a slice of protobuf messages
// Note: this function does not convert the payload encoding
func EventsToMessages(flowEvents []flow.Event) []*entities.Event {
events := make([]*entities.Event, len(flowEvents))
for i, e := range flowEvents {
event := EventToMessage(e)
events[i] = event
}
return events
}
// MessagesToEvents converts a slice of protobuf messages to a slice of flow.Events
// Note: this function does not convert the payload encoding
func MessagesToEvents(l []*entities.Event) []flow.Event {
events := make([]flow.Event, len(l))
for i, m := range l {
events[i] = MessageToEvent(m)
}
return events
}
// EventToMessageFromVersion converts a flow.Event to a protobuf message, converting the payload
// encoding from CCF to JSON if the input version is CCF
func EventToMessageFromVersion(e flow.Event, version entities.EventEncodingVersion) (*entities.Event, error) {
message := EventToMessage(e)
if len(e.Payload) > 0 {
switch version {
case entities.EventEncodingVersion_CCF_V0:
convertedPayload, err := CcfPayloadToJsonPayload(e.Payload)
if err != nil {
return nil, fmt.Errorf("could not convert event payload from CCF to Json: %w", err)
}
message.Payload = convertedPayload
case entities.EventEncodingVersion_JSON_CDC_V0:
default:
return nil, fmt.Errorf("invalid encoding format %d", version)
}
}
return message, nil
}
// MessageToEventFromVersion converts a protobuf message to a flow.Event, and converts the payload
// encoding from CCF to JSON if the input version is CCF
func MessageToEventFromVersion(m *entities.Event, inputVersion entities.EventEncodingVersion) (*flow.Event, error) {
event := MessageToEvent(m)
switch inputVersion {
case entities.EventEncodingVersion_CCF_V0:
convertedPayload, err := CcfPayloadToJsonPayload(event.Payload)
if err != nil {
return nil, fmt.Errorf("could not convert event payload from CCF to Json: %w", err)
}
event.Payload = convertedPayload
return &event, nil
case entities.EventEncodingVersion_JSON_CDC_V0:
return &event, nil
default:
return nil, fmt.Errorf("invalid encoding format %d", inputVersion)
}
}
// EventsToMessagesWithEncodingConversion converts a slice of flow.Events to a slice of protobuf messages, converting
// the payload encoding from CCF to JSON if the input version is CCF
func EventsToMessagesWithEncodingConversion(
flowEvents []flow.Event,
from entities.EventEncodingVersion,
to entities.EventEncodingVersion,
) ([]*entities.Event, error) {
if from == entities.EventEncodingVersion_JSON_CDC_V0 && to == entities.EventEncodingVersion_CCF_V0 {
return nil, fmt.Errorf("conversion from format %s to %s is not supported", from.String(), to.String())
}
if from == to {
return EventsToMessages(flowEvents), nil
}
events := make([]*entities.Event, len(flowEvents))
for i, e := range flowEvents {
event, err := EventToMessageFromVersion(e, from)
if err != nil {
return nil, fmt.Errorf("could not convert event at index %d from format %d: %w",
e.EventIndex, from, err)
}
events[i] = event
}
return events, nil
}
// MessagesToEventsWithEncodingConversion converts a slice of protobuf messages to a slice of flow.Events, converting
// the payload encoding from CCF to JSON if the input version is CCF
func MessagesToEventsWithEncodingConversion(
messageEvents []*entities.Event,
from entities.EventEncodingVersion,
to entities.EventEncodingVersion,
) ([]flow.Event, error) {
if from == entities.EventEncodingVersion_JSON_CDC_V0 && to == entities.EventEncodingVersion_CCF_V0 {
return nil, fmt.Errorf("conversion from format %s to %s is not supported", from.String(), to.String())
}
if from == to {
return MessagesToEvents(messageEvents), nil
}
events := make([]flow.Event, len(messageEvents))
for i, m := range messageEvents {
event, err := MessageToEventFromVersion(m, from)
if err != nil {
return nil, fmt.Errorf("could not convert event at index %d from format %d: %w",
m.EventIndex, from, err)
}
events[i] = *event
}
return events, nil
}
// ServiceEventToMessage converts a flow.ServiceEvent to a protobuf message
func ServiceEventToMessage(event flow.ServiceEvent) (*entities.ServiceEvent, error) {
bytes, err := json.Marshal(event.Event)
if err != nil {
return nil, fmt.Errorf("cannot marshal service event: %w", err)
}
return &entities.ServiceEvent{
Type: event.Type.String(),
Payload: bytes,
}, nil
}
// MessageToServiceEvent converts a protobuf message to a flow.ServiceEvent
func MessageToServiceEvent(m *entities.ServiceEvent) (*flow.ServiceEvent, error) {
rawEvent := m.Payload
eventType := flow.ServiceEventType(m.Type)
se, err := flow.ServiceEventJSONMarshaller.UnmarshalWithType(rawEvent, eventType)
return &se, err
}
// ServiceEventListToMessages converts a slice of flow.ServiceEvents to a slice of protobuf messages
func ServiceEventListToMessages(list flow.ServiceEventList) (
[]*entities.ServiceEvent,
error,
) {
entities := make([]*entities.ServiceEvent, len(list))
for i, serviceEvent := range list {
m, err := ServiceEventToMessage(serviceEvent)
if err != nil {
return nil, fmt.Errorf("failed to convert service event at index %d to message: %w", i, err)
}
entities[i] = m
}
return entities, nil
}
// MessagesToServiceEventList converts a slice of flow.ServiceEvents to a slice of protobuf messages
func MessagesToServiceEventList(m []*entities.ServiceEvent) (
flow.ServiceEventList,
error,
) {
parsedServiceEvents := make(flow.ServiceEventList, len(m))
for i, serviceEvent := range m {
parsedServiceEvent, err := MessageToServiceEvent(serviceEvent)
if err != nil {
return nil, fmt.Errorf("failed to parse service event at index %d from message: %w", i, err)
}
parsedServiceEvents[i] = *parsedServiceEvent
}
return parsedServiceEvents, nil
}
// CcfPayloadToJsonPayload converts a CCF-encoded payload to a JSON-encoded payload
func CcfPayloadToJsonPayload(p []byte) ([]byte, error) {
if len(p) == 0 {
return p, nil
}
val, err := ccf.Decode(nil, p)
if err != nil {
return nil, fmt.Errorf("unable to decode from ccf format: %w", err)
}
res, err := jsoncdc.Encode(val)
if err != nil {
return nil, fmt.Errorf("unable to encode to json-cdc format: %w", err)
}
return res, nil
}
// CcfEventToJsonEvent returns a new event with the payload converted from CCF to JSON
func CcfEventToJsonEvent(e flow.Event) (*flow.Event, error) {
convertedPayload, err := CcfPayloadToJsonPayload(e.Payload)
if err != nil {
return nil, err
}
return &flow.Event{
Type: e.Type,
TransactionID: e.TransactionID,
TransactionIndex: e.TransactionIndex,
EventIndex: e.EventIndex,
Payload: convertedPayload,
}, nil
}
// MessagesToBlockEvents converts a protobuf EventsResponse_Result messages to a slice of flow.BlockEvents.
func MessagesToBlockEvents(blocksEvents []*accessproto.EventsResponse_Result) []flow.BlockEvents {
evs := make([]flow.BlockEvents, len(blocksEvents))
for i, ev := range blocksEvents {
evs[i] = MessageToBlockEvents(ev)
}
return evs
}
// MessageToBlockEvents converts a protobuf EventsResponse_Result message to a flow.BlockEvents.
func MessageToBlockEvents(blockEvents *accessproto.EventsResponse_Result) flow.BlockEvents {
return flow.BlockEvents{
BlockHeight: blockEvents.BlockHeight,
BlockID: MessageToIdentifier(blockEvents.BlockId),
BlockTimestamp: blockEvents.BlockTimestamp.AsTime(),
Events: MessagesToEvents(blockEvents.Events),
}
}
func BlockEventsToMessages(blocks []flow.BlockEvents) ([]*accessproto.EventsResponse_Result, error) {
results := make([]*accessproto.EventsResponse_Result, len(blocks))
for i, block := range blocks {
event, err := BlockEventsToMessage(block)
if err != nil {
return nil, err
}
results[i] = event
}
return results, nil
}
func BlockEventsToMessage(block flow.BlockEvents) (*accessproto.EventsResponse_Result, error) {
eventMessages := make([]*entities.Event, len(block.Events))
for i, event := range block.Events {
eventMessages[i] = EventToMessage(event)
}
timestamp := timestamppb.New(block.BlockTimestamp)
return &accessproto.EventsResponse_Result{
BlockId: block.BlockID[:],
BlockHeight: block.BlockHeight,
BlockTimestamp: timestamp,
Events: eventMessages,
}, nil
}