-
Notifications
You must be signed in to change notification settings - Fork 211
/
threshold.go
104 lines (95 loc) · 3.34 KB
/
threshold.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
package tortoise
import (
"fmt"
"math/big"
"sort"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/datastore"
)
var (
// by assumption adversarial weight can't be larger than 1/3.
adversarialWeightFraction = big.NewRat(1, 3)
// nodes should not be on different sides of the local threshold if they receive different adversarial votes.
localThresholdFraction = big.NewRat(1, 3)
// for comleteness:
// global threshold is set in a such way so that if adversary
// cancels their weight (adversarialWeightFraction) - honest nodes should still cross local threshold.
)
func getBallotHeight(cdb *datastore.CachedDB, ballot *types.Ballot) (uint64, error) {
atx, err := cdb.GetAtxHeader(ballot.AtxID)
if err != nil {
return 0, fmt.Errorf("read atx for ballot height: %w", err)
}
return atx.TickHeight(), nil
}
func extractAtxsData(cdb *datastore.CachedDB, epoch types.EpochID) (uint64, uint64, error) {
var (
weight uint64
heights []uint64
)
if err := cdb.IterateEpochATXHeaders(epoch, func(header *types.ActivationTxHeader) bool {
weight += header.GetWeight()
heights = append(heights, header.TickHeight())
return true
}); err != nil {
return 0, 0, fmt.Errorf("computing epoch data for %d: %w", epoch, err)
}
return weight, getMedian(heights), nil
}
func getMedian(heights []uint64) uint64 {
if len(heights) == 0 {
return 0
}
sort.Slice(heights, func(i, j int) bool {
return heights[i] < heights[j]
})
mid := len(heights) / 2
if len(heights)%2 == 0 {
return (heights[mid-1] + heights[mid]) / 2
}
return heights[mid]
}
func computeExpectedWeight(epochs map[types.EpochID]*epochInfo, target, last types.LayerID) weight {
// all layers after target are voting on the target layer
// therefore expected weight for the target layer is a sum of all weights
// within (target, last]
start := target.Add(1)
startEpoch := start.GetEpoch()
lastEpoch := last.GetEpoch()
length := types.GetLayersPerEpoch()
if startEpoch == lastEpoch {
einfo := epochs[startEpoch]
return util.WeightFromUint64(einfo.weight).Fraction(big.NewRat(
int64(last.Difference(start)+1),
int64(length),
))
}
weight := util.WeightFromUint64(epochs[startEpoch].weight).Fraction(big.NewRat(
int64(length-start.OrdinalInEpoch()),
int64(length),
))
for epoch := startEpoch + 1; epoch < lastEpoch; epoch++ {
einfo := epochs[epoch]
weight = weight.Add(util.WeightFromUint64(einfo.weight))
}
weight = weight.Add(util.WeightFromUint64(epochs[lastEpoch].weight).
Fraction(big.NewRat(int64(last.OrdinalInEpoch()+1), int64(length))))
return weight
}
// computeGlobalTreshold computes global treshold based on the expected weight.
func computeGlobalThreshold(config Config, localThreshold weight, epochs map[types.EpochID]*epochInfo, target, processed, last types.LayerID) util.Weight {
return computeExpectedWeightInWindow(config, epochs, target, processed, last).
Fraction(adversarialWeightFraction).
Add(localThreshold)
}
func computeExpectedWeightInWindow(config Config, epochs map[types.EpochID]*epochInfo, target, processed, last types.LayerID) util.Weight {
window := last
if last.Difference(target) > config.WindowSize {
window = target.Add(config.WindowSize)
if processed.After(window) {
window = processed
}
}
return computeExpectedWeight(epochs, target, window)
}