-
Notifications
You must be signed in to change notification settings - Fork 212
/
algorithm.go
510 lines (470 loc) · 14.1 KB
/
algorithm.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 tortoise
import (
"context"
"fmt"
"sync"
"time"
"go.uber.org/zap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/types/result"
"github.com/spacemeshos/go-spacemesh/log"
)
// Config for protocol parameters.
type Config struct {
Hdist uint32 `mapstructure:"tortoise-hdist"` // hare output lookback distance
Zdist uint32 `mapstructure:"tortoise-zdist"` // hare result wait distance
// how long we are waiting for a switch from verifying to full. relevant during rerun.
WindowSize uint32 `mapstructure:"tortoise-window-size"` // size of the tortoise sliding window (in layers)
MaxExceptions int `mapstructure:"tortoise-max-exceptions"` // if candidate for base ballot has more than max exceptions it will be ignored
// number of layers to delay votes for blocks with bad beacon values during self-healing. ideally a full epoch.
BadBeaconVoteDelayLayers uint32 `mapstructure:"tortoise-delay-layers"`
// EnableTracer will write tortoise traces to the stderr.
EnableTracer bool `mapstructure:"tortoise-enable-tracer"`
// MinimalActiveSetWeight denotes weight that will replace weight
// recorded in the first ballot, if that weight is less than minimal
// for purposes of eligibility computation.
MinimalActiveSetWeight uint64 `mapstructure:"tortoise-activeset-weight"`
LayerSize uint32
}
// DefaultConfig for Tortoise.
func DefaultConfig() Config {
return Config{
LayerSize: 50,
Hdist: 10,
Zdist: 8,
WindowSize: 1000,
BadBeaconVoteDelayLayers: 0,
MaxExceptions: 50 * 100, // 100 layers of average size
}
}
// Tortoise is a thread safe verifying tortoise wrapper, it just locks all actions.
type Tortoise struct {
logger *zap.Logger
ctx context.Context
cfg Config
mu sync.Mutex
trtl *turtle
tracer *tracer
}
// Opt for configuring tortoise.
type Opt func(t *Tortoise)
// WithLogger defines logger for tortoise.
func WithLogger(logger log.Log) Opt {
return func(t *Tortoise) {
t.logger = logger.Zap()
}
}
// WithConfig defines protocol parameters.
func WithConfig(cfg Config) Opt {
return func(t *Tortoise) {
t.cfg = cfg
}
}
// WithTracer enables tracing of every call to the tortoise.
func WithTracer(opts ...TraceOpt) Opt {
return func(t *Tortoise) {
t.tracer = newTracer(opts...)
}
}
// New creates Tortoise instance.
func New(opts ...Opt) (*Tortoise, error) {
t := &Tortoise{
ctx: context.Background(),
logger: log.NewNop().Zap(),
cfg: DefaultConfig(),
}
for _, opt := range opts {
opt(t)
}
if t.cfg.Hdist < t.cfg.Zdist {
t.logger.Panic("hdist must be >= zdist",
zap.Uint32("hdist", t.cfg.Hdist),
zap.Uint32("zdist", t.cfg.Zdist),
)
}
t.trtl = newTurtle(t.logger, t.cfg)
if t.tracer != nil {
t.tracer.On(&ConfigTrace{
Hdist: t.cfg.Hdist,
Zdist: t.cfg.Zdist,
WindowSize: t.cfg.WindowSize,
MaxExceptions: uint32(t.cfg.MaxExceptions),
BadBeaconVoteDelayLayers: t.cfg.BadBeaconVoteDelayLayers,
LayerSize: t.cfg.LayerSize,
EpochSize: types.GetLayersPerEpoch(),
EffectiveGenesis: types.GetEffectiveGenesis().Uint32(),
})
}
return t, nil
}
// LatestComplete returns the latest verified layer.
func (t *Tortoise) LatestComplete() types.LayerID {
t.mu.Lock()
defer t.mu.Unlock()
return t.trtl.verified
}
func (t *Tortoise) OnWeakCoin(lid types.LayerID, coin bool) {
t.mu.Lock()
defer t.mu.Unlock()
t.logger.Debug("on weakcoin",
zap.Uint32("layer_id", lid.Uint32()),
zap.Uint32("evicted", t.trtl.evicted.Uint32()),
zap.Bool("coin", coin),
)
if lid <= t.trtl.evicted {
return
}
layer := t.trtl.layer(lid)
if coin {
layer.coinflip = support
} else {
layer.coinflip = against
}
if t.tracer != nil {
t.tracer.On(&WeakCoinTrace{Layer: lid, Coin: coin})
}
}
// OnMalfeasance registers node id as malfeasent.
// - ballots from this id will have zero weight
// - atxs - will not be counted towards global/local threhsolds
// If node registers equivocating ballot/atx it should
// call OnMalfeasance before storing ballot/atx.
func (t *Tortoise) OnMalfeasance(id types.NodeID) {
t.mu.Lock()
defer t.mu.Unlock()
if t.trtl.isMalfeasant(id) {
return
}
t.logger.Debug("on malfeasence", zap.Stringer("id", id))
t.trtl.makrMalfeasant(id)
malfeasantNumber.Inc()
if t.tracer != nil {
t.tracer.On(&MalfeasanceTrace{ID: id})
}
}
func (t *Tortoise) OnBeacon(eid types.EpochID, beacon types.Beacon) {
t.mu.Lock()
defer t.mu.Unlock()
firstInWindow := t.trtl.evicted.Add(1).GetEpoch()
t.logger.Debug("on beacon",
zap.Uint32("epoch id", eid.Uint32()),
zap.Uint32("first epoch", firstInWindow.Uint32()),
zap.Stringer("beacon", beacon),
)
if eid < firstInWindow {
return
}
epoch := t.trtl.epoch(eid)
epoch.beacon = &beacon
if t.tracer != nil {
t.tracer.On(&BeaconTrace{Epoch: eid, Beacon: beacon})
}
}
type encodeConf struct {
current *types.LayerID
}
// EncodeVotesOpts is for configuring EncodeVotes options.
type EncodeVotesOpts func(*encodeConf)
// EncodeVotesWithCurrent changes last known layer that will be used for encoding votes.
//
// NOTE(dshulyak) why do we need this?
// tortoise computes threshold from last non-verified till the last known layer,
// since we dont download atxs before starting tortoise we won't be able to compute threshold
// based on the last clock layer (see https://github.com/spacemeshos/go-spacemesh/issues/3003)
func EncodeVotesWithCurrent(current types.LayerID) EncodeVotesOpts {
return func(conf *encodeConf) {
conf.current = ¤t
}
}
// EncodeVotes chooses a base ballot and creates a differences list. needs the hare results for latest layers.
func (t *Tortoise) EncodeVotes(ctx context.Context, opts ...EncodeVotesOpts) (*types.Opinion, error) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitEncodeVotes.Observe(float64(time.Since(start).Nanoseconds()))
start = time.Now()
conf := &encodeConf{}
for _, opt := range opts {
opt(conf)
}
opinion, err := t.trtl.EncodeVotes(ctx, conf)
executeEncodeVotes.Observe(float64(time.Since(start).Nanoseconds()))
if err != nil {
errorsCounter.Inc()
}
if t.tracer != nil {
event := &EncodeVotesTrace{
Opinion: opinion,
}
if err != nil {
event.Error = err.Error()
}
if conf.current != nil {
event.Layer = *conf.current
} else {
event.Layer = t.trtl.last + 1
}
t.tracer.On(event)
}
return opinion, err
}
// TallyVotes up to the specified layer.
func (t *Tortoise) TallyVotes(ctx context.Context, lid types.LayerID) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitTallyVotes.Observe(float64(time.Since(start).Nanoseconds()))
start = time.Now()
t.trtl.onLayer(ctx, lid)
executeTallyVotes.Observe(float64(time.Since(start).Nanoseconds()))
if t.tracer != nil {
t.tracer.On(&TallyTrace{Layer: lid})
}
}
// OnAtx is expected to be called before ballots that use this atx.
func (t *Tortoise) OnAtx(header *types.AtxTortoiseData) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitAtxDuration.Observe(float64(time.Since(start).Nanoseconds()))
t.trtl.onAtx(header)
if t.tracer != nil {
t.tracer.On(&AtxTrace{Header: header})
}
}
// OnBlock updates tortoise with information that data is available locally.
func (t *Tortoise) OnBlock(header types.BlockHeader) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitBlockDuration.Observe(float64(time.Since(start).Nanoseconds()))
t.trtl.onBlock(header, true, false)
if t.tracer != nil {
t.tracer.On(&BlockTrace{Header: header})
}
}
// OnValidBlock inserts block, updates that data is stored locally
// and that block was previously considered valid by tortoise.
func (t *Tortoise) OnValidBlock(header types.BlockHeader) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitBlockDuration.Observe(float64(time.Since(start).Nanoseconds()))
t.trtl.onBlock(header, true, true)
if t.tracer != nil {
t.tracer.On(&BlockTrace{Header: header, Valid: true})
}
}
// OnBallot should be called every time new ballot is received.
// Dependencies (base ballot, ref ballot, active set and its own atx) must
// be processed before ballot.
func (t *Tortoise) OnBallot(ballot *types.BallotTortoiseData) {
t.mu.Lock()
defer t.mu.Unlock()
err := t.trtl.onBallot(ballot)
if err != nil {
errorsCounter.Inc()
t.logger.Error("failed to save state from ballot",
zap.Stringer("ballot", ballot.ID),
zap.Error(err))
}
if t.tracer != nil {
t.tracer.On(&BallotTrace{Ballot: ballot})
}
if t.tracer != nil {
t.tracer.On(&BallotTrace{Ballot: ballot})
}
}
// DecodedBallot created after unwrapping exceptions list and computing internal opinion.
type DecodedBallot struct {
*types.BallotTortoiseData
info *ballotInfo
// after validation is finished we need to add new vote targets
// for tortoise from the decoded votes. minHint identifies the boundary
// until which we have to scan.
minHint types.LayerID
}
// DecodeBallot decodes ballot if it wasn't processed earlier.
func (t *Tortoise) DecodeBallot(ballot *types.BallotTortoiseData) (*DecodedBallot, error) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitBallotDuration.Observe(float64(time.Since(start).Nanoseconds()))
decoded, err := t.decodeBallot(ballot)
if t.tracer != nil {
ev := &DecodeBallotTrace{Ballot: ballot}
if err != nil {
ev.Error = err.Error()
}
t.tracer.On(ev)
}
return decoded, err
}
func (t *Tortoise) decodeBallot(ballot *types.BallotTortoiseData) (*DecodedBallot, error) {
info, min, err := t.trtl.decodeBallot(ballot)
if err != nil {
errorsCounter.Inc()
return nil, err
}
if info == nil {
errorsCounter.Inc()
return nil, fmt.Errorf("can't decode ballot %s", ballot.ID)
}
if info.opinion() != ballot.Opinion.Hash {
errorsCounter.Inc()
return nil, fmt.Errorf(
"computed opinion hash %s doesn't match signed %s for ballot %d / %s",
info.opinion().ShortString(), ballot.Opinion.Hash.ShortString(), ballot.Layer, ballot.ID,
)
}
return &DecodedBallot{BallotTortoiseData: ballot, info: info, minHint: min}, nil
}
// StoreBallot stores previously decoded ballot.
func (t *Tortoise) StoreBallot(decoded *DecodedBallot) error {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitBallotDuration.Observe(float64(time.Since(start).Nanoseconds()))
if decoded.Malicious {
decoded.info.malicious = true
}
err := t.trtl.storeBallot(decoded.info, decoded.minHint)
if t.tracer != nil {
ev := &StoreBallotTrace{ID: decoded.ID, Malicious: decoded.Malicious}
if err != nil {
ev.Error = err.Error()
}
t.tracer.On(ev)
}
return err
}
// OnHareOutput should be called when hare terminated or certificate for a block
// was synced from a peer.
// This method is expected to be called any number of times, with layers in any order.
//
// This method should be called with EmptyBlockID if node received proof of malicious behavior,
// such as signing same block id by members of the same committee.
func (t *Tortoise) OnHareOutput(lid types.LayerID, bid types.BlockID) {
start := time.Now()
t.mu.Lock()
defer t.mu.Unlock()
waitHareOutputDuration.Observe(float64(time.Since(start).Nanoseconds()))
t.trtl.onHareOutput(lid, bid)
if t.tracer != nil {
t.tracer.On(&HareTrace{Layer: lid, Vote: bid})
}
}
// GetMissingActiveSet returns unknown atxs from the original list. It is done for a specific epoch
// as active set atxs never cross epoch boundary.
func (t *Tortoise) GetMissingActiveSet(epoch types.EpochID, atxs []types.ATXID) []types.ATXID {
t.mu.Lock()
defer t.mu.Unlock()
edata, exists := t.trtl.epochs[epoch]
if !exists {
return atxs
}
var missing []types.ATXID
for _, atx := range atxs {
_, exists := edata.atxs[atx]
if !exists {
missing = append(missing, atx)
}
}
return missing
}
// Updates returns list of layers where opinion was changed since previous call.
func (t *Tortoise) Updates() []result.Layer {
t.mu.Lock()
defer t.mu.Unlock()
if t.trtl.pending == 0 {
return nil
}
rst, err := t.results(t.trtl.pending, t.trtl.processed)
if err != nil {
t.logger.Panic("unexpected error",
zap.Uint32("pending", t.trtl.pending.Uint32()),
zap.Uint32("processed", t.trtl.pending.Uint32()),
zap.Error(err),
)
}
t.trtl.pending = 0
if t.tracer != nil {
t.tracer.On(&UpdatesTrace{ResultsTrace{
From: t.trtl.pending, To: t.trtl.processed,
Results: rst,
}})
}
return rst
}
// Results returns layers that crossed threshold in range [from, to].
func (t *Tortoise) Results(from, to types.LayerID) ([]result.Layer, error) {
t.mu.Lock()
defer t.mu.Unlock()
rst, err := t.results(from, to)
if t.tracer != nil {
ev := &ResultsTrace{
From: from, To: to,
Results: rst,
}
if err != nil {
ev.Error = err.Error()
}
t.tracer.On(ev)
}
return rst, err
}
func (t *Tortoise) results(from, to types.LayerID) ([]result.Layer, error) {
if from <= t.trtl.evicted {
return nil, fmt.Errorf("requested layer %d is before evicted %d", from, t.trtl.evicted)
}
rst := make([]result.Layer, 0, to-from)
for lid := from; lid <= to; lid++ {
layer := t.trtl.layer(lid)
blocks := make([]result.Block, 0, len(layer.blocks))
for _, block := range layer.blocks {
blocks = append(blocks, result.Block{
Header: block.header(),
Data: block.data,
Hare: block.hare == support,
Valid: block.validity == support,
Invalid: block.validity == against,
Local: crossesThreshold(block.margin, t.trtl.localThreshold) == support,
})
}
rst = append(rst, result.Layer{
Layer: lid,
Blocks: blocks,
Verified: t.trtl.verified >= lid,
Opinion: layer.opinion,
})
}
return rst, nil
}
type Mode int
func (m Mode) String() string {
if m == 0 {
return "verifying"
}
return "full"
}
const (
Verifying = 0
Full = 1
)
// Mode returns 0 for verifying.
func (t *Tortoise) Mode() Mode {
t.mu.Lock()
defer t.mu.Unlock()
if t.trtl.isFull {
return Full
}
return Verifying
}
// resetPending compares stored opinion with computed opinion and sets
// pending layer to the layer above equal layer.
// this method is meant to be used only in recovery from disk codepath.
func (t *Tortoise) resetPending(lid types.LayerID, opinion types.Hash32) {
if t.trtl.layer(lid).opinion == opinion {
t.trtl.pending = lid + 1
}
}