@@ -91,8 +91,21 @@ type EventSystem struct {
91
91
backend Backend
92
92
lightMode bool
93
93
lastHead * types.Header
94
- install chan * subscription // install filter for event notification
95
- uninstall chan * subscription // remove filter for event notification
94
+
95
+ // Subscriptions
96
+ txSub event.Subscription // Subscription for new transaction event
97
+ logsSub event.Subscription // Subscription for new log event
98
+ rmLogsSub event.Subscription // Subscription for removed log event
99
+ chainSub event.Subscription // Subscription for new chain event
100
+ pendingLogSub * event.TypeMuxSubscription // Subscription for pending log event
101
+
102
+ // Channels
103
+ install chan * subscription // install filter for event notification
104
+ uninstall chan * subscription // remove filter for event notification
105
+ txCh chan core.TxPreEvent // Channel to receive new transaction event
106
+ logsCh chan []* types.Log // Channel to receive new log event
107
+ rmLogsCh chan core.RemovedLogsEvent // Channel to receive removed log event
108
+ chainCh chan core.ChainEvent // Channel to receive new chain event
96
109
}
97
110
98
111
// NewEventSystem creates a new manager that listens for event on the given mux,
@@ -108,10 +121,36 @@ func NewEventSystem(mux *event.TypeMux, backend Backend, lightMode bool) *EventS
108
121
lightMode : lightMode ,
109
122
install : make (chan * subscription ),
110
123
uninstall : make (chan * subscription ),
124
+ txCh : make (chan core.TxPreEvent , txChanSize ),
125
+ logsCh : make (chan []* types.Log , logsChanSize ),
126
+ rmLogsCh : make (chan core.RemovedLogsEvent , rmLogsChanSize ),
127
+ chainCh : make (chan core.ChainEvent , chainEvChanSize ),
111
128
}
112
129
113
- go m .eventLoop ()
130
+ // Subscribe events
131
+ m .txSub = m .backend .SubscribeTxPreEvent (m .txCh )
132
+ if m .txSub == nil {
133
+ return nil
134
+ }
135
+ m .logsSub = m .backend .SubscribeLogsEvent (m .logsCh )
136
+ if m .logsSub == nil {
137
+ return nil
138
+ }
139
+ m .rmLogsSub = m .backend .SubscribeRemovedLogsEvent (m .rmLogsCh )
140
+ if m .rmLogsSub == nil {
141
+ return nil
142
+ }
143
+ m .chainSub = m .backend .SubscribeChainEvent (m .chainCh )
144
+ if m .chainSub == nil {
145
+ return nil
146
+ }
147
+ // TODO(rjl493456442): use feed to subscribe pending log event
148
+ m .pendingLogSub = m .mux .Subscribe (core.PendingLogsEvent {})
149
+ if m .pendingLogSub .Closed () {
150
+ return nil
151
+ }
114
152
153
+ go m .eventLoop ()
115
154
return m
116
155
}
117
156
@@ -411,40 +450,15 @@ func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.
411
450
412
451
// eventLoop (un)installs filters and processes mux events.
413
452
func (es * EventSystem ) eventLoop () {
414
- var (
415
- index = make (filterIndex )
416
- sub = es .mux .Subscribe (core.PendingLogsEvent {})
417
- // Subscribe TxPreEvent form txpool
418
- txCh = make (chan core.TxPreEvent , txChanSize )
419
- txSub = es .backend .SubscribeTxPreEvent (txCh )
420
- // Subscribe RemovedLogsEvent
421
- rmLogsCh = make (chan core.RemovedLogsEvent , rmLogsChanSize )
422
- rmLogsSub = es .backend .SubscribeRemovedLogsEvent (rmLogsCh )
423
- // Subscribe []*types.Log
424
- logsCh = make (chan []* types.Log , logsChanSize )
425
- logsSub = es .backend .SubscribeLogsEvent (logsCh )
426
- // Subscribe ChainEvent
427
- chainEvCh = make (chan core.ChainEvent , chainEvChanSize )
428
- chainEvSub = es .backend .SubscribeChainEvent (chainEvCh )
429
- )
453
+ var index = make (filterIndex )
430
454
431
455
defer func () {
432
456
// Unsubscribe all events
433
- if sub != nil {
434
- sub .Unsubscribe ()
435
- }
436
- if txSub != nil {
437
- txSub .Unsubscribe ()
438
- }
439
- if rmLogsSub != nil {
440
- rmLogsSub .Unsubscribe ()
441
- }
442
- if logsSub != nil {
443
- logsSub .Unsubscribe ()
444
- }
445
- if chainEvSub != nil {
446
- chainEvSub .Unsubscribe ()
447
- }
457
+ es .pendingLogSub .Unsubscribe ()
458
+ es .txSub .Unsubscribe ()
459
+ es .logsSub .Unsubscribe ()
460
+ es .rmLogsSub .Unsubscribe ()
461
+ es .chainSub .Unsubscribe ()
448
462
}()
449
463
450
464
for i := UnknownSubscription ; i < LastIndexSubscription ; i ++ {
@@ -453,20 +467,19 @@ func (es *EventSystem) eventLoop() {
453
467
454
468
for {
455
469
select {
456
- case ev , active := <- sub .Chan ():
457
- if ! active { // system stopped
458
- return
459
- }
460
- es .broadcast (index , ev )
461
-
462
470
// Handle subscribed events
463
- case ev := <- txCh :
471
+ case ev := <- es . txCh :
464
472
es .broadcast (index , ev )
465
- case ev := <- rmLogsCh :
473
+ case ev := <- es . logsCh :
466
474
es .broadcast (index , ev )
467
- case ev := <- logsCh :
475
+ case ev := <- es . rmLogsCh :
468
476
es .broadcast (index , ev )
469
- case ev := <- chainEvCh :
477
+ case ev := <- es .chainCh :
478
+ es .broadcast (index , ev )
479
+ case ev , active := <- es .pendingLogSub .Chan ():
480
+ if ! active { // system stopped
481
+ return
482
+ }
470
483
es .broadcast (index , ev )
471
484
472
485
case f := <- es .install :
@@ -478,6 +491,7 @@ func (es *EventSystem) eventLoop() {
478
491
index [f.typ ][f.id ] = f
479
492
}
480
493
close (f .installed )
494
+
481
495
case f := <- es .uninstall :
482
496
if f .typ == MinedAndPendingLogsSubscription {
483
497
// the type are logs and pending logs subscriptions
@@ -489,13 +503,13 @@ func (es *EventSystem) eventLoop() {
489
503
close (f .err )
490
504
491
505
// System stopped
492
- case <- txSub .Err ():
506
+ case <- es . txSub .Err ():
493
507
return
494
- case <- rmLogsSub .Err ():
508
+ case <- es . logsSub .Err ():
495
509
return
496
- case <- logsSub .Err ():
510
+ case <- es . rmLogsSub .Err ():
497
511
return
498
- case <- chainEvSub .Err ():
512
+ case <- es . chainSub .Err ():
499
513
return
500
514
}
501
515
}
0 commit comments