2020-03-14 16:31:21 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-23 00:35:30 +00:00
|
|
|
"math"
|
2020-03-14 16:31:21 +00:00
|
|
|
|
2020-08-27 22:29:59 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2021-03-08 22:37:33 +00:00
|
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
2020-08-27 22:29:59 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-07-28 18:35:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-08-03 18:34:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-10-23 00:35:30 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-03-14 16:31:21 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-08-27 22:29:59 +00:00
|
|
|
// SaveState saves the state in the cache and/or DB.
|
2021-03-08 22:37:33 +00:00
|
|
|
func (s *State) SaveState(ctx context.Context, root [32]byte, st iface.BeaconState) error {
|
2020-03-14 16:31:21 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.SaveState")
|
|
|
|
defer span.End()
|
|
|
|
|
2021-01-04 20:07:12 +00:00
|
|
|
return s.saveStateByRoot(ctx, root, st)
|
2020-03-14 16:31:21 +00:00
|
|
|
}
|
2020-07-28 18:35:17 +00:00
|
|
|
|
|
|
|
// ForceCheckpoint initiates a cold state save of the given state. This method does not update the
|
|
|
|
// "last archived state" but simply saves the specified state from the root argument into the DB.
|
|
|
|
func (s *State) ForceCheckpoint(ctx context.Context, root []byte) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.ForceCheckpoint")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
root32 := bytesutil.ToBytes32(root)
|
2020-08-03 18:34:54 +00:00
|
|
|
// Before the first finalized check point, the finalized root is zero hash.
|
|
|
|
// Return early if there hasn't been a finalized check point.
|
|
|
|
if root32 == params.BeaconConfig().ZeroHash {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:29:59 +00:00
|
|
|
fs, err := s.loadStateByRoot(ctx, root32)
|
2020-07-28 18:35:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:39:23 +00:00
|
|
|
return s.beaconDB.SaveState(ctx, fs, root32)
|
2020-07-28 18:35:17 +00:00
|
|
|
}
|
2020-08-27 22:29:59 +00:00
|
|
|
|
|
|
|
// This saves a post beacon state. On the epoch boundary,
|
|
|
|
// it saves a full state. On an intermediate slot, it saves a back pointer to the
|
|
|
|
// nearest epoch boundary state.
|
2021-03-08 22:37:33 +00:00
|
|
|
func (s *State) saveStateByRoot(ctx context.Context, blockRoot [32]byte, st iface.BeaconState) error {
|
2020-08-27 22:29:59 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.saveStateByRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-10-23 00:35:30 +00:00
|
|
|
// Duration can't be 0 to prevent panic for division.
|
|
|
|
duration := uint64(math.Max(float64(s.saveHotStateDB.duration), 1))
|
|
|
|
|
|
|
|
s.saveHotStateDB.lock.Lock()
|
2021-02-16 07:45:34 +00:00
|
|
|
if s.saveHotStateDB.enabled && st.Slot().Mod(duration) == 0 {
|
2021-01-04 20:07:12 +00:00
|
|
|
if err := s.beaconDB.SaveState(ctx, st, blockRoot); err != nil {
|
2020-10-29 16:14:57 +00:00
|
|
|
s.saveHotStateDB.lock.Unlock()
|
2020-10-23 00:35:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.saveHotStateDB.savedStateRoots = append(s.saveHotStateDB.savedStateRoots, blockRoot)
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
2021-01-04 20:07:12 +00:00
|
|
|
"slot": st.Slot(),
|
2020-10-23 00:35:30 +00:00
|
|
|
"totalHotStateSavedInDB": len(s.saveHotStateDB.savedStateRoots),
|
|
|
|
}).Info("Saving hot state to DB")
|
|
|
|
}
|
|
|
|
s.saveHotStateDB.lock.Unlock()
|
|
|
|
|
2020-08-27 22:29:59 +00:00
|
|
|
// If the hot state is already in cache, one can be sure the state was processed and in the DB.
|
2020-12-17 20:40:47 +00:00
|
|
|
if s.hotStateCache.has(blockRoot) {
|
2020-08-27 22:29:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only on an epoch boundary slot, saves epoch boundary state in epoch boundary root state cache.
|
2021-01-04 20:07:12 +00:00
|
|
|
if helpers.IsEpochStart(st.Slot()) {
|
|
|
|
if err := s.epochBoundaryStateCache.put(blockRoot, st); err != nil {
|
2020-08-27 22:29:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 16:56:21 +00:00
|
|
|
// On an intermediate slots, save state summary.
|
|
|
|
if err := s.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
2021-01-04 20:07:12 +00:00
|
|
|
Slot: st.Slot(),
|
2020-08-27 22:29:59 +00:00
|
|
|
Root: blockRoot[:],
|
2020-12-16 16:56:21 +00:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-27 22:29:59 +00:00
|
|
|
|
|
|
|
// Store the copied state in the hot state cache.
|
2021-01-04 20:07:12 +00:00
|
|
|
s.hotStateCache.put(blockRoot, st)
|
2020-08-27 22:29:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-23 00:35:30 +00:00
|
|
|
|
|
|
|
// EnableSaveHotStateToDB enters the mode that saves hot beacon state to the DB.
|
|
|
|
// This usually gets triggered when there's long duration since finality.
|
|
|
|
func (s *State) EnableSaveHotStateToDB(_ context.Context) {
|
|
|
|
s.saveHotStateDB.lock.Lock()
|
|
|
|
defer s.saveHotStateDB.lock.Unlock()
|
|
|
|
if s.saveHotStateDB.enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.saveHotStateDB.enabled = true
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"enabled": s.saveHotStateDB.enabled,
|
|
|
|
"slotsInterval": s.saveHotStateDB.duration,
|
|
|
|
}).Warn("Entering mode to save hot states in DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableSaveHotStateToDB exits the mode that saves beacon state to DB for the hot states.
|
|
|
|
// This usually gets triggered once there's finality after long duration since finality.
|
|
|
|
func (s *State) DisableSaveHotStateToDB(ctx context.Context) error {
|
|
|
|
s.saveHotStateDB.lock.Lock()
|
|
|
|
defer s.saveHotStateDB.lock.Unlock()
|
|
|
|
if !s.saveHotStateDB.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"enabled": s.saveHotStateDB.enabled,
|
|
|
|
"deletedHotStates": len(s.saveHotStateDB.savedStateRoots),
|
|
|
|
}).Warn("Exiting mode to save hot states in DB")
|
|
|
|
|
|
|
|
// Delete previous saved states in DB as we are turning this mode off.
|
|
|
|
s.saveHotStateDB.enabled = false
|
|
|
|
if err := s.beaconDB.DeleteStates(ctx, s.saveHotStateDB.savedStateRoots); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.saveHotStateDB.savedStateRoots = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|