/
hare.go
615 lines (570 loc) · 17.4 KB
/
hare.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package hare3
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"github.com/jonboulle/clockwork"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/exp/maps"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/layerpatrol"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/proposals/store"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/beacons"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/system"
)
type Config struct {
Enable bool `mapstructure:"enable"`
EnableLayer types.LayerID `mapstructure:"enable-layer"`
DisableLayer types.LayerID `mapstructure:"disable-layer"`
Committee uint16 `mapstructure:"committee"`
Leaders uint16 `mapstructure:"leaders"`
IterationsLimit uint8 `mapstructure:"iterations-limit"`
PreroundDelay time.Duration `mapstructure:"preround-delay"`
RoundDuration time.Duration `mapstructure:"round-duration"`
// LogStats if true will log iteration statistics with INFO level at the start of the next iteration.
// This requires additional computation and should be used for debugging only.
LogStats bool `mapstructure:"log-stats"`
ProtocolName string `mapstructure:"protocolname"`
}
func (cfg *Config) Validate(zdist time.Duration) error {
terminates := cfg.roundStart(IterRound{Iter: cfg.IterationsLimit, Round: hardlock})
if terminates > zdist {
return fmt.Errorf("hare terminates later (%v) than expected (%v)", terminates, zdist)
}
if cfg.Enable && cfg.DisableLayer <= cfg.EnableLayer {
return fmt.Errorf("disabled layer (%d) must be larger than enabled (%d)",
cfg.DisableLayer, cfg.EnableLayer)
}
return nil
}
func (cfg *Config) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddBool("enabled", cfg.Enable)
encoder.AddUint32("enabled layer", cfg.EnableLayer.Uint32())
encoder.AddUint32("disabled layer", cfg.DisableLayer.Uint32())
encoder.AddUint16("committee", cfg.Committee)
encoder.AddUint16("leaders", cfg.Leaders)
encoder.AddUint8("iterations limit", cfg.IterationsLimit)
encoder.AddDuration("preround delay", cfg.PreroundDelay)
encoder.AddDuration("round duration", cfg.RoundDuration)
encoder.AddBool("log stats", cfg.LogStats)
encoder.AddString("p2p protocol", cfg.ProtocolName)
return nil
}
// roundStart returns expected time for iter/round relative to
// layer start.
func (cfg *Config) roundStart(round IterRound) time.Duration {
if round.Round == 0 {
return cfg.PreroundDelay
}
return cfg.PreroundDelay + time.Duration(round.Absolute()-1)*cfg.RoundDuration
}
func DefaultConfig() Config {
return Config{
// NOTE(talm) We aim for a 2^{-40} error probability; if the population at large has a 2/3 honest majority,
// we need a committee of size ~800 to guarantee this error rate (at least,
// this is what the Chernoff bound gives you; the actual value is a bit lower,
// so we can probably get away with a smaller committee). For a committee of size 400,
// the Chernoff bound gives 2^{-20} probability of a dishonest majority when 1/3 of the population is dishonest.
Committee: 800,
Leaders: 5,
IterationsLimit: 4,
PreroundDelay: 25 * time.Second,
RoundDuration: 12 * time.Second,
// can be bumped to 3.1 when oracle upgrades
ProtocolName: "/h/3.0",
DisableLayer: math.MaxUint32,
}
}
type ConsensusOutput struct {
Layer types.LayerID
Proposals []types.ProposalID
}
type WeakCoinOutput struct {
Layer types.LayerID
Coin bool
}
type Opt func(*Hare)
func WithWallclock(clock clockwork.Clock) Opt {
return func(hr *Hare) {
hr.wallclock = clock
}
}
func WithConfig(cfg Config) Opt {
return func(hr *Hare) {
hr.config = cfg
hr.oracle.config = cfg
}
}
func WithLogger(logger *zap.Logger) Opt {
return func(hr *Hare) {
hr.log = logger
hr.oracle.log = logger
}
}
func WithTracer(tracer Tracer) Opt {
return func(hr *Hare) {
hr.tracer = tracer
}
}
type nodeclock interface {
AwaitLayer(types.LayerID) <-chan struct{}
CurrentLayer() types.LayerID
LayerToTime(types.LayerID) time.Time
}
func New(
nodeclock nodeclock,
pubsub pubsub.PublishSubsciber,
db *sql.Database,
atxsdata *atxsdata.Data,
proposals *store.Store,
verifier *signing.EdVerifier,
oracle oracle,
sync system.SyncStateProvider,
patrol *layerpatrol.LayerPatrol,
opts ...Opt,
) *Hare {
ctx, cancel := context.WithCancel(context.Background())
hr := &Hare{
ctx: ctx,
cancel: cancel,
results: make(chan ConsensusOutput, 32),
coins: make(chan WeakCoinOutput, 32),
signers: map[string]*signing.EdSigner{},
sessions: map[types.LayerID]*protocol{},
config: DefaultConfig(),
log: zap.NewNop(),
wallclock: clockwork.NewRealClock(),
nodeclock: nodeclock,
pubsub: pubsub,
db: db,
atxsdata: atxsdata,
proposals: proposals,
verifier: verifier,
oracle: &legacyOracle{
log: zap.NewNop(),
oracle: oracle,
config: DefaultConfig(),
},
sync: sync,
patrol: patrol,
tracer: noopTracer{},
}
for _, opt := range opts {
opt(hr)
}
return hr
}
type Hare struct {
// state
ctx context.Context
cancel context.CancelFunc
eg errgroup.Group
results chan ConsensusOutput
coins chan WeakCoinOutput
mu sync.Mutex
signers map[string]*signing.EdSigner
sessions map[types.LayerID]*protocol
// options
config Config
log *zap.Logger
wallclock clockwork.Clock
// dependencies
nodeclock nodeclock
pubsub pubsub.PublishSubsciber
db *sql.Database
atxsdata *atxsdata.Data
proposals *store.Store
verifier *signing.EdVerifier
oracle *legacyOracle
sync system.SyncStateProvider
patrol *layerpatrol.LayerPatrol
tracer Tracer
}
func (h *Hare) Register(sig *signing.EdSigner) {
h.mu.Lock()
defer h.mu.Unlock()
h.log.Info("registered signing key", log.ZShortStringer("id", sig.NodeID()))
h.signers[string(sig.NodeID().Bytes())] = sig
}
func (h *Hare) Results() <-chan ConsensusOutput {
return h.results
}
func (h *Hare) Coins() <-chan WeakCoinOutput {
return h.coins
}
func (h *Hare) Start() {
h.pubsub.Register(h.config.ProtocolName, h.Handler, pubsub.WithValidatorInline(true))
current := h.nodeclock.CurrentLayer() + 1
enabled := max(current, h.config.EnableLayer, types.GetEffectiveGenesis()+1)
disabled := types.LayerID(math.MaxUint32)
if h.config.DisableLayer > 0 {
disabled = h.config.DisableLayer
}
h.log.Info("started",
zap.Inline(&h.config),
zap.Uint32("enabled", enabled.Uint32()),
zap.Uint32("disabled", disabled.Uint32()),
)
h.eg.Go(func() error {
for next := enabled; next < disabled; next++ {
select {
case <-h.nodeclock.AwaitLayer(next):
h.log.Debug("notified", zap.Uint32("lid", next.Uint32()))
h.onLayer(next)
case <-h.ctx.Done():
return nil
}
}
return nil
})
}
func (h *Hare) Running() int {
h.mu.Lock()
defer h.mu.Unlock()
return len(h.sessions)
}
func (h *Hare) Handler(ctx context.Context, peer p2p.Peer, buf []byte) error {
msg := &Message{}
if err := codec.Decode(buf, msg); err != nil {
malformedError.Inc()
return fmt.Errorf("%w: decoding error %s", pubsub.ErrValidationReject, err.Error())
}
if err := msg.Validate(); err != nil {
malformedError.Inc()
return fmt.Errorf("%w: validation %s", pubsub.ErrValidationReject, err.Error())
}
h.tracer.OnMessageReceived(msg)
h.mu.Lock()
session, registered := h.sessions[msg.Layer]
h.mu.Unlock()
if !registered {
notRegisteredError.Inc()
return fmt.Errorf("layer %d is not registered", msg.Layer)
}
if !h.verifier.Verify(signing.HARE, msg.Sender, msg.ToMetadata().ToBytes(), msg.Signature) {
signatureError.Inc()
return fmt.Errorf("%w: invalid signature", pubsub.ErrValidationReject)
}
malicious := h.atxsdata.IsMalicious(msg.Sender)
start := time.Now()
g := h.oracle.validate(msg)
oracleLatency.Observe(time.Since(start).Seconds())
if g == grade0 {
oracleError.Inc()
return errors.New("zero grade")
}
start = time.Now()
input := &input{
Message: msg,
msgHash: msg.ToHash(),
malicious: malicious,
atxgrade: g,
}
h.log.Debug("on message", zap.Inline(input))
gossip, equivocation := session.OnInput(input)
h.log.Debug("after on message", log.ZShortStringer("hash", input.msgHash), zap.Bool("gossip", gossip))
submitLatency.Observe(time.Since(start).Seconds())
if equivocation != nil && !malicious {
h.log.Debug("registered equivocation",
zap.Uint32("lid", msg.Layer.Uint32()),
zap.Stringer("sender", equivocation.Messages[0].SmesherID))
proof := equivocation.ToMalfeasanceProof()
if err := identities.SetMalicious(
h.db, equivocation.Messages[0].SmesherID, codec.MustEncode(proof), time.Now()); err != nil {
h.log.Error("failed to save malicious identity", zap.Error(err))
}
h.atxsdata.SetMalicious(equivocation.Messages[0].SmesherID)
}
if !gossip {
droppedMessages.Inc()
return errors.New("dropped by graded gossip")
}
expected := h.nodeclock.LayerToTime(msg.Layer).Add(h.config.roundStart(msg.IterRound))
metrics.ReportMessageLatency(h.config.ProtocolName, msg.Round.String(), time.Since(expected))
return nil
}
func (h *Hare) onLayer(layer types.LayerID) {
h.proposals.OnLayer(layer)
if !h.sync.IsSynced(h.ctx) {
h.log.Debug("not synced", zap.Uint32("lid", layer.Uint32()))
return
}
beacon, err := beacons.Get(h.db, layer.GetEpoch())
if err != nil || beacon == types.EmptyBeacon {
h.log.Debug("no beacon",
zap.Uint32("epoch", layer.GetEpoch().Uint32()),
zap.Uint32("lid", layer.Uint32()),
zap.Error(err),
)
return
}
h.patrol.SetHareInCharge(layer)
h.mu.Lock()
// signer can't join mid session
s := &session{
lid: layer,
beacon: beacon,
signers: maps.Values(h.signers),
vrfs: make([]*types.HareEligibility, len(h.signers)),
proto: newProtocol(h.config.Committee/2 + 1),
}
h.sessions[layer] = s.proto
h.mu.Unlock()
sessionStart.Inc()
h.tracer.OnStart(layer)
h.log.Debug("registered layer", zap.Uint32("lid", layer.Uint32()))
h.eg.Go(func() error {
if err := h.run(s); err != nil {
h.log.Warn("failed",
zap.Uint32("lid", layer.Uint32()),
zap.Error(err),
)
exitErrors.Inc()
// if terminated successfully it will notify block generator
// and it will have to CompleteHare
h.patrol.CompleteHare(layer)
} else {
h.log.Debug("terminated",
zap.Uint32("lid", layer.Uint32()),
)
}
h.mu.Lock()
delete(h.sessions, layer)
h.mu.Unlock()
sessionTerminated.Inc()
h.tracer.OnStop(layer)
return nil
})
}
func (h *Hare) run(session *session) error {
// oracle may load non-negligible amount of data from disk
// we do it before preround starts, so that load can have some slack time
// before it needs to be used in validation
var (
current = IterRound{Round: preround}
start = time.Now()
active bool
)
for i := range session.signers {
session.vrfs[i] = h.oracle.active(session.signers[i], session.beacon, session.lid, current)
active = active || session.vrfs[i] != nil
}
h.tracer.OnActive(session.vrfs)
activeLatency.Observe(time.Since(start).Seconds())
walltime := h.nodeclock.LayerToTime(session.lid).Add(h.config.PreroundDelay)
if active {
h.log.Debug("active in preround. waiting for preround delay", zap.Uint32("lid", session.lid.Uint32()))
// initial set is not needed if node is not active in preround
select {
case <-h.wallclock.After(walltime.Sub(h.wallclock.Now())):
case <-h.ctx.Done():
return h.ctx.Err()
}
start := time.Now()
session.proto.OnInitial(h.selectProposals(session))
proposalsLatency.Observe(time.Since(start).Seconds())
}
if err := h.onOutput(session, current, session.proto.Next()); err != nil {
return err
}
result := false
for {
walltime = walltime.Add(h.config.RoundDuration)
current = session.proto.IterRound
start = time.Now()
for i := range session.signers {
if current.IsMessageRound() {
session.vrfs[i] = h.oracle.active(session.signers[i], session.beacon, session.lid, current)
} else {
session.vrfs[i] = nil
}
}
h.tracer.OnActive(session.vrfs)
activeLatency.Observe(time.Since(start).Seconds())
select {
case <-h.wallclock.After(walltime.Sub(h.wallclock.Now())):
h.log.Debug("execute round",
zap.Uint32("lid", session.lid.Uint32()),
zap.Uint8("iter", session.proto.Iter), zap.Stringer("round", session.proto.Round),
zap.Bool("active", active),
)
out := session.proto.Next()
if out.result != nil {
result = true
}
if err := h.onOutput(session, current, out); err != nil {
return err
}
// we are logginng stats 1 network delay after new iteration start
// so that we can receive notify messages from previous iteration
if session.proto.Round == softlock && h.config.LogStats {
h.log.Info("stats", zap.Uint32("lid", session.lid.Uint32()), zap.Inline(session.proto.Stats()))
}
if out.terminated {
if !result {
return errors.New("terminated without result")
}
return nil
}
if current.Iter == h.config.IterationsLimit {
return fmt.Errorf("hare failed to reach consensus in %d iterations",
h.config.IterationsLimit)
}
case <-h.ctx.Done():
return nil
}
}
}
func (h *Hare) onOutput(session *session, ir IterRound, out output) error {
for i, vrf := range session.vrfs {
if vrf == nil || out.message == nil {
continue
}
msg := *out.message // shallow copy
msg.Layer = session.lid
msg.Eligibility = *vrf
msg.Sender = session.signers[i].NodeID()
msg.Signature = session.signers[i].Sign(signing.HARE, msg.ToMetadata().ToBytes())
if err := h.pubsub.Publish(h.ctx, h.config.ProtocolName, msg.ToBytes()); err != nil {
h.log.Error("failed to publish", zap.Inline(&msg), zap.Error(err))
}
}
h.tracer.OnMessageSent(out.message)
h.log.Debug("round output",
zap.Uint32("lid", session.lid.Uint32()),
zap.Uint8("iter", ir.Iter), zap.Stringer("round", ir.Round),
zap.Inline(&out),
)
if out.coin != nil {
select {
case <-h.ctx.Done():
return h.ctx.Err()
case h.coins <- WeakCoinOutput{Layer: session.lid, Coin: *out.coin}:
}
sessionCoin.Inc()
}
if out.result != nil {
select {
case <-h.ctx.Done():
return h.ctx.Err()
case h.results <- ConsensusOutput{Layer: session.lid, Proposals: out.result}:
}
sessionResult.Inc()
}
return nil
}
func (h *Hare) selectProposals(session *session) []types.ProposalID {
h.log.Debug("requested proposals",
zap.Uint32("lid", session.lid.Uint32()),
zap.Stringer("beacon", session.beacon),
)
var (
result []types.ProposalID
min *atxsdata.ATX
)
target := session.lid.GetEpoch()
publish := target - 1
for _, signer := range session.signers {
atxid, err := atxs.GetIDByEpochAndNodeID(h.db, publish, signer.NodeID())
switch {
case errors.Is(err, sql.ErrNotFound):
// if atx is not registered for identity we will get sql.ErrNotFound
case err != nil:
h.log.Error("failed to get atx id by epoch and node id", zap.Error(err))
return []types.ProposalID{}
default:
own := h.atxsdata.Get(target, atxid)
if min == nil || (min != nil && own != nil && own.Height < min.Height) {
min = own
}
}
}
if min == nil {
h.log.Debug("no atxs in the requested epoch", zap.Uint32("epoch", session.lid.GetEpoch().Uint32()-1))
return []types.ProposalID{}
}
candidates := h.proposals.GetForLayer(session.lid)
atxs := map[types.ATXID]int{}
for _, p := range candidates {
atxs[p.AtxID]++
}
for _, p := range candidates {
if h.atxsdata.IsMalicious(p.SmesherID) || p.IsMalicious() {
h.log.Warn("not voting on proposal from malicious identity",
zap.Stringer("id", p.ID()),
)
continue
}
// double check that a single smesher is not included twice
// theoretically it should never happen as it is covered
// by the malicious check above.
if n := atxs[p.AtxID]; n > 1 {
h.log.Error("proposal with same atx added several times in the recorded set",
zap.Int("n", n),
zap.Stringer("id", p.ID()),
zap.Stringer("atxid", p.AtxID),
)
continue
}
header := h.atxsdata.Get(target, p.AtxID)
if header == nil {
h.log.Error("atx is not loaded", zap.Stringer("atxid", p.AtxID))
return []types.ProposalID{}
}
if header.BaseHeight >= min.Height {
// does not vote for future proposal
h.log.Warn("proposal base tick height too high. skipping",
zap.Uint32("lid", session.lid.Uint32()),
zap.Uint64("proposal_height", header.BaseHeight),
zap.Uint64("min_height", min.Height),
)
continue
}
if p.Beacon() == session.beacon {
result = append(result, p.ID())
} else {
h.log.Warn("proposal has different beacon value",
zap.Uint32("lid", session.lid.Uint32()),
zap.Stringer("id", p.ID()),
zap.String("proposal_beacon", p.Beacon().ShortString()),
zap.String("epoch_beacon", session.beacon.ShortString()),
)
}
}
return result
}
func (h *Hare) IsKnown(layer types.LayerID, proposal types.ProposalID) bool {
return h.proposals.Get(layer, proposal) != nil
}
func (h *Hare) OnProposal(p *types.Proposal) error {
return h.proposals.Add(p)
}
func (h *Hare) Stop() {
h.cancel()
h.eg.Wait()
close(h.results)
close(h.coins)
h.log.Info("stopped")
}
type session struct {
proto *protocol
lid types.LayerID
beacon types.Beacon
signers []*signing.EdSigner
vrfs []*types.HareEligibility
}