2020-03-08 06:24:57 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-12 20:48:07 +00:00
|
|
|
"encoding/hex"
|
2020-03-08 06:24:57 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-03-09 16:22:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-03-08 06:24:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-03-12 20:48:07 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-03-08 06:24:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-03-12 20:48:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-03-08 06:24:57 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-03-12 20:48:07 +00:00
|
|
|
// This saves a post finalized beacon state in the hot section of the DB. On the epoch boundary,
|
|
|
|
// it saves a full state. On an intermediate slot, it saves a back pointer to the
|
|
|
|
// nearest epoch boundary state.
|
|
|
|
func (s *State) saveHotState(ctx context.Context, blockRoot [32]byte, state *state.BeaconState) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.saveHotState")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
// If the hot state is already in cache, one can be sure the state was processed and in the DB.
|
|
|
|
if s.hotStateCache.Has(blockRoot) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only on an epoch boundary slot, saves the whole state.
|
|
|
|
if helpers.IsEpochStart(state.Slot()) {
|
|
|
|
if err := s.beaconDB.SaveState(ctx, state, blockRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"slot": state.Slot(),
|
|
|
|
"blockRoot": hex.EncodeToString(bytesutil.Trunc(blockRoot[:]))}).Info("Saved full state on epoch boundary")
|
|
|
|
}
|
|
|
|
|
|
|
|
// On an intermediate slots, save the hot state summary.
|
2020-03-30 22:10:45 +00:00
|
|
|
s.stateSummaryCache.Put(blockRoot, &pb.StateSummary{
|
2020-03-27 20:28:38 +00:00
|
|
|
Slot: state.Slot(),
|
|
|
|
Root: blockRoot[:],
|
2020-03-30 22:10:45 +00:00
|
|
|
})
|
2020-03-12 20:48:07 +00:00
|
|
|
|
|
|
|
// Store the copied state in the cache.
|
2020-03-27 20:28:38 +00:00
|
|
|
s.hotStateCache.Put(blockRoot, state)
|
2020-03-12 20:48:07 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 06:24:57 +00:00
|
|
|
// This loads a post finalized beacon state from the hot section of the DB. If necessary it will
|
|
|
|
// replay blocks starting from the nearest epoch boundary. It returns the beacon state that
|
|
|
|
// corresponds to the input block root.
|
|
|
|
func (s *State) loadHotStateByRoot(ctx context.Context, blockRoot [32]byte) (*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.loadHotStateByRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
// Load the hot state from cache.
|
2020-03-08 06:24:57 +00:00
|
|
|
cachedState := s.hotStateCache.Get(blockRoot)
|
|
|
|
if cachedState != nil {
|
|
|
|
return cachedState, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
// Load the hot state from DB.
|
|
|
|
if s.beaconDB.HasState(ctx, blockRoot) {
|
|
|
|
return s.beaconDB.State(ctx, blockRoot)
|
2020-03-08 06:24:57 +00:00
|
|
|
}
|
2020-03-30 22:10:45 +00:00
|
|
|
|
|
|
|
summary, err := s.stateSummary(ctx, blockRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get state summary")
|
2020-03-08 06:24:57 +00:00
|
|
|
}
|
2020-03-16 19:07:07 +00:00
|
|
|
|
2020-04-17 02:06:23 +00:00
|
|
|
// Since the hot state is not in cache nor DB, start replaying using the parent state which is
|
|
|
|
// retrieved using input block's parent root.
|
|
|
|
blk, err := s.beaconDB.Block(ctx, blockRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if blk == nil {
|
|
|
|
return nil, errUnknownBlock
|
|
|
|
}
|
|
|
|
startState, err := s.loadHotStateByRoot(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot))
|
2020-03-08 06:24:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 20:28:38 +00:00
|
|
|
if startState == nil {
|
|
|
|
return nil, errUnknownBoundaryState
|
2020-03-08 06:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:28:38 +00:00
|
|
|
// Don't need to replay the blocks if start state is the same state for the block root.
|
2020-03-08 06:24:57 +00:00
|
|
|
var hotState *state.BeaconState
|
|
|
|
targetSlot := summary.Slot
|
2020-03-27 20:28:38 +00:00
|
|
|
if targetSlot == startState.Slot() {
|
|
|
|
hotState = startState
|
2020-03-08 06:24:57 +00:00
|
|
|
} else {
|
2020-03-27 20:28:38 +00:00
|
|
|
blks, err := s.LoadBlocks(ctx, startState.Slot()+1, targetSlot, bytesutil.ToBytes32(summary.Root))
|
2020-03-08 06:24:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not load blocks for hot state using root")
|
|
|
|
}
|
2020-03-27 20:28:38 +00:00
|
|
|
hotState, err = s.ReplayBlocks(ctx, startState, blks, targetSlot)
|
2020-03-08 06:24:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not replay blocks for hot state using root")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the copied state because the reference also returned in the end.
|
|
|
|
s.hotStateCache.Put(blockRoot, hotState.Copy())
|
|
|
|
|
|
|
|
return hotState, nil
|
|
|
|
}
|
2020-03-09 16:22:45 +00:00
|
|
|
|
|
|
|
// This loads a hot state by slot where the slot lies between the epoch boundary points.
|
|
|
|
// This is a slower implementation (versus ByRoot) as slot is the only argument. It require fetching
|
|
|
|
// all the blocks between the epoch boundary points for playback.
|
|
|
|
// Use `loadHotStateByRoot` unless you really don't know the root.
|
|
|
|
func (s *State) loadHotStateBySlot(ctx context.Context, slot uint64) (*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.loadHotStateBySlot")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
// Return genesis state if slot is 0.
|
|
|
|
if slot == 0 {
|
|
|
|
return s.beaconDB.GenesisState(ctx)
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:28:38 +00:00
|
|
|
// Gather last saved state, that is where node starts to replay the blocks.
|
|
|
|
startState, err := s.lastSavedState(ctx, slot)
|
2020-03-09 16:22:45 +00:00
|
|
|
|
|
|
|
// Gather the last saved block root and the slot number.
|
|
|
|
lastValidRoot, lastValidSlot, err := s.lastSavedBlock(ctx, slot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get last valid block for hot state using slot")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load and replay blocks to get the intermediate state.
|
2020-03-27 20:28:38 +00:00
|
|
|
replayBlks, err := s.LoadBlocks(ctx, startState.Slot()+1, lastValidSlot, lastValidRoot)
|
2020-03-09 16:22:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:28:38 +00:00
|
|
|
return s.ReplayBlocks(ctx, startState, replayBlks, slot)
|
2020-03-12 20:00:37 +00:00
|
|
|
}
|