-
Notifications
You must be signed in to change notification settings - Fork 178
/
identity.go
119 lines (104 loc) · 3.25 KB
/
identity.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package filter
import (
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/flow"
)
// Any will always be true.
func Any(*flow.Identity) bool {
return true
}
// And combines two or more filters that all need to be true.
func And(filters ...flow.IdentityFilter) flow.IdentityFilter {
return func(identity *flow.Identity) bool {
for _, filter := range filters {
if !filter(identity) {
return false
}
}
return true
}
}
// Or combines two or more filters and only needs one of them to be true.
func Or(filters ...flow.IdentityFilter) flow.IdentityFilter {
return func(identity *flow.Identity) bool {
for _, filter := range filters {
if filter(identity) {
return true
}
}
return false
}
}
// Not returns a filter equivalent to the inverse of the input filter.
func Not(filter flow.IdentityFilter) flow.IdentityFilter {
return func(identity *flow.Identity) bool {
return !filter(identity)
}
}
// In returns a filter for identities within the input list. This is equivalent
// to HasNodeID, but for list-typed inputs.
func In(list flow.IdentityList) flow.IdentityFilter {
return HasNodeID(list.NodeIDs()...)
}
// HasNodeID returns a filter that returns true for any identity with an ID
// matching any of the inputs.
func HasNodeID(nodeIDs ...flow.Identifier) flow.IdentityFilter {
lookup := make(map[flow.Identifier]struct{})
for _, nodeID := range nodeIDs {
lookup[nodeID] = struct{}{}
}
return func(identity *flow.Identity) bool {
_, ok := lookup[identity.NodeID]
return ok
}
}
// HasNetworkingKey returns a filter that returns true for any identity with a
// networking public key matching any of the inputs.
func HasNetworkingKey(keys ...crypto.PublicKey) flow.IdentityFilter {
return func(identity *flow.Identity) bool {
for _, key := range keys {
if key.Equals(identity.NetworkPubKey) {
return true
}
}
return false
}
}
// HasWeight returns a filter for nodes with non-zero weight.
func HasWeight(hasWeight bool) flow.IdentityFilter {
return func(identity *flow.Identity) bool {
return (identity.Weight > 0) == hasWeight
}
}
// Ejected is a filter that returns true if the node is ejected.
func Ejected(identity *flow.Identity) bool {
return identity.Ejected
}
// HasRole returns a filter for nodes with one of the input roles.
func HasRole(roles ...flow.Role) flow.IdentityFilter {
lookup := make(map[flow.Role]struct{})
for _, role := range roles {
lookup[role] = struct{}{}
}
return func(identity *flow.Identity) bool {
_, ok := lookup[identity.Role]
return ok
}
}
// IsValidCurrentEpochParticipant is an identity filter for members of the
// current epoch in good standing.
var IsValidCurrentEpochParticipant = And(
HasWeight(true),
Not(Ejected), // ejection will change signer index
)
// IsVotingConsensusCommitteeMember is a identity filter for all members of
// the consensus committee allowed to vote.
var IsVotingConsensusCommitteeMember = And(
HasRole(flow.RoleConsensus),
IsValidCurrentEpochParticipant,
)
// IsValidDKGParticipant is an identity filter for all DKG participants. It is
// equivalent to the filter for consensus committee members, as these are
// the same group for now.
var IsValidDKGParticipant = IsVotingConsensusCommitteeMember