-
Notifications
You must be signed in to change notification settings - Fork 212
/
state.go
536 lines (477 loc) · 12.8 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package tortoise
import (
"fmt"
"math/big"
"sort"
"github.com/spacemeshos/fixed"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"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 {
// weight is a sum of all atxs
weight weight
// median height from atxs
height uint64
beacon *types.Beacon
}
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
atxsdata *atxsdata.Data
epochs map[types.EpochID]*epochInfo
layers layerSlice
// 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
// malnodes is a collection with all nodes that equivocated in history.
// each node id is 32 bytes. 100 000 of such nodes is only about ~3MB
malnodes map[types.NodeID]struct{}
}
)
func newState(atxdata *atxsdata.Data) *state {
return &state{
atxsdata: atxdata,
epochs: map[types.EpochID]*epochInfo{},
ballots: map[types.LayerID][]*ballotInfo{},
ballotRefs: map[types.BallotID]*ballotInfo{},
malnodes: map[types.NodeID]struct{}{},
}
}
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 {
return s.layers.get(s.evicted, lid)
}
func (s *state) epoch(eid types.EpochID) *epochInfo {
epoch, exist := s.epochs[eid]
if !exist {
epochsNumber.Inc()
epoch = &epochInfo{}
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)
if layer.hareTerminated {
block.hare = against
}
layer.blocks = append(layer.blocks, block)
sortBlocks(layer.blocks)
s.updateRefHeight(layer, block)
}
func (s *state) getBlock(header types.Vote) *blockInfo {
layer := s.layer(header.LayerID)
for _, block := range layer.blocks {
if block.id == header.ID && block.height == header.Height {
return block
}
}
return nil
}
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) {
epoch, exist := s.epochs[block.layer.GetEpoch()]
if !exist {
return
}
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
}
}
func (s *state) isMalfeasant(id types.NodeID) bool {
_, exists := s.malnodes[id]
return exists
}
func (s *state) markMalfeasant(id types.NodeID) {
s.malnodes[id] = struct{}{}
}
type layerInfo struct {
lid types.LayerID
empty weight
hareTerminated bool
blocks []*blockInfo
verifying verifyingInfo
coinflip sign
// unique opinions recorded from the ballots in this layer.
// ballot votes an opinion and encodes sidecar
opinions map[types.Hash32]votes
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 {
smesher types.NodeID
atxid types.ATXID
expectedBallots uint32
beacon types.Beacon
weight *big.Rat
height uint64
}
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()
}
func (b *ballotInfo) overwriteOpinion(opinion types.Hash32) {
b.votes.tail.opinion = 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]headerWithSign) (votes, error) {
if v.tail == nil {
return votes{}, nil
}
tail, err := v.tail.update(from, diff)
if err != nil {
return votes{}, err
}
return votes{tail: tail}, nil
}
// 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 {
lid types.LayerID
opinion types.Hash32
vote sign
supported []*blockInfo
prev *layerVote
}
func (l *layerVote) getVote(binfo *blockInfo) sign {
for _, block := range l.supported {
if block == binfo {
return support
}
}
return against
}
func (l *layerVote) copy() *layerVote {
return &layerVote{
lid: l.lid,
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]headerWithSign,
) (*layerVote, error) {
if l.lid.Before(from) {
return l, nil
}
copied := l.copy()
if copied.prev != nil {
prev, err := copied.prev.update(from, diff)
if err != nil {
return nil, err
}
copied.prev = prev
}
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 l.supported {
vote, exist := layerdiff[block.id]
if !exist {
supported = append(supported, block)
} else if vote.sign == against && vote.header != block.header() {
return nil, fmt.Errorf("wrong target. last supported is (%s/%d), but is against (%s/%d)",
block.id, block.height, vote.header.ID, vote.header.Height)
}
}
for _, vote := range layerdiff {
if vote.sign == against {
continue
}
supported = append(supported, newBlockInfo(vote.header))
}
copied.supported = supported
copied.sortSupported()
}
copied.computeOpinion()
return copied, nil
}
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)
})
}
func newBlockInfo(header types.Vote) *blockInfo {
return &blockInfo{
id: header.ID,
layer: header.LayerID,
height: header.Height,
hare: neutral,
}
}
type blockInfo struct {
id types.BlockID
layer types.LayerID
height uint64
hare sign
margin weight
validity sign
data bool // set to true if block is available locally
}
func (b *blockInfo) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddString("block", b.id.String())
encoder.AddUint32("layer", b.layer.Uint32())
encoder.AddUint64("height", b.height)
return nil
}
func (b *blockInfo) header() types.Vote {
return types.Vote{ID: b.id, LayerID: b.layer, Height: b.height}
}
type headerWithSign struct {
header types.Vote
sign sign
}
func decodeVotes(evicted, blid types.LayerID, base *ballotInfo, exceptions types.Votes) (votes, types.LayerID, error) {
from := base.layer
diff := map[types.LayerID]map[types.BlockID]headerWithSign{}
for _, header := range exceptions.Against {
from = min(from, header.LayerID)
layerdiff, exist := diff[header.LayerID]
if !exist {
layerdiff = map[types.BlockID]headerWithSign{}
diff[header.LayerID] = layerdiff
}
existing, exist := layerdiff[header.ID]
if exist {
return votes{}, 0, fmt.Errorf(
"conflicting votes on the same id %v with different heights %d conflict with %d",
existing.header.ID,
existing.header.Height,
header.Height,
)
}
layerdiff[header.ID] = headerWithSign{header, against}
}
for _, header := range exceptions.Support {
from = min(from, header.LayerID)
layerdiff, exist := diff[header.LayerID]
if !exist {
layerdiff = map[types.BlockID]headerWithSign{}
diff[header.LayerID] = layerdiff
}
existing, exist := layerdiff[header.ID]
if exist {
return votes{}, 0, fmt.Errorf(
"conflicting votes on the same id %v with different heights %d conflict with %d",
existing.header.ID,
existing.header.Height,
header.Height,
)
}
layerdiff[header.ID] = headerWithSign{header, support}
}
for _, lid := range exceptions.Abstain {
from = min(from, lid)
_, exist := diff[lid]
if !exist {
diff[lid] = map[types.BlockID]headerWithSign{}
} else {
return votes{}, 0, fmt.Errorf("votes on layer %d conflict with abstain", lid)
}
}
// FIXME(dshulyak) this needs to be ignored when recovering from disk
// if from <= evicted {
// return votes{}, 0, fmt.Errorf("votes for a block in the layer (%d) outside the window (evicted %d)",
// from, evicted,
// )
// }
// inherit opinion from the base ballot by copying votes
decoded, err := base.votes.update(from, diff)
if err != nil {
return votes{}, 0, err
}
// add new opinions after the base layer
for lid := base.layer; lid.Before(blid); lid = lid.Add(1) {
lvote := layerVote{
lid: lid,
vote: against,
}
layerdiff, exist := diff[lid]
if exist && len(layerdiff) == 0 {
lvote.vote = abstain
} else if exist && len(layerdiff) > 0 {
for _, vote := range layerdiff {
if vote.sign != support {
continue
}
lvote.supported = append(lvote.supported, newBlockInfo(vote.header))
}
}
decoded.append(&lvote)
}
return decoded, from, nil
}
type layerSlice struct {
data []*layerInfo
}
func (s *layerSlice) get(offset, index types.LayerID) *layerInfo {
i := index - offset - 1
lth := types.LayerID(len(s.data))
if i < lth {
return s.data[i]
}
last := offset + lth
for lid := last + 1; lid <= index; lid++ {
s.data = append(s.data, &layerInfo{lid: lid, opinions: map[types.Hash32]votes{}})
}
return s.data[i]
}
func (s *layerSlice) pop() {
s.data = s.data[1:]
}