-
Notifications
You must be signed in to change notification settings - Fork 32
/
event.go
341 lines (295 loc) · 9.35 KB
/
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package loomchain
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/go-kit/kit/metrics"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/gogo/protobuf/proto"
"github.com/loomnetwork/go-loom/plugin/types"
"github.com/loomnetwork/loomchain/eth/subs"
"github.com/loomnetwork/loomchain/log"
pubsub "github.com/phonkee/go-pubsub"
stdprometheus "github.com/prometheus/client_golang/prometheus"
)
type EventData types.EventData
// EventHandler handles collection, storage, and dispatch of tx & block events.
type EventHandler interface {
// Post stores an event in transient storage, it may be emitted later by EmitBlockTx.
Post(height uint64, e *types.EventData) error
// Commit adds events posted since the last Rollback to the set of events to be emitted when
// EmitBlockTx is called.
Commit(height uint64)
// Rollback discards any posted events that haven't been committed.
Rollback()
// Emits all events committed while processing the specified block.
EmitBlockTx(height uint64, blockTime time.Time) error
SubscriptionSet() *SubscriptionSet
EthSubscriptionSet() *subs.EthSubscriptionSet
LegacyEthSubscriptionSet() *subs.LegacyEthSubscriptionSet
}
type EventDispatcher interface {
Send(blockHeight uint64, eventIndex int, msg []byte) error
Flush()
}
type DefaultEventHandler struct {
dispatcher EventDispatcher
stash *stash
eventCache []*EventData
subscriptions *SubscriptionSet
ethSubscriptions *subs.EthSubscriptionSet
legacyEthSubscriptions *subs.LegacyEthSubscriptionSet
}
func NewDefaultEventHandler(dispatcher EventDispatcher) *DefaultEventHandler {
return &DefaultEventHandler{
dispatcher: dispatcher,
stash: newStash(),
subscriptions: NewSubscriptionSet(),
ethSubscriptions: subs.NewEthSubscriptionSet(),
legacyEthSubscriptions: subs.NewLegacyEthSubscriptionSet(),
}
}
func (ed *DefaultEventHandler) SubscriptionSet() *SubscriptionSet {
return ed.subscriptions
}
func (ed *DefaultEventHandler) EthSubscriptionSet() *subs.EthSubscriptionSet {
return ed.ethSubscriptions
}
func (ed *DefaultEventHandler) LegacyEthSubscriptionSet() *subs.LegacyEthSubscriptionSet {
return ed.legacyEthSubscriptions
}
func (ed *DefaultEventHandler) Post(height uint64, msg *types.EventData) error {
if msg.BlockHeight == 0 {
msg.BlockHeight = height
}
// TODO: this is stupid, fix it
eventData := EventData(*msg)
ed.eventCache = append(ed.eventCache, &eventData)
return nil
}
func (ed *DefaultEventHandler) Commit(height uint64) {
ed.stash.add(height, ed.eventCache)
ed.eventCache = nil
}
func (ed *DefaultEventHandler) Rollback() {
ed.eventCache = nil
}
func (ed *DefaultEventHandler) EmitBlockTx(height uint64, blockTime time.Time) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("caught panic publishing event: %v", r)
}
}()
msgs, err := ed.stash.fetch(height)
if err != nil {
return err
}
ed.legacyEthSubscriptions.Reset()
ed.ethSubscriptions.Reset()
// Timestamp added here rather than being stored in the event itself so
// as to avoid altering the data saved to the app-store.
timestamp := blockTime.Unix()
for i, msg := range msgs {
msg.BlockTime = timestamp
emitMsg, err := json.Marshal(&msg)
if err != nil {
log.Default.Error("Error in event marshalling for event", "message", emitMsg)
}
eventData := types.EventData(*msg)
ethMsg, err := proto.Marshal(&eventData)
if err != nil {
log.Default.Error("Error in event marshalling for event", "message", emitMsg)
}
log.Debug("sending event:", "height", height, "contract", msg.PluginName)
if err := ed.dispatcher.Send(height, i, emitMsg); err != nil {
log.Default.Error("Failed to dispatch event", "err", err, "height", height, "msg", msg)
}
contractTopic := "contract:" + msg.PluginName
ed.subscriptions.Publish(pubsub.NewMessage(contractTopic, emitMsg))
if err := ed.ethSubscriptions.EmitEvent(eventData); err != nil {
log.Error("Failed to emit subscription event", "err", err, "height", height, "msg", msg)
}
ed.legacyEthSubscriptions.Publish(pubsub.NewMessage(string(ethMsg), emitMsg))
for _, topic := range msg.Topics {
ed.subscriptions.Publish(pubsub.NewMessage(topic, emitMsg))
log.Debug("published WS event", "topic", topic)
}
}
ed.dispatcher.Flush()
ed.stash.purge(height)
return nil
}
// InstrumentingEventHandler captures metrics and implements EventHandler
type InstrumentingEventHandler struct {
methodDuration metrics.Histogram
next EventHandler
}
var _ EventHandler = &InstrumentingEventHandler{}
// NewInstrumentingEventHandler initializes the metrics and maintains event handler
func NewInstrumentingEventHandler(handler EventHandler) EventHandler {
// initialize metrics
fieldKeys := []string{"method", "error"}
methodDuration := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "loomchain",
Subsystem: "event_handler",
Name: "method_duration",
Help: "Total duration of requests in seconds.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, fieldKeys)
return &InstrumentingEventHandler{
methodDuration: methodDuration,
next: handler,
}
}
// Post captures the metrics
func (m InstrumentingEventHandler) Post(height uint64, e *types.EventData) (err error) {
defer func(begin time.Time) {
lvs := []string{"method", "Post", "error", fmt.Sprint(err != nil)}
m.methodDuration.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
err = m.next.Post(height, e)
return
}
func (m InstrumentingEventHandler) Commit(height uint64) {
m.next.Commit(height)
}
func (m InstrumentingEventHandler) Rollback() {
m.next.Rollback()
}
// EmitBlockTx captures the metrics
func (m InstrumentingEventHandler) EmitBlockTx(height uint64, blockTime time.Time) (err error) {
defer func(begin time.Time) {
lvs := []string{"method", "EmitBlockTx", "error", fmt.Sprint(err != nil)}
m.methodDuration.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
err = m.next.EmitBlockTx(height, blockTime)
return
}
func (m InstrumentingEventHandler) SubscriptionSet() *SubscriptionSet {
return m.next.SubscriptionSet()
}
func (m InstrumentingEventHandler) EthSubscriptionSet() *subs.EthSubscriptionSet {
return m.next.EthSubscriptionSet()
}
func (m InstrumentingEventHandler) LegacyEthSubscriptionSet() *subs.LegacyEthSubscriptionSet {
return m.next.LegacyEthSubscriptionSet()
}
// TODO: remove? It's just a wrapper of []*EventData
// events set implementation
type eventSet struct {
events []*EventData
}
func newEventSet() *eventSet {
s := &eventSet{}
s.events = []*EventData{}
return s
}
func (s *eventSet) Add(value *EventData) {
s.events = append(s.events, value)
}
func (s *eventSet) Values() []*EventData {
return s.events
}
// Set of subscription channels
type SubscriptionSet struct {
pubsub.Hub
// maps ID (remote socket address) to subscriber
clients map[string]pubsub.Subscriber
sync.RWMutex
}
func NewSubscriptionSet() *SubscriptionSet {
s := &SubscriptionSet{
Hub: pubsub.New(),
clients: make(map[string]pubsub.Subscriber),
}
return s
}
// For returns a subscriber matching the given ID, creating a new one if needed.
// New subscribers are subscribed to a single "system:" topic.
// Returns true if the subscriber already existed, and false if a new one was created.
func (s *SubscriptionSet) For(id string) (pubsub.Subscriber, bool) {
s.Lock()
_, exists := s.clients[id]
if !exists {
s.clients[id] = s.Subscribe("system:")
}
res := s.clients[id]
s.Unlock()
return res, exists
}
// AddSubscription subscribes the subscriber matching the given ID to additional topics (existing
// topics are retained).
// An error will be returned if a subscriber matching the given ID doesn't exist.
func (s *SubscriptionSet) AddSubscription(id string, topics []string) error {
var err error
s.Lock()
sub, exists := s.clients[id]
if !exists {
err = fmt.Errorf("Subscription %s not found", id)
} else {
log.Debug("Adding WS subscriptions", "topics", topics)
sub.Subscribe(append(sub.Topics(), topics...)...)
}
s.Unlock()
return err
}
func (s *SubscriptionSet) Purge(id string) {
s.Lock()
c := s.clients[id]
s.CloseSubscriber(c)
delete(s.clients, id)
s.Unlock()
}
// Remove unsubscribes a subscriber from the specified topic, if this is the only topic the subscriber
// was subscribed to then the subscriber is removed from the set.
// An error will be returned if a subscriber matching the given ID doesn't exist.
func (s *SubscriptionSet) Remove(id string, topic string) (err error) {
s.Lock()
c, ok := s.clients[id]
if !ok {
err = fmt.Errorf("Subscription not found")
} else {
c.Unsubscribe(topic)
if len(c.Topics()) == 0 {
s.Purge(id)
}
}
s.Unlock()
return err
}
// stash is a map of height -> byteStringSet
type stash struct {
m map[uint64]*eventSet
sync.Mutex
}
func newStash() *stash {
return &stash{
m: make(map[uint64]*eventSet),
}
}
func (s *stash) add(height uint64, msgs []*EventData) {
s.Lock()
defer s.Unlock()
_, ok := s.m[height]
if !ok {
s.m[height] = newEventSet()
}
for _, msg := range msgs {
s.m[height].Add(msg)
}
}
func (s *stash) fetch(height uint64) ([]*EventData, error) {
s.Lock()
defer s.Unlock()
set, ok := s.m[height]
if !ok {
return nil, nil
}
return set.Values(), nil
}
func (s *stash) purge(height uint64) {
s.Lock()
defer s.Unlock()
delete(s.m, height)
}