2020-11-06 15:54:20 +00:00
|
|
|
package initialsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-02-16 07:45:34 +00:00
|
|
|
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-11-06 15:54:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
firstBlock := fork.blocks[0].Block()
|
|
|
|
if firstBlock == nil || firstBlock.IsNil() {
|
2020-11-06 15:54:20 +00:00
|
|
|
return errors.New("invalid first block in fork data")
|
|
|
|
}
|
|
|
|
|
|
|
|
blocksPerRequest := q.blocksFetcher.blocksPerSecond
|
|
|
|
if err := q.smm.removeAllStateMachines(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
fsm := q.smm.addStateMachine(firstBlock.Slot())
|
2020-11-06 15:54:20 +00:00
|
|
|
fsm.pid = fork.peer
|
|
|
|
fsm.blocks = fork.blocks
|
|
|
|
fsm.state = stateDataParsed
|
|
|
|
|
|
|
|
// The rest of machines are in skipped state.
|
2021-05-26 16:19:54 +00:00
|
|
|
startSlot := firstBlock.Slot().Add(uint64(len(fork.blocks)))
|
2021-02-16 07:45:34 +00:00
|
|
|
for i := startSlot; i < startSlot.Add(blocksPerRequest*(lookaheadSteps-1)); i += types.Slot(blocksPerRequest) {
|
2020-11-06 15:54:20 +00:00
|
|
|
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).
|
2021-02-16 07:45:34 +00:00
|
|
|
func (q *blocksQueue) resetFromSlot(ctx context.Context, startSlot types.Slot) error {
|
2020-11-06 15:54:20 +00:00
|
|
|
// 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
|
|
|
|
}
|
2021-02-16 07:45:34 +00:00
|
|
|
for i := startSlot; i < startSlot.Add(blocksPerRequest*(lookaheadSteps-1)); i += types.Slot(blocksPerRequest) {
|
2020-11-06 15:54:20 +00:00
|
|
|
q.smm.addStateMachine(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the last (currently activated) state machine to start with best known non-skipped slot.
|
2021-02-16 07:45:34 +00:00
|
|
|
nonSkippedSlot, err := q.blocksFetcher.nonSkippedSlotAfter(ctx, startSlot.Add(blocksPerRequest*(lookaheadSteps-1)-1))
|
2020-11-06 15:54:20 +00:00
|
|
|
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 {
|
2021-02-16 07:45:34 +00:00
|
|
|
nonSkippedSlot = startSlot.Add(blocksPerRequest * (lookaheadSteps - 1))
|
2020-11-06 15:54:20 +00:00
|
|
|
}
|
|
|
|
q.smm.addStateMachine(nonSkippedSlot)
|
|
|
|
return nil
|
|
|
|
}
|