github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/eth/filters/filter.go (about)

     1  // Copyright 2014 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  	"errors"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/bloombits"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/rpc"
    28  )
    29  
    30  // Filter can be used to retrieve and filter logs.
    31  type Filter struct {
    32  	sys *FilterSystem
    33  
    34  	addresses []common.Address
    35  	topics    [][]common.Hash
    36  
    37  	block      common.Hash // Block hash if filtering a single block
    38  	begin, end int64       // Range interval if filtering multiple blocks
    39  
    40  	matcher *bloombits.Matcher
    41  }
    42  
    43  // NewRangeFilter creates a new filter which uses a bloom filter on blocks to
    44  // figure out whether a particular block is interesting or not.
    45  func (sys *FilterSystem) NewRangeFilter(begin, end int64, addresses []common.Address, topics [][]common.Hash) *Filter {
    46  	// Flatten the address and topic filter clauses into a single bloombits filter
    47  	// system. Since the bloombits are not positional, nil topics are permitted,
    48  	// which get flattened into a nil byte slice.
    49  	var filters [][][]byte
    50  	if len(addresses) > 0 {
    51  		filter := make([][]byte, len(addresses))
    52  		for i, address := range addresses {
    53  			filter[i] = address.Bytes()
    54  		}
    55  		filters = append(filters, filter)
    56  	}
    57  	for _, topicList := range topics {
    58  		filter := make([][]byte, len(topicList))
    59  		for i, topic := range topicList {
    60  			filter[i] = topic.Bytes()
    61  		}
    62  		filters = append(filters, filter)
    63  	}
    64  	size, _ := sys.backend.BloomStatus()
    65  
    66  	// Create a generic filter and convert it into a range filter
    67  	filter := newFilter(sys, addresses, topics)
    68  
    69  	filter.matcher = bloombits.NewMatcher(size, filters)
    70  	filter.begin = begin
    71  	filter.end = end
    72  
    73  	return filter
    74  }
    75  
    76  // NewBlockFilter creates a new filter which directly inspects the contents of
    77  // a block to figure out whether it is interesting or not.
    78  func (sys *FilterSystem) NewBlockFilter(block common.Hash, addresses []common.Address, topics [][]common.Hash) *Filter {
    79  	// Create a generic filter and convert it into a block filter
    80  	filter := newFilter(sys, addresses, topics)
    81  	filter.block = block
    82  	return filter
    83  }
    84  
    85  // newFilter creates a generic filter that can either filter based on a block hash,
    86  // or based on range queries. The search criteria needs to be explicitly set.
    87  func newFilter(sys *FilterSystem, addresses []common.Address, topics [][]common.Hash) *Filter {
    88  	return &Filter{
    89  		sys:       sys,
    90  		addresses: addresses,
    91  		topics:    topics,
    92  	}
    93  }
    94  
    95  // Logs searches the blockchain for matching log entries, returning all from the
    96  // first block that contains matches, updating the start of the filter accordingly.
    97  func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) {
    98  	// If we're doing singleton block filtering, execute and return
    99  	if f.block != (common.Hash{}) {
   100  		header, err := f.sys.backend.HeaderByHash(ctx, f.block)
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  		if header == nil {
   105  			return nil, errors.New("unknown block")
   106  		}
   107  		return f.blockLogs(ctx, header, false)
   108  	}
   109  	// Short-cut if all we care about is pending logs
   110  	if f.begin == rpc.PendingBlockNumber.Int64() {
   111  		if f.end != rpc.PendingBlockNumber.Int64() {
   112  			return nil, errors.New("invalid block range")
   113  		}
   114  		return f.pendingLogs()
   115  	}
   116  	// Figure out the limits of the filter range
   117  	header, _ := f.sys.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
   118  	if header == nil {
   119  		return nil, nil
   120  	}
   121  	var (
   122  		head    = header.Number.Uint64()
   123  		end     = uint64(f.end)
   124  		pending = f.end == rpc.PendingBlockNumber.Int64()
   125  	)
   126  	if f.begin == rpc.LatestBlockNumber.Int64() {
   127  		f.begin = int64(head)
   128  	}
   129  	if f.end == rpc.LatestBlockNumber.Int64() || f.end == rpc.PendingBlockNumber.Int64() {
   130  		end = head
   131  	}
   132  	// Gather all indexed logs, and finish with non indexed ones
   133  	var (
   134  		logs           []*types.Log
   135  		err            error
   136  		size, sections = f.sys.backend.BloomStatus()
   137  	)
   138  	if indexed := sections * size; indexed > uint64(f.begin) {
   139  		if indexed > end {
   140  			logs, err = f.indexedLogs(ctx, end)
   141  		} else {
   142  			logs, err = f.indexedLogs(ctx, indexed-1)
   143  		}
   144  		if err != nil {
   145  			return logs, err
   146  		}
   147  	}
   148  	rest, err := f.unindexedLogs(ctx, end)
   149  	logs = append(logs, rest...)
   150  	if pending {
   151  		pendingLogs, err := f.pendingLogs()
   152  		if err != nil {
   153  			return nil, err
   154  		}
   155  		logs = append(logs, pendingLogs...)
   156  	}
   157  	return logs, err
   158  }
   159  
   160  // indexedLogs returns the logs matching the filter criteria based on the bloom
   161  // bits indexed available locally or via the network.
   162  func (f *Filter) indexedLogs(ctx context.Context, end uint64) ([]*types.Log, error) {
   163  	// Create a matcher session and request servicing from the backend
   164  	matches := make(chan uint64, 64)
   165  
   166  	session, err := f.matcher.Start(ctx, uint64(f.begin), end, matches)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	defer session.Close()
   171  
   172  	f.sys.backend.ServiceFilter(ctx, session)
   173  
   174  	// Iterate over the matches until exhausted or context closed
   175  	var logs []*types.Log
   176  
   177  	for {
   178  		select {
   179  		case number, ok := <-matches:
   180  			// Abort if all matches have been fulfilled
   181  			if !ok {
   182  				err := session.Error()
   183  				if err == nil {
   184  					f.begin = int64(end) + 1
   185  				}
   186  				return logs, err
   187  			}
   188  			f.begin = int64(number) + 1
   189  
   190  			// Retrieve the suggested block and pull any truly matching logs
   191  			header, err := f.sys.backend.HeaderByNumber(ctx, rpc.BlockNumber(number))
   192  			if header == nil || err != nil {
   193  				return logs, err
   194  			}
   195  			found, err := f.blockLogs(ctx, header, true)
   196  			if err != nil {
   197  				return logs, err
   198  			}
   199  			logs = append(logs, found...)
   200  
   201  		case <-ctx.Done():
   202  			return logs, ctx.Err()
   203  		}
   204  	}
   205  }
   206  
   207  // unindexedLogs returns the logs matching the filter criteria based on raw block
   208  // iteration and bloom matching.
   209  func (f *Filter) unindexedLogs(ctx context.Context, end uint64) ([]*types.Log, error) {
   210  	var logs []*types.Log
   211  
   212  	for ; f.begin <= int64(end); f.begin++ {
   213  		header, err := f.sys.backend.HeaderByNumber(ctx, rpc.BlockNumber(f.begin))
   214  		if header == nil || err != nil {
   215  			return logs, err
   216  		}
   217  		found, err := f.blockLogs(ctx, header, false)
   218  		if err != nil {
   219  			return logs, err
   220  		}
   221  		logs = append(logs, found...)
   222  	}
   223  	return logs, nil
   224  }
   225  
   226  // blockLogs returns the logs matching the filter criteria within a single block.
   227  func (f *Filter) blockLogs(ctx context.Context, header *types.Header, skipBloom bool) ([]*types.Log, error) {
   228  	// Fast track: no filtering criteria
   229  	if len(f.addresses) == 0 && len(f.topics) == 0 {
   230  		list, err := f.sys.cachedGetLogs(ctx, header.Hash(), header.Number.Uint64())
   231  		if err != nil {
   232  			return nil, err
   233  		}
   234  		return flatten(list), nil
   235  	} else if skipBloom || bloomFilter(header.Bloom, f.addresses, f.topics) {
   236  		return f.checkMatches(ctx, header)
   237  	}
   238  	return nil, nil
   239  }
   240  
   241  // checkMatches checks if the receipts belonging to the given header contain any log events that
   242  // match the filter criteria. This function is called when the bloom filter signals a potential match.
   243  func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*types.Log, error) {
   244  	logsList, err := f.sys.cachedGetLogs(ctx, header.Hash(), header.Number.Uint64())
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  
   249  	unfiltered := flatten(logsList)
   250  	logs := filterLogs(unfiltered, nil, nil, f.addresses, f.topics)
   251  	if len(logs) > 0 {
   252  		// We have matching logs, check if we need to resolve full logs via the light client
   253  		if logs[0].TxHash == (common.Hash{}) {
   254  			receipts, err := f.sys.backend.GetReceipts(ctx, header.Hash())
   255  			if err != nil {
   256  				return nil, err
   257  			}
   258  			unfiltered = unfiltered[:0]
   259  			for _, receipt := range receipts {
   260  				unfiltered = append(unfiltered, receipt.Logs...)
   261  			}
   262  			logs = filterLogs(unfiltered, nil, nil, f.addresses, f.topics)
   263  		}
   264  		return logs, nil
   265  	}
   266  	return nil, nil
   267  }
   268  
   269  // pendingLogs returns the logs matching the filter criteria within the pending block.
   270  func (f *Filter) pendingLogs() ([]*types.Log, error) {
   271  	block, receipts := f.sys.backend.PendingBlockAndReceipts()
   272  	if bloomFilter(block.Bloom(), f.addresses, f.topics) {
   273  		var unfiltered []*types.Log
   274  		for _, r := range receipts {
   275  			unfiltered = append(unfiltered, r.Logs...)
   276  		}
   277  		return filterLogs(unfiltered, nil, nil, f.addresses, f.topics), nil
   278  	}
   279  	return nil, nil
   280  }
   281  
   282  func includes(addresses []common.Address, a common.Address) bool {
   283  	for _, addr := range addresses {
   284  		if addr == a {
   285  			return true
   286  		}
   287  	}
   288  
   289  	return false
   290  }
   291  
   292  // filterLogs creates a slice of logs matching the given criteria.
   293  func filterLogs(logs []*types.Log, fromBlock, toBlock *big.Int, addresses []common.Address, topics [][]common.Hash) []*types.Log {
   294  	var ret []*types.Log
   295  Logs:
   296  	for _, log := range logs {
   297  		if fromBlock != nil && fromBlock.Int64() >= 0 && fromBlock.Uint64() > log.BlockNumber {
   298  			continue
   299  		}
   300  		if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber {
   301  			continue
   302  		}
   303  
   304  		if len(addresses) > 0 && !includes(addresses, log.Address) {
   305  			continue
   306  		}
   307  		// If the to filtered topics is greater than the amount of topics in logs, skip.
   308  		if len(topics) > len(log.Topics) {
   309  			continue
   310  		}
   311  		for i, sub := range topics {
   312  			match := len(sub) == 0 // empty rule set == wildcard
   313  			for _, topic := range sub {
   314  				if log.Topics[i] == topic {
   315  					match = true
   316  					break
   317  				}
   318  			}
   319  			if !match {
   320  				continue Logs
   321  			}
   322  		}
   323  		ret = append(ret, log)
   324  	}
   325  	return ret
   326  }
   327  
   328  func bloomFilter(bloom types.Bloom, addresses []common.Address, topics [][]common.Hash) bool {
   329  	if len(addresses) > 0 {
   330  		var included bool
   331  		for _, addr := range addresses {
   332  			if types.BloomLookup(bloom, addr) {
   333  				included = true
   334  				break
   335  			}
   336  		}
   337  		if !included {
   338  			return false
   339  		}
   340  	}
   341  
   342  	for _, sub := range topics {
   343  		included := len(sub) == 0 // empty rule set == wildcard
   344  		for _, topic := range sub {
   345  			if types.BloomLookup(bloom, topic) {
   346  				included = true
   347  				break
   348  			}
   349  		}
   350  		if !included {
   351  			return false
   352  		}
   353  	}
   354  	return true
   355  }
   356  
   357  func flatten(list [][]*types.Log) []*types.Log {
   358  	var flat []*types.Log
   359  	for _, logs := range list {
   360  		flat = append(flat, logs...)
   361  	}
   362  	return flat
   363  }