-
Notifications
You must be signed in to change notification settings - Fork 112
/
babe.go
478 lines (393 loc) · 11.3 KB
/
babe.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
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package babe
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
ethmetrics "github.com/ethereum/go-ethereum/metrics"
)
var logger = log.NewFromGlobal(log.AddContext("pkg", "babe"))
// Service contains the VRF keys for the validator, as well as BABE configuation data
type Service struct {
ctx context.Context
cancel context.CancelFunc
authority bool
dev bool
// lead is used when setting up a new network from genesis.
// the "lead" node is the node that is designated to build block 1, after which the rest of the nodes
// will sync block 1 and determine the first slot of the network based on it
lead bool
constants constants
epochHandler *epochHandler
// Storage interfaces
blockState BlockState
storageState StorageState
transactionState TransactionState
epochState EpochState
blockImportHandler BlockImportHandler
// BABE authority keypair
keypair *sr25519.Keypair // TODO: change to BABE keystore (#1864)
// State variables
sync.RWMutex
pause chan struct{}
telemetry telemetry.Client
}
// ServiceConfig represents a BABE configuration
type ServiceConfig struct {
LogLvl log.Level
BlockState BlockState
StorageState StorageState
TransactionState TransactionState
EpochState EpochState
BlockImportHandler BlockImportHandler
Keypair *sr25519.Keypair
AuthData []types.Authority
IsDev bool
Authority bool
Lead bool
Telemetry telemetry.Client
}
// NewService returns a new Babe Service using the provided VRF keys and runtime
func NewService(cfg *ServiceConfig) (*Service, error) {
if cfg.Keypair == nil && cfg.Authority {
return nil, errors.New("cannot create BABE service as authority; no keypair provided")
}
if cfg.BlockState == nil {
return nil, ErrNilBlockState
}
if cfg.EpochState == nil {
return nil, errNilEpochState
}
if cfg.BlockImportHandler == nil {
return nil, errNilBlockImportHandler
}
logger.Patch(log.SetLevel(cfg.LogLvl))
slotDuration, err := cfg.EpochState.GetSlotDuration()
if err != nil {
return nil, fmt.Errorf("cannot get slot duration: %w", err)
}
epochLength, err := cfg.EpochState.GetEpochLength()
if err != nil {
return nil, fmt.Errorf("cannot get epoch length: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
babeService := &Service{
ctx: ctx,
cancel: cancel,
blockState: cfg.BlockState,
storageState: cfg.StorageState,
epochState: cfg.EpochState,
keypair: cfg.Keypair,
transactionState: cfg.TransactionState,
pause: make(chan struct{}),
authority: cfg.Authority,
dev: cfg.IsDev,
blockImportHandler: cfg.BlockImportHandler,
lead: cfg.Lead,
constants: constants{
slotDuration: slotDuration,
epochLength: epochLength,
},
telemetry: cfg.Telemetry,
}
logger.Debugf(
"created service with block producer ID=%v, slot duration %s, epoch length (slots) %d",
cfg.Authority, babeService.constants.slotDuration, babeService.constants.epochLength,
)
if cfg.Lead {
logger.Debug("node designated to build block 1")
}
return babeService, nil
}
// Start starts BABE block authoring
func (b *Service) Start() error {
if !b.authority {
return nil
}
// if we aren't leading node, wait for first block
if !b.lead {
if err := b.waitForFirstBlock(); err != nil {
return err
}
}
go b.initiate()
return nil
}
func (b *Service) waitForFirstBlock() error {
head, err := b.blockState.BestBlockHeader()
if err != nil {
return fmt.Errorf("cannot get best block header: %w", err)
}
if head.Number.Uint64() > 0 {
return nil
}
ch := b.blockState.GetImportedBlockNotifierChannel()
defer b.blockState.FreeImportedBlockNotifierChannel(ch)
const firstBlockTimeout = time.Minute * 5
timer := time.NewTimer(firstBlockTimeout)
cleanup := func() {
if !timer.Stop() {
<-timer.C
}
}
// loop until block 1
for {
select {
case block, ok := <-ch:
if !ok {
cleanup()
return errChannelClosed
}
if ok && block.Header.Number.Int64() > 0 {
cleanup()
return nil
}
case <-timer.C:
return errFirstBlockTimeout
case <-b.ctx.Done():
cleanup()
return b.ctx.Err()
}
}
}
// SlotDuration returns the current service slot duration in milliseconds
func (b *Service) SlotDuration() uint64 {
return uint64(b.constants.slotDuration.Milliseconds())
}
// EpochLength returns the current service epoch duration
func (b *Service) EpochLength() uint64 {
return b.constants.epochLength
}
// Pause pauses the service ie. halts block production
func (b *Service) Pause() error {
b.Lock()
defer b.Unlock()
if b.IsPaused() {
return nil
}
close(b.pause)
return nil
}
// Resume resumes the service ie. resumes block production
func (b *Service) Resume() error {
b.Lock()
defer b.Unlock()
if !b.IsPaused() {
return nil
}
b.pause = make(chan struct{})
go b.initiate()
logger.Debug("service resumed")
return nil
}
// IsPaused returns if the service is paused or not (ie. producing blocks)
func (b *Service) IsPaused() bool {
select {
case <-b.pause:
return true
default:
return false
}
}
// Stop stops the service. If stop is called, it cannot be resumed.
func (b *Service) Stop() error {
if !b.authority {
return nil
}
b.Lock()
defer b.Unlock()
if b.ctx.Err() != nil {
return errors.New("service already stopped")
}
ethmetrics.Unregister(buildBlockTimer)
ethmetrics.Unregister(buildBlockErrors)
b.cancel()
return nil
}
// Authorities returns the current BABE authorities
func (b *Service) Authorities() []types.Authority {
auths := make([]types.Authority, len(b.epochHandler.epochData.authorities))
for i, auth := range b.epochHandler.epochData.authorities {
auths[i] = *auth.DeepCopy()
}
return auths
}
// IsStopped returns true if the service is stopped (ie not producing blocks)
func (b *Service) IsStopped() bool {
return b.ctx.Err() != nil
}
func (b *Service) getAuthorityIndex(Authorities []types.Authority) (uint32, error) {
if !b.authority {
return 0, ErrNotAuthority
}
pub := b.keypair.Public()
for i, auth := range Authorities {
if bytes.Equal(pub.Encode(), auth.Key.Encode()) {
return uint32(i), nil
}
}
return 0, fmt.Errorf("key not in BABE authority data")
}
func (b *Service) initiate() {
if b.blockState == nil {
logger.Errorf("block authoring: %s", ErrNilBlockState)
return
}
if b.storageState == nil {
logger.Errorf("block authoring: %s", errNilStorageState)
return
}
// we should consider better error handling for this - we should
// retry to run the engine at some point (maybe the next epoch) if
// there's an error.
if err := b.runEngine(); err != nil {
logger.Criticalf("failed to run block production engine: %s", err)
}
}
func (b *Service) initiateAndGetEpochHandler(epoch uint64) (*epochHandler, error) {
epochData, err := b.initiateEpoch(epoch)
if err != nil {
return nil, fmt.Errorf("failed to initiate epoch %d: %w", epoch, err)
}
logger.Debugf("initiated epoch with threshold %s, randomness 0x%x and authorities %v",
epochData.threshold, epochData.randomness[:], epochData.authorities)
epochStartSlot, err := b.epochState.GetStartSlotForEpoch(epoch)
if err != nil {
return nil, fmt.Errorf("failed to get start slot for current epoch %d: %w", epoch, err)
}
return newEpochHandler(epoch,
epochStartSlot,
epochData,
b.constants,
b.handleSlot,
b.keypair,
)
}
func (b *Service) runEngine() error {
epoch, err := b.epochState.GetCurrentEpoch()
if err != nil {
return fmt.Errorf("failed to get current epoch: %s", err)
}
for {
next, err := b.handleEpoch(epoch)
if errors.Is(err, errServicePaused) || errors.Is(err, context.Canceled) {
return nil
} else if err != nil {
return err
}
epoch = next
}
}
func (b *Service) handleEpoch(epoch uint64) (next uint64, err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
b.epochHandler, err = b.initiateAndGetEpochHandler(epoch)
if err != nil {
return 0, fmt.Errorf("cannot initiate and get epoch handler for epoch %d: %w", epoch, err)
}
// get start slot for current epoch
nextEpochStart, err := b.epochState.GetStartSlotForEpoch(epoch + 1)
if err != nil {
return 0, fmt.Errorf("failed to get start slot for next epoch %d: %w", epoch+1, err)
}
nextEpochStartTime := getSlotStartTime(nextEpochStart, b.constants.slotDuration)
epochTimer := time.NewTimer(time.Until(nextEpochStartTime))
cleanup := func() {
if !epochTimer.Stop() {
<-epochTimer.C
}
}
errCh := make(chan error)
go b.epochHandler.run(ctx, errCh)
select {
case <-b.ctx.Done():
cleanup()
return 0, b.ctx.Err()
case <-b.pause:
cleanup()
return 0, errServicePaused
case <-epochTimer.C:
// stop current epoch handler
cancel()
case err := <-errCh:
cleanup()
logger.Errorf("error from epochHandler: %s", err)
}
// setup next epoch, re-invoke block authoring
next, err = b.incrementEpoch()
if err != nil {
return 0, fmt.Errorf("failed to increment epoch: %w", err)
}
logger.Infof("epoch %d complete, upcoming epoch: %d", epoch, next)
return next, nil
}
func (b *Service) handleSlot(epoch, slotNum uint64, authorityIndex uint32, proof *VrfOutputAndProof) error {
parentHeader, err := b.blockState.BestBlockHeader()
if err != nil {
return err
}
if parentHeader == nil {
return errNilParentHeader
}
// there is a chance that the best block header may change in the course of building the block,
// so let's copy it first.
parent, err := parentHeader.DeepCopy()
if err != nil {
return err
}
currentSlot := Slot{
start: time.Now(),
duration: b.constants.slotDuration,
number: slotNum,
}
b.storageState.Lock()
defer b.storageState.Unlock()
// set runtime trie before building block
// if block building is successful, store the resulting trie in the storage state
ts, err := b.storageState.TrieState(&parent.StateRoot)
if err != nil || ts == nil {
logger.Errorf("failed to get parent trie with parent state root %s: %s", parent.StateRoot, err)
return err
}
hash := parent.Hash()
rt, err := b.blockState.GetRuntime(&hash)
if err != nil {
return err
}
rt.SetContextStorage(ts)
block, err := b.buildBlock(parent, currentSlot, rt, authorityIndex, proof)
if err != nil {
return err
}
logger.Infof(
"built block %d with hash %s, state root %s, epoch %d and slot %d",
block.Header.Number, block.Header.Hash(), block.Header.StateRoot, epoch, slotNum)
logger.Tracef(
"built block with parent hash %s, header %s and body %s",
parent.Hash(), block.Header.String(), block.Body)
b.telemetry.SendMessage(
telemetry.NewPreparedBlockForProposing(
block.Header.Hash(),
block.Header.Number.String(),
),
)
if err := b.blockImportHandler.HandleBlockProduced(block, ts); err != nil {
logger.Warnf("failed to import built block: %s", err)
return err
}
return nil
}
func getCurrentSlot(slotDuration time.Duration) uint64 {
return uint64(time.Now().UnixNano()) / uint64(slotDuration.Nanoseconds())
}
func getSlotStartTime(slot uint64, slotDuration time.Duration) time.Time {
return time.Unix(0, int64(slot)*slotDuration.Nanoseconds())
}