/
broadcaster.go
468 lines (399 loc) · 14.2 KB
/
broadcaster.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package log
import (
"context"
"errors"
"sync"
"sync/atomic"
"time"
"github.com/GoPlugin/Plugin/core/null"
"gorm.io/gorm"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/tevino/abool"
"github.com/GoPlugin/Plugin/core/internal/gethwrappers/generated"
"github.com/GoPlugin/Plugin/core/logger"
"github.com/GoPlugin/Plugin/core/service"
"github.com/GoPlugin/Plugin/core/services/eth"
httypes "github.com/GoPlugin/Plugin/core/services/headtracker/types"
"github.com/GoPlugin/Plugin/core/store/models"
"github.com/GoPlugin/Plugin/core/utils"
)
//go:generate mockery --name Broadcaster --output ./mocks/ --case=underscore --structname Broadcaster --filename broadcaster.go
//go:generate mockery --name Listener --output ./mocks/ --case=underscore --structname Listener --filename listener.go
type (
// The Broadcaster manages log subscription requests for the Chainlink node. Instead
// of creating a new subscription for each request, it multiplexes all subscriptions
// to all of the relevant contracts over a single connection and forwards the logs to the
// relevant subscribers.
//
// In case of node crash and/or restart, the logs will be backfilled from the latest head from DB,
// for subscribers that are added before all dependents of LogBroadcaster are done.
//
// If a subscriber is added after the LogBroadcaster does the initial backfill,
// then it's possible/likely that the backfill fill only have depth: 1 (from latest head)
//
// Of course, these backfilled logs + any new logs will only be sent after the NumConfirmations for given subscriber.
Broadcaster interface {
utils.DependentAwaiter
service.Service
httypes.HeadTrackable
IsConnected() bool
Register(listener Listener, opts ListenerOpts) (unsubscribe func())
LatestHead() *models.Head
TrackedAddressesCount() uint32
// DB interactions
WasAlreadyConsumed(db *gorm.DB, lb Broadcast) (bool, error)
MarkConsumed(db *gorm.DB, lb Broadcast) error
}
broadcaster struct {
orm ORM
config Config
connected *abool.AtomicBool
latestHeadFromDb *models.Head
ethSubscriber *ethSubscriber
registrations *registrations
logPool *logPool
addSubscriber *utils.Mailbox
rmSubscriber *utils.Mailbox
newHeads *utils.Mailbox
utils.StartStopOnce
utils.DependentAwaiter
chStop chan struct{}
wgDone sync.WaitGroup
trackedAddressesCount uint32
}
Config interface {
BlockBackfillDepth() uint64
EthFinalityDepth() uint
EthLogBackfillBatchSize() uint32
}
ListenerOpts struct {
Contract common.Address
// Event types to receive, with value filter for each field in the event
// No filter or an empty filter for a given field position mean: all values allowed
// the key should be a result of AbigenLog.Topic() call
LogsWithTopics map[common.Hash][][]Topic
ParseLog ParseLogFunc
// Minimum number of block confirmations before the log is received
NumConfirmations uint64
}
ParseLogFunc func(log types.Log) (generated.AbigenLog, error)
registration struct {
listener Listener
opts ListenerOpts
}
Topic common.Hash
)
var _ Broadcaster = (*broadcaster)(nil)
// NewBroadcaster creates a new instance of the broadcaster
func NewBroadcaster(orm ORM, ethClient eth.Client, config Config, highestSavedHead *models.Head) *broadcaster {
chStop := make(chan struct{})
return &broadcaster{
orm: orm,
config: config,
connected: abool.New(),
ethSubscriber: newEthSubscriber(ethClient, config, chStop),
registrations: newRegistrations(),
logPool: newLogPool(),
addSubscriber: utils.NewMailbox(0),
rmSubscriber: utils.NewMailbox(0),
newHeads: utils.NewMailbox(1),
DependentAwaiter: utils.NewDependentAwaiter(),
chStop: chStop,
latestHeadFromDb: highestSavedHead,
}
}
func (b *broadcaster) Start() error {
return b.StartOnce("LogBroadcaster", func() error {
b.wgDone.Add(2)
go b.awaitInitialSubscribers()
return nil
})
}
func (b *broadcaster) LatestHead() *models.Head {
return b.latestHeadFromDb
}
func (b *broadcaster) TrackedAddressesCount() uint32 {
return atomic.LoadUint32(&b.trackedAddressesCount)
}
func (b *broadcaster) Close() error {
return b.StopOnce("LogBroadcaster", func() error {
close(b.chStop)
b.wgDone.Wait()
return nil
})
}
func (b *broadcaster) awaitInitialSubscribers() {
defer b.wgDone.Done()
for {
select {
case <-b.addSubscriber.Notify():
b.onAddSubscribers()
case <-b.rmSubscriber.Notify():
b.onRmSubscribers()
case <-b.DependentAwaiter.AwaitDependents():
go b.startResubscribeLoop()
return
case <-b.chStop:
return
}
}
}
func (b *broadcaster) Register(listener Listener, opts ListenerOpts) (unsubscribe func()) {
if len(opts.LogsWithTopics) == 0 {
logger.Fatal("Must supply at least 1 Log to Register")
}
wasOverCapacity := b.addSubscriber.Deliver(registration{listener, opts})
if wasOverCapacity {
logger.Error("LogBroadcaster: subscription mailbox is over capacity - dropped the oldest unprocessed subscription")
}
return func() {
wasOverCapacity := b.rmSubscriber.Deliver(registration{listener, opts})
if wasOverCapacity {
logger.Error("LogBroadcaster: subscription removal mailbox is over capacity - dropped the oldest unprocessed removal")
}
}
}
func (b *broadcaster) Connect(head *models.Head) error { return nil }
func (b *broadcaster) OnNewLongestChain(ctx context.Context, head models.Head) {
wasOverCapacity := b.newHeads.Deliver(head)
if wasOverCapacity {
logger.Tracew("LogBroadcaster: Dropped the older head in the mailbox, while inserting latest (which is fine)", "latestBlockNumber", head.Number)
}
}
func (b *broadcaster) IsConnected() bool {
return b.connected.IsSet()
}
// The subscription is closed in two cases:
// - intentionally, when the set of contracts we're listening to changes
// - on a connection error
//
// This method recreates the subscription in both cases. In the event of a connection
// error, it attempts to reconnect. Any time there'b a change in connection state, it
// notifies its subscribers.
func (b *broadcaster) startResubscribeLoop() {
defer b.wgDone.Done()
var subscription managedSubscription = newNoopSubscription()
defer func() { subscription.Unsubscribe() }()
var chRawLogs chan types.Log
for {
logger.Debug("LogBroadcaster: Resubscribing and backfilling logs...")
addresses, topics := b.registrations.addressesAndTopics()
newSubscription, abort := b.ethSubscriber.createSubscription(addresses, topics)
if abort {
return
}
var backfillFrom null.Int64
if b.latestHeadFromDb != nil {
// The backfill needs to start at an earlier block than the one last saved in DB, to account for:
// - keeping logs in the in-memory buffers in registration.go
// (which will be lost on node restart) for MAX(NumConfirmations of subscribers)
// - HeadTracker saving the heads to DB asynchronously versus LogBroadcaster, where a head
// (or more heads on fast chains) may be saved but not yet processed by LB
// using BlockBackfillDepth makes sure the backfill will be dependent on the per-chain configuration
from := b.latestHeadFromDb.Number -
int64(b.registrations.highestNumConfirmations) -
int64(b.config.BlockBackfillDepth())
logger.Debugw("LogBroadcaster: Using highest seen head as part of the initial backfill",
"blockNumber", b.latestHeadFromDb.Number, "blockHash", b.latestHeadFromDb.Hash,
"highestNumConfirmations", b.registrations.highestNumConfirmations, "blockBackfillDepth", b.config.BlockBackfillDepth(),
)
if from < 0 {
from = 0
}
backfillFrom = null.Int64From(from)
}
chBackfilledLogs, abort := b.ethSubscriber.backfillLogs(backfillFrom, addresses, topics)
if abort {
return
}
// latestHeadFromDb is only used in the first backfill
b.latestHeadFromDb = nil
// Each time this loop runs, chRawLogs is reconstituted as:
// "remaining logs from last subscription <- backfilled logs <- logs from new subscription"
// There will be duplicated logs in this channel. It is the responsibility of subscribers
// to account for this using the helpers on the Broadcast type.
chRawLogs = b.appendLogChannel(chRawLogs, chBackfilledLogs)
chRawLogs = b.appendLogChannel(chRawLogs, newSubscription.Logs())
subscription.Unsubscribe()
subscription = newSubscription
b.connected.Set()
atomic.StoreUint32(&b.trackedAddressesCount, uint32(len(addresses)))
shouldResubscribe, err := b.eventLoop(chRawLogs, subscription.Err())
if err != nil {
logger.Warnw("LogBroadcaster: error in the event loop - will reconnect", "err", err)
b.connected.UnSet()
continue
} else if !shouldResubscribe {
b.connected.UnSet()
return
}
}
}
func (b *broadcaster) eventLoop(chRawLogs <-chan types.Log, chErr <-chan error) (shouldResubscribe bool, _ error) {
// We debounce requests to subscribe and unsubscribe to avoid making too many
// RPC calls to the Ethereum node, particularly on startup.
var needsResubscribe bool
debounceResubscribe := time.NewTicker(1 * time.Second)
defer debounceResubscribe.Stop()
logger.Debug("LogBroadcaster: starting the event loop")
for {
select {
case rawLog := <-chRawLogs:
b.onNewLog(rawLog)
case <-b.newHeads.Notify():
b.onNewHeads()
case err := <-chErr:
// Note we'll get a message on this channel
// if the eth node terminates the connection.
return true, err
case <-b.addSubscriber.Notify():
needsResubscribe = b.onAddSubscribers() || needsResubscribe
case <-b.rmSubscriber.Notify():
needsResubscribe = b.onRmSubscribers() || needsResubscribe
case <-debounceResubscribe.C:
if needsResubscribe {
logger.Debug("LogBroadcaster: returning from the event loop to resubscribe")
return true, nil
}
case <-b.chStop:
return false, nil
}
}
}
func (b *broadcaster) onNewLog(log types.Log) {
if log.Removed {
return
} else if !b.registrations.isAddressRegistered(log.Address) {
return
}
b.logPool.addLog(log)
}
func (b *broadcaster) onNewHeads() {
var latestHead *models.Head
for {
// We only care about the most recent head
item := b.newHeads.RetrieveLatestAndClear()
if item == nil {
break
}
head, ok := item.(models.Head)
if !ok {
logger.Errorf("expected `models.Head`, got %T", item)
continue
}
latestHead = &head
}
// latestHead may sometimes be nil on high rate of heads,
// when 'b.newHeads.Notify()' receives more times that the number of items in the mailbox
// Some heads may be missed (which is fine for LogBroadcaster logic) but the latest one in a burst will be received
if latestHead != nil {
logger.Debugw("LogBroadcaster: Received head", "blockNumber", latestHead.Number, "blockHash", latestHead.Hash)
logs := b.logPool.getLogsToSend(*latestHead, b.registrations.highestNumConfirmations, uint64(b.config.EthFinalityDepth()))
b.registrations.sendLogs(logs, b.orm, *latestHead)
}
}
func (b *broadcaster) onAddSubscribers() (needsResubscribe bool) {
for {
x, exists := b.addSubscriber.Retrieve()
if !exists {
break
}
reg, ok := x.(registration)
if !ok {
logger.Errorf("expected `registration`, got %T", x)
continue
}
logger.Debugw("LogBroadcaster: Subscribing listener", "requiredBlockConfirmations", reg.opts.NumConfirmations)
needsResub := b.registrations.addSubscriber(reg)
if needsResub {
needsResubscribe = true
}
}
return
}
func (b *broadcaster) onRmSubscribers() (needsResubscribe bool) {
for {
x, exists := b.rmSubscriber.Retrieve()
if !exists {
break
}
reg, ok := x.(registration)
if !ok {
logger.Errorf("expected `registration`, got %T", x)
continue
}
logger.Debugw("LogBroadcaster: Unsubscribing listener", "requiredBlockConfirmations", reg.opts.NumConfirmations)
needsResub := b.registrations.removeSubscriber(reg)
if needsResub {
needsResubscribe = true
}
}
return
}
func (b *broadcaster) appendLogChannel(ch1, ch2 <-chan types.Log) chan types.Log {
if ch1 == nil && ch2 == nil {
return nil
}
chCombined := make(chan types.Log)
go func() {
defer close(chCombined)
if ch1 != nil {
for rawLog := range ch1 {
select {
case chCombined <- rawLog:
case <-b.chStop:
return
}
}
}
if ch2 != nil {
for rawLog := range ch2 {
select {
case chCombined <- rawLog:
case <-b.chStop:
return
}
}
}
}()
return chCombined
}
// WasAlreadyConsumed reports whether the given consumer had already consumed the given log
func (b *broadcaster) WasAlreadyConsumed(db *gorm.DB, lb Broadcast) (bool, error) {
return b.orm.WasBroadcastConsumed(db, lb.RawLog().BlockHash, lb.RawLog().Index, lb.JobID())
}
// MarkConsumed marks the log as having been successfully consumed by the subscriber
func (b *broadcaster) MarkConsumed(db *gorm.DB, lb Broadcast) error {
return b.orm.MarkBroadcastConsumed(db, lb.RawLog().BlockHash, lb.RawLog().BlockNumber, lb.RawLog().Index, lb.JobID())
}
type NullBroadcaster struct{ ErrMsg string }
func (n *NullBroadcaster) IsConnected() bool { return false }
func (n *NullBroadcaster) Register(listener Listener, opts ListenerOpts) (unsubscribe func()) {
return func() {}
}
func (n *NullBroadcaster) LatestHead() *models.Head {
return nil
}
func (n *NullBroadcaster) TrackedAddressesCount() uint32 {
return 0
}
func (n *NullBroadcaster) WasAlreadyConsumed(db *gorm.DB, lb Broadcast) (bool, error) {
return false, errors.New(n.ErrMsg)
}
func (n *NullBroadcaster) MarkConsumed(db *gorm.DB, lb Broadcast) error {
return errors.New(n.ErrMsg)
}
func (n *NullBroadcaster) AddDependents(int) {}
func (n *NullBroadcaster) AwaitDependents() <-chan struct{} {
ch := make(chan struct{})
close(ch)
return ch
}
func (n *NullBroadcaster) DependentReady() {}
func (n *NullBroadcaster) Start() error { return nil }
func (n *NullBroadcaster) Close() error { return nil }
func (n *NullBroadcaster) Healthy() error { return nil }
func (n *NullBroadcaster) Ready() error { return nil }
func (n *NullBroadcaster) Connect(*models.Head) error { return nil }
func (n *NullBroadcaster) OnNewLongestChain(context.Context, models.Head) {}