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