github.com/koko1123/flow-go-1@v0.29.6/model/flow/role.go (about) 1 // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED 2 3 package flow 4 5 import ( 6 "fmt" 7 "sort" 8 9 "github.com/pkg/errors" 10 ) 11 12 // Role represents a role in the flow system. 13 type Role uint8 14 15 // Enumeration of the available flow node roles. 16 const ( 17 RoleCollection Role = 1 18 RoleConsensus Role = 2 19 RoleExecution Role = 3 20 RoleVerification Role = 4 21 RoleAccess Role = 5 22 ) 23 24 func (r Role) Valid() bool { 25 return r >= 1 && r <= 5 26 } 27 28 // String returns a string version of role. 29 func (r Role) String() string { 30 switch r { 31 case RoleCollection: 32 return "collection" 33 case RoleConsensus: 34 return "consensus" 35 case RoleExecution: 36 return "execution" 37 case RoleVerification: 38 return "verification" 39 case RoleAccess: 40 return "access" 41 default: 42 panic(fmt.Sprintf("invalid role (%d)", r)) 43 } 44 } 45 46 // ParseRole will parse a role from string. 47 func ParseRole(role string) (Role, error) { 48 switch role { 49 case "collection": 50 return RoleCollection, nil 51 case "consensus": 52 return RoleConsensus, nil 53 case "execution": 54 return RoleExecution, nil 55 case "verification": 56 return RoleVerification, nil 57 case "access": 58 return RoleAccess, nil 59 default: 60 return 0, errors.Errorf("invalid role string (%s)", role) 61 } 62 } 63 64 func (r Role) MarshalText() ([]byte, error) { 65 return []byte(r.String()), nil 66 } 67 68 func (r *Role) UnmarshalText(text []byte) error { 69 var err error 70 *r, err = ParseRole(string(text)) 71 return err 72 } 73 74 func Roles() RoleList { 75 return []Role{RoleCollection, RoleConsensus, RoleExecution, RoleVerification, RoleAccess} 76 } 77 78 // RoleList defines a slice of roles in flow system. 79 type RoleList []Role 80 81 // Contains returns true if RoleList contains the role, otherwise false. 82 func (r RoleList) Contains(role Role) bool { 83 for _, each := range r { 84 if each == role { 85 return true 86 } 87 } 88 return false 89 } 90 91 // Union returns a new role list containing every role that occurs in 92 // either `r`, or `other`, or both. There are no duplicate roles in the output, 93 func (r RoleList) Union(other RoleList) RoleList { 94 // stores the output, the union of the two lists 95 union := make(RoleList, 0, len(r)+len(other)) 96 97 // efficient lookup to avoid duplicates 98 added := make(map[Role]struct{}) 99 100 // adds all roles, skips duplicates 101 for _, role := range append(r, other...) { 102 if _, exists := added[role]; exists { 103 continue 104 } 105 union = append(union, role) 106 added[role] = struct{}{} 107 } 108 109 return union 110 } 111 112 // Len returns length of the RoleList in the number of stored roles. 113 // It satisfies the sort.Interface making the RoleList sortable. 114 func (r RoleList) Len() int { 115 return len(r) 116 } 117 118 // Less returns true if element i in the RoleList is less than j based on the numerical value of its role. 119 // Otherwise it returns true. 120 // It satisfies the sort.Interface making the RoleList sortable. 121 func (r RoleList) Less(i, j int) bool { 122 return r[i] < r[j] 123 } 124 125 // Swap swaps the element i and j in the RoleList. 126 // It satisfies the sort.Interface making the RoleList sortable. 127 func (r RoleList) Swap(i, j int) { 128 r[i], r[j] = r[j], r[i] 129 } 130 131 // ID returns hash of the content of RoleList. It first sorts the RoleList and then takes its 132 // hash value. 133 func (r RoleList) ID() Identifier { 134 sort.Sort(r) 135 return MakeID(r) 136 }