-
Notifications
You must be signed in to change notification settings - Fork 167
/
service_event.go
229 lines (200 loc) · 4.71 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
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"
)
const (
ServiceEventSetup = "setup"
ServiceEventCommit = "commit"
)
// 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 string
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
}
func (se *ServiceEvent) UnmarshalJSON(b []byte) error {
var enc map[string]interface{}
err := json.Unmarshal(b, &enc)
if err != nil {
return err
}
tp, ok := enc["Type"].(string)
if !ok {
return fmt.Errorf("missing type key")
}
ev, ok := enc["Event"]
if !ok {
return fmt.Errorf("missing event key")
}
// re-marshal the event, we'll unmarshal it into the appropriate type
evb, err := json.Marshal(ev)
if err != nil {
return err
}
var event interface{}
switch tp {
case ServiceEventSetup:
setup := new(EpochSetup)
err = json.Unmarshal(evb, setup)
if err != nil {
return err
}
event = setup
case ServiceEventCommit:
commit := new(EpochCommit)
err = json.Unmarshal(evb, commit)
if err != nil {
return err
}
event = commit
default:
return fmt.Errorf("invalid type: %s", tp)
}
*se = ServiceEvent{
Type: tp,
Event: event,
}
return nil
}
func (se *ServiceEvent) UnmarshalMsgpack(b []byte) error {
var enc map[string]interface{}
err := msgpack.Unmarshal(b, &enc)
if err != nil {
return err
}
tp, ok := enc["Type"].(string)
if !ok {
return fmt.Errorf("missing type key")
}
ev, ok := enc["Event"]
if !ok {
return fmt.Errorf("missing event key")
}
// re-marshal the event, we'll unmarshal it into the appropriate type
evb, err := msgpack.Marshal(ev)
if err != nil {
return err
}
var event interface{}
switch tp {
case ServiceEventSetup:
setup := new(EpochSetup)
err = msgpack.Unmarshal(evb, setup)
if err != nil {
return err
}
event = setup
case ServiceEventCommit:
commit := new(EpochCommit)
err = msgpack.Unmarshal(evb, commit)
if err != nil {
return err
}
event = commit
default:
return fmt.Errorf("invalid type: %s", tp)
}
*se = ServiceEvent{
Type: tp,
Event: event,
}
return nil
}
func (se *ServiceEvent) UnmarshalCBOR(b []byte) error {
var enc map[string]interface{}
err := cbor.Unmarshal(b, &enc)
if err != nil {
return err
}
tp, ok := enc["Type"].(string)
if !ok {
return fmt.Errorf("missing type key")
}
ev, ok := enc["Event"]
if !ok {
return fmt.Errorf("missing event key")
}
evb, err := cborcodec.EncMode.Marshal(ev)
if err != nil {
return err
}
var event interface{}
switch tp {
case ServiceEventSetup:
setup := new(EpochSetup)
err = cbor.Unmarshal(evb, setup)
if err != nil {
return err
}
event = setup
case ServiceEventCommit:
commit := new(EpochCommit)
err = cbor.Unmarshal(evb, commit)
if err != nil {
return err
}
event = commit
default:
return fmt.Errorf("invalid type: %s", tp)
}
*se = ServiceEvent{
Type: tp,
Event: event,
}
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
default:
return false, fmt.Errorf("unknown serice event type: %s", se.Type)
}
}