github.com/status-im/status-go@v1.1.0/services/rpcfilters/hash_filter.go (about)

     1  package rpcfilters
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/ethereum/go-ethereum/common"
     9  )
    10  
    11  type hashFilter struct {
    12  	hashes []common.Hash
    13  	mu     sync.Mutex
    14  	done   chan struct{}
    15  	timer  *time.Timer
    16  }
    17  
    18  // add adds a hash to the hashFilter
    19  func (f *hashFilter) add(data interface{}) error {
    20  	hash, ok := data.(common.Hash)
    21  	if !ok {
    22  		return errors.New("provided data is not a common.Hash")
    23  	}
    24  	f.mu.Lock()
    25  	defer f.mu.Unlock()
    26  	f.hashes = append(f.hashes, hash)
    27  	return nil
    28  }
    29  
    30  // pop returns all the hashes stored in the hashFilter and clears the hashFilter contents
    31  func (f *hashFilter) pop() interface{} {
    32  	f.mu.Lock()
    33  	defer f.mu.Unlock()
    34  	hashes := f.hashes
    35  	f.hashes = nil
    36  	return hashes
    37  }
    38  
    39  func (f *hashFilter) stop() {
    40  	select {
    41  	case <-f.done:
    42  		return
    43  	default:
    44  		close(f.done)
    45  	}
    46  }
    47  
    48  func (f *hashFilter) deadline() *time.Timer {
    49  	return f.timer
    50  }
    51  
    52  func newHashFilter() *hashFilter {
    53  	return &hashFilter{
    54  		done: make(chan struct{}),
    55  	}
    56  }