-
Notifications
You must be signed in to change notification settings - Fork 210
/
state.go
367 lines (321 loc) · 8.38 KB
/
state.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package tortoise
import (
"fmt"
"math/big"
"sort"
"github.com/spacemeshos/fixed"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/tortoise/opinionhash"
)
type (
weight = fixed.Fixed
verifyingInfo struct {
// goodUncounted is a weight that doesn't vote for this layer
//
// for example ballot created in layer 10 doesn't vote for layers
// 10 and above, therefore its weight needs to be added to goodUncounted
// for layers 10 and above
goodUncounted weight
referenceHeight uint64
}
epochInfo struct {
atxs map[types.ATXID]uint64
// weight is a sum of all atxs
weight weight
// median height from atxs
height uint64
}
blockInfo struct {
id types.BlockID
layer types.LayerID
height uint64
hare sign
margin weight
validity sign
// emitted validity
emitted sign
}
state struct {
// last received layer
// TODO should be last layer according to the clock
// https://github.com/spacemeshos/go-spacemesh/issues/2921
last types.LayerID
// localThreshold is updated together with the last layer.
localThreshold weight
// last verified layer
verified types.LayerID
// last processed layer
processed types.LayerID
// last evicted layer
evicted types.LayerID
changedOpinion struct {
// sector of layers where opinion is different from previously computed opinion
min, max types.LayerID
}
epochs map[types.EpochID]*epochInfo
layers map[types.LayerID]*layerInfo
// ballots should not be referenced by other ballots
// each ballot stores references (votes) for X previous layers
// those X layers may reference another set of ballots that will
// reference recursively more layers with another set of ballots
ballots map[types.LayerID][]*ballotInfo
// to efficiently find base and reference ballots
ballotRefs map[types.BallotID]*ballotInfo
// to efficiently decode exceptions
blockRefs map[types.BlockID]*blockInfo
}
)
func newState() *state {
return &state{
epochs: map[types.EpochID]*epochInfo{},
layers: map[types.LayerID]*layerInfo{},
ballots: map[types.LayerID][]*ballotInfo{},
ballotRefs: map[types.BallotID]*ballotInfo{},
blockRefs: map[types.BlockID]*blockInfo{},
}
}
func (s *state) globalThreshold(cfg Config, target types.LayerID) weight {
return computeGlobalThreshold(cfg, s.localThreshold, s.epochs, target, s.processed, s.last)
}
func (s *state) expectedWeight(cfg Config, target types.LayerID) weight {
return computeExpectedWeightInWindow(cfg, s.epochs, target, s.processed, s.last)
}
func (s *state) layer(lid types.LayerID) *layerInfo {
layer, exist := s.layers[lid]
if !exist {
layersNumber.Inc()
layer = &layerInfo{lid: lid}
s.layers[lid] = layer
}
return layer
}
func (s *state) epoch(eid types.EpochID) *epochInfo {
epoch, exist := s.epochs[eid]
if !exist {
epochsNumber.Inc()
epoch = &epochInfo{atxs: map[types.ATXID]uint64{}}
s.epochs[eid] = epoch
}
return epoch
}
func (s *state) addBallot(ballot *ballotInfo) {
ballotsNumber.Inc()
s.ballots[ballot.layer] = append(s.ballots[ballot.layer], ballot)
s.ballotRefs[ballot.id] = ballot
}
func (s *state) addBlock(block *blockInfo) {
blocksNumber.Inc()
layer := s.layer(block.layer)
layer.blocks = append(layer.blocks, block)
sortBlocks(layer.blocks)
s.blockRefs[block.id] = block
s.updateRefHeight(layer, block)
}
func (s *state) findRefHeightBelow(lid types.LayerID) uint64 {
for lid = lid.Sub(1); lid.After(s.evicted); lid = lid.Sub(1) {
layer := s.layer(lid)
if len(layer.blocks) == 0 {
continue
}
return layer.verifying.referenceHeight
}
return 0
}
func (s *state) updateRefHeight(layer *layerInfo, block *blockInfo) error {
epoch, exist := s.epochs[block.layer.GetEpoch()]
if !exist {
return fmt.Errorf("reference height for epoch %v wasn't computed", block.layer.GetEpoch())
}
if layer.verifying.referenceHeight == 0 && layer.lid.After(s.evicted) {
layer.verifying.referenceHeight = s.findRefHeightBelow(layer.lid)
}
if block.height <= epoch.height &&
block.height > layer.verifying.referenceHeight {
layer.verifying.referenceHeight = block.height
}
return nil
}
type layerInfo struct {
lid types.LayerID
empty weight
hareTerminated bool
blocks []*blockInfo
verifying verifyingInfo
opinion types.Hash32
// a pointer to the value stored on the previous layerInfo object
// it is stored as a pointer so that when previous layerInfo is evicted
// we still have access in case we need to recompute opinion for this layer
prevOpinion *types.Hash32
}
func (l *layerInfo) computeOpinion(hdist uint32, last types.LayerID) {
hasher := opinionhash.New()
if l.prevOpinion != nil {
hasher.WritePrevious(*l.prevOpinion)
}
if !l.hareTerminated {
hasher.WriteAbstain()
} else if withinDistance(hdist, l.lid, last) {
for _, block := range l.blocks {
if block.hare == support {
hasher.WriteSupport(block.id, block.height)
}
}
} else {
for _, block := range l.blocks {
if block.validity == support {
hasher.WriteSupport(block.id, block.height)
}
}
}
hasher.Sum(l.opinion[:0])
}
type (
baseInfo struct {
id types.BallotID
layer types.LayerID
}
conditions struct {
// set after comparing with local beacon
badBeacon bool
}
referenceInfo struct {
weight *big.Rat
height uint64
beacon types.Beacon
}
ballotInfo struct {
id types.BallotID
layer types.LayerID
base baseInfo
malicious bool
weight weight
reference *referenceInfo
votes votes
conditions conditions
}
)
func (b *ballotInfo) opinion() types.Hash32 {
return b.votes.opinion()
}
type votes struct {
tail *layerVote
}
func (v *votes) append(lv *layerVote) {
if v.tail == nil {
v.tail = lv
} else {
if v.tail.lid.Add(1) != lv.lid {
panic("bug: added vote with a gap")
}
v.tail = v.tail.append(lv)
}
v.tail.sortSupported()
v.tail.computeOpinion()
}
func (v *votes) update(from types.LayerID, diff map[types.LayerID]map[types.BlockID]sign) votes {
if v.tail == nil {
return votes{}
}
return votes{tail: v.tail.update(from, diff)}
}
// cutBefore cuts all pointers to votes before the specified layer.
func (v *votes) cutBefore(lid types.LayerID) {
for current := v.tail; current != nil; current = current.prev {
prev := current.prev
if prev != nil && prev.lid.Before(lid) {
current.prev = nil
return
}
if current.lid.Before(lid) {
v.tail = nil
return
}
}
}
func (v *votes) opinion() types.Hash32 {
if v.tail == nil {
return types.Hash32{}
}
return v.tail.opinion
}
type layerVote struct {
*layerInfo
opinion types.Hash32
vote sign
supported []*blockInfo
prev *layerVote
}
func (l *layerVote) getVote(bid types.BlockID) sign {
for _, block := range l.supported {
if block.id == bid {
return support
}
}
return against
}
func (l *layerVote) copy() *layerVote {
return &layerVote{
layerInfo: l.layerInfo,
vote: l.vote,
supported: l.supported,
prev: l.prev,
}
}
func (l *layerVote) append(lv *layerVote) *layerVote {
lv.prev = l
return lv
}
func (l *layerVote) update(from types.LayerID, diff map[types.LayerID]map[types.BlockID]sign) *layerVote {
if l.lid.Before(from) {
return l
}
copied := l.copy()
if copied.prev != nil {
copied.prev = copied.prev.update(from, diff)
}
layerdiff, exist := diff[copied.lid]
if exist && len(layerdiff) == 0 {
copied.vote = abstain
copied.supported = nil
} else if exist && len(layerdiff) > 0 {
var supported []*blockInfo
for _, block := range copied.blocks {
vote, exist := layerdiff[block.id]
if exist && vote == against {
continue
}
if (exist && vote == support) || l.getVote(block.id) == support {
supported = append(supported, block)
}
}
copied.supported = supported
copied.sortSupported()
}
copied.computeOpinion()
return copied
}
func (l *layerVote) sortSupported() {
sortBlocks(l.supported)
}
func (l *layerVote) computeOpinion() {
hasher := opinionhash.New()
if l.prev != nil {
hasher.WritePrevious(l.prev.opinion)
}
if len(l.supported) > 0 {
for _, block := range l.supported {
hasher.WriteSupport(block.id, block.height)
}
} else if l.vote == abstain {
hasher.WriteAbstain()
}
hasher.Sum(l.opinion[:0])
}
func sortBlocks(blocks []*blockInfo) {
sort.Slice(blocks, func(i, j int) bool {
if blocks[i].height != blocks[j].height {
return blocks[i].height < blocks[j].height
}
return blocks[i].id.Compare(blocks[j].id)
})
}