forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrapper.go
611 lines (515 loc) · 17.5 KB
/
bootstrapper.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package bootstrap
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"go.uber.org/zap"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/proto/pb/p2p"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/snow/choices"
"github.com/MetalBlockchain/metalgo/snow/consensus/snowman"
"github.com/MetalBlockchain/metalgo/snow/engine/common"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
"github.com/MetalBlockchain/metalgo/utils/set"
"github.com/MetalBlockchain/metalgo/utils/timer"
"github.com/MetalBlockchain/metalgo/version"
)
// Parameters for delaying bootstrapping to avoid potential CPU burns
const bootstrappingDelay = 10 * time.Second
var (
_ common.BootstrapableEngine = (*bootstrapper)(nil)
errUnexpectedTimeout = errors.New("unexpected timeout fired")
)
// Invariant: The VM is not guaranteed to be initialized until Start has been
// called, so it must be guaranteed the VM is not used until after Start.
type bootstrapper struct {
Config
// list of NoOpsHandler for messages dropped by bootstrapper
common.StateSummaryFrontierHandler
common.AcceptedStateSummaryHandler
common.PutHandler
common.QueryHandler
common.ChitsHandler
common.AppHandler
common.Bootstrapper
common.Fetcher
*metrics
started bool
// Greatest height of the blocks passed in ForceAccepted
tipHeight uint64
// Height of the last accepted block when bootstrapping starts
startingHeight uint64
// Number of blocks that were fetched on ForceAccepted
initiallyFetched uint64
// Time that ForceAccepted was last called
startTime time.Time
// number of state transitions executed
executedStateTransitions int
parser *parser
awaitingTimeout bool
// fetchFrom is the set of nodes that we can fetch the next container from.
// When a container is fetched, the nodeID is removed from [fetchFrom] to
// attempt to limit a single request to a peer at any given time. When the
// response is received, either and Ancestors or an AncestorsFailed, the
// nodeID will be added back to [fetchFrom] unless the Ancestors message is
// empty. This is to attempt to prevent requesting containers from that peer
// again.
fetchFrom set.Set[ids.NodeID]
// bootstrappedOnce ensures that the [Bootstrapped] callback is only invoked
// once, even if bootstrapping is retried.
bootstrappedOnce sync.Once
}
func New(config Config, onFinished func(ctx context.Context, lastReqID uint32) error) (common.BootstrapableEngine, error) {
metrics, err := newMetrics("bs", config.Ctx.Registerer)
if err != nil {
return nil, err
}
b := &bootstrapper{
Config: config,
metrics: metrics,
StateSummaryFrontierHandler: common.NewNoOpStateSummaryFrontierHandler(config.Ctx.Log),
AcceptedStateSummaryHandler: common.NewNoOpAcceptedStateSummaryHandler(config.Ctx.Log),
PutHandler: common.NewNoOpPutHandler(config.Ctx.Log),
QueryHandler: common.NewNoOpQueryHandler(config.Ctx.Log),
ChitsHandler: common.NewNoOpChitsHandler(config.Ctx.Log),
AppHandler: config.VM,
Fetcher: common.Fetcher{
OnFinished: onFinished,
},
executedStateTransitions: math.MaxInt32,
}
config.Bootstrapable = b
b.Bootstrapper = common.NewCommonBootstrapper(config.Config)
return b, nil
}
func (b *bootstrapper) Start(ctx context.Context, startReqID uint32) error {
b.Ctx.Log.Info("starting bootstrapper")
b.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_SNOWMAN,
State: snow.Bootstrapping,
})
if err := b.VM.SetState(ctx, snow.Bootstrapping); err != nil {
return fmt.Errorf("failed to notify VM that bootstrapping has started: %w",
err)
}
b.parser = &parser{
log: b.Ctx.Log,
numAccepted: b.numAccepted,
numDropped: b.numDropped,
vm: b.VM,
}
if err := b.Blocked.SetParser(ctx, b.parser); err != nil {
return err
}
// Set the starting height
lastAcceptedID, err := b.VM.LastAccepted(ctx)
if err != nil {
return fmt.Errorf("couldn't get last accepted ID: %w", err)
}
lastAccepted, err := b.VM.GetBlock(ctx, lastAcceptedID)
if err != nil {
return fmt.Errorf("couldn't get last accepted block: %w", err)
}
b.startingHeight = lastAccepted.Height()
b.Config.SharedCfg.RequestID = startReqID
if !b.StartupTracker.ShouldStart() {
return nil
}
b.started = true
return b.Startup(ctx)
}
// Ancestors handles the receipt of multiple containers. Should be received in
// response to a GetAncestors message to [nodeID] with request ID [requestID]
func (b *bootstrapper) Ancestors(ctx context.Context, nodeID ids.NodeID, requestID uint32, blks [][]byte) error {
// Make sure this is in response to a request we made
wantedBlkID, ok := b.OutstandingRequests.Remove(nodeID, requestID)
if !ok { // this message isn't in response to a request we made
b.Ctx.Log.Debug("received unexpected Ancestors",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
return nil
}
lenBlks := len(blks)
if lenBlks == 0 {
b.Ctx.Log.Debug("received Ancestors with no block",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
b.markUnavailable(nodeID)
// Send another request for this
return b.fetch(ctx, wantedBlkID)
}
// This node has responded - so add it back into the set
b.fetchFrom.Add(nodeID)
if lenBlks > b.Config.AncestorsMaxContainersReceived {
blks = blks[:b.Config.AncestorsMaxContainersReceived]
b.Ctx.Log.Debug("ignoring containers in Ancestors",
zap.Int("numContainers", lenBlks-b.Config.AncestorsMaxContainersReceived),
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
}
blocks, err := block.BatchedParseBlock(ctx, b.VM, blks)
if err != nil { // the provided blocks couldn't be parsed
b.Ctx.Log.Debug("failed to parse blocks in Ancestors",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
zap.Error(err),
)
return b.fetch(ctx, wantedBlkID)
}
if len(blocks) == 0 {
b.Ctx.Log.Debug("parsing blocks returned an empty set of blocks",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
return b.fetch(ctx, wantedBlkID)
}
requestedBlock := blocks[0]
if actualID := requestedBlock.ID(); actualID != wantedBlkID {
b.Ctx.Log.Debug("first block is not the requested block",
zap.Stringer("expectedBlkID", wantedBlkID),
zap.Stringer("blkID", actualID),
)
return b.fetch(ctx, wantedBlkID)
}
blockSet := make(map[ids.ID]snowman.Block, len(blocks))
for _, block := range blocks[1:] {
blockSet[block.ID()] = block
}
return b.process(ctx, requestedBlock, blockSet)
}
func (b *bootstrapper) GetAncestorsFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
blkID, ok := b.OutstandingRequests.Remove(nodeID, requestID)
if !ok {
b.Ctx.Log.Debug("unexpectedly called GetAncestorsFailed",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
return nil
}
// This node timed out their request, so we can add them back to [fetchFrom]
b.fetchFrom.Add(nodeID)
// Send another request for this
return b.fetch(ctx, blkID)
}
func (b *bootstrapper) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
if err := b.VM.Connected(ctx, nodeID, nodeVersion); err != nil {
return err
}
if err := b.StartupTracker.Connected(ctx, nodeID, nodeVersion); err != nil {
return err
}
// Ensure fetchFrom reflects proper validator list
if b.Beacons.Contains(nodeID) {
b.fetchFrom.Add(nodeID)
}
if b.started || !b.StartupTracker.ShouldStart() {
return nil
}
b.started = true
return b.Startup(ctx)
}
func (b *bootstrapper) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
if err := b.VM.Disconnected(ctx, nodeID); err != nil {
return err
}
if err := b.StartupTracker.Disconnected(ctx, nodeID); err != nil {
return err
}
b.markUnavailable(nodeID)
return nil
}
func (b *bootstrapper) Timeout(ctx context.Context) error {
if !b.awaitingTimeout {
return errUnexpectedTimeout
}
b.awaitingTimeout = false
if !b.Config.BootstrapTracker.IsBootstrapped() {
return b.Restart(ctx, true)
}
b.fetchETA.Set(0)
return b.OnFinished(ctx, b.Config.SharedCfg.RequestID)
}
func (*bootstrapper) Gossip(context.Context) error {
return nil
}
func (b *bootstrapper) Shutdown(ctx context.Context) error {
b.Ctx.Log.Info("shutting down bootstrapper")
return b.VM.Shutdown(ctx)
}
func (b *bootstrapper) Notify(_ context.Context, msg common.Message) error {
if msg != common.StateSyncDone {
b.Ctx.Log.Warn("received an unexpected message from the VM",
zap.Stringer("msg", msg),
)
return nil
}
b.Ctx.StateSyncing.Set(false)
return nil
}
func (b *bootstrapper) HealthCheck(ctx context.Context) (interface{}, error) {
vmIntf, vmErr := b.VM.HealthCheck(ctx)
intf := map[string]interface{}{
"consensus": struct{}{},
"vm": vmIntf,
}
return intf, vmErr
}
func (b *bootstrapper) GetVM() common.VM {
return b.VM
}
func (b *bootstrapper) ForceAccepted(ctx context.Context, acceptedContainerIDs []ids.ID) error {
pendingContainerIDs := b.Blocked.MissingIDs()
// Initialize the fetch from set to the currently preferred peers
b.fetchFrom = b.StartupTracker.PreferredPeers()
// Append the list of accepted container IDs to pendingContainerIDs to ensure
// we iterate over every container that must be traversed.
pendingContainerIDs = append(pendingContainerIDs, acceptedContainerIDs...)
toProcess := make([]snowman.Block, 0, len(pendingContainerIDs))
b.Ctx.Log.Debug("starting bootstrapping",
zap.Int("numPendingBlocks", len(pendingContainerIDs)),
zap.Int("numAcceptedBlocks", len(acceptedContainerIDs)),
)
for _, blkID := range pendingContainerIDs {
b.Blocked.AddMissingID(blkID)
// TODO: if `GetBlock` returns an error other than
// `database.ErrNotFound`, then the error should be propagated.
blk, err := b.VM.GetBlock(ctx, blkID)
if err != nil {
if err := b.fetch(ctx, blkID); err != nil {
return err
}
continue
}
toProcess = append(toProcess, blk)
}
b.initiallyFetched = b.Blocked.PendingJobs()
b.startTime = time.Now()
// Process received blocks
for _, blk := range toProcess {
if err := b.process(ctx, blk, nil); err != nil {
return err
}
}
return b.checkFinish(ctx)
}
// Get block [blkID] and its ancestors from a validator
func (b *bootstrapper) fetch(ctx context.Context, blkID ids.ID) error {
// Make sure we haven't already requested this block
if b.OutstandingRequests.Contains(blkID) {
return nil
}
// Make sure we don't already have this block
if _, err := b.VM.GetBlock(ctx, blkID); err == nil {
return b.checkFinish(ctx)
}
validatorID, ok := b.fetchFrom.Peek()
if !ok {
return fmt.Errorf("dropping request for %s as there are no validators", blkID)
}
// We only allow one outbound request at a time from a node
b.markUnavailable(validatorID)
b.Config.SharedCfg.RequestID++
b.OutstandingRequests.Add(validatorID, b.Config.SharedCfg.RequestID, blkID)
b.Config.Sender.SendGetAncestors(ctx, validatorID, b.Config.SharedCfg.RequestID, blkID) // request block and ancestors
return nil
}
// markUnavailable removes [nodeID] from the set of peers used to fetch
// ancestors. If the set becomes empty, it is reset to the currently preferred
// peers so bootstrapping can continue.
func (b *bootstrapper) markUnavailable(nodeID ids.NodeID) {
b.fetchFrom.Remove(nodeID)
// if [fetchFrom] has become empty, reset it to the currently preferred
// peers
if b.fetchFrom.Len() == 0 {
b.fetchFrom = b.StartupTracker.PreferredPeers()
}
}
func (b *bootstrapper) Clear() error {
if err := b.Config.Blocked.Clear(); err != nil {
return err
}
return b.Config.Blocked.Commit()
}
// process a series of consecutive blocks starting at [blk].
//
// - blk is a block that is assumed to have been marked as acceptable by the
// bootstrapping engine.
// - processingBlocks is a set of blocks that can be used to lookup blocks.
// This enables the engine to process multiple blocks without relying on the
// VM to have stored blocks during `ParseBlock`.
//
// If [blk]'s height is <= the last accepted height, then it will be removed
// from the missingIDs set.
func (b *bootstrapper) process(ctx context.Context, blk snowman.Block, processingBlocks map[ids.ID]snowman.Block) error {
for {
blkID := blk.ID()
if b.Halted() {
// We must add in [blkID] to the set of missing IDs so that we are
// guaranteed to continue processing from this state when the
// bootstrapper is restarted.
b.Blocked.AddMissingID(blkID)
return b.Blocked.Commit()
}
b.Blocked.RemoveMissingID(blkID)
status := blk.Status()
// The status should never be rejected here - but we check to fail as
// quickly as possible
if status == choices.Rejected {
return fmt.Errorf("bootstrapping wants to accept %s, however it was previously rejected", blkID)
}
blkHeight := blk.Height()
if status == choices.Accepted || blkHeight <= b.startingHeight {
// We can stop traversing, as we have reached the accepted frontier
if err := b.Blocked.Commit(); err != nil {
return err
}
return b.checkFinish(ctx)
}
// If this block is going to be accepted, make sure to update the
// tipHeight for logging
if blkHeight > b.tipHeight {
b.tipHeight = blkHeight
}
pushed, err := b.Blocked.Push(ctx, &blockJob{
log: b.Ctx.Log,
numAccepted: b.numAccepted,
numDropped: b.numDropped,
blk: blk,
vm: b.VM,
})
if err != nil {
return err
}
if !pushed {
// We can stop traversing, as we have reached a block that we
// previously pushed onto the jobs queue
if err := b.Blocked.Commit(); err != nil {
return err
}
return b.checkFinish(ctx)
}
// We added a new block to the queue, so track that it was fetched
b.numFetched.Inc()
// Periodically log progress
blocksFetchedSoFar := b.Blocked.Jobs.PendingJobs()
if blocksFetchedSoFar%common.StatusUpdateFrequency == 0 {
totalBlocksToFetch := b.tipHeight - b.startingHeight
eta := timer.EstimateETA(
b.startTime,
blocksFetchedSoFar-b.initiallyFetched, // Number of blocks we have fetched during this run
totalBlocksToFetch-b.initiallyFetched, // Number of blocks we expect to fetch during this run
)
b.fetchETA.Set(float64(eta))
if !b.Config.SharedCfg.Restarted {
b.Ctx.Log.Info("fetching blocks",
zap.Uint64("numFetchedBlocks", blocksFetchedSoFar),
zap.Uint64("numTotalBlocks", totalBlocksToFetch),
zap.Duration("eta", eta),
)
} else {
b.Ctx.Log.Debug("fetching blocks",
zap.Uint64("numFetchedBlocks", blocksFetchedSoFar),
zap.Uint64("numTotalBlocks", totalBlocksToFetch),
zap.Duration("eta", eta),
)
}
}
// Attempt to traverse to the next block
parentID := blk.Parent()
// First check if the parent is in the processing blocks set
parent, ok := processingBlocks[parentID]
if ok {
blk = parent
continue
}
// If the parent is not available in processing blocks, attempt to get
// the block from the vm
parent, err = b.VM.GetBlock(ctx, parentID)
if err == nil {
blk = parent
continue
}
// TODO: report errors that aren't `database.ErrNotFound`
// If the block wasn't able to be acquired immediately, attempt to fetch
// it
b.Blocked.AddMissingID(parentID)
if err := b.fetch(ctx, parentID); err != nil {
return err
}
if err := b.Blocked.Commit(); err != nil {
return err
}
return b.checkFinish(ctx)
}
}
// checkFinish repeatedly executes pending transactions and requests new frontier vertices until there aren't any new ones
// after which it finishes the bootstrap process
func (b *bootstrapper) checkFinish(ctx context.Context) error {
if numPending := b.Blocked.NumMissingIDs(); numPending != 0 {
return nil
}
if b.IsBootstrapped() || b.awaitingTimeout {
return nil
}
if !b.Config.SharedCfg.Restarted {
b.Ctx.Log.Info("executing blocks",
zap.Uint64("numPendingJobs", b.Blocked.PendingJobs()),
)
} else {
b.Ctx.Log.Debug("executing blocks",
zap.Uint64("numPendingJobs", b.Blocked.PendingJobs()),
)
}
executedBlocks, err := b.Blocked.ExecuteAll(
ctx,
b.Config.Ctx,
b,
b.Config.SharedCfg.Restarted,
b.Ctx.BlockAcceptor,
)
if err != nil || b.Halted() {
return err
}
previouslyExecuted := b.executedStateTransitions
b.executedStateTransitions = executedBlocks
// Note that executedBlocks < c*previouslyExecuted ( 0 <= c < 1 ) is enforced
// so that the bootstrapping process will terminate even as new blocks are
// being issued.
if b.Config.RetryBootstrap && executedBlocks > 0 && executedBlocks < previouslyExecuted/2 {
return b.Restart(ctx, true)
}
// If there is an additional callback, notify them that this chain has been
// synced.
if b.Bootstrapped != nil {
b.bootstrappedOnce.Do(func() {
b.Bootstrapped()
})
}
// Notify the subnet that this chain is synced
b.Config.BootstrapTracker.Bootstrapped(b.Ctx.ChainID)
// If the subnet hasn't finished bootstrapping, this chain should remain
// syncing.
if !b.Config.BootstrapTracker.IsBootstrapped() {
if !b.Config.SharedCfg.Restarted {
b.Ctx.Log.Info("waiting for the remaining chains in this subnet to finish syncing")
} else {
b.Ctx.Log.Debug("waiting for the remaining chains in this subnet to finish syncing")
}
// Restart bootstrapping after [bootstrappingDelay] to keep up to date
// on the latest tip.
b.Config.Timer.RegisterTimeout(bootstrappingDelay)
b.awaitingTimeout = true
return nil
}
b.fetchETA.Set(0)
return b.OnFinished(ctx, b.Config.SharedCfg.RequestID)
}