github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/eth/filters/filter_system.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package filters implements an ethereum filtering system for block,
    18  // transactions and log events.
    19  package filters
    20  
    21  import (
    22  	"context"
    23  	"errors"
    24  	"fmt"
    25  	"sync"
    26  	"time"
    27  
    28  	ethereum "github.com/ethereum/go-ethereum"
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/core"
    31  	"github.com/ethereum/go-ethereum/core/rawdb"
    32  	"github.com/ethereum/go-ethereum/core/types"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/log"
    35  	"github.com/ethereum/go-ethereum/rpc"
    36  )
    37  
    38  // Type determines the kind of filter and is used to put the filter in to
    39  // the correct bucket when added.
    40  type Type byte
    41  
    42  const (
    43  	// UnknownSubscription indicates an unknown subscription type
    44  	UnknownSubscription Type = iota
    45  	// LogsSubscription queries for new or removed (chain reorg) logs
    46  	LogsSubscription
    47  	// PendingLogsSubscription queries for logs in pending blocks
    48  	PendingLogsSubscription
    49  	// MinedAndPendingLogsSubscription queries for logs in mined and pending blocks.
    50  	MinedAndPendingLogsSubscription
    51  	// PendingTransactionsSubscription queries tx hashes for pending
    52  	// transactions entering the pending state
    53  	PendingTransactionsSubscription
    54  	// BlocksSubscription queries hashes for blocks that are imported
    55  	BlocksSubscription
    56  	// LastSubscription keeps track of the last index
    57  	LastIndexSubscription
    58  )
    59  
    60  const (
    61  
    62  	// txChanSize is the size of channel listening to NewTxsEvent.
    63  	// The number is referenced from the size of tx pool.
    64  	txChanSize = 4096
    65  	// rmLogsChanSize is the size of channel listening to RemovedLogsEvent.
    66  	rmLogsChanSize = 10
    67  	// logsChanSize is the size of channel listening to LogsEvent.
    68  	logsChanSize = 10
    69  	// chainEvChanSize is the size of channel listening to ChainEvent.
    70  	chainEvChanSize = 10
    71  )
    72  
    73  var (
    74  	ErrInvalidSubscriptionID = errors.New("invalid id")
    75  )
    76  
    77  type subscription struct {
    78  	id        rpc.ID
    79  	typ       Type
    80  	created   time.Time
    81  	logsCrit  ethereum.FilterQuery
    82  	logs      chan []*types.Log
    83  	hashes    chan []common.Hash
    84  	headers   chan *types.Header
    85  	installed chan struct{} // closed when the filter is installed
    86  	err       chan error    // closed when the filter is uninstalled
    87  }
    88  
    89  // EventSystem creates subscriptions, processes events and broadcasts them to the
    90  // subscription which match the subscription criteria.
    91  type EventSystem struct {
    92  	mux       *event.TypeMux
    93  	backend   Backend
    94  	lightMode bool
    95  	lastHead  *types.Header
    96  
    97  	// Subscriptions
    98  	txsSub        event.Subscription         // Subscription for new transaction event
    99  	logsSub       event.Subscription         // Subscription for new log event
   100  	rmLogsSub     event.Subscription         // Subscription for removed log event
   101  	chainSub      event.Subscription         // Subscription for new chain event
   102  	pendingLogSub *event.TypeMuxSubscription // Subscription for pending log event
   103  
   104  	// Channels
   105  	install   chan *subscription         // install filter for event notification
   106  	uninstall chan *subscription         // remove filter for event notification
   107  	txsCh     chan core.NewTxsEvent      // Channel to receive new transactions event
   108  	logsCh    chan []*types.Log          // Channel to receive new log event
   109  	rmLogsCh  chan core.RemovedLogsEvent // Channel to receive removed log event
   110  	chainCh   chan core.ChainEvent       // Channel to receive new chain event
   111  }
   112  
   113  // NewEventSystem creates a new manager that listens for event on the given mux,
   114  // parses and filters them. It uses the all map to retrieve filter changes. The
   115  // work loop holds its own index that is used to forward events to filters.
   116  //
   117  // The returned manager has a loop that needs to be stopped with the Stop function
   118  // or by stopping the given mux.
   119  func NewEventSystem(mux *event.TypeMux, backend Backend, lightMode bool) *EventSystem {
   120  	m := &EventSystem{
   121  		mux:       mux,
   122  		backend:   backend,
   123  		lightMode: lightMode,
   124  		install:   make(chan *subscription),
   125  		uninstall: make(chan *subscription),
   126  		txsCh:     make(chan core.NewTxsEvent, txChanSize),
   127  		logsCh:    make(chan []*types.Log, logsChanSize),
   128  		rmLogsCh:  make(chan core.RemovedLogsEvent, rmLogsChanSize),
   129  		chainCh:   make(chan core.ChainEvent, chainEvChanSize),
   130  	}
   131  
   132  	// Subscribe events
   133  	m.txsSub = m.backend.SubscribeNewTxsEvent(m.txsCh)
   134  	m.logsSub = m.backend.SubscribeLogsEvent(m.logsCh)
   135  	m.rmLogsSub = m.backend.SubscribeRemovedLogsEvent(m.rmLogsCh)
   136  	m.chainSub = m.backend.SubscribeChainEvent(m.chainCh)
   137  	// TODO(rjl493456442): use feed to subscribe pending log event
   138  	m.pendingLogSub = m.mux.Subscribe(core.PendingLogsEvent{})
   139  
   140  	// Make sure none of the subscriptions are empty
   141  	if m.txsSub == nil || m.logsSub == nil || m.rmLogsSub == nil || m.chainSub == nil ||
   142  		m.pendingLogSub.Closed() {
   143  		log.Crit("Subscribe for event system failed")
   144  	}
   145  
   146  	go m.eventLoop()
   147  	return m
   148  }
   149  
   150  // Subscription is created when the client registers itself for a particular event.
   151  type Subscription struct {
   152  	ID        rpc.ID
   153  	f         *subscription
   154  	es        *EventSystem
   155  	unsubOnce sync.Once
   156  }
   157  
   158  // Err returns a channel that is closed when unsubscribed.
   159  func (sub *Subscription) Err() <-chan error {
   160  	return sub.f.err
   161  }
   162  
   163  // Unsubscribe uninstalls the subscription from the event broadcast loop.
   164  func (sub *Subscription) Unsubscribe() {
   165  	sub.unsubOnce.Do(func() {
   166  	uninstallLoop:
   167  		for {
   168  			// write uninstall request and consume logs/hashes. This prevents
   169  			// the eventLoop broadcast method to deadlock when writing to the
   170  			// filter event channel while the subscription loop is waiting for
   171  			// this method to return (and thus not reading these events).
   172  			select {
   173  			case sub.es.uninstall <- sub.f:
   174  				break uninstallLoop
   175  			case <-sub.f.logs:
   176  			case <-sub.f.hashes:
   177  			case <-sub.f.headers:
   178  			}
   179  		}
   180  
   181  		// wait for filter to be uninstalled in work loop before returning
   182  		// this ensures that the manager won't use the event channel which
   183  		// will probably be closed by the client asap after this method returns.
   184  		<-sub.Err()
   185  	})
   186  }
   187  
   188  // subscribe installs the subscription in the event broadcast loop.
   189  func (es *EventSystem) subscribe(sub *subscription) *Subscription {
   190  	es.install <- sub
   191  	<-sub.installed
   192  	return &Subscription{ID: sub.id, f: sub, es: es}
   193  }
   194  
   195  // SubscribeLogs creates a subscription that will write all logs matching the
   196  // given criteria to the given logs channel. Default value for the from and to
   197  // block is "latest". If the fromBlock > toBlock an error is returned.
   198  func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*types.Log) (*Subscription, error) {
   199  	var from, to rpc.BlockNumber
   200  	if crit.FromBlock == nil {
   201  		from = rpc.LatestBlockNumber
   202  	} else {
   203  		from = rpc.BlockNumber(crit.FromBlock.Int64())
   204  	}
   205  	if crit.ToBlock == nil {
   206  		to = rpc.LatestBlockNumber
   207  	} else {
   208  		to = rpc.BlockNumber(crit.ToBlock.Int64())
   209  	}
   210  
   211  	// only interested in pending logs
   212  	if from == rpc.PendingBlockNumber && to == rpc.PendingBlockNumber {
   213  		return es.subscribePendingLogs(crit, logs), nil
   214  	}
   215  	// only interested in new mined logs
   216  	if from == rpc.LatestBlockNumber && to == rpc.LatestBlockNumber {
   217  		return es.subscribeLogs(crit, logs), nil
   218  	}
   219  	// only interested in mined logs within a specific block range
   220  	if from >= 0 && to >= 0 && to >= from {
   221  		return es.subscribeLogs(crit, logs), nil
   222  	}
   223  	// interested in mined logs from a specific block number, new logs and pending logs
   224  	if from >= rpc.LatestBlockNumber && to == rpc.PendingBlockNumber {
   225  		return es.subscribeMinedPendingLogs(crit, logs), nil
   226  	}
   227  	// interested in logs from a specific block number to new mined blocks
   228  	if from >= 0 && to == rpc.LatestBlockNumber {
   229  		return es.subscribeLogs(crit, logs), nil
   230  	}
   231  	return nil, fmt.Errorf("invalid from and to block combination: from > to")
   232  }
   233  
   234  // subscribeMinedPendingLogs creates a subscription that returned mined and
   235  // pending logs that match the given criteria.
   236  func (es *EventSystem) subscribeMinedPendingLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
   237  	sub := &subscription{
   238  		id:        rpc.NewID(),
   239  		typ:       MinedAndPendingLogsSubscription,
   240  		logsCrit:  crit,
   241  		created:   time.Now(),
   242  		logs:      logs,
   243  		hashes:    make(chan []common.Hash),
   244  		headers:   make(chan *types.Header),
   245  		installed: make(chan struct{}),
   246  		err:       make(chan error),
   247  	}
   248  	return es.subscribe(sub)
   249  }
   250  
   251  // subscribeLogs creates a subscription that will write all logs matching the
   252  // given criteria to the given logs channel.
   253  func (es *EventSystem) subscribeLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
   254  	sub := &subscription{
   255  		id:        rpc.NewID(),
   256  		typ:       LogsSubscription,
   257  		logsCrit:  crit,
   258  		created:   time.Now(),
   259  		logs:      logs,
   260  		hashes:    make(chan []common.Hash),
   261  		headers:   make(chan *types.Header),
   262  		installed: make(chan struct{}),
   263  		err:       make(chan error),
   264  	}
   265  	return es.subscribe(sub)
   266  }
   267  
   268  // subscribePendingLogs creates a subscription that writes transaction hashes for
   269  // transactions that enter the transaction pool.
   270  func (es *EventSystem) subscribePendingLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
   271  	sub := &subscription{
   272  		id:        rpc.NewID(),
   273  		typ:       PendingLogsSubscription,
   274  		logsCrit:  crit,
   275  		created:   time.Now(),
   276  		logs:      logs,
   277  		hashes:    make(chan []common.Hash),
   278  		headers:   make(chan *types.Header),
   279  		installed: make(chan struct{}),
   280  		err:       make(chan error),
   281  	}
   282  	return es.subscribe(sub)
   283  }
   284  
   285  // SubscribeNewHeads creates a subscription that writes the header of a block that is
   286  // imported in the chain.
   287  func (es *EventSystem) SubscribeNewHeads(headers chan *types.Header) *Subscription {
   288  	sub := &subscription{
   289  		id:        rpc.NewID(),
   290  		typ:       BlocksSubscription,
   291  		created:   time.Now(),
   292  		logs:      make(chan []*types.Log),
   293  		hashes:    make(chan []common.Hash),
   294  		headers:   headers,
   295  		installed: make(chan struct{}),
   296  		err:       make(chan error),
   297  	}
   298  	return es.subscribe(sub)
   299  }
   300  
   301  // SubscribePendingTxs creates a subscription that writes transaction hashes for
   302  // transactions that enter the transaction pool.
   303  func (es *EventSystem) SubscribePendingTxs(hashes chan []common.Hash) *Subscription {
   304  	sub := &subscription{
   305  		id:        rpc.NewID(),
   306  		typ:       PendingTransactionsSubscription,
   307  		created:   time.Now(),
   308  		logs:      make(chan []*types.Log),
   309  		hashes:    hashes,
   310  		headers:   make(chan *types.Header),
   311  		installed: make(chan struct{}),
   312  		err:       make(chan error),
   313  	}
   314  	return es.subscribe(sub)
   315  }
   316  
   317  type filterIndex map[Type]map[rpc.ID]*subscription
   318  
   319  // broadcast event to filters that match criteria.
   320  func (es *EventSystem) broadcast(filters filterIndex, ev interface{}) {
   321  	if ev == nil {
   322  		return
   323  	}
   324  
   325  	switch e := ev.(type) {
   326  	case []*types.Log:
   327  		if len(e) > 0 {
   328  			for _, f := range filters[LogsSubscription] {
   329  				if matchedLogs := filterLogs(e, f.logsCrit.FromBlock, f.logsCrit.ToBlock, f.logsCrit.Addresses, f.logsCrit.Topics); len(matchedLogs) > 0 {
   330  					f.logs <- matchedLogs
   331  				}
   332  			}
   333  		}
   334  	case core.RemovedLogsEvent:
   335  		for _, f := range filters[LogsSubscription] {
   336  			if matchedLogs := filterLogs(e.Logs, f.logsCrit.FromBlock, f.logsCrit.ToBlock, f.logsCrit.Addresses, f.logsCrit.Topics); len(matchedLogs) > 0 {
   337  				f.logs <- matchedLogs
   338  			}
   339  		}
   340  	case *event.TypeMuxEvent:
   341  		switch muxe := e.Data.(type) {
   342  		case core.PendingLogsEvent:
   343  			for _, f := range filters[PendingLogsSubscription] {
   344  				if e.Time.After(f.created) {
   345  					if matchedLogs := filterLogs(muxe.Logs, nil, f.logsCrit.ToBlock, f.logsCrit.Addresses, f.logsCrit.Topics); len(matchedLogs) > 0 {
   346  						f.logs <- matchedLogs
   347  					}
   348  				}
   349  			}
   350  		}
   351  	case core.NewTxsEvent:
   352  		hashes := make([]common.Hash, 0, len(e.Txs))
   353  		for _, tx := range e.Txs {
   354  			hashes = append(hashes, tx.Hash())
   355  		}
   356  		for _, f := range filters[PendingTransactionsSubscription] {
   357  			f.hashes <- hashes
   358  		}
   359  	case core.ChainEvent:
   360  		for _, f := range filters[BlocksSubscription] {
   361  			f.headers <- e.Block.Header()
   362  		}
   363  		if es.lightMode && len(filters[LogsSubscription]) > 0 {
   364  			es.lightFilterNewHead(e.Block.Header(), func(header *types.Header, remove bool) {
   365  				for _, f := range filters[LogsSubscription] {
   366  					if matchedLogs := es.lightFilterLogs(header, f.logsCrit.Addresses, f.logsCrit.Topics, remove); len(matchedLogs) > 0 {
   367  						f.logs <- matchedLogs
   368  					}
   369  				}
   370  			})
   371  		}
   372  	}
   373  }
   374  
   375  func (es *EventSystem) lightFilterNewHead(newHeader *types.Header, callBack func(*types.Header, bool)) {
   376  	oldh := es.lastHead
   377  	es.lastHead = newHeader
   378  	if oldh == nil {
   379  		return
   380  	}
   381  	newh := newHeader
   382  	// find common ancestor, create list of rolled back and new block hashes
   383  	var oldHeaders, newHeaders []*types.Header
   384  	for oldh.Hash() != newh.Hash() {
   385  		if oldh.Number.Uint64() >= newh.Number.Uint64() {
   386  			oldHeaders = append(oldHeaders, oldh)
   387  			oldh = rawdb.ReadHeader(es.backend.ChainDb(), oldh.ParentHash, oldh.Number.Uint64()-1)
   388  		}
   389  		if oldh.Number.Uint64() < newh.Number.Uint64() {
   390  			newHeaders = append(newHeaders, newh)
   391  			newh = rawdb.ReadHeader(es.backend.ChainDb(), newh.ParentHash, newh.Number.Uint64()-1)
   392  			if newh == nil {
   393  				// happens when CHT syncing, nothing to do
   394  				newh = oldh
   395  			}
   396  		}
   397  	}
   398  	// roll back old blocks
   399  	for _, h := range oldHeaders {
   400  		callBack(h, true)
   401  	}
   402  	// check new blocks (array is in reverse order)
   403  	for i := len(newHeaders) - 1; i >= 0; i-- {
   404  		callBack(newHeaders[i], false)
   405  	}
   406  }
   407  
   408  // filter logs of a single header in light client mode
   409  func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.Address, topics [][]common.Hash, remove bool) []*types.Log {
   410  	if bloomFilter(header.Bloom, addresses, topics) {
   411  		// Get the logs of the block
   412  		ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
   413  		defer cancel()
   414  		logsList, err := es.backend.GetLogs(ctx, header.Hash())
   415  		if err != nil {
   416  			return nil
   417  		}
   418  		var unfiltered []*types.Log
   419  		for _, logs := range logsList {
   420  			for _, log := range logs {
   421  				logcopy := *log
   422  				logcopy.Removed = remove
   423  				unfiltered = append(unfiltered, &logcopy)
   424  			}
   425  		}
   426  		logs := filterLogs(unfiltered, nil, nil, addresses, topics)
   427  		if len(logs) > 0 && logs[0].TxHash == (common.Hash{}) {
   428  			// We have matching but non-derived logs
   429  			receipts, err := es.backend.GetReceipts(ctx, header.Hash())
   430  			if err != nil {
   431  				return nil
   432  			}
   433  			unfiltered = unfiltered[:0]
   434  			for _, receipt := range receipts {
   435  				for _, log := range receipt.Logs {
   436  					logcopy := *log
   437  					logcopy.Removed = remove
   438  					unfiltered = append(unfiltered, &logcopy)
   439  				}
   440  			}
   441  			logs = filterLogs(unfiltered, nil, nil, addresses, topics)
   442  		}
   443  		return logs
   444  	}
   445  	return nil
   446  }
   447  
   448  // eventLoop (un)installs filters and processes mux events.
   449  func (es *EventSystem) eventLoop() {
   450  	// Ensure all subscriptions get cleaned up
   451  	defer func() {
   452  		es.pendingLogSub.Unsubscribe()
   453  		es.txsSub.Unsubscribe()
   454  		es.logsSub.Unsubscribe()
   455  		es.rmLogsSub.Unsubscribe()
   456  		es.chainSub.Unsubscribe()
   457  	}()
   458  
   459  	index := make(filterIndex)
   460  	for i := UnknownSubscription; i < LastIndexSubscription; i++ {
   461  		index[i] = make(map[rpc.ID]*subscription)
   462  	}
   463  
   464  	for {
   465  		select {
   466  		// Handle subscribed events
   467  		case ev := <-es.txsCh:
   468  			es.broadcast(index, ev)
   469  		case ev := <-es.logsCh:
   470  			es.broadcast(index, ev)
   471  		case ev := <-es.rmLogsCh:
   472  			es.broadcast(index, ev)
   473  		case ev := <-es.chainCh:
   474  			es.broadcast(index, ev)
   475  		case ev, active := <-es.pendingLogSub.Chan():
   476  			if !active { // system stopped
   477  				return
   478  			}
   479  			es.broadcast(index, ev)
   480  
   481  		case f := <-es.install:
   482  			if f.typ == MinedAndPendingLogsSubscription {
   483  				// the type are logs and pending logs subscriptions
   484  				index[LogsSubscription][f.id] = f
   485  				index[PendingLogsSubscription][f.id] = f
   486  			} else {
   487  				index[f.typ][f.id] = f
   488  			}
   489  			close(f.installed)
   490  
   491  		case f := <-es.uninstall:
   492  			if f.typ == MinedAndPendingLogsSubscription {
   493  				// the type are logs and pending logs subscriptions
   494  				delete(index[LogsSubscription], f.id)
   495  				delete(index[PendingLogsSubscription], f.id)
   496  			} else {
   497  				delete(index[f.typ], f.id)
   498  			}
   499  			close(f.err)
   500  
   501  		// System stopped
   502  		case <-es.txsSub.Err():
   503  			return
   504  		case <-es.logsSub.Err():
   505  			return
   506  		case <-es.rmLogsSub.Err():
   507  			return
   508  		case <-es.chainSub.Err():
   509  			return
   510  		}
   511  	}
   512  }