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
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
2020-03-12 02:27:16 +00:00
|
|
|
"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
|
2022-05-09 22:25:47 +00:00
|
|
|
// only preserves the ones that are 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()
|
|
|
|
|
2022-10-05 19:11:03 +00:00
|
|
|
// When migrating states we choose to acquire the migration lock before
|
|
|
|
// proceeding. This is to prevent multiple migration routines from overwriting each
|
|
|
|
// other.
|
|
|
|
s.migrationLock.Lock()
|
|
|
|
defer s.migrationLock.Unlock()
|
|
|
|
|
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
|
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-09 22:25:47 +00:00
|
|
|
// Start at previous finalized slot, stop at current finalized slot (it will be handled in the next migration).
|
2020-07-06 17:22:12 +00:00
|
|
|
// 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
|
2021-07-23 16:11:21 +00:00
|
|
|
var aState state.BeaconState
|
2020-07-06 17:22:12 +00:00
|
|
|
|
|
|
|
// 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 {
|
2022-06-07 16:47:42 +00:00
|
|
|
_, roots, err := s.beaconDB.HighestRootsBelowSlot(ctx, slot)
|
2020-07-06 17:22:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-03 18:36:06 +00:00
|
|
|
// 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.
|
2022-06-07 16:47:42 +00:00
|
|
|
if len(roots) != 1 {
|
2022-06-03 18:36:06 +00:00
|
|
|
return errUnknownBlock
|
|
|
|
}
|
2022-06-07 16:47:42 +00:00
|
|
|
aRoot = roots[0]
|
2022-05-09 22:25:47 +00:00
|
|
|
// There's no need to generate the state if the state already exists in the DB.
|
2020-10-31 19:01:05 +00:00
|
|
|
// We can skip saving the state.
|
|
|
|
if !s.beaconDB.HasState(ctx, aRoot) {
|
2022-06-07 16:47:42 +00:00
|
|
|
aState, err = s.StateByRoot(ctx, aRoot)
|
2020-10-31 19:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|
2020-10-31 19:01:05 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
if s.beaconDB.HasState(ctx, aRoot) {
|
2022-05-09 22:25:47 +00:00
|
|
|
// If you are migrating a state and its already part of the hot state cache saved to the db,
|
|
|
|
// you can just remove it from the hot state cache as it becomes redundant.
|
2020-10-23 00:35:30 +00:00
|
|
|
s.saveHotStateDB.lock.Lock()
|
2022-05-09 22:25:47 +00:00
|
|
|
roots := s.saveHotStateDB.blockRootsOfSavedStates
|
2020-10-23 00:35:30 +00:00
|
|
|
for i := 0; i < len(roots); i++ {
|
|
|
|
if aRoot == roots[i] {
|
2022-05-09 22:25:47 +00:00
|
|
|
s.saveHotStateDB.blockRootsOfSavedStates = append(roots[:i], roots[i+1:]...)
|
|
|
|
// There shouldn't be duplicated roots in `blockRootsOfSavedStates`.
|
2020-10-23 00:35:30 +00:00
|
|
|
// Break here is ok.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.saveHotStateDB.lock.Unlock()
|
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
|
|
|
// Update finalized info in memory.
|
2022-05-09 22:25:47 +00:00
|
|
|
fInfo, ok, err := s.epochBoundaryStateCache.getByBlockRoot(fRoot)
|
2020-07-06 17:22:12 +00:00
|
|
|
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
|
|
|
}
|