github.com/lzl124631x/go-ethereum@v1.8.12-0.20180615081455-574378edb50c/eth/filters/api.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
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"errors"
    23  	"fmt"
    24  	"math/big"
    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/common/hexutil"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/ethdb"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/rpc"
    35  )
    36  
    37  var (
    38  	deadline = 5 * time.Minute // consider a filter inactive if it has not been polled for within deadline
    39  )
    40  
    41  // filter is a helper struct that holds meta information over the filter type
    42  // and associated subscription in the event system.
    43  type filter struct {
    44  	typ      Type
    45  	deadline *time.Timer // filter is inactiv when deadline triggers
    46  	hashes   []common.Hash
    47  	crit     FilterCriteria
    48  	logs     []*types.Log
    49  	s        *Subscription // associated subscription in event system
    50  }
    51  
    52  // PublicFilterAPI offers support to create and manage filters. This will allow external clients to retrieve various
    53  // information related to the Ethereum protocol such als blocks, transactions and logs.
    54  type PublicFilterAPI struct {
    55  	backend   Backend
    56  	mux       *event.TypeMux
    57  	quit      chan struct{}
    58  	chainDb   ethdb.Database
    59  	events    *EventSystem
    60  	filtersMu sync.Mutex
    61  	filters   map[rpc.ID]*filter
    62  }
    63  
    64  // NewPublicFilterAPI returns a new PublicFilterAPI instance.
    65  func NewPublicFilterAPI(backend Backend, lightMode bool) *PublicFilterAPI {
    66  	api := &PublicFilterAPI{
    67  		backend: backend,
    68  		mux:     backend.EventMux(),
    69  		chainDb: backend.ChainDb(),
    70  		events:  NewEventSystem(backend.EventMux(), backend, lightMode),
    71  		filters: make(map[rpc.ID]*filter),
    72  	}
    73  	go api.timeoutLoop()
    74  
    75  	return api
    76  }
    77  
    78  // timeoutLoop runs every 5 minutes and deletes filters that have not been recently used.
    79  // Tt is started when the api is created.
    80  func (api *PublicFilterAPI) timeoutLoop() {
    81  	ticker := time.NewTicker(5 * time.Minute)
    82  	for {
    83  		<-ticker.C
    84  		api.filtersMu.Lock()
    85  		for id, f := range api.filters {
    86  			select {
    87  			case <-f.deadline.C:
    88  				f.s.Unsubscribe()
    89  				delete(api.filters, id)
    90  			default:
    91  				continue
    92  			}
    93  		}
    94  		api.filtersMu.Unlock()
    95  	}
    96  }
    97  
    98  // NewPendingTransactionFilter creates a filter that fetches pending transaction hashes
    99  // as transactions enter the pending state.
   100  //
   101  // It is part of the filter package because this filter can be used through the
   102  // `eth_getFilterChanges` polling method that is also used for log filters.
   103  //
   104  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newpendingtransactionfilter
   105  func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
   106  	var (
   107  		pendingTxs   = make(chan []common.Hash)
   108  		pendingTxSub = api.events.SubscribePendingTxs(pendingTxs)
   109  	)
   110  
   111  	api.filtersMu.Lock()
   112  	api.filters[pendingTxSub.ID] = &filter{typ: PendingTransactionsSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: pendingTxSub}
   113  	api.filtersMu.Unlock()
   114  
   115  	go func() {
   116  		for {
   117  			select {
   118  			case ph := <-pendingTxs:
   119  				api.filtersMu.Lock()
   120  				if f, found := api.filters[pendingTxSub.ID]; found {
   121  					f.hashes = append(f.hashes, ph...)
   122  				}
   123  				api.filtersMu.Unlock()
   124  			case <-pendingTxSub.Err():
   125  				api.filtersMu.Lock()
   126  				delete(api.filters, pendingTxSub.ID)
   127  				api.filtersMu.Unlock()
   128  				return
   129  			}
   130  		}
   131  	}()
   132  
   133  	return pendingTxSub.ID
   134  }
   135  
   136  // NewPendingTransactions creates a subscription that is triggered each time a transaction
   137  // enters the transaction pool and was signed from one of the transactions this nodes manages.
   138  func (api *PublicFilterAPI) NewPendingTransactions(ctx context.Context) (*rpc.Subscription, error) {
   139  	notifier, supported := rpc.NotifierFromContext(ctx)
   140  	if !supported {
   141  		return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
   142  	}
   143  
   144  	rpcSub := notifier.CreateSubscription()
   145  
   146  	go func() {
   147  		txHashes := make(chan []common.Hash, 128)
   148  		pendingTxSub := api.events.SubscribePendingTxs(txHashes)
   149  
   150  		for {
   151  			select {
   152  			case hashes := <-txHashes:
   153  				// To keep the original behaviour, send a single tx hash in one notification.
   154  				// TODO(rjl493456442) Send a batch of tx hashes in one notification
   155  				for _, h := range hashes {
   156  					notifier.Notify(rpcSub.ID, h)
   157  				}
   158  			case <-rpcSub.Err():
   159  				pendingTxSub.Unsubscribe()
   160  				return
   161  			case <-notifier.Closed():
   162  				pendingTxSub.Unsubscribe()
   163  				return
   164  			}
   165  		}
   166  	}()
   167  
   168  	return rpcSub, nil
   169  }
   170  
   171  // NewBlockFilter creates a filter that fetches blocks that are imported into the chain.
   172  // It is part of the filter package since polling goes with eth_getFilterChanges.
   173  //
   174  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newblockfilter
   175  func (api *PublicFilterAPI) NewBlockFilter() rpc.ID {
   176  	var (
   177  		headers   = make(chan *types.Header)
   178  		headerSub = api.events.SubscribeNewHeads(headers)
   179  	)
   180  
   181  	api.filtersMu.Lock()
   182  	api.filters[headerSub.ID] = &filter{typ: BlocksSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: headerSub}
   183  	api.filtersMu.Unlock()
   184  
   185  	go func() {
   186  		for {
   187  			select {
   188  			case h := <-headers:
   189  				api.filtersMu.Lock()
   190  				if f, found := api.filters[headerSub.ID]; found {
   191  					f.hashes = append(f.hashes, h.Hash())
   192  				}
   193  				api.filtersMu.Unlock()
   194  			case <-headerSub.Err():
   195  				api.filtersMu.Lock()
   196  				delete(api.filters, headerSub.ID)
   197  				api.filtersMu.Unlock()
   198  				return
   199  			}
   200  		}
   201  	}()
   202  
   203  	return headerSub.ID
   204  }
   205  
   206  // NewHeads send a notification each time a new (header) block is appended to the chain.
   207  func (api *PublicFilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
   208  	notifier, supported := rpc.NotifierFromContext(ctx)
   209  	if !supported {
   210  		return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
   211  	}
   212  
   213  	rpcSub := notifier.CreateSubscription()
   214  
   215  	go func() {
   216  		headers := make(chan *types.Header)
   217  		headersSub := api.events.SubscribeNewHeads(headers)
   218  
   219  		for {
   220  			select {
   221  			case h := <-headers:
   222  				notifier.Notify(rpcSub.ID, h)
   223  			case <-rpcSub.Err():
   224  				headersSub.Unsubscribe()
   225  				return
   226  			case <-notifier.Closed():
   227  				headersSub.Unsubscribe()
   228  				return
   229  			}
   230  		}
   231  	}()
   232  
   233  	return rpcSub, nil
   234  }
   235  
   236  // Logs creates a subscription that fires for all new log that match the given filter criteria.
   237  func (api *PublicFilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subscription, error) {
   238  	notifier, supported := rpc.NotifierFromContext(ctx)
   239  	if !supported {
   240  		return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
   241  	}
   242  
   243  	var (
   244  		rpcSub      = notifier.CreateSubscription()
   245  		matchedLogs = make(chan []*types.Log)
   246  	)
   247  
   248  	logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), matchedLogs)
   249  	if err != nil {
   250  		return nil, err
   251  	}
   252  
   253  	go func() {
   254  
   255  		for {
   256  			select {
   257  			case logs := <-matchedLogs:
   258  				for _, log := range logs {
   259  					notifier.Notify(rpcSub.ID, &log)
   260  				}
   261  			case <-rpcSub.Err(): // client send an unsubscribe request
   262  				logsSub.Unsubscribe()
   263  				return
   264  			case <-notifier.Closed(): // connection dropped
   265  				logsSub.Unsubscribe()
   266  				return
   267  			}
   268  		}
   269  	}()
   270  
   271  	return rpcSub, nil
   272  }
   273  
   274  // FilterCriteria represents a request to create a new filter.
   275  // Same as ethereum.FilterQuery but with UnmarshalJSON() method.
   276  type FilterCriteria ethereum.FilterQuery
   277  
   278  // NewFilter creates a new filter and returns the filter id. It can be
   279  // used to retrieve logs when the state changes. This method cannot be
   280  // used to fetch logs that are already stored in the state.
   281  //
   282  // Default criteria for the from and to block are "latest".
   283  // Using "latest" as block number will return logs for mined blocks.
   284  // Using "pending" as block number returns logs for not yet mined (pending) blocks.
   285  // In case logs are removed (chain reorg) previously returned logs are returned
   286  // again but with the removed property set to true.
   287  //
   288  // In case "fromBlock" > "toBlock" an error is returned.
   289  //
   290  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
   291  func (api *PublicFilterAPI) NewFilter(crit FilterCriteria) (rpc.ID, error) {
   292  	logs := make(chan []*types.Log)
   293  	logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), logs)
   294  	if err != nil {
   295  		return rpc.ID(""), err
   296  	}
   297  
   298  	api.filtersMu.Lock()
   299  	api.filters[logsSub.ID] = &filter{typ: LogsSubscription, crit: crit, deadline: time.NewTimer(deadline), logs: make([]*types.Log, 0), s: logsSub}
   300  	api.filtersMu.Unlock()
   301  
   302  	go func() {
   303  		for {
   304  			select {
   305  			case l := <-logs:
   306  				api.filtersMu.Lock()
   307  				if f, found := api.filters[logsSub.ID]; found {
   308  					f.logs = append(f.logs, l...)
   309  				}
   310  				api.filtersMu.Unlock()
   311  			case <-logsSub.Err():
   312  				api.filtersMu.Lock()
   313  				delete(api.filters, logsSub.ID)
   314  				api.filtersMu.Unlock()
   315  				return
   316  			}
   317  		}
   318  	}()
   319  
   320  	return logsSub.ID, nil
   321  }
   322  
   323  // GetLogs returns logs matching the given argument that are stored within the state.
   324  //
   325  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs
   326  func (api *PublicFilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*types.Log, error) {
   327  	// Convert the RPC block numbers into internal representations
   328  	if crit.FromBlock == nil {
   329  		crit.FromBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
   330  	}
   331  	if crit.ToBlock == nil {
   332  		crit.ToBlock = big.NewInt(rpc.LatestBlockNumber.Int64())
   333  	}
   334  	// Create and run the filter to get all the logs
   335  	filter := New(api.backend, crit.FromBlock.Int64(), crit.ToBlock.Int64(), crit.Addresses, crit.Topics)
   336  
   337  	logs, err := filter.Logs(ctx)
   338  	if err != nil {
   339  		return nil, err
   340  	}
   341  	return returnLogs(logs), err
   342  }
   343  
   344  // UninstallFilter removes the filter with the given filter id.
   345  //
   346  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_uninstallfilter
   347  func (api *PublicFilterAPI) UninstallFilter(id rpc.ID) bool {
   348  	api.filtersMu.Lock()
   349  	f, found := api.filters[id]
   350  	if found {
   351  		delete(api.filters, id)
   352  	}
   353  	api.filtersMu.Unlock()
   354  	if found {
   355  		f.s.Unsubscribe()
   356  	}
   357  
   358  	return found
   359  }
   360  
   361  // GetFilterLogs returns the logs for the filter with the given id.
   362  // If the filter could not be found an empty array of logs is returned.
   363  //
   364  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterlogs
   365  func (api *PublicFilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*types.Log, error) {
   366  	api.filtersMu.Lock()
   367  	f, found := api.filters[id]
   368  	api.filtersMu.Unlock()
   369  
   370  	if !found || f.typ != LogsSubscription {
   371  		return nil, fmt.Errorf("filter not found")
   372  	}
   373  
   374  	begin := rpc.LatestBlockNumber.Int64()
   375  	if f.crit.FromBlock != nil {
   376  		begin = f.crit.FromBlock.Int64()
   377  	}
   378  	end := rpc.LatestBlockNumber.Int64()
   379  	if f.crit.ToBlock != nil {
   380  		end = f.crit.ToBlock.Int64()
   381  	}
   382  	// Create and run the filter to get all the logs
   383  	filter := New(api.backend, begin, end, f.crit.Addresses, f.crit.Topics)
   384  
   385  	logs, err := filter.Logs(ctx)
   386  	if err != nil {
   387  		return nil, err
   388  	}
   389  	return returnLogs(logs), nil
   390  }
   391  
   392  // GetFilterChanges returns the logs for the filter with the given id since
   393  // last time it was called. This can be used for polling.
   394  //
   395  // For pending transaction and block filters the result is []common.Hash.
   396  // (pending)Log filters return []Log.
   397  //
   398  // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterchanges
   399  func (api *PublicFilterAPI) GetFilterChanges(id rpc.ID) (interface{}, error) {
   400  	api.filtersMu.Lock()
   401  	defer api.filtersMu.Unlock()
   402  
   403  	if f, found := api.filters[id]; found {
   404  		if !f.deadline.Stop() {
   405  			// timer expired but filter is not yet removed in timeout loop
   406  			// receive timer value and reset timer
   407  			<-f.deadline.C
   408  		}
   409  		f.deadline.Reset(deadline)
   410  
   411  		switch f.typ {
   412  		case PendingTransactionsSubscription, BlocksSubscription:
   413  			hashes := f.hashes
   414  			f.hashes = nil
   415  			return returnHashes(hashes), nil
   416  		case LogsSubscription:
   417  			logs := f.logs
   418  			f.logs = nil
   419  			return returnLogs(logs), nil
   420  		}
   421  	}
   422  
   423  	return []interface{}{}, fmt.Errorf("filter not found")
   424  }
   425  
   426  // returnHashes is a helper that will return an empty hash array case the given hash array is nil,
   427  // otherwise the given hashes array is returned.
   428  func returnHashes(hashes []common.Hash) []common.Hash {
   429  	if hashes == nil {
   430  		return []common.Hash{}
   431  	}
   432  	return hashes
   433  }
   434  
   435  // returnLogs is a helper that will return an empty log array in case the given logs array is nil,
   436  // otherwise the given logs array is returned.
   437  func returnLogs(logs []*types.Log) []*types.Log {
   438  	if logs == nil {
   439  		return []*types.Log{}
   440  	}
   441  	return logs
   442  }
   443  
   444  // UnmarshalJSON sets *args fields with given data.
   445  func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
   446  	type input struct {
   447  		From      *rpc.BlockNumber `json:"fromBlock"`
   448  		ToBlock   *rpc.BlockNumber `json:"toBlock"`
   449  		Addresses interface{}      `json:"address"`
   450  		Topics    []interface{}    `json:"topics"`
   451  	}
   452  
   453  	var raw input
   454  	if err := json.Unmarshal(data, &raw); err != nil {
   455  		return err
   456  	}
   457  
   458  	if raw.From != nil {
   459  		args.FromBlock = big.NewInt(raw.From.Int64())
   460  	}
   461  
   462  	if raw.ToBlock != nil {
   463  		args.ToBlock = big.NewInt(raw.ToBlock.Int64())
   464  	}
   465  
   466  	args.Addresses = []common.Address{}
   467  
   468  	if raw.Addresses != nil {
   469  		// raw.Address can contain a single address or an array of addresses
   470  		switch rawAddr := raw.Addresses.(type) {
   471  		case []interface{}:
   472  			for i, addr := range rawAddr {
   473  				if strAddr, ok := addr.(string); ok {
   474  					addr, err := decodeAddress(strAddr)
   475  					if err != nil {
   476  						return fmt.Errorf("invalid address at index %d: %v", i, err)
   477  					}
   478  					args.Addresses = append(args.Addresses, addr)
   479  				} else {
   480  					return fmt.Errorf("non-string address at index %d", i)
   481  				}
   482  			}
   483  		case string:
   484  			addr, err := decodeAddress(rawAddr)
   485  			if err != nil {
   486  				return fmt.Errorf("invalid address: %v", err)
   487  			}
   488  			args.Addresses = []common.Address{addr}
   489  		default:
   490  			return errors.New("invalid addresses in query")
   491  		}
   492  	}
   493  
   494  	// topics is an array consisting of strings and/or arrays of strings.
   495  	// JSON null values are converted to common.Hash{} and ignored by the filter manager.
   496  	if len(raw.Topics) > 0 {
   497  		args.Topics = make([][]common.Hash, len(raw.Topics))
   498  		for i, t := range raw.Topics {
   499  			switch topic := t.(type) {
   500  			case nil:
   501  				// ignore topic when matching logs
   502  
   503  			case string:
   504  				// match specific topic
   505  				top, err := decodeTopic(topic)
   506  				if err != nil {
   507  					return err
   508  				}
   509  				args.Topics[i] = []common.Hash{top}
   510  
   511  			case []interface{}:
   512  				// or case e.g. [null, "topic0", "topic1"]
   513  				for _, rawTopic := range topic {
   514  					if rawTopic == nil {
   515  						// null component, match all
   516  						args.Topics[i] = nil
   517  						break
   518  					}
   519  					if topic, ok := rawTopic.(string); ok {
   520  						parsed, err := decodeTopic(topic)
   521  						if err != nil {
   522  							return err
   523  						}
   524  						args.Topics[i] = append(args.Topics[i], parsed)
   525  					} else {
   526  						return fmt.Errorf("invalid topic(s)")
   527  					}
   528  				}
   529  			default:
   530  				return fmt.Errorf("invalid topic(s)")
   531  			}
   532  		}
   533  	}
   534  
   535  	return nil
   536  }
   537  
   538  func decodeAddress(s string) (common.Address, error) {
   539  	b, err := hexutil.Decode(s)
   540  	if err == nil && len(b) != common.AddressLength {
   541  		err = fmt.Errorf("hex has invalid length %d after decoding", len(b))
   542  	}
   543  	return common.BytesToAddress(b), err
   544  }
   545  
   546  func decodeTopic(s string) (common.Hash, error) {
   547  	b, err := hexutil.Decode(s)
   548  	if err == nil && len(b) != common.HashLength {
   549  		err = fmt.Errorf("hex has invalid length %d after decoding", len(b))
   550  	}
   551  	return common.BytesToHash(b), err
   552  }