2020-03-12 02:27:16 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
2020-07-06 17:22:12 +00:00
|
|
|
"fmt"
|
2020-03-12 02:27:16 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-03-12 02:27:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
// MigrateToCold advances the finalized info in between the cold and hot state sections.
|
2020-03-12 02:27:16 +00:00
|
|
|
// It moves the recent finalized states from the hot section to the cold section and
|
|
|
|
// only preserve the ones that's on archived point.
|
2020-07-09 23:50:48 +00:00
|
|
|
func (s *State) MigrateToCold(ctx context.Context, fRoot [32]byte) error {
|
2020-03-12 02:27:16 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.MigrateToCold")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
s.finalizedInfo.lock.RLock()
|
|
|
|
oldFSlot := s.finalizedInfo.slot
|
|
|
|
s.finalizedInfo.lock.RUnlock()
|
2020-04-11 20:54:19 +00:00
|
|
|
|
2020-07-09 23:50:48 +00:00
|
|
|
fBlock, err := s.beaconDB.Block(ctx, fRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fSlot := fBlock.Block.Slot
|
2020-07-06 17:22:12 +00:00
|
|
|
if oldFSlot > fSlot {
|
|
|
|
return nil
|
2020-03-12 02:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
// Start at previous finalized slot, stop at current finalized slot.
|
|
|
|
// If the slot is on archived point, save the state of that slot to the DB.
|
2020-07-18 18:05:04 +00:00
|
|
|
for slot := oldFSlot; slot < fSlot; slot++ {
|
2020-07-06 17:22:12 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
2020-03-12 02:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:05:04 +00:00
|
|
|
if slot%s.slotsPerArchivedPoint == 0 && slot != 0 {
|
|
|
|
cached, exists, err := s.epochBoundaryStateCache.getBySlot(slot)
|
2020-07-06 17:22:12 +00:00
|
|
|
if err != nil {
|
2020-07-18 18:05:04 +00:00
|
|
|
return fmt.Errorf("could not get epoch boundary state for slot %d", slot)
|
2020-07-06 17:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var aRoot [32]byte
|
|
|
|
var aState *stateTrie.BeaconState
|
|
|
|
|
|
|
|
// When the epoch boundary state is not in cache due to skip slot scenario,
|
|
|
|
// we have to regenerate the state which will represent epoch boundary.
|
|
|
|
// By finding the highest available block below epoch boundary slot, we
|
|
|
|
// generate the state for that block root.
|
|
|
|
if exists {
|
|
|
|
aRoot = cached.root
|
|
|
|
aState = cached.state
|
|
|
|
} else {
|
2020-07-18 18:05:04 +00:00
|
|
|
blks, err := s.beaconDB.HighestSlotBlocksBelow(ctx, slot)
|
2020-07-06 17:22:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Given the block has been finalized, the db should not have more than one block in a given slot.
|
|
|
|
// We should error out when this happens.
|
|
|
|
if len(blks) != 1 {
|
|
|
|
return errUnknownBlock
|
|
|
|
}
|
2020-08-27 18:13:32 +00:00
|
|
|
missingRoot, err := blks[0].Block.HashTreeRoot()
|
2020-07-06 17:22:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
missingState, err := s.StateByRoot(ctx, missingRoot)
|
2020-05-22 01:15:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
aRoot = missingRoot
|
|
|
|
aState = missingState
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
if s.beaconDB.HasState(ctx, aRoot) {
|
2020-04-21 18:18:55 +00:00
|
|
|
continue
|
2020-03-12 02:27:16 +00:00
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
|
|
|
|
if err := s.beaconDB.SaveState(ctx, aState, aRoot); err != nil {
|
2020-03-16 19:07:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
log.WithFields(
|
|
|
|
logrus.Fields{
|
2020-07-18 18:05:04 +00:00
|
|
|
"slot": aState.Slot(),
|
|
|
|
"root": hex.EncodeToString(bytesutil.Trunc(aRoot[:])),
|
2020-07-06 17:22:12 +00:00
|
|
|
}).Info("Saved state in DB")
|
2020-03-12 02:27:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
// Migrate all state summary objects from state summary cache to DB.
|
|
|
|
if err := s.beaconDB.SaveStateSummaries(ctx, s.stateSummaryCache.GetAll()); err != nil {
|
|
|
|
return err
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
s.stateSummaryCache.Clear()
|
2020-05-22 01:15:16 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
// Update finalized info in memory.
|
|
|
|
fInfo, ok, err := s.epochBoundaryStateCache.getByRoot(fRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
if ok {
|
|
|
|
s.SaveFinalizedState(fSlot, fRoot, fInfo.state)
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
return nil
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|