github.com/koko1123/flow-go-1@v0.29.6/model/libp2p/peer/filters.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package peer
     4  
     5  import (
     6  	"encoding/hex"
     7  
     8  	libp2p2 "github.com/koko1123/flow-go-1/model/libp2p"
     9  )
    10  
    11  // Not negates the wrapped filter.
    12  func Not(filter libp2p2.PeerFilter) libp2p2.PeerFilter {
    13  	return func(p *libp2p2.Peer) bool {
    14  		return !filter(p)
    15  	}
    16  }
    17  
    18  // HasSeen filters for peers that have seen the event with the given ID.
    19  func HasSeen(eventID []byte) libp2p2.PeerFilter {
    20  	key := hex.EncodeToString(eventID)
    21  	return func(p *libp2p2.Peer) bool {
    22  		_, ok := p.Seen[key]
    23  		return ok
    24  	}
    25  }
    26  
    27  // HasSeenAllOf filters for peers that have seen all of the events with the given
    28  // IDs.
    29  func HasSeenAllOf(eventIDs ...[]byte) libp2p2.PeerFilter {
    30  	keys := make([]string, 0, len(eventIDs))
    31  	for _, eventID := range eventIDs {
    32  		key := hex.EncodeToString(eventID)
    33  		keys = append(keys, key)
    34  	}
    35  	return func(p *libp2p2.Peer) bool {
    36  		for _, key := range keys {
    37  			_, ok := p.Seen[key]
    38  			if !ok {
    39  				return false
    40  			}
    41  		}
    42  		return true
    43  	}
    44  }
    45  
    46  // HasSeenOneOf filters for peers that have seen at least one of the events with the
    47  // given IDs.
    48  func HasSeenOneOf(eventIDs ...[]byte) libp2p2.PeerFilter {
    49  	keys := make([]string, 0, len(eventIDs))
    50  	for _, eventID := range eventIDs {
    51  		key := hex.EncodeToString(eventID)
    52  		keys = append(keys, key)
    53  	}
    54  	return func(i *libp2p2.Peer) bool {
    55  		for _, key := range keys {
    56  			_, ok := i.Seen[key]
    57  			if ok {
    58  				return true
    59  			}
    60  		}
    61  		return false
    62  	}
    63  }