-
Notifications
You must be signed in to change notification settings - Fork 177
/
service_event.go
242 lines (214 loc) · 5.41 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
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"
)
// 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
}
type ServiceEventMarshaller interface {
Unmarshal(b []byte) (ServiceEvent, error)
UnmarshalWithType(
b []byte,
eventType ServiceEventType,
) (
ServiceEvent,
error,
)
}
type marshallerImpl struct {
MarshalFunc func(v interface{}) ([]byte, error)
UnmarshalFunc func(data []byte, v interface{}) error
}
var (
ServiceEventJSONMarshaller = marshallerImpl{
MarshalFunc: json.Marshal,
UnmarshalFunc: json.Unmarshal,
}
ServiceEventMSGPACKMarshaller = marshallerImpl{
MarshalFunc: msgpack.Marshal,
UnmarshalFunc: msgpack.Unmarshal,
}
ServiceEventCBORMarshaller = marshallerImpl{
MarshalFunc: cborcodec.EncMode.Marshal,
UnmarshalFunc: cbor.Unmarshal,
}
)
func (marshaller marshallerImpl) Unmarshal(b []byte) (
ServiceEvent,
error,
) {
var enc map[string]interface{}
err := marshaller.UnmarshalFunc(b, &enc)
if err != nil {
return ServiceEvent{}, err
}
tp, ok := enc["Type"].(string)
if !ok {
return ServiceEvent{}, fmt.Errorf("missing type key")
}
ev, ok := enc["Event"]
if !ok {
return ServiceEvent{}, fmt.Errorf("missing event key")
}
// re-marshal the event, we'll unmarshal it into the appropriate type
evb, err := marshaller.MarshalFunc(ev)
if err != nil {
return ServiceEvent{}, err
}
return marshaller.UnmarshalWithType(evb, ServiceEventType(tp))
}
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)
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.Unmarshal(b)
if err != nil {
return err
}
*se = e
return nil
}
func (se *ServiceEvent) UnmarshalMsgpack(b []byte) error {
e, err := ServiceEventMSGPACKMarshaller.Unmarshal(b)
if err != nil {
return err
}
*se = e
return nil
}
func (se *ServiceEvent) UnmarshalCBOR(b []byte) error {
e, err := ServiceEventCBORMarshaller.Unmarshal(b)
if err != nil {
return err
}
*se = e
return nil
}
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
default:
return false, fmt.Errorf("unknown serice event type: %s", se.Type)
}
}