github.com/koko1123/flow-go-1@v0.29.6/model/flow/filter/identity.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package filter
     4  
     5  import (
     6  	"github.com/onflow/flow-go/crypto"
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  )
     9  
    10  // Any will always be true.
    11  func Any(*flow.Identity) bool {
    12  	return true
    13  }
    14  
    15  // And combines two or more filters that all need to be true.
    16  func And(filters ...flow.IdentityFilter) flow.IdentityFilter {
    17  	return func(identity *flow.Identity) bool {
    18  		for _, filter := range filters {
    19  			if !filter(identity) {
    20  				return false
    21  			}
    22  		}
    23  		return true
    24  	}
    25  }
    26  
    27  // Or combines two or more filters and only needs one of them to be true.
    28  func Or(filters ...flow.IdentityFilter) flow.IdentityFilter {
    29  	return func(identity *flow.Identity) bool {
    30  		for _, filter := range filters {
    31  			if filter(identity) {
    32  				return true
    33  			}
    34  		}
    35  		return false
    36  	}
    37  }
    38  
    39  // Not returns a filter equivalent to the inverse of the input filter.
    40  func Not(filter flow.IdentityFilter) flow.IdentityFilter {
    41  	return func(identity *flow.Identity) bool {
    42  		return !filter(identity)
    43  	}
    44  }
    45  
    46  // In returns a filter for identities within the input list. This is equivalent
    47  // to HasNodeID, but for list-typed inputs.
    48  func In(list flow.IdentityList) flow.IdentityFilter {
    49  	return HasNodeID(list.NodeIDs()...)
    50  }
    51  
    52  // HasNodeID returns a filter that returns true for any identity with an ID
    53  // matching any of the inputs.
    54  func HasNodeID(nodeIDs ...flow.Identifier) flow.IdentityFilter {
    55  	lookup := make(map[flow.Identifier]struct{})
    56  	for _, nodeID := range nodeIDs {
    57  		lookup[nodeID] = struct{}{}
    58  	}
    59  	return func(identity *flow.Identity) bool {
    60  		_, ok := lookup[identity.NodeID]
    61  		return ok
    62  	}
    63  }
    64  
    65  // HasNetworkingKey returns a filter that returns true for any identity with a
    66  // networking public key matching any of the inputs.
    67  func HasNetworkingKey(keys ...crypto.PublicKey) flow.IdentityFilter {
    68  	return func(identity *flow.Identity) bool {
    69  		for _, key := range keys {
    70  			if key.Equals(identity.NetworkPubKey) {
    71  				return true
    72  			}
    73  		}
    74  		return false
    75  	}
    76  }
    77  
    78  // HasWeight returns a filter for nodes with non-zero weight.
    79  func HasWeight(hasWeight bool) flow.IdentityFilter {
    80  	return func(identity *flow.Identity) bool {
    81  		return (identity.Weight > 0) == hasWeight
    82  	}
    83  }
    84  
    85  // Ejected is a filter that returns true if the node is ejected.
    86  func Ejected(identity *flow.Identity) bool {
    87  	return identity.Ejected
    88  }
    89  
    90  // HasRole returns a filter for nodes with one of the input roles.
    91  func HasRole(roles ...flow.Role) flow.IdentityFilter {
    92  	lookup := make(map[flow.Role]struct{})
    93  	for _, role := range roles {
    94  		lookup[role] = struct{}{}
    95  	}
    96  	return func(identity *flow.Identity) bool {
    97  		_, ok := lookup[identity.Role]
    98  		return ok
    99  	}
   100  }
   101  
   102  // IsValidCurrentEpochParticipant is an identity filter for members of the
   103  // current epoch in good standing.
   104  var IsValidCurrentEpochParticipant = And(
   105  	HasWeight(true),
   106  	Not(Ejected), // ejection will change signer index
   107  )
   108  
   109  // IsVotingConsensusCommitteeMember is a identity filter for all members of
   110  // the consensus committee allowed to vote.
   111  var IsVotingConsensusCommitteeMember = And(
   112  	HasRole(flow.RoleConsensus),
   113  	IsValidCurrentEpochParticipant,
   114  )
   115  
   116  // IsValidDKGParticipant is an identity filter for all DKG participants. It is
   117  // equivalent to the filter for consensus committee members, as these are
   118  // the same group for now.
   119  var IsValidDKGParticipant = IsVotingConsensusCommitteeMember