@@ -31,6 +31,7 @@ import (
3131 "github.com/ethereum/go-ethereum/core/vm"
3232 "github.com/ethereum/go-ethereum/event"
3333 "github.com/ethereum/go-ethereum/rpc"
34+ "golang.org/x/net/context"
3435)
3536
3637// Type determines the kind of filter and is used to put the filter in to
@@ -95,6 +96,9 @@ type subscription struct {
9596type EventSystem struct {
9697 mux * event.TypeMux
9798 sub event.Subscription
99+ backend Backend
100+ lightMode bool
101+ lastHead * types.Header
98102 install chan * subscription // install filter for event notification
99103 uninstall chan * subscription // remove filter for event notification
100104}
@@ -105,9 +109,11 @@ type EventSystem struct {
105109//
106110// The returned manager has a loop that needs to be stopped with the Stop function
107111// or by stopping the given mux.
108- func NewEventSystem (mux * event.TypeMux ) * EventSystem {
112+ func NewEventSystem (mux * event.TypeMux , backend Backend , lightMode bool ) * EventSystem {
109113 m := & EventSystem {
110114 mux : mux ,
115+ backend : backend ,
116+ lightMode : lightMode ,
111117 install : make (chan * subscription ),
112118 uninstall : make (chan * subscription ),
113119 }
@@ -235,7 +241,7 @@ func (es *EventSystem) SubscribeNewHeads(headers chan *types.Header) *Subscripti
235241type filterIndex map [Type ]map [rpc.ID ]* subscription
236242
237243// broadcast event to filters that match criteria.
238- func broadcast (filters filterIndex , ev * event.Event ) {
244+ func ( es * EventSystem ) broadcast (filters filterIndex , ev * event.Event ) {
239245 if ev == nil {
240246 return
241247 }
@@ -279,7 +285,77 @@ func broadcast(filters filterIndex, ev *event.Event) {
279285 f .headers <- e .Block .Header ()
280286 }
281287 }
288+ if es .lightMode && len (filters [LogsSubscription ]) > 0 {
289+ es .lightFilterNewHead (e .Block .Header (), func (header * types.Header , remove bool ) {
290+ for _ , f := range filters [LogsSubscription ] {
291+ if ev .Time .After (f .created ) {
292+ if matchedLogs := es .lightFilterLogs (header , f .logsCrit .Addresses , f .logsCrit .Topics , remove ); len (matchedLogs ) > 0 {
293+ f .logs <- matchedLogs
294+ }
295+ }
296+ }
297+ })
298+ }
299+ }
300+ }
301+
302+ func (es * EventSystem ) lightFilterNewHead (newHeader * types.Header , callBack func (* types.Header , bool )) {
303+ oldh := es .lastHead
304+ es .lastHead = newHeader
305+ if oldh == nil {
306+ return
307+ }
308+ newh := newHeader
309+ // find common ancestor, create list of rolled back and new block hashes
310+ var oldHeaders , newHeaders []* types.Header
311+ for oldh .Hash () != newh .Hash () {
312+ if oldh .GetNumberU64 () >= newh .GetNumberU64 () {
313+ oldHeaders = append (oldHeaders , oldh )
314+ oldh = core .GetHeader (es .backend .ChainDb (), oldh .ParentHash , oldh .Number .Uint64 ()- 1 )
315+ }
316+ if oldh .GetNumberU64 () < newh .GetNumberU64 () {
317+ newHeaders = append (newHeaders , newh )
318+ newh = core .GetHeader (es .backend .ChainDb (), newh .ParentHash , newh .Number .Uint64 ()- 1 )
319+ if newh == nil {
320+ // happens when CHT syncing, nothing to do
321+ newh = oldh
322+ }
323+ }
324+ }
325+ // roll back old blocks
326+ for _ , h := range oldHeaders {
327+ callBack (h , true )
328+ }
329+ // check new blocks (array is in reverse order)
330+ for i := len (newHeaders ) - 1 ; i >= 0 ; i -- {
331+ callBack (newHeaders [i ], false )
332+ }
333+ }
334+
335+ // filter logs of a single header in light client mode
336+ func (es * EventSystem ) lightFilterLogs (header * types.Header , addresses []common.Address , topics [][]common.Hash , remove bool ) []Log {
337+ //fmt.Println("lightFilterLogs", header.Number.Uint64(), remove)
338+ if bloomFilter (header .Bloom , addresses , topics ) {
339+ //fmt.Println("bloom match")
340+ // Get the logs of the block
341+ ctx , _ := context .WithTimeout (context .Background (), time .Second * 5 )
342+ receipts , err := es .backend .GetReceipts (ctx , header .Hash ())
343+ if err != nil {
344+ return nil
345+ }
346+ var unfiltered []Log
347+ for _ , receipt := range receipts {
348+ rl := make ([]Log , len (receipt .Logs ))
349+ for i , l := range receipt .Logs {
350+ rl [i ] = Log {l , remove }
351+ }
352+ unfiltered = append (unfiltered , rl ... )
353+ }
354+ logs := filterLogs (unfiltered , addresses , topics )
355+ //fmt.Println("found", len(logs))
356+ return logs
282357 }
358+ return nil
283359}
284360
285361// eventLoop (un)installs filters and processes mux events.
@@ -294,7 +370,7 @@ func (es *EventSystem) eventLoop() {
294370 if ! active { // system stopped
295371 return
296372 }
297- broadcast (index , ev )
373+ es . broadcast (index , ev )
298374 case f := <- es .install :
299375 if _ , found := index [f .typ ]; ! found {
300376 index [f .typ ] = make (map [rpc.ID ]* subscription )
0 commit comments