forked from 0xPolygon/polygon-edge
/
filter_manager.go
857 lines (671 loc) · 19.2 KB
/
filter_manager.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package jsonrpc
import (
"container/heap"
"encoding/json"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/Gabulhas/polygon-external-consensus/blockchain"
"github.com/Gabulhas/polygon-external-consensus/types"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/hashicorp/go-hclog"
)
var (
ErrFilterNotFound = errors.New("filter not found")
ErrWSFilterDoesNotSupportGetChanges = errors.New("web socket Filter doesn't support to return a batch of the changes")
ErrCastingFilterToLogFilter = errors.New("casting filter object to logFilter error")
ErrBlockNotFound = errors.New("block not found")
ErrIncorrectBlockRange = errors.New("incorrect range")
ErrBlockRangeTooHigh = errors.New("block range too high")
ErrPendingBlockNumber = errors.New("pending block number is not supported")
ErrNoWSConnection = errors.New("no websocket connection")
)
// defaultTimeout is the timeout to remove the filters that don't have a web socket stream
var defaultTimeout = 1 * time.Minute
const (
// The index in heap which is indicating the element is not in the heap
NoIndexInHeap = -1
)
// filter is an interface that BlockFilter and LogFilter implement
type filter interface {
// hasWSConn returns the flag indicating the filter has web socket stream
hasWSConn() bool
// getFilterBase returns filterBase that has common fields
getFilterBase() *filterBase
// getUpdates returns stored data in a JSON serializable form
getUpdates() (interface{}, error)
// sendUpdates write stored data to web socket stream
sendUpdates() error
}
// filterBase is a struct for common fields between BlockFilter and LogFilter
type filterBase struct {
// UUID, a key of filter for client
id string
// index in the timeouts heap, -1 for non-existing index
heapIndex int
// timestamp to be expired
expiresAt time.Time
// websocket connection
ws wsConn
}
// newFilterBase initializes filterBase with unique ID
func newFilterBase(ws wsConn) filterBase {
return filterBase{
id: uuid.New().String(),
ws: ws,
heapIndex: NoIndexInHeap,
}
}
// getFilterBase returns its own reference so that child struct can return base
func (f *filterBase) getFilterBase() *filterBase {
return f
}
// hasWSConn returns the flag indicating this filter has websocket connection
func (f *filterBase) hasWSConn() bool {
return f.ws != nil
}
const ethSubscriptionTemplate = `{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription":"%s",
"result": %s
}
}`
// writeMessageToWs sends given message to websocket stream
func (f *filterBase) writeMessageToWs(msg string) error {
if !f.hasWSConn() {
return ErrNoWSConnection
}
return f.ws.WriteMessage(
websocket.TextMessage,
[]byte(fmt.Sprintf(ethSubscriptionTemplate, f.id, msg)),
)
}
// blockFilter is a filter to store the updates of block
type blockFilter struct {
filterBase
sync.Mutex
block *headElem
}
// takeBlockUpdates advances blocks from head to latest and returns header array
func (f *blockFilter) takeBlockUpdates() []*types.Header {
updates, newHead := f.block.getUpdates()
f.setHeadElem(newHead)
return updates
}
// setHeadElem sets the block filter head
func (f *blockFilter) setHeadElem(head *headElem) {
f.Lock()
defer f.Unlock()
f.block = head
}
// getUpdates returns updates of blocks in string
func (f *blockFilter) getUpdates() (interface{}, error) {
headers := f.takeBlockUpdates()
updates := make([]string, len(headers))
for index, header := range headers {
updates[index] = header.Hash.String()
}
return updates, nil
}
// sendUpdates writes the updates of blocks to web socket stream
func (f *blockFilter) sendUpdates() error {
updates := f.takeBlockUpdates()
for _, header := range updates {
raw, err := json.Marshal(header)
if err != nil {
return err
}
if err := f.writeMessageToWs(string(raw)); err != nil {
return err
}
}
return nil
}
// logFilter is a filter to store logs that meet the conditions in query
type logFilter struct {
filterBase
sync.Mutex
query *LogQuery
logs []*Log
}
// appendLog appends new log to logs
func (f *logFilter) appendLog(log *Log) {
f.Lock()
defer f.Unlock()
f.logs = append(f.logs, log)
}
// takeLogUpdates returns all saved logs in filter and set new log slice
func (f *logFilter) takeLogUpdates() []*Log {
f.Lock()
defer f.Unlock()
logs := f.logs
f.logs = []*Log{} // create brand-new slice so that prevent new logs from being added to current logs
return logs
}
// getUpdates returns stored logs in string
func (f *logFilter) getUpdates() (interface{}, error) {
logs := f.takeLogUpdates()
return logs, nil
}
// sendUpdates writes stored logs to web socket stream
func (f *logFilter) sendUpdates() error {
logs := f.takeLogUpdates()
for _, log := range logs {
res, err := json.Marshal(log)
if err != nil {
return err
}
if err := f.writeMessageToWs(string(res)); err != nil {
return err
}
}
return nil
}
// filterManagerStore provides methods required by FilterManager
type filterManagerStore interface {
// Header returns the current header of the chain (genesis if empty)
Header() *types.Header
// SubscribeEvents subscribes for chain head events
SubscribeEvents() blockchain.Subscription
// GetReceiptsByHash returns the receipts for a block hash
GetReceiptsByHash(hash types.Hash) ([]*types.Receipt, error)
// GetBlockByHash returns the block using the block hash
GetBlockByHash(hash types.Hash, full bool) (*types.Block, bool)
// GetBlockByNumber returns a block using the provided number
GetBlockByNumber(num uint64, full bool) (*types.Block, bool)
}
// FilterManager manages all running filters
type FilterManager struct {
sync.RWMutex
logger hclog.Logger
timeout time.Duration
store filterManagerStore
subscription blockchain.Subscription
blockStream *blockStream
blockRangeLimit uint64
filters map[string]filter
timeouts timeHeapImpl
updateCh chan struct{}
closeCh chan struct{}
}
func NewFilterManager(logger hclog.Logger, store filterManagerStore, blockRangeLimit uint64) *FilterManager {
m := &FilterManager{
logger: logger.Named("filter"),
timeout: defaultTimeout,
store: store,
blockStream: &blockStream{},
blockRangeLimit: blockRangeLimit,
filters: make(map[string]filter),
timeouts: timeHeapImpl{},
updateCh: make(chan struct{}),
closeCh: make(chan struct{}),
}
// start blockstream with the current header
header := store.Header()
m.blockStream.push(header)
// start the head watcher
m.subscription = store.SubscribeEvents()
return m
}
// Run starts worker process to handle events
func (f *FilterManager) Run() {
// watch for new events in the blockchain
watchCh := make(chan *blockchain.Event)
go func() {
for {
evnt := f.subscription.GetEvent()
if evnt == nil {
return
}
watchCh <- evnt
}
}()
var timeoutCh <-chan time.Time
for {
// check for the next filter to be removed
filterBase := f.nextTimeoutFilter()
// set timer to remove filter
if filterBase != nil {
timeoutCh = time.After(time.Until(filterBase.expiresAt))
}
select {
case evnt := <-watchCh:
// new blockchain event
if err := f.dispatchEvent(evnt); err != nil {
f.logger.Error("failed to dispatch event", "err", err)
}
case <-timeoutCh:
// timeout for filter
// if filter still exists
if !f.Uninstall(filterBase.id) {
f.logger.Warn("failed to uninstall filter", "id", filterBase.id)
}
case <-f.updateCh:
// filters change, reset the loop to start the timeout timer
case <-f.closeCh:
// stop the filter manager
return
}
}
}
// Close closed closeCh so that terminate worker
func (f *FilterManager) Close() {
close(f.closeCh)
}
// NewBlockFilter adds new BlockFilter
func (f *FilterManager) NewBlockFilter(ws wsConn) string {
filter := &blockFilter{
filterBase: newFilterBase(ws),
block: f.blockStream.Head(),
}
if filter.hasWSConn() {
ws.SetFilterID(filter.id)
}
return f.addFilter(filter)
}
// NewLogFilter adds new LogFilter
func (f *FilterManager) NewLogFilter(logQuery *LogQuery, ws wsConn) string {
filter := &logFilter{
filterBase: newFilterBase(ws),
query: logQuery,
}
if filter.hasWSConn() {
ws.SetFilterID(filter.id)
}
return f.addFilter(filter)
}
// Exists checks the filter with given ID exists
func (f *FilterManager) Exists(id string) bool {
f.RLock()
defer f.RUnlock()
_, ok := f.filters[id]
return ok
}
func (f *FilterManager) getLogsFromBlock(query *LogQuery, block *types.Block) ([]*Log, error) {
receipts, err := f.store.GetReceiptsByHash(block.Header.Hash)
if err != nil {
return nil, err
}
logs := make([]*Log, 0)
for idx, receipt := range receipts {
for logIdx, log := range receipt.Logs {
if query.Match(log) {
logs = append(logs, &Log{
Address: log.Address,
Topics: log.Topics,
Data: log.Data,
BlockNumber: argUint64(block.Header.Number),
BlockHash: block.Header.Hash,
TxHash: block.Transactions[idx].Hash,
TxIndex: argUint64(idx),
LogIndex: argUint64(logIdx),
})
}
}
}
return logs, nil
}
func (f *FilterManager) getLogsFromBlocks(query *LogQuery) ([]*Log, error) {
latestBlockNumber := f.store.Header().Number
resolveNum := func(num BlockNumber) (uint64, error) {
switch num {
case PendingBlockNumber:
return 0, ErrPendingBlockNumber
case EarliestBlockNumber:
num = 0
case LatestBlockNumber:
return latestBlockNumber, nil
}
return uint64(num), nil
}
from, err := resolveNum(query.fromBlock)
if err != nil {
return nil, err
}
to, err := resolveNum(query.toBlock)
if err != nil {
return nil, err
}
if to < from {
return nil, ErrIncorrectBlockRange
}
// If from equals genesis block
// skip it
if from == 0 {
from = 1
}
// if not disabled, avoid handling large block ranges
if f.blockRangeLimit != 0 && to-from > f.blockRangeLimit {
return nil, ErrBlockRangeTooHigh
}
logs := make([]*Log, 0)
for i := from; i <= to; i++ {
block, ok := f.store.GetBlockByNumber(i, true)
if !ok {
break
}
if len(block.Transactions) == 0 {
// do not check logs if no txs
continue
}
blockLogs, err := f.getLogsFromBlock(query, block)
if err != nil {
return nil, err
}
logs = append(logs, blockLogs...)
}
return logs, nil
}
// GetLogsForQuery return array of logs for given query
func (f *FilterManager) GetLogsForQuery(query *LogQuery) ([]*Log, error) {
if query.BlockHash != nil {
// BlockHash is set -> fetch logs from this block only
block, ok := f.store.GetBlockByHash(*query.BlockHash, true)
if !ok {
return nil, ErrBlockNotFound
}
if len(block.Transactions) == 0 {
// no txs in block, return empty response
return []*Log{}, nil
}
return f.getLogsFromBlock(query, block)
}
// gets logs from a range of blocks
return f.getLogsFromBlocks(query)
}
// getFilterByID fetches the filter by the ID
func (f *FilterManager) getFilterByID(filterID string) filter {
f.RLock()
defer f.RUnlock()
return f.filters[filterID]
}
// GetLogFilterFromID return log filter for given filterID
func (f *FilterManager) GetLogFilterFromID(filterID string) (*logFilter, error) {
filterRaw := f.getFilterByID(filterID)
if filterRaw == nil {
return nil, ErrFilterNotFound
}
logFilter, ok := filterRaw.(*logFilter)
if !ok {
return nil, ErrCastingFilterToLogFilter
}
return logFilter, nil
}
// GetFilterChanges returns the updates of the filter with given ID in string, and refreshes the timeout on the filter
func (f *FilterManager) GetFilterChanges(id string) (interface{}, error) {
filter, res, err := f.getFilterAndChanges(id)
if err == nil && !filter.hasWSConn() {
// Refresh the timeout on this filter
f.Lock()
f.refreshFilterTimeout(filter.getFilterBase())
f.Unlock()
}
return res, err
}
// getFilterAndChanges returns the updates of the filter with given ID in string (read lock only)
func (f *FilterManager) getFilterAndChanges(id string) (filter, interface{}, error) {
f.RLock()
defer f.RUnlock()
filter, ok := f.filters[id]
if !ok {
return nil, nil, ErrFilterNotFound
}
// we cannot get updates from a ws filter with getFilterChanges
if filter.hasWSConn() {
return nil, nil, ErrWSFilterDoesNotSupportGetChanges
}
res, err := filter.getUpdates()
if err != nil {
return nil, nil, err
}
return filter, res, nil
}
// Uninstall removes the filter with given ID from list
func (f *FilterManager) Uninstall(id string) bool {
f.Lock()
defer f.Unlock()
return f.removeFilterByID(id)
}
// removeFilterByID removes the filter with given ID [NOT Thread Safe]
func (f *FilterManager) removeFilterByID(id string) bool {
// Make sure filter exists
filter, ok := f.filters[id]
if !ok {
return false
}
delete(f.filters, id)
if removed := f.timeouts.removeFilter(filter.getFilterBase()); removed {
f.emitSignalToUpdateCh()
}
return true
}
// RemoveFilterByWs removes the filter with given WS [Thread safe]
func (f *FilterManager) RemoveFilterByWs(ws wsConn) {
f.Lock()
defer f.Unlock()
f.removeFilterByID(ws.GetFilterID())
}
// refreshFilterTimeout updates the timeout for a filter to the current time
func (f *FilterManager) refreshFilterTimeout(filter *filterBase) {
f.timeouts.removeFilter(filter)
f.addFilterTimeout(filter)
}
// addFilterTimeout set timeout and add to heap
func (f *FilterManager) addFilterTimeout(filter *filterBase) {
filter.expiresAt = time.Now().Add(f.timeout)
f.timeouts.addFilter(filter)
f.emitSignalToUpdateCh()
}
// addFilter is an internal method to add given filter to list and heap
func (f *FilterManager) addFilter(filter filter) string {
f.Lock()
defer f.Unlock()
base := filter.getFilterBase()
f.filters[base.id] = filter
// Set timeout and add to heap if filter doesn't have web socket connection
if !filter.hasWSConn() {
f.addFilterTimeout(base)
}
return base.id
}
func (f *FilterManager) emitSignalToUpdateCh() {
select {
// notify worker of new filter with timeout
case f.updateCh <- struct{}{}:
default:
}
}
// nextTimeoutFilter returns the filter that will be expired next
// nextTimeoutFilter returns the only filter with timeout
func (f *FilterManager) nextTimeoutFilter() *filterBase {
f.RLock()
defer f.RUnlock()
if len(f.timeouts) == 0 {
return nil
}
// peek the first item
base := f.timeouts[0]
return base
}
// dispatchEvent is an event handler for new block event
func (f *FilterManager) dispatchEvent(evnt *blockchain.Event) error {
// store new event in each filters
f.processEvent(evnt)
// send data to web socket stream
if err := f.flushWsFilters(); err != nil {
return err
}
return nil
}
// processEvent makes each filter append the new data that interests them
func (f *FilterManager) processEvent(evnt *blockchain.Event) {
f.RLock()
defer f.RUnlock()
for _, header := range evnt.NewChain {
// first include all the new headers in the blockstream for BlockFilter
f.blockStream.push(header)
// process new chain to include new logs for LogFilter
if processErr := f.appendLogsToFilters(header); processErr != nil {
f.logger.Error(fmt.Sprintf("Unable to process block, %v", processErr))
}
}
}
// appendLogsToFilters makes each LogFilters append logs in the header
func (f *FilterManager) appendLogsToFilters(header *types.Header) error {
receipts, err := f.store.GetReceiptsByHash(header.Hash)
if err != nil {
return err
}
// Get logFilters from filters
logFilters := f.getLogFilters()
if len(logFilters) == 0 {
return nil
}
block, ok := f.store.GetBlockByHash(header.Hash, true)
if !ok {
f.logger.Error("could not find block in store", "hash", header.Hash.String())
return nil
}
for indx, receipt := range receipts {
if receipt.TxHash == types.ZeroHash {
// Extract tx Hash
receipt.TxHash = block.Transactions[indx].Hash
}
// check the logs with the filters
for _, log := range receipt.Logs {
for _, f := range logFilters {
if f.query.Match(log) {
f.appendLog(&Log{
Address: log.Address,
Topics: log.Topics,
Data: argBytes(log.Data),
BlockNumber: argUint64(header.Number),
BlockHash: header.Hash,
TxHash: receipt.TxHash,
TxIndex: argUint64(indx),
Removed: false,
})
}
}
}
}
return nil
}
// flushWsFilters make each filters with web socket connection write the updates to web socket stream
// flushWsFilters also removes the filters if flushWsFilters notices the connection is closed
func (f *FilterManager) flushWsFilters() error {
closedFilterIDs := make([]string, 0)
f.RLock()
for id, filter := range f.filters {
if !filter.hasWSConn() {
continue
}
if flushErr := filter.sendUpdates(); flushErr != nil {
// mark as closed if the connection is closed
if errors.Is(flushErr, websocket.ErrCloseSent) || errors.Is(flushErr, net.ErrClosed) {
closedFilterIDs = append(closedFilterIDs, id)
f.logger.Warn(fmt.Sprintf("Subscription %s has been closed", id))
continue
}
f.logger.Error(fmt.Sprintf("Unable to process flush, %v", flushErr))
}
}
f.RUnlock()
// remove filters with closed web socket connections from FilterManager
if len(closedFilterIDs) > 0 {
f.Lock()
for _, id := range closedFilterIDs {
f.removeFilterByID(id)
}
f.Unlock()
f.logger.Info(fmt.Sprintf("Removed %d filters due to closed connections", len(closedFilterIDs)))
}
return nil
}
// getLogFilters returns logFilters
func (f *FilterManager) getLogFilters() []*logFilter {
f.RLock()
defer f.RUnlock()
logFilters := make([]*logFilter, 0)
for _, f := range f.filters {
if logFilter, ok := f.(*logFilter); ok {
logFilters = append(logFilters, logFilter)
}
}
return logFilters
}
type timeHeapImpl []*filterBase
func (t *timeHeapImpl) addFilter(filter *filterBase) {
heap.Push(t, filter)
}
func (t *timeHeapImpl) removeFilter(filter *filterBase) bool {
if filter.heapIndex == NoIndexInHeap {
return false
}
heap.Remove(t, filter.heapIndex)
return true
}
func (t timeHeapImpl) Len() int { return len(t) }
func (t timeHeapImpl) Less(i, j int) bool {
return t[i].expiresAt.Before(t[j].expiresAt)
}
func (t timeHeapImpl) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
t[i].heapIndex = i
t[j].heapIndex = j
}
func (t *timeHeapImpl) Push(x interface{}) {
n := len(*t)
item := x.(*filterBase) //nolint: forcetypeassert
item.heapIndex = n
*t = append(*t, item)
}
func (t *timeHeapImpl) Pop() interface{} {
old := *t
n := len(old)
item := old[n-1]
old[n-1] = nil
item.heapIndex = -1
*t = old[0 : n-1]
return item
}
// blockStream is used to keep the stream of new block hashes and allow subscriptions
// of the stream at any point
type blockStream struct {
lock sync.Mutex
head *headElem
}
func (b *blockStream) Head() *headElem {
b.lock.Lock()
defer b.lock.Unlock()
return b.head
}
func (b *blockStream) push(header *types.Header) {
b.lock.Lock()
defer b.lock.Unlock()
newHead := &headElem{
header: header.Copy(),
}
if b.head != nil {
b.head.next = newHead
}
b.head = newHead
}
type headElem struct {
header *types.Header
next *headElem
}
func (h *headElem) getUpdates() ([]*types.Header, *headElem) {
res := make([]*types.Header, 0)
cur := h
for cur.next != nil {
cur = cur.next
res = append(res, cur.header)
}
return res, cur
}