/
service_event.go
293 lines (264 loc) · 8.75 KB
/
service_event.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
291
292
293
package flow
import (
"encoding/json"
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/vmihailenco/msgpack/v4"
cborcodec "github.com/onflow/flow-go/model/encoding/cbor"
)
type ServiceEventType string
// String returns the string representation of the service event type.
// TODO: this should not be needed. We should use ServiceEventType directly everywhere.
func (set ServiceEventType) String() string {
return string(set)
}
const (
ServiceEventSetup ServiceEventType = "setup"
ServiceEventCommit ServiceEventType = "commit"
ServiceEventVersionBeacon ServiceEventType = "version-beacon" // VersionBeacon only controls version of ENs, describing software compatability via semantic versioning
ServiceEventProtocolStateVersionUpgrade ServiceEventType = "protocol-state-version-upgrade" // Protocol State version applies to all nodes and uses an _integer version_ of the _protocol_
)
// ServiceEvent represents a service event, which is a special event that when
// emitted from a service account smart contract, is propagated to the protocol
// and included in blocks. Service events typically cause changes to the
// protocol state. See EpochSetup and EpochCommit events in this package for examples.
//
// This type represents a generic service event and primarily exists to simplify
// encoding and decoding.
type ServiceEvent struct {
Type ServiceEventType
Event interface{}
}
// ServiceEventList is a handy container to enable comparisons
type ServiceEventList []ServiceEvent
func (sel ServiceEventList) EqualTo(other ServiceEventList) (bool, error) {
if len(sel) != len(other) {
return false, nil
}
for i, se := range sel {
equalTo, err := se.EqualTo(&other[i])
if err != nil {
return false, fmt.Errorf(
"error while comparing service event index %d: %w",
i,
err,
)
}
if !equalTo {
return false, nil
}
}
return true, nil
}
// ServiceEventMarshaller marshals and unmarshals all types of service events.
type ServiceEventMarshaller interface {
// UnmarshalWrapped unmarshals the service event and returns it as a wrapped ServiceEvent type.
// The input bytes must be encoded as a generic wrapped ServiceEvent type.
// Forwards errors from the underlying marshaller (treat errors as you would from eg. json.Unmarshal)
UnmarshalWrapped(b []byte) (ServiceEvent, error)
// UnmarshalWithType unmarshals the service event and returns it as a wrapped ServiceEvent type.
// The input bytes must be encoded as a specific event type (for example, EpochSetup).
// Forwards errors from the underlying marshaller (treat errors as you would from eg. json.Unmarshal)
UnmarshalWithType(b []byte, eventType ServiceEventType) (ServiceEvent, error)
}
type marshallerImpl struct {
marshalFunc func(v interface{}) ([]byte, error)
unmarshalFunc func(data []byte, v interface{}) error
}
var _ ServiceEventMarshaller = (*marshallerImpl)(nil)
var (
// CAUTION: Json and MsgPack are to be used only for trusted data sources
ServiceEventJSONMarshaller = marshallerImpl{
marshalFunc: json.Marshal,
unmarshalFunc: json.Unmarshal,
}
// CAUTION: Json and MsgPack are to be used only for trusted data sources
ServiceEventMSGPACKMarshaller = marshallerImpl{
marshalFunc: msgpack.Marshal,
unmarshalFunc: msgpack.Unmarshal,
}
ServiceEventCBORMarshaller = marshallerImpl{
marshalFunc: cborcodec.EncMode.Marshal,
unmarshalFunc: cbor.Unmarshal,
}
)
// UnmarshalWrapped unmarshals the service event `b` and returns it as a wrapped ServiceEvent type.
// The input bytes must be encoded as a generic wrapped ServiceEvent type.
// Forwards errors from the underlying marshaller (treat errors as you would from eg. json.Unmarshal)
func (marshaller marshallerImpl) UnmarshalWrapped(b []byte) (ServiceEvent, error) {
var eventTypeWrapper struct {
Type ServiceEventType
}
err := marshaller.unmarshalFunc(b, &eventTypeWrapper)
if err != nil {
return ServiceEvent{}, err
}
eventType := eventTypeWrapper.Type
var event any
switch eventType {
case ServiceEventSetup:
event, err = unmarshalWrapped[EpochSetup](b, marshaller)
case ServiceEventCommit:
event, err = unmarshalWrapped[EpochCommit](b, marshaller)
case ServiceEventVersionBeacon:
event, err = unmarshalWrapped[VersionBeacon](b, marshaller)
case ServiceEventProtocolStateVersionUpgrade:
event, err = unmarshalWrapped[ProtocolStateVersionUpgrade](b, marshaller)
default:
return ServiceEvent{}, fmt.Errorf("invalid type: %s", eventType)
}
if err != nil {
return ServiceEvent{}, fmt.Errorf("failed to unmarshal to service event to type %s: %w", eventType, err)
}
return ServiceEvent{
Type: eventType,
Event: event,
}, nil
}
// unmarshalWrapped is a helper function for UnmarshalWrapped which unmarshals the
// Event portion of a ServiceEvent into a specific typed structure.
// Forwards errors from the underlying marshaller (treat errors as you would from eg. json.Unmarshal)
func unmarshalWrapped[E any](b []byte, marshaller marshallerImpl) (*E, error) {
wrapper := struct {
Type ServiceEventType
Event E
}{}
err := marshaller.unmarshalFunc(b, &wrapper)
if err != nil {
return nil, err
}
return &wrapper.Event, nil
}
// UnmarshalWithType unmarshals the service event and returns it as a wrapped ServiceEvent type.
// The input bytes must be encoded as a specific event type (for example, EpochSetup).
// Forwards errors from the underlying marshaller (treat errors as you would from eg. json.Unmarshal)
func (marshaller marshallerImpl) UnmarshalWithType(b []byte, eventType ServiceEventType) (ServiceEvent, error) {
var event interface{}
switch eventType {
case ServiceEventSetup:
event = new(EpochSetup)
case ServiceEventCommit:
event = new(EpochCommit)
case ServiceEventVersionBeacon:
event = new(VersionBeacon)
case ServiceEventProtocolStateVersionUpgrade:
event = new(ProtocolStateVersionUpgrade)
default:
return ServiceEvent{}, fmt.Errorf("invalid type: %s", eventType)
}
err := marshaller.unmarshalFunc(b, event)
if err != nil {
return ServiceEvent{},
fmt.Errorf(
"failed to unmarshal to service event ot type %s: %w",
eventType,
err,
)
}
return ServiceEvent{
Type: eventType,
Event: event,
}, nil
}
func (se *ServiceEvent) UnmarshalJSON(b []byte) error {
e, err := ServiceEventJSONMarshaller.UnmarshalWrapped(b)
if err != nil {
return err
}
*se = e
return nil
}
func (se *ServiceEvent) UnmarshalMsgpack(b []byte) error {
e, err := ServiceEventMSGPACKMarshaller.UnmarshalWrapped(b)
if err != nil {
return err
}
*se = e
return nil
}
func (se *ServiceEvent) UnmarshalCBOR(b []byte) error {
e, err := ServiceEventCBORMarshaller.UnmarshalWrapped(b)
if err != nil {
return err
}
*se = e
return nil
}
// EqualTo checks whether two service events are equal, as defined by the underlying Event type.
// Inputs must have already been independently validated and well-formed.
// No errors are expected during normal operation.
func (se *ServiceEvent) EqualTo(other *ServiceEvent) (bool, error) {
if se.Type != other.Type {
return false, nil
}
switch se.Type {
case ServiceEventSetup:
setup, ok := se.Event.(*EpochSetup)
if !ok {
return false, fmt.Errorf(
"internal invalid type for ServiceEventSetup: %T",
se.Event,
)
}
otherSetup, ok := other.Event.(*EpochSetup)
if !ok {
return false, fmt.Errorf(
"internal invalid type for ServiceEventSetup: %T",
other.Event,
)
}
return setup.EqualTo(otherSetup), nil
case ServiceEventCommit:
commit, ok := se.Event.(*EpochCommit)
if !ok {
return false, fmt.Errorf(
"internal invalid type for ServiceEventCommit: %T",
se.Event,
)
}
otherCommit, ok := other.Event.(*EpochCommit)
if !ok {
return false, fmt.Errorf(
"internal invalid type for ServiceEventCommit: %T",
other.Event,
)
}
return commit.EqualTo(otherCommit), nil
case ServiceEventVersionBeacon:
version, ok := se.Event.(*VersionBeacon)
if !ok {
return false, fmt.Errorf(
"internal invalid type for ServiceEventVersionBeacon: %T",
se.Event,
)
}
otherVersion, ok := other.Event.(*VersionBeacon)
if !ok {
return false,
fmt.Errorf(
"internal invalid type for ServiceEventVersionBeacon: %T",
other.Event,
)
}
return version.EqualTo(otherVersion), nil
case ServiceEventProtocolStateVersionUpgrade:
version, ok := se.Event.(*ProtocolStateVersionUpgrade)
if !ok {
return false, fmt.Errorf(
"internal invalid type for ProtocolStateVersionUpgrade: %T",
se.Event,
)
}
otherVersion, ok := other.Event.(*ProtocolStateVersionUpgrade)
if !ok {
return false,
fmt.Errorf(
"internal invalid type for ProtocolStateVersionUpgrade: %T",
other.Event,
)
}
return version.EqualTo(otherVersion), nil
default:
return false, fmt.Errorf("unknown serice event type: %s", se.Type)
}
}