/
ninja_tortoise.go
510 lines (447 loc) · 15.9 KB
/
ninja_tortoise.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
package consensus
import (
"container/list"
"fmt"
"github.com/spacemeshos/go-spacemesh/common"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/mesh"
"hash/fnv"
"math"
"sort"
)
type vec [2]int
type PatternId uint32
type LayerId mesh.LayerID
const ( //Threshold
K = 5 //number of explicit layers to vote for
Window = 100
LocalThreshold = 0.8 //ThetaL
GlobalThreshold = 0.6 //ThetaG
Genesis = 0
)
var ( //correction vectors type
//Opinion
Support = vec{1, 0}
Against = vec{0, 1}
Abstain = vec{0, 0}
)
func Max(i mesh.LayerID, j mesh.LayerID) mesh.LayerID {
if i > j {
return i
}
return j
}
func (a vec) Add(v vec) vec {
return vec{a[0] + v[0], a[1] + v[1]}
}
func (a vec) Negate() vec {
a[0] = a[0] * -1
a[1] = a[1] * -1
return a
}
func (a vec) Multiply(x int) vec {
a[0] = a[0] * x
a[1] = a[1] * x
return a
}
type votingPattern struct {
id PatternId //cant put a slice here wont work well with maps, we need to hash the blockids
mesh.LayerID
}
func (vp votingPattern) Layer() mesh.LayerID {
return vp.LayerID
}
//todo memory optimizations
type ninjaTortoise struct {
log.Log
avgLayerSize uint32
pBase votingPattern
blocks map[mesh.BlockID]*mesh.Block //block cache
tEffective map[mesh.BlockID]votingPattern //Explicit voting pattern of latest layer for a block
tCorrect map[mesh.BlockID]map[mesh.BlockID]vec //correction vectors
tExplicit map[mesh.BlockID]map[mesh.LayerID]votingPattern //explict votes from block to layer pattern
layerBlocks map[mesh.LayerID][]mesh.BlockID //block ids in each layer
tGood map[mesh.LayerID]votingPattern //good pattern for layer i
tSupport map[votingPattern]int //for pattern p the number of blocks that support p
tComplete map[votingPattern]struct{} //complete voting patterns
tEffectiveToBlocks map[votingPattern][]mesh.BlockID //inverse blocks effective pattern
tVote map[votingPattern]map[mesh.BlockID]vec //global opinion
tTally map[votingPattern]map[mesh.BlockID]vec //for pattern p and block b count votes for b according to p
tPattern map[votingPattern]map[mesh.BlockID]struct{} //set of blocks that comprise pattern p
tPatSupport map[votingPattern]map[mesh.LayerID]votingPattern //pattern support count
}
func NewNinjaTortoise(layerSize uint32, log log.Log) *ninjaTortoise {
return &ninjaTortoise{
Log: log,
avgLayerSize: layerSize,
pBase: votingPattern{},
blocks: map[mesh.BlockID]*mesh.Block{},
tEffective: map[mesh.BlockID]votingPattern{},
tCorrect: map[mesh.BlockID]map[mesh.BlockID]vec{},
layerBlocks: map[mesh.LayerID][]mesh.BlockID{},
tExplicit: map[mesh.BlockID]map[mesh.LayerID]votingPattern{},
tGood: map[mesh.LayerID]votingPattern{},
tSupport: map[votingPattern]int{},
tPattern: map[votingPattern]map[mesh.BlockID]struct{}{},
tVote: map[votingPattern]map[mesh.BlockID]vec{},
tTally: map[votingPattern]map[mesh.BlockID]vec{},
tComplete: map[votingPattern]struct{}{},
tEffectiveToBlocks: map[votingPattern][]mesh.BlockID{},
tPatSupport: map[votingPattern]map[mesh.LayerID]votingPattern{},
}
}
func (ni *ninjaTortoise) processBlock(b *mesh.Block) {
ni.Debug("process block: %d layer: %d ", b.Id, b.Layer())
if b.Layer() == Genesis {
return
}
patternMap := make(map[mesh.LayerID]map[mesh.BlockID]struct{})
for _, bid := range b.BlockVotes {
ni.Debug("block votes %d", bid)
bl, found := ni.blocks[bid]
if !found {
panic(fmt.Sprintf("error block not found ID %d", bid))
}
if _, found := patternMap[bl.Layer()]; !found {
patternMap[bl.Layer()] = map[mesh.BlockID]struct{}{}
}
patternMap[bl.Layer()][bl.ID()] = struct{}{}
}
var effective votingPattern
ni.tExplicit[b.ID()] = make(map[mesh.LayerID]votingPattern, K)
for layerId, v := range patternMap {
vp := votingPattern{id: getIdsFromSet(v), LayerID: layerId}
ni.tPattern[vp] = v
ni.tExplicit[b.ID()][layerId] = vp
if layerId >= effective.Layer() {
effective = vp
}
}
ni.tEffective[b.ID()] = effective
v, found := ni.tEffectiveToBlocks[effective]
if !found {
v = make([]mesh.BlockID, 0, ni.avgLayerSize)
}
var pattern []mesh.BlockID = nil
pattern = append(v, b.ID())
ni.tEffectiveToBlocks[effective] = pattern
ni.Debug("effective pattern to blocks %d %d", effective, pattern)
return
}
func getId(bids []mesh.BlockID) PatternId {
sort.Slice(bids, func(i, j int) bool { return bids[i] < bids[j] })
// calc
h := fnv.New32()
for i := 0; i < len(bids); i++ {
h.Write(common.Uint32ToBytes(uint32(bids[i])))
}
// update
sum := h.Sum32()
return PatternId(sum)
}
func getIdsFromSet(bids map[mesh.BlockID]struct{}) PatternId {
keys := make([]mesh.BlockID, 0, len(bids))
for k := range bids {
keys = append(keys, k)
}
return getId(keys)
}
func forBlockInView(blocks map[mesh.BlockID]struct{}, blockCache map[mesh.BlockID]*mesh.Block, layer mesh.LayerID, foo func(block *mesh.Block)) {
stack := list.New()
for b := range blocks {
stack.PushFront(b)
}
set := make(map[mesh.BlockID]struct{})
for b := stack.Front(); b != nil; b = stack.Front() {
a := stack.Remove(stack.Front()).(mesh.BlockID)
block, found := blockCache[a]
if !found {
panic(fmt.Sprintf("error block not found ID %d", block.ID()))
}
foo(block)
//push children to bfs queue
for _, bChild := range block.ViewEdges {
if blockCache[bChild].Layer() >= layer { //dont traverse too deep
if _, found := set[bChild]; !found {
set[bChild] = struct{}{}
stack.PushBack(bChild)
}
}
}
}
return
}
func globalOpinion(v vec, layerSize uint32, delta float64) vec {
threshold := float64(GlobalThreshold*delta) * float64(layerSize)
if float64(v[0]) > threshold {
return Support
} else if float64(v[1]) > threshold {
return Against
} else {
return Abstain
}
}
func (ni *ninjaTortoise) updateCorrectionVectors(p votingPattern, bottomOfWindow mesh.LayerID) {
foo := func(x *mesh.Block) {
for _, bid := range ni.tEffectiveToBlocks[p] { //for all b who's effective vote is p
b := ni.blocks[bid]
if _, found := ni.tExplicit[b.Id][x.Layer()]; found { //if Texplicit[b][x]!=0 check correctness of x.layer and found
ni.Debug(" blocks pattern %d block %d layer %d", p, b.ID(), b.Layer())
if _, found := ni.tCorrect[b.Id]; !found {
ni.tCorrect[b.Id] = make(map[mesh.BlockID]vec)
}
vo := ni.tVote[p][x.ID()]
ni.Debug("vote from pattern %d to block %d layer %d vote %d ", p, x.ID(), x.Layer(), vo)
ni.tCorrect[b.Id][x.ID()] = vo.Negate() //Tcorrect[b][x] = -Tvote[p][x]
ni.Debug("update correction vector for block %d layer %d , pattern %d vote %d for block %d ", b.ID(), b.Layer(), p, ni.tCorrect[b.Id][x.ID()], x.ID())
} else {
ni.Debug("block %d from layer %d dose'nt explicitly vote for layer %d", b.ID(), b.Layer(), x.Layer())
}
}
}
forBlockInView(ni.tPattern[p], ni.blocks, bottomOfWindow, foo)
}
func (ni *ninjaTortoise) updatePatternTally(newMinGood votingPattern, botomOfWindow mesh.LayerID, correctionMap map[mesh.BlockID]vec, effCountMap map[mesh.LayerID]int) {
ni.Debug("update tally pbase id:%d layer:%d p id:%d layer:%d", ni.pBase.id, ni.pBase.Layer(), newMinGood.id, newMinGood.Layer())
for idx, effc := range effCountMap {
g := ni.tGood[idx]
for b, v := range ni.tVote[g] {
tally := ni.tTally[newMinGood][b]
tally = tally.Add(v.Multiply(effc))
if count, found := correctionMap[b]; found {
tally = tally.Add(count)
} else {
ni.Debug("no correction vectors for %", g)
}
ni.Debug("tally for pattern %d and block %d is %d", newMinGood.id, b, tally)
ni.tTally[newMinGood][b] = tally //in g's view -> in p's view
}
}
}
func (ni *ninjaTortoise) getCorrEffCounter() (map[mesh.BlockID]vec, map[mesh.LayerID]int, func(b *mesh.Block)) {
correctionMap := make(map[mesh.BlockID]vec)
effCountMap := make(map[mesh.LayerID]int)
foo := func(b *mesh.Block) {
if b.Layer() > ni.pBase.Layer() { //because we already copied pbase's votes
if eff, found := ni.tEffective[b.ID()]; found {
if p, found := ni.tGood[eff.Layer()]; found && eff == p {
effCountMap[eff.Layer()] = effCountMap[eff.Layer()] + 1
for k, v := range ni.tCorrect[b.ID()] {
correctionMap[k] = correctionMap[k].Add(v)
}
}
}
}
}
return correctionMap, effCountMap, foo
}
//for all layers from pBase to i add b's votes, mark good layers
// return new minimal good layer
func (ni *ninjaTortoise) findMinimalNewlyGoodLayer(layer *mesh.Layer) mesh.LayerID {
minGood := mesh.LayerID(math.MaxUint32)
var j mesh.LayerID
if Window > layer.Index() {
j = ni.pBase.Layer() + 1
} else {
j = Max(ni.pBase.Layer()+1, layer.Index()-Window+1)
}
for ; j < layer.Index(); j++ {
// update block votes on all patterns in blocks view
sUpdated := ni.updateBlocksSupport(layer.Blocks(), j)
//todo do this as part of previous for if possible
//for each p that was updated and not the good layer of j check if it is the good layer
for p := range sUpdated {
//if a majority supports p (p is good)
//according to tal we dont have to know the exact amount, we can multiply layer size by number of layers
jGood, found := ni.tGood[j]
threshold := 0.5 * float64(mesh.LayerID(ni.avgLayerSize)*(layer.Index()-p.Layer()))
if (jGood != p || !found) && float64(ni.tSupport[p]) > threshold {
ni.tGood[p.Layer()] = p
//if p is the new minimal good layer
if p.Layer() < minGood {
minGood = p.Layer()
}
}
}
}
ni.Debug("found minimal good layer %d", minGood)
return minGood
}
//update block support for pattern in layer j
func (ni *ninjaTortoise) updateBlocksSupport(b []*mesh.Block, j mesh.LayerID) map[votingPattern]struct{} {
sUpdated := map[votingPattern]struct{}{}
for _, block := range b {
//check if block votes for layer j explicitly or implicitly
p, found := ni.tExplicit[block.ID()][j]
if found {
//explicit
ni.tSupport[p]++ //add to supporting patterns
sUpdated[p] = struct{}{} //add to updated patterns
//implicit
} else if eff, effFound := ni.tEffective[block.ID()]; effFound {
p, found = ni.tPatSupport[eff][j]
if found {
ni.tSupport[p]++ //add to supporting patterns
sUpdated[p] = struct{}{} //add to updated patterns
}
}
}
return sUpdated
}
func (ni *ninjaTortoise) addPatternVote(p votingPattern, view map[mesh.BlockID]struct{}) func(b mesh.BlockID) {
addPatternVote := func(b mesh.BlockID) {
var vp map[mesh.LayerID]votingPattern
var found bool
bl := ni.blocks[b]
if bl.Layer() <= ni.pBase.Layer() {
return
}
if vp, found = ni.tExplicit[b]; !found {
panic(fmt.Sprintf("block %d has no explicit voting, something went wrong ", b))
}
for _, ex := range vp {
for _, bl := range ni.layerBlocks[ex.Layer()] {
if _, found := ni.tPattern[ex][bl]; found {
ni.tTally[p][bl] = ni.tTally[p][bl].Add(Support)
} else if _, inSet := view[bl]; inSet { //in view but not in pattern
ni.tTally[p][bl] = ni.tTally[p][bl].Add(Against)
}
}
}
}
return addPatternVote
}
func sumNodesInView(layerBlockCounter map[mesh.LayerID]int, layer mesh.LayerID, pLayer mesh.LayerID) vec {
var sum int
for sum = 0; layer <= pLayer; layer++ {
sum = sum + layerBlockCounter[layer]
}
return Against.Multiply(sum)
}
func (ni *ninjaTortoise) processBlocks(layer *mesh.Layer) {
for _, block := range layer.Blocks() {
ni.processBlock(block)
ni.blocks[block.ID()] = block
ni.layerBlocks[layer.Index()] = append(ni.layerBlocks[layer.Index()], block.ID())
}
}
func (ni *ninjaTortoise) handleGenesis(genesis *mesh.Layer) {
vp := votingPattern{id: getId(ni.layerBlocks[Genesis]), LayerID: Genesis}
ni.pBase = vp
ni.tGood[Genesis] = vp
ni.tExplicit[genesis.Blocks()[0].ID()] = make(map[mesh.LayerID]votingPattern, K*ni.avgLayerSize)
}
//todo send map instead of ni
func updatePatSupport(ni *ninjaTortoise, p votingPattern, bids []mesh.BlockID, idx mesh.LayerID) {
if val, found := ni.tPatSupport[p]; !found || val == nil {
ni.tPatSupport[p] = make(map[mesh.LayerID]votingPattern)
}
pid := getId(bids)
ni.Debug("update support for %d layer %d supported pattern %d", p, idx, pid)
ni.tPatSupport[p][idx] = votingPattern{id: pid, LayerID: idx}
}
func initTallyToBase(tally map[votingPattern]map[mesh.BlockID]vec, base votingPattern, p votingPattern) {
if _, found := tally[p]; !found {
tally[p] = make(map[mesh.BlockID]vec)
}
for k, v := range tally[base] {
tally[p][k] = v
}
}
func (ni *ninjaTortoise) latestComplete() mesh.LayerID {
return ni.pBase.Layer()
}
func (ni *ninjaTortoise) getVotes() map[mesh.BlockID]vec {
return ni.tVote[ni.pBase]
}
func (ni *ninjaTortoise) getVote(id mesh.BlockID) vec {
block, found := ni.blocks[id]
if !found {
ni.Error("block not found !")
return Against
}
if block.Layer() > ni.pBase.Layer() {
ni.Error("we dont have an opinion on block according to current pbase")
return Against
}
return ni.tVote[ni.pBase][id]
}
func (ni *ninjaTortoise) handleIncomingLayer(newlyr *mesh.Layer) { //i most recent layer
ni.Info("update tables layer %d with %d blocks", newlyr.Index(), len(newlyr.Blocks()))
ni.processBlocks(newlyr)
if newlyr.Index() == Genesis {
ni.handleGenesis(newlyr)
return
}
l := ni.findMinimalNewlyGoodLayer(newlyr)
//from minimal newly good pattern to current layer
//update pattern tally for all good layers
for j := l; j > 0 && j < newlyr.Index(); j++ {
if p, gfound := ni.tGood[j]; gfound {
//init p's tally to pBase tally
initTallyToBase(ni.tTally, ni.pBase, p)
//find bottom of window
var windowStart mesh.LayerID
if Window > newlyr.Index() {
windowStart = 0
} else {
windowStart = newlyr.Index() - Window + 1
}
view := make(map[mesh.BlockID]struct{})
lCntr := make(map[mesh.LayerID]int)
correctionMap, effCountMap, getCrrEffCnt := ni.getCorrEffCounter()
foo := func(block *mesh.Block) {
view[block.ID()] = struct{}{} //all blocks in view
for _, id := range block.BlockVotes {
view[id] = struct{}{}
}
lCntr[block.Layer()]++ //amount of blocks for each layer in view
getCrrEffCnt(block) //calc correction and eff count
}
forBlockInView(ni.tPattern[p], ni.blocks, ni.pBase.Layer()+1, foo)
//add corrected implicit votes
ni.updatePatternTally(p, windowStart, correctionMap, effCountMap)
//add explicit votes
addPtrnVt := ni.addPatternVote(p, view)
for bl := range view {
addPtrnVt(bl)
}
complete := true
for idx := windowStart; idx < j; idx++ {
layer, _ := ni.layerBlocks[idx]
bids := make([]mesh.BlockID, 0, ni.avgLayerSize)
for _, bid := range layer {
//if bid is not in p's view.
//add negative vote multiplied by the amount of blocks in the view
//explicit votes against (not in view )
if _, found := view[bid]; idx >= ni.pBase.Layer() && !found {
ni.tTally[p][bid] = sumNodesInView(lCntr, idx+1, p.Layer())
}
if val, found := ni.tVote[p]; !found || val == nil {
ni.tVote[p] = make(map[mesh.BlockID]vec)
}
if vote := globalOpinion(ni.tTally[p][bid], ni.avgLayerSize, float64(p.LayerID-idx)); vote != Abstain {
ni.tVote[p][bid] = vote
if vote == Support {
bids = append(bids, bid)
}
} else {
ni.tVote[p][bid] = vote
complete = false //not complete
}
}
updatePatSupport(ni, p, bids, idx)
}
//update correction vectors after vote count
ni.updateCorrectionVectors(p, windowStart)
// update completeness of p
if _, found := ni.tComplete[p]; complete && !found {
ni.tComplete[p] = struct{}{}
ni.pBase = p
ni.Debug("found new complete and good pattern for layer %d pattern %d with %d support ", l, p.id, ni.tSupport[p])
}
}
}
ni.Info("finished layer %d pbase is %d", newlyr.Index(), ni.pBase.Layer())
return
}