-
Notifications
You must be signed in to change notification settings - Fork 211
/
full.go
189 lines (167 loc) · 5.08 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package tortoise
import (
"container/list"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/log"
)
func newFullTortoise(config Config, common *commonState) *full {
return &full{
Config: config,
commonState: common,
votes: map[types.BallotID]votes{},
abstain: map[types.BallotID]map[types.LayerID]struct{}{},
base: map[types.BallotID]types.BallotID{},
weights: map[types.BlockID]util.Weight{},
delayedQueue: list.New(),
}
}
type full struct {
Config
*commonState
votes map[types.BallotID]votes
abstain map[types.BallotID]map[types.LayerID]struct{}
base map[types.BallotID]types.BallotID
// 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
// counted weights of all blocks up to counted layer.
weights map[types.BlockID]util.Weight
// queue of the ballots with bad beacon
delayedQueue *list.List
}
func (f *full) processBallots(ballots []tortoiseBallot) {
for _, ballot := range ballots {
f.base[ballot.id] = ballot.base
f.votes[ballot.id] = ballot.votes
f.abstain[ballot.id] = ballot.abstain
}
}
func (f *full) onBallot(ballot *tortoiseBallot) {
f.base[ballot.id] = ballot.base
f.votes[ballot.id] = ballot.votes
f.abstain[ballot.id] = ballot.abstain
}
func (f *full) onBlock(block types.BlockID) {
f.weights[block] = util.WeightFromUint64(0)
}
func (f *full) getVote(logger log.Log, ballot types.BallotID, blocklid types.LayerID, block types.BlockID) sign {
sign, exist := f.votes[ballot][block]
if !exist {
_, exist = f.abstain[ballot][blocklid]
if exist {
return abstain
}
base, exist := f.base[ballot]
if !exist {
return against
}
return f.getVote(logger, base, blocklid, block)
}
return sign
}
func (f *full) countVotesFromBallots(logger log.Log, ballotlid types.LayerID, ballots []types.BallotID) {
var delayed []types.BallotID
for _, ballot := range ballots {
if f.shouldBeDelayed(ballot, ballotlid) {
delayed = append(delayed, ballot)
continue
}
ballotWeight := f.ballotWeight[ballot]
if ballotWeight.IsNil() {
continue
}
for lid := f.verified.Add(1); lid.Before(ballotlid); lid = lid.Add(1) {
for _, block := range f.blocks[lid] {
vote := f.getVote(logger, ballot, lid, block)
current := f.weights[block]
switch vote {
case support:
current = current.Add(ballotWeight)
case against:
current = current.Sub(ballotWeight)
}
f.weights[block] = current
}
}
}
if len(delayed) > 0 {
f.delayedQueue.PushBack(delayedBallots{
lid: ballotlid,
ballots: delayed,
})
}
}
func (f *full) countLayerVotes(logger log.Log, lid types.LayerID) {
if !lid.After(f.counted) {
return
}
f.counted = lid
for front := f.delayedQueue.Front(); front != nil; {
delayed := front.Value.(delayedBallots)
if f.last.Difference(delayed.lid) <= f.BadBeaconVoteDelayLayers {
break
}
logger.With().Debug("adding weight from delayed ballots",
log.Stringer("ballots_layer", delayed.lid),
)
f.countVotesFromBallots(
logger.WithFields(log.Bool("delayed", true)), delayed.lid, delayed.ballots)
next := front.Next()
f.delayedQueue.Remove(front)
front = next
}
f.countVotesFromBallots(logger, lid, f.ballots[lid])
}
func (f *full) countVotes(logger log.Log) {
for lid := f.counted.Add(1); !lid.After(f.processed); lid = lid.Add(1) {
f.countLayerVotes(logger, lid)
}
f.counted = f.processed
}
func (f *full) verify(logger log.Log, lid types.LayerID) bool {
logger = logger.WithFields(
log.String("verifier", fullTortoise),
log.Stringer("counted_layer", f.counted),
log.Stringer("candidate_layer", lid),
log.Stringer("local_threshold", f.localThreshold),
log.Stringer("global_threshold", f.globalThreshold),
)
blocks := f.blocks[lid]
// necessary only to log debug only once for each block
decisions := make([]sign, 0, len(blocks))
for _, block := range blocks {
current := f.weights[block]
decision := sign(current.Cmp(f.globalThreshold))
if decision == abstain {
logger.With().Info("candidate layer is not verified. not enough weight in votes",
log.Stringer("block", block),
log.Stringer("voting_weight", current),
)
return false
}
decisions = append(decisions, decision)
}
for i, block := range blocks {
logger.With().Debug("full tortoise decided on a block",
log.Stringer("block", block),
log.Stringer("decision", decisions[i]),
)
f.validity[block] = decisions[i]
}
logger.With().Info("candidate layer is verified")
return true
}
// shouldBeDelayed is true if ballot has a different beacon and it wasn't created sufficiently
// long time ago.
func (f *full) shouldBeDelayed(ballotID types.BallotID, ballotlid types.LayerID) bool {
_, bad := f.badBeaconBallots[ballotID]
return bad && f.last.Difference(ballotlid) <= f.BadBeaconVoteDelayLayers
}
type delayedBallots struct {
lid types.LayerID
ballots []types.BallotID
}