github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/libp2p/peer/filters.go (about)

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