-
Notifications
You must be signed in to change notification settings - Fork 211
/
full.go
174 lines (162 loc) · 4.36 KB
/
full.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
package tortoise
import (
"time"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
)
func newFullTortoise(config Config, state *state) *full {
return &full{
Config: config,
state: state,
delayed: map[types.LayerID][]*ballotInfo{},
}
}
type full struct {
Config
*state
// counted weights up to this layer.
//
// counting votes is what makes full tortoise expensive during rerun.
// we want to wait until verifying can't make progress before they are counted.
// storing them in current version is cheap.
counted types.LayerID
// delayed ballots by the layer when they are safe to count
delayed map[types.LayerID][]*ballotInfo
}
func (f *full) countBallot(logger log.Log, ballot *ballotInfo) {
start := time.Now()
if f.shouldBeDelayed(logger, ballot) {
return
}
if ballot.malicious {
return
}
for lvote := ballot.votes.tail; lvote != nil; lvote = lvote.prev {
if !lvote.lid.After(f.evicted) {
break
}
if lvote.vote == abstain {
continue
}
layer := f.layer(lvote.lid)
empty := true
for _, block := range layer.blocks {
if block.height > ballot.reference.height {
continue
}
vote := lvote.getVote(block)
switch vote {
case support:
empty = false
block.margin = block.margin.Add(ballot.weight)
case against:
block.margin = block.margin.Sub(ballot.weight)
}
logger.With().Debug("counted votes from ballot",
log.Stringer("id", ballot.id),
log.Uint32("lid", ballot.layer.Uint32()),
log.Stringer("block", block.id),
log.Stringer("vote", vote),
log.Stringer("margin", block.margin),
)
}
if empty {
layer.empty = layer.empty.Add(ballot.weight)
} else {
layer.empty = layer.empty.Sub(ballot.weight)
}
}
fcountBallotDuration.Observe(float64(time.Since(start).Nanoseconds()))
}
func (f *full) countForLateBlock(block *blockInfo) {
start := time.Now()
for lid := block.layer.Add(1); !lid.After(f.counted); lid = lid.Add(1) {
for _, ballot := range f.ballots[lid] {
if block.height > ballot.reference.height {
continue
}
for current := ballot.votes.tail; current != nil && !current.lid.Before(block.layer); current = current.prev {
if current.lid != block.layer {
continue
}
if current.vote == abstain {
continue
}
block.margin = block.margin.Sub(ballot.weight)
}
}
}
lateBlockDuration.Observe(float64(time.Since(start).Nanoseconds()))
}
func (f *full) countDelayed(logger log.Log, lid types.LayerID) {
delayed, exist := f.delayed[lid]
if !exist {
return
}
delete(f.delayed, lid)
for _, ballot := range delayed {
delayedBallots.Dec()
f.countBallot(logger.WithFields(log.Bool("delayed", true)), ballot)
}
}
func (f *full) countVotes(logger log.Log) {
for lid := f.counted.Add(1); !lid.After(f.processed); lid = lid.Add(1) {
for _, ballot := range f.ballots[lid] {
f.countBallot(logger, ballot)
}
}
f.counted = f.processed
}
func (f *full) verify(logger log.Log, lid types.LayerID) bool {
threshold := f.globalThreshold(f.Config, lid)
layer := f.state.layer(lid)
empty := crossesThreshold(layer.empty, threshold) == support
logger = logger.WithFields(
log.String("verifier", "full"),
log.Stringer("counted layer", f.counted),
log.Stringer("candidate layer", lid),
log.Stringer("local threshold", f.localThreshold),
log.Stringer("global threshold", threshold),
log.Stringer("empty weight", layer.empty),
log.Bool("is empty", empty),
)
if len(layer.blocks) == 0 {
if empty {
logger.With().Debug("candidate layer is empty")
} else {
logger.With().Debug("margin is too low to terminate layer as empty",
lid,
log.Stringer("margin", layer.empty),
)
}
return empty
}
return verifyLayer(
logger,
layer.blocks,
func(block *blockInfo) sign {
decision := crossesThreshold(block.margin, threshold)
if decision == neutral && empty {
return against
}
return decision
},
)
}
func (f *full) shouldBeDelayed(logger log.Log, ballot *ballotInfo) bool {
if !ballot.conditions.badBeacon {
return false
}
delay := ballot.layer.Add(f.BadBeaconVoteDelayLayers)
if !delay.After(f.last) {
return false
}
logger.With().Debug("ballot is delayed",
log.Stringer("id", ballot.id),
log.Uint32("ballot lid", ballot.layer.Uint32()),
log.Uint32("counted at", delay.Uint32()),
)
delayedBallots.Inc()
f.delayed[delay] = append(f.delayed[delay], ballot)
return true
}