@@ -30,8 +30,6 @@ import (
30
30
"github.com/ethereum/go-ethereum/common/lru"
31
31
"github.com/ethereum/go-ethereum/core"
32
32
"github.com/ethereum/go-ethereum/core/bloombits"
33
- "github.com/ethereum/go-ethereum/core/rawdb"
34
- "github.com/ethereum/go-ethereum/core/state"
35
33
"github.com/ethereum/go-ethereum/core/types"
36
34
"github.com/ethereum/go-ethereum/ethdb"
37
35
"github.com/ethereum/go-ethereum/event"
@@ -63,7 +61,6 @@ type Backend interface {
63
61
GetBody (ctx context.Context , hash common.Hash , number rpc.BlockNumber ) (* types.Body , error )
64
62
GetReceipts (ctx context.Context , blockHash common.Hash ) (types.Receipts , error )
65
63
GetLogs (ctx context.Context , blockHash common.Hash , number uint64 ) ([][]* types.Log , error )
66
- Pending () (* types.Block , types.Receipts , * state.StateDB )
67
64
68
65
CurrentHeader () * types.Header
69
66
ChainConfig () * params.ChainConfig
@@ -152,10 +149,6 @@ const (
152
149
UnknownSubscription Type = iota
153
150
// LogsSubscription queries for new or removed (chain reorg) logs
154
151
LogsSubscription
155
- // PendingLogsSubscription queries for logs in pending blocks
156
- PendingLogsSubscription
157
- // MinedAndPendingLogsSubscription queries for logs in mined and pending blocks.
158
- MinedAndPendingLogsSubscription
159
152
// PendingTransactionsSubscription queries for pending transactions entering
160
153
// the pending state
161
154
PendingTransactionsSubscription
@@ -192,10 +185,8 @@ type subscription struct {
192
185
// EventSystem creates subscriptions, processes events and broadcasts them to the
193
186
// subscription which match the subscription criteria.
194
187
type EventSystem struct {
195
- backend Backend
196
- sys * FilterSystem
197
- lightMode bool
198
- lastHead * types.Header
188
+ backend Backend
189
+ sys * FilterSystem
199
190
200
191
// Subscriptions
201
192
txsSub event.Subscription // Subscription for new transaction event
@@ -218,11 +209,10 @@ type EventSystem struct {
218
209
//
219
210
// The returned manager has a loop that needs to be stopped with the Stop function
220
211
// or by stopping the given mux.
221
- func NewEventSystem (sys * FilterSystem , lightMode bool ) * EventSystem {
212
+ func NewEventSystem (sys * FilterSystem ) * EventSystem {
222
213
m := & EventSystem {
223
214
sys : sys ,
224
215
backend : sys .backend ,
225
- lightMode : lightMode ,
226
216
install : make (chan * subscription ),
227
217
uninstall : make (chan * subscription ),
228
218
txsCh : make (chan core.NewTxsEvent , txChanSize ),
@@ -310,10 +300,11 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
310
300
to = rpc .BlockNumber (crit .ToBlock .Int64 ())
311
301
}
312
302
313
- // only interested in pending logs
314
- if from == rpc .PendingBlockNumber && to == rpc .PendingBlockNumber {
315
- return es . subscribePendingLogs ( crit , logs ), nil
303
+ // Pending logs are not supported anymore.
304
+ if from == rpc .PendingBlockNumber || to == rpc .PendingBlockNumber {
305
+ return nil , errPendingLogsUnsupported
316
306
}
307
+
317
308
// only interested in new mined logs
318
309
if from == rpc .LatestBlockNumber && to == rpc .LatestBlockNumber {
319
310
return es .subscribeLogs (crit , logs ), nil
@@ -322,34 +313,13 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
322
313
if from >= 0 && to >= 0 && to >= from {
323
314
return es .subscribeLogs (crit , logs ), nil
324
315
}
325
- // interested in mined logs from a specific block number, new logs and pending logs
326
- if from >= rpc .LatestBlockNumber && to == rpc .PendingBlockNumber {
327
- return es .subscribeMinedPendingLogs (crit , logs ), nil
328
- }
329
316
// interested in logs from a specific block number to new mined blocks
330
317
if from >= 0 && to == rpc .LatestBlockNumber {
331
318
return es .subscribeLogs (crit , logs ), nil
332
319
}
333
320
return nil , errInvalidBlockRange
334
321
}
335
322
336
- // subscribeMinedPendingLogs creates a subscription that returned mined and
337
- // pending logs that match the given criteria.
338
- func (es * EventSystem ) subscribeMinedPendingLogs (crit ethereum.FilterQuery , logs chan []* types.Log ) * Subscription {
339
- sub := & subscription {
340
- id : rpc .NewID (),
341
- typ : MinedAndPendingLogsSubscription ,
342
- logsCrit : crit ,
343
- created : time .Now (),
344
- logs : logs ,
345
- txs : make (chan []* types.Transaction ),
346
- headers : make (chan * types.Header ),
347
- installed : make (chan struct {}),
348
- err : make (chan error ),
349
- }
350
- return es .subscribe (sub )
351
- }
352
-
353
323
// subscribeLogs creates a subscription that will write all logs matching the
354
324
// given criteria to the given logs channel.
355
325
func (es * EventSystem ) subscribeLogs (crit ethereum.FilterQuery , logs chan []* types.Log ) * Subscription {
@@ -367,23 +337,6 @@ func (es *EventSystem) subscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
367
337
return es .subscribe (sub )
368
338
}
369
339
370
- // subscribePendingLogs creates a subscription that writes contract event logs for
371
- // transactions that enter the transaction pool.
372
- func (es * EventSystem ) subscribePendingLogs (crit ethereum.FilterQuery , logs chan []* types.Log ) * Subscription {
373
- sub := & subscription {
374
- id : rpc .NewID (),
375
- typ : PendingLogsSubscription ,
376
- logsCrit : crit ,
377
- created : time .Now (),
378
- logs : logs ,
379
- txs : make (chan []* types.Transaction ),
380
- headers : make (chan * types.Header ),
381
- installed : make (chan struct {}),
382
- err : make (chan error ),
383
- }
384
- return es .subscribe (sub )
385
- }
386
-
387
340
// SubscribeNewHeads creates a subscription that writes the header of a block that is
388
341
// imported in the chain.
389
342
func (es * EventSystem ) SubscribeNewHeads (headers chan * types.Header ) * Subscription {
@@ -430,18 +383,6 @@ func (es *EventSystem) handleLogs(filters filterIndex, ev []*types.Log) {
430
383
}
431
384
}
432
385
433
- func (es * EventSystem ) handlePendingLogs (filters filterIndex , logs []* types.Log ) {
434
- if len (logs ) == 0 {
435
- return
436
- }
437
- for _ , f := range filters [PendingLogsSubscription ] {
438
- matchedLogs := filterLogs (logs , nil , f .logsCrit .ToBlock , f .logsCrit .Addresses , f .logsCrit .Topics )
439
- if len (matchedLogs ) > 0 {
440
- f .logs <- matchedLogs
441
- }
442
- }
443
- }
444
-
445
386
func (es * EventSystem ) handleTxsEvent (filters filterIndex , ev core.NewTxsEvent ) {
446
387
for _ , f := range filters [PendingTransactionsSubscription ] {
447
388
f .txs <- ev .Txs
@@ -452,91 +393,6 @@ func (es *EventSystem) handleChainEvent(filters filterIndex, ev core.ChainEvent)
452
393
for _ , f := range filters [BlocksSubscription ] {
453
394
f .headers <- ev .Block .Header ()
454
395
}
455
- if es .lightMode && len (filters [LogsSubscription ]) > 0 {
456
- es .lightFilterNewHead (ev .Block .Header (), func (header * types.Header , remove bool ) {
457
- for _ , f := range filters [LogsSubscription ] {
458
- if f .logsCrit .FromBlock != nil && header .Number .Cmp (f .logsCrit .FromBlock ) < 0 {
459
- continue
460
- }
461
- if f .logsCrit .ToBlock != nil && header .Number .Cmp (f .logsCrit .ToBlock ) > 0 {
462
- continue
463
- }
464
- if matchedLogs := es .lightFilterLogs (header , f .logsCrit .Addresses , f .logsCrit .Topics , remove ); len (matchedLogs ) > 0 {
465
- f .logs <- matchedLogs
466
- }
467
- }
468
- })
469
- }
470
- }
471
-
472
- func (es * EventSystem ) lightFilterNewHead (newHeader * types.Header , callBack func (* types.Header , bool )) {
473
- oldh := es .lastHead
474
- es .lastHead = newHeader
475
- if oldh == nil {
476
- return
477
- }
478
- newh := newHeader
479
- // find common ancestor, create list of rolled back and new block hashes
480
- var oldHeaders , newHeaders []* types.Header
481
- for oldh .Hash () != newh .Hash () {
482
- if oldh .Number .Uint64 () >= newh .Number .Uint64 () {
483
- oldHeaders = append (oldHeaders , oldh )
484
- oldh = rawdb .ReadHeader (es .backend .ChainDb (), oldh .ParentHash , oldh .Number .Uint64 ()- 1 )
485
- }
486
- if oldh .Number .Uint64 () < newh .Number .Uint64 () {
487
- newHeaders = append (newHeaders , newh )
488
- newh = rawdb .ReadHeader (es .backend .ChainDb (), newh .ParentHash , newh .Number .Uint64 ()- 1 )
489
- if newh == nil {
490
- // happens when CHT syncing, nothing to do
491
- newh = oldh
492
- }
493
- }
494
- }
495
- // roll back old blocks
496
- for _ , h := range oldHeaders {
497
- callBack (h , true )
498
- }
499
- // check new blocks (array is in reverse order)
500
- for i := len (newHeaders ) - 1 ; i >= 0 ; i -- {
501
- callBack (newHeaders [i ], false )
502
- }
503
- }
504
-
505
- // filter logs of a single header in light client mode
506
- func (es * EventSystem ) lightFilterLogs (header * types.Header , addresses []common.Address , topics [][]common.Hash , remove bool ) []* types.Log {
507
- if ! bloomFilter (header .Bloom , addresses , topics ) {
508
- return nil
509
- }
510
- // Get the logs of the block
511
- ctx , cancel := context .WithTimeout (context .Background (), time .Second * 5 )
512
- defer cancel ()
513
- cached , err := es .sys .cachedLogElem (ctx , header .Hash (), header .Number .Uint64 ())
514
- if err != nil {
515
- return nil
516
- }
517
- unfiltered := append ([]* types.Log {}, cached .logs ... )
518
- for i , log := range unfiltered {
519
- // Don't modify in-cache elements
520
- logcopy := * log
521
- logcopy .Removed = remove
522
- // Swap copy in-place
523
- unfiltered [i ] = & logcopy
524
- }
525
- logs := filterLogs (unfiltered , nil , nil , addresses , topics )
526
- // Txhash is already resolved
527
- if len (logs ) > 0 && logs [0 ].TxHash != (common.Hash {}) {
528
- return logs
529
- }
530
- // Resolve txhash
531
- body , err := es .sys .cachedGetBody (ctx , cached , header .Hash (), header .Number .Uint64 ())
532
- if err != nil {
533
- return nil
534
- }
535
- for _ , log := range logs {
536
- // logs are already copied, safe to modify
537
- log .TxHash = body .Transactions [log .TxIndex ].Hash ()
538
- }
539
- return logs
540
396
}
541
397
542
398
// eventLoop (un)installs filters and processes mux events.
@@ -564,46 +420,13 @@ func (es *EventSystem) eventLoop() {
564
420
es .handleLogs (index , ev .Logs )
565
421
case ev := <- es .chainCh :
566
422
es .handleChainEvent (index , ev )
567
- // If we have no pending log subscription,
568
- // we don't need to collect any pending logs.
569
- if len (index [PendingLogsSubscription ]) == 0 {
570
- continue
571
- }
572
-
573
- // Pull the pending logs if there is a new chain head.
574
- pendingBlock , pendingReceipts , _ := es .backend .Pending ()
575
- if pendingBlock == nil || pendingReceipts == nil {
576
- continue
577
- }
578
- if pendingBlock .ParentHash () != ev .Block .Hash () {
579
- continue
580
- }
581
- var logs []* types.Log
582
- for _ , receipt := range pendingReceipts {
583
- if len (receipt .Logs ) > 0 {
584
- logs = append (logs , receipt .Logs ... )
585
- }
586
- }
587
- es .handlePendingLogs (index , logs )
588
423
589
424
case f := <- es .install :
590
- if f .typ == MinedAndPendingLogsSubscription {
591
- // the type are logs and pending logs subscriptions
592
- index [LogsSubscription ][f.id ] = f
593
- index [PendingLogsSubscription ][f.id ] = f
594
- } else {
595
- index [f.typ ][f.id ] = f
596
- }
425
+ index [f.typ ][f.id ] = f
597
426
close (f .installed )
598
427
599
428
case f := <- es .uninstall :
600
- if f .typ == MinedAndPendingLogsSubscription {
601
- // the type are logs and pending logs subscriptions
602
- delete (index [LogsSubscription ], f .id )
603
- delete (index [PendingLogsSubscription ], f .id )
604
- } else {
605
- delete (index [f .typ ], f .id )
606
- }
429
+ delete (index [f .typ ], f .id )
607
430
close (f .err )
608
431
609
432
// System stopped
0 commit comments