-
Notifications
You must be signed in to change notification settings - Fork 3
/
blocks_queue_utils.go
73 lines (67 loc) · 2.49 KB
/
blocks_queue_utils.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
package initialsync
import (
"context"
"errors"
)
// resetWithBlocks removes all state machines, then re-adds enough machines to contain all provided
// blocks (machines are set into stateDataParsed state, so that their content is immediately
// consumable). It is assumed that blocks come in an ascending order.
func (q *blocksQueue) resetFromFork(ctx context.Context, fork *forkData) error {
if fork == nil {
return errors.New("nil fork data")
}
if len(fork.blocks) == 0 {
return errors.New("no blocks to reset from")
}
firstBlock := fork.blocks[0].Block
if firstBlock == nil {
return errors.New("invalid first block in fork data")
}
blocksPerRequest := q.blocksFetcher.blocksPerSecond
if err := q.smm.removeAllStateMachines(); err != nil {
return err
}
fsm := q.smm.addStateMachine(firstBlock.Slot)
fsm.pid = fork.peer
fsm.blocks = fork.blocks
fsm.state = stateDataParsed
// The rest of machines are in skipped state.
startSlot := firstBlock.Slot + uint64(len(fork.blocks))
for i := startSlot; i < startSlot+blocksPerRequest*(lookaheadSteps-1); i += blocksPerRequest {
fsm := q.smm.addStateMachine(i)
fsm.state = stateSkipped
}
return nil
}
// resetFromSlot removes all state machines, and re-adds them starting with a given slot.
// The last machine added relies on calculated non-skipped slot (to allow FSMs to jump over
// long periods with skipped slots).
func (q *blocksQueue) resetFromSlot(ctx context.Context, startSlot uint64) error {
// Shift start position of all the machines except for the last one.
blocksPerRequest := q.blocksFetcher.blocksPerSecond
if err := q.smm.removeAllStateMachines(); err != nil {
return err
}
for i := startSlot; i < startSlot+blocksPerRequest*(lookaheadSteps-1); i += blocksPerRequest {
q.smm.addStateMachine(i)
}
// Replace the last (currently activated) state machine to start with best known non-skipped slot.
nonSkippedSlot, err := q.blocksFetcher.nonSkippedSlotAfter(ctx, startSlot+blocksPerRequest*(lookaheadSteps-1)-1)
if err != nil {
return err
}
if q.mode == modeStopOnFinalizedEpoch {
if q.highestExpectedSlot < q.blocksFetcher.bestFinalizedSlot() {
q.highestExpectedSlot = q.blocksFetcher.bestFinalizedSlot()
}
} else {
if q.highestExpectedSlot < q.blocksFetcher.bestNonFinalizedSlot() {
q.highestExpectedSlot = q.blocksFetcher.bestNonFinalizedSlot()
}
}
if nonSkippedSlot > q.highestExpectedSlot {
nonSkippedSlot = startSlot + blocksPerRequest*(lookaheadSteps-1)
}
q.smm.addStateMachine(nonSkippedSlot)
return nil
}