-
Notifications
You must be signed in to change notification settings - Fork 211
/
hare.go
442 lines (376 loc) · 13 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
package hare
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/go-spacemesh/blocks"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/hare/config"
"github.com/spacemeshos/go-spacemesh/hare/metrics"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
)
// LayerBuffer is the number of layer results we keep at a given time.
const LayerBuffer = 20
type consensusFactory func(cfg config.Config, instanceId types.LayerID, s *Set, oracle Rolacle, signing Signer, p2p pubsub.Publisher, clock RoundClock, terminationReport chan TerminationOutput) Consensus
// Consensus represents an item that acts like a consensus process.
type Consensus interface {
ID() types.LayerID
Close()
CloseChannel() chan struct{}
Start(ctx context.Context) error
SetInbox(chan *Msg)
}
// TerminationOutput represents an output of a consensus process.
type TerminationOutput interface {
ID() types.LayerID
Set() *Set
Coinflip() bool
Completed() bool
}
// RoundClock is a timer interface.
type RoundClock interface {
AwaitWakeup() <-chan struct{}
AwaitEndOfRound(round uint32) <-chan struct{}
}
// LayerClock provides a timer for the start of a given layer, as well as the current layer and allows converting a
// layer number to a clock time.
type LayerClock interface {
LayerToTime(id types.LayerID) time.Time
AwaitLayer(layerID types.LayerID) chan struct{}
GetCurrentLayer() types.LayerID
}
// Hare is the orchestrator that starts new consensus processes and collects their output.
type Hare struct {
util.Closer
log.Log
config config.Config
publisher pubsub.Publisher
layerClock LayerClock
newRoundClock func(LayerID types.LayerID) RoundClock
broker *Broker
sign Signer
mesh meshProvider
beacons blocks.BeaconGetter
rolacle Rolacle
patrol layerPatrol
networkDelta time.Duration
layerLock sync.RWMutex
lastLayer types.LayerID
bufferSize uint32
outputChan chan TerminationOutput
mu sync.RWMutex
outputs map[types.LayerID][]types.BlockID
factory consensusFactory
nid types.NodeID
totalCPs int32
}
// New returns a new Hare struct.
func New(
conf config.Config,
pid peer.ID,
publisher pubsub.PublishSubsciber,
sign Signer,
nid types.NodeID,
syncState syncStateFunc,
mesh meshProvider,
beacons blocks.BeaconGetter,
rolacle Rolacle,
patrol layerPatrol,
layersPerEpoch uint16,
idProvider identityProvider,
stateQ StateQuerier,
layerClock LayerClock,
logger log.Log,
) *Hare {
h := new(Hare)
h.Closer = util.NewCloser()
h.Log = logger
h.config = conf
h.publisher = publisher
h.layerClock = layerClock
h.newRoundClock = func(layerID types.LayerID) RoundClock {
layerTime := layerClock.LayerToTime(layerID)
wakeupDelta := time.Duration(conf.WakeupDelta) * time.Second
roundDuration := time.Duration(h.config.RoundDuration) * time.Second
h.With().Info("creating hare round clock", layerID,
log.String("layer_time", layerTime.String()),
log.Duration("wakeup_delta", wakeupDelta),
log.Duration("round_duration", roundDuration))
return NewSimpleRoundClock(layerTime, wakeupDelta, roundDuration)
}
ev := newEligibilityValidator(rolacle, layersPerEpoch, idProvider, conf.N, conf.ExpectedLeaders, logger)
h.broker = newBroker(pid, ev, stateQ, syncState, layersPerEpoch, conf.LimitConcurrent, h.Closer, logger)
publisher.Register(protoName, h.broker.HandleMessage)
h.sign = sign
h.mesh = mesh
h.beacons = beacons
h.rolacle = rolacle
h.patrol = patrol
h.networkDelta = time.Duration(conf.WakeupDelta) * time.Second
// todo: this should be loaded from global config
h.bufferSize = LayerBuffer // XXX: must be at least the size of `hdist`
h.outputChan = make(chan TerminationOutput, h.bufferSize)
h.outputs = make(map[types.LayerID][]types.BlockID, h.bufferSize) // we keep results about LayerBuffer past layers
h.factory = func(conf config.Config, instanceId types.LayerID, s *Set, oracle Rolacle, signing Signer, p2p pubsub.Publisher, clock RoundClock, terminationReport chan TerminationOutput) Consensus {
return newConsensusProcess(conf, instanceId, s, oracle, stateQ, layersPerEpoch, signing, nid, p2p, terminationReport, ev, clock, logger)
}
h.nid = nid
return h
}
func (h *Hare) getLastLayer() types.LayerID {
h.layerLock.RLock()
defer h.layerLock.RUnlock()
return h.lastLayer
}
// checks if the provided id is too late/old to be requested.
func (h *Hare) outOfBufferRange(id types.LayerID) bool {
last := h.getLastLayer()
if !last.After(types.NewLayerID(h.bufferSize)) {
return false
}
if id.Before(last.Sub(h.bufferSize)) { // bufferSize>=0
return true
}
return false
}
func (h *Hare) oldestResultInBuffer() types.LayerID {
// buffer is usually quite small so its cheap to iterate.
// TODO: if it gets bigger change `outputs` to array.
lyr := h.getLastLayer()
for k := range h.outputs {
if k.Before(lyr) {
lyr = k
}
}
return lyr
}
// ErrTooLate means that the consensus was terminated too late.
var ErrTooLate = errors.New("consensus process finished too late")
// records the provided output.
func (h *Hare) collectOutput(ctx context.Context, output TerminationOutput) error {
set := output.Set()
blocks := make([]types.BlockID, len(set.values))
i := 0
for v := range set.values {
blocks[i] = v
i++
}
id := output.ID()
h.mesh.HandleValidatedLayer(ctx, id, blocks)
if h.outOfBufferRange(id) {
return ErrTooLate
}
h.mu.Lock()
defer h.mu.Unlock()
if uint32(len(h.outputs)) >= h.bufferSize {
delete(h.outputs, h.oldestResultInBuffer())
}
h.outputs[id] = blocks
return nil
}
// the logic that happens when a new layer arrives.
// this function triggers the start of new consensus processes.
func (h *Hare) onTick(ctx context.Context, id types.LayerID) (bool, error) {
logger := h.WithContext(ctx).WithFields(id)
if h.IsClosed() {
logger.Info("hare exiting")
return false, nil
}
h.layerLock.Lock()
if id.After(h.lastLayer) {
h.lastLayer = id
} else {
logger.With().Error("received out of order layer tick", log.FieldNamed("last_layer", h.lastLayer))
}
h.layerLock.Unlock()
if id.GetEpoch().IsGenesis() {
logger.Info("not starting hare since we are in genesis epoch")
return false, nil
}
var err error
defer func() {
// it must not return without starting consensus process or mark result as fail
// except if it's genesis layer
if err != nil {
h.outputChan <- procReport{id, &Set{}, false, notCompleted}
}
}()
beacon, err := h.beacons.GetBeacon(id.GetEpoch())
if err != nil {
logger.Info("not starting hare since beacon is not retrieved")
return false, nil
}
// call to start the calculation of active set size beforehand
go func() {
// this is called only for its side effects, but at least print the error if it returns one
if isActive, err := h.rolacle.IsIdentityActiveOnConsensusView(ctx, h.nid.Key, id); err != nil {
logger.With().Error("error checking if identity is active",
log.Bool("isActive", isActive), log.Err(err))
}
}()
logger.With().Debug("hare got tick, sleeping", log.String("delta", fmt.Sprint(h.networkDelta)))
clock := h.newRoundClock(id)
select {
case <-clock.AwaitWakeup():
break // keep going
case <-h.CloseChannel():
return false, errors.New("closed while waiting for hare delta")
}
if !h.broker.Synced(ctx, id) {
// if not currently synced don't start consensus process
logger.Info("not processing hare tick since node is not synced")
return false, nil
}
blocks := h.getGoodBlocks(h.lastLayer, beacon, logger)
logger.With().Info("starting hare consensus with blocks", log.Int("num_blocks", len(blocks)))
set := NewSet(blocks)
instID := id
c, err := h.broker.Register(ctx, instID)
if err != nil {
logger.With().Error("could not register consensus process on broker", log.Err(err))
return false, fmt.Errorf("broker register: %w", err)
}
cp := h.factory(h.config, instID, set, h.rolacle, h.sign, h.publisher, clock, h.outputChan)
cp.SetInbox(c)
if err = cp.Start(ctx); err != nil {
logger.With().Error("could not start consensus process", log.Err(err))
h.broker.Unregister(ctx, cp.ID())
return false, fmt.Errorf("start consensus: %w", err)
}
h.patrol.SetHareInCharge(instID)
logger.With().Info("number of consensus processes (after register)",
log.Int32("count", atomic.AddInt32(&h.totalCPs, 1)))
metrics.TotalConsensusProcesses.With(prometheus.Labels{"layer": id.String()}).Inc()
return true, nil
}
// getGoodBlocks finds the "good blocks" for the specified layer. a block is considered a good block if
// it has the same beacon value as the node's beacon value.
// any error encountered will be ignored and an empty set is returned.
func (h *Hare) getGoodBlocks(lyrID types.LayerID, epochBeacon []byte, logger log.Log) []types.BlockID {
blocks, err := h.mesh.LayerBlocks(lyrID)
if err != nil {
if err != database.ErrNotFound {
logger.With().Error("no blocks found for hare, using empty set", log.Err(err))
}
return []types.BlockID{}
}
var goodBlocks []types.BlockID
for _, b := range blocks {
beacon := b.TortoiseBeacon
if b.RefBlock != nil {
refBlock, err := h.mesh.GetBlock(*b.RefBlock)
if err != nil {
logger.With().Error("failed to find ref block",
b.ID(),
log.String("ref_block_id", b.RefBlock.AsHash32().ShortString()))
return []types.BlockID{}
}
beacon = refBlock.TortoiseBeacon
}
if bytes.Equal(beacon, epochBeacon) {
goodBlocks = append(goodBlocks, b.ID())
} else {
logger.With().Warning("block has different beacon value",
b.ID(),
log.String("block_beacon", types.BytesToHash(beacon).ShortString()),
log.String("epoch_beacon", types.BytesToHash(epochBeacon).ShortString()))
}
}
return goodBlocks
}
var (
errTooOld = errors.New("layer has already been evacuated from buffer")
errNoResult = errors.New("no result for the requested layer")
)
// GetResult returns the hare output for the provided range.
// Returns error if the requested layer is too old.
func (h *Hare) GetResult(lid types.LayerID) ([]types.BlockID, error) {
if h.outOfBufferRange(lid) {
return nil, errTooOld
}
h.mu.RLock()
defer h.mu.RUnlock()
blks, ok := h.outputs[lid]
if !ok {
return nil, errNoResult
}
return blks, nil
}
// listens to outputs arriving from consensus processes.
func (h *Hare) outputCollectionLoop(ctx context.Context) {
h.WithContext(ctx).With().Info("starting collection loop")
for {
select {
case out := <-h.outputChan:
layerID := out.ID()
coin := out.Coinflip()
ctx := log.WithNewSessionID(ctx)
logger := h.WithContext(ctx).WithFields(layerID)
// collect coinflip, regardless of success
logger.With().Info("recording weak coinflip result for layer",
log.Bool("coinflip", coin))
h.mesh.RecordCoinflip(ctx, layerID, coin)
if out.Completed() { // CP completed, collect the output
logger.With().Info("collecting results for completed hare instance", layerID)
if err := h.collectOutput(ctx, out); err != nil {
logger.With().Warning("error collecting output from hare", log.Err(err))
}
} else {
// Notify the mesh that Hare failed
h.WithContext(ctx).With().Info("recording hare instance failure", layerID)
h.mesh.InvalidateLayer(ctx, layerID)
}
h.broker.Unregister(ctx, out.ID())
logger.With().Info("number of consensus processes (after unregister)",
log.Int32("count", atomic.AddInt32(&h.totalCPs, -1)))
metrics.TotalConsensusProcesses.With(prometheus.Labels{"layer": out.ID().String()}).Dec()
case <-h.CloseChannel():
return
}
}
}
// listens to new layers.
func (h *Hare) tickLoop(ctx context.Context) {
for layer := h.layerClock.GetCurrentLayer(); ; layer = layer.Add(1) {
select {
case <-h.layerClock.AwaitLayer(layer):
if h.layerClock.LayerToTime(layer).Sub(time.Now()) > (time.Duration(h.config.WakeupDelta) * time.Second) {
h.With().Warning("missed hare window, skipping layer", layer)
continue
}
go func(l types.LayerID) {
started, err := h.onTick(ctx, l)
if err != nil {
h.With().Error("failed to handle tick", log.Err(err))
} else if !started {
h.WithContext(ctx).With().Warning("consensus not started for layer", layer)
}
}(layer)
case <-h.CloseChannel():
return
}
}
}
// Start starts listening for layers and outputs.
func (h *Hare) Start(ctx context.Context) error {
h.WithContext(ctx).With().Info("starting protocol", log.String("protocol", protoName))
// Create separate contexts for each subprocess. This allows us to better track the flow of messages.
ctxBroker := log.WithNewSessionID(ctx, log.String("protocol", protoName+"_broker"))
ctxTickLoop := log.WithNewSessionID(ctx, log.String("protocol", protoName+"_tickloop"))
ctxOutputLoop := log.WithNewSessionID(ctx, log.String("protocol", protoName+"_outputloop"))
if err := h.broker.Start(ctxBroker); err != nil {
return fmt.Errorf("start broker: %w", err)
}
go h.tickLoop(ctxTickLoop)
go h.outputCollectionLoop(ctxOutputLoop)
return nil
}