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