-
Notifications
You must be signed in to change notification settings - Fork 211
/
haretypes.go
299 lines (240 loc) · 5.72 KB
/
haretypes.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package hare
import (
"bytes"
"sort"
"strings"
"sync"
"golang.org/x/exp/maps"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/hare/eligibility"
"github.com/spacemeshos/go-spacemesh/hash"
)
// MessageType is a message type.
type MessageType byte
// declare all known message types.
const (
status MessageType = 0
proposal MessageType = 1
commit MessageType = 2
notify MessageType = 3
pre MessageType = 10
)
const (
preRound = eligibility.HarePreRound
statusRound = eligibility.HareStatusRound
proposalRound = eligibility.HareProposalRound
commitRound = eligibility.HareCommitRound
notifyRound = eligibility.HareNotifyRound
)
const defaultSetSize = 200
func (mType MessageType) String() string {
switch mType {
case status:
return "status"
case proposal:
return "proposal"
case commit:
return "commit"
case notify:
return "notify"
case pre:
return "preround"
default:
return "Unknown message type"
}
}
// Set represents a unique set of values.
type Set struct {
mu sync.RWMutex
values map[types.ProposalID]struct{}
sid types.Hash32
}
// NewDefaultEmptySet creates an empty set with the default size.
func NewDefaultEmptySet() *Set {
return NewEmptySet(defaultSetSize)
}
// NewEmptySet creates an empty set with the provided size.
func NewEmptySet(size int) *Set {
return &Set{values: make(map[types.ProposalID]struct{}, size)}
}
// NewSetFromValues creates a set of the provided values.
// Note: duplicated values are ignored.
func NewSetFromValues(values ...types.ProposalID) *Set {
s := &Set{values: make(map[types.ProposalID]struct{}, len(values))}
for _, v := range values {
s.values[v] = struct{}{}
}
return s
}
// NewSet creates a set from the provided array of values.
// Note: duplicated values are ignored.
func NewSet(data []types.ProposalID) *Set {
s := &Set{values: make(map[types.ProposalID]struct{}, len(data))}
for _, v := range data {
s.values[v] = struct{}{}
}
return s
}
// Clone creates a copy of the set.
func (s *Set) Clone() *Set {
s.mu.RLock()
defer s.mu.RUnlock()
clone := NewEmptySet(len(s.values))
for v := range s.values {
clone.values[v] = struct{}{}
}
return clone
}
// Contains returns true if the provided value is contained in the set, false otherwise.
func (s *Set) Contains(id types.ProposalID) bool {
s.mu.RLock()
defer s.mu.RUnlock()
_, ok := s.values[id]
return ok
}
// Add a value to the set.
// It has no effect if the value already exists in the set.
func (s *Set) Add(id types.ProposalID) {
s.mu.Lock()
defer s.mu.Unlock()
if _, exist := s.values[id]; exist {
return
}
s.sid = types.Hash32{}
s.values[id] = struct{}{}
}
// Remove a value from the set.
// It has no effect if the value doesn't exist in the set.
func (s *Set) Remove(id types.ProposalID) {
s.mu.Lock()
defer s.mu.Unlock()
if _, exist := s.values[id]; !exist {
return
}
s.sid = types.Hash32{}
delete(s.values, id)
}
// Equals returns true if the provided set represents this set, false otherwise.
func (s *Set) Equals(g *Set) bool {
s.mu.RLock()
defer s.mu.RUnlock()
g.mu.RLock()
defer g.mu.RUnlock()
if len(s.values) != len(g.values) {
return false
}
for v := range s.values {
if _, exist := g.values[v]; !exist {
return false
}
}
return true
}
// ToSlice returns the array representation of the set.
func (s *Set) ToSlice() []types.ProposalID {
s.mu.RLock()
defer s.mu.RUnlock()
// order keys
return s.sortedLocked()
}
// ID returns the ObjectID of the set.
func (s *Set) ID() types.Hash32 {
s.mu.Lock()
defer s.mu.Unlock()
if s.sid != (types.Hash32{}) {
return s.sid
}
// order keys
keys := s.sortedLocked()
// calc
h := hash.New()
for i := 0; i < len(keys); i++ {
h.Write(keys[i].Bytes())
}
// update
s.sid = types.BytesToHash(h.Sum([]byte{}))
return s.sid
}
func (s *Set) sortedLocked() []types.ProposalID {
result := maps.Keys(s.values)
sort.Slice(result, func(i, j int) bool { return bytes.Compare(result[i].Bytes(), result[j].Bytes()) == -1 })
return result
}
func (s *Set) String() string {
s.mu.RLock()
defer s.mu.RUnlock()
var sb strings.Builder
idx := len(s.values)
for v := range s.values {
idx--
sb.WriteString(v.String())
if idx > 0 {
sb.WriteString(",")
}
}
return sb.String()
}
// IsSubSetOf returns true if s is a subset of g, false otherwise.
func (s *Set) IsSubSetOf(g *Set) bool {
s.mu.RLock()
defer s.mu.RUnlock()
for v := range s.values {
if !g.Contains(v) {
return false
}
}
return true
}
// Intersection returns the intersection a new set which represents the intersection of s and g.
func (s *Set) Intersection(g *Set) *Set {
s.mu.RLock()
defer s.mu.RUnlock()
both := NewEmptySet(len(s.values))
for v := range s.values {
if g.Contains(v) {
both.Add(v)
}
}
return both
}
// Union returns a new set which represents the union set of s and g.
func (s *Set) Union(g *Set) *Set {
s.mu.RLock()
defer s.mu.RUnlock()
g.mu.RLock()
defer g.mu.RUnlock()
union := NewEmptySet(len(s.values) + len(g.values))
for v := range s.values {
union.values[v] = struct{}{}
}
for v := range g.values {
union.values[v] = struct{}{}
}
return union
}
// Complement returns a new set that represents the complement of s relatively to the world u.
func (s *Set) Complement(u *Set) *Set {
u.mu.RLock()
defer u.mu.RUnlock()
comp := NewEmptySet(len(u.values))
for v := range u.values {
if !s.Contains(v) {
comp.values[v] = struct{}{}
}
}
return comp
}
// Subtract g from s.
func (s *Set) Subtract(g *Set) {
g.mu.RLock()
defer g.mu.RUnlock()
for v := range g.values {
s.Remove(v)
}
}
// Size returns the number of SortedElements in the set.
func (s *Set) Size() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.values)
}