2020-03-12 00:38:30 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-12 12:58:06 +00:00
|
|
|
"encoding/hex"
|
2020-03-12 00:38:30 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2020-03-12 12:58:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-03-12 00:38:30 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
2020-03-12 12:58:06 +00:00
|
|
|
// This saves a pre finalized beacon state in the cold section of the DB. The returns an error
|
|
|
|
// and not store anything if the state does not lie on an archive point boundary.
|
|
|
|
func (s *State) saveColdState(ctx context.Context, blockRoot [32]byte, state *state.BeaconState) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.saveColdState")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
if state.Slot()%s.slotsPerArchivedPoint != 0 {
|
2020-04-21 18:18:55 +00:00
|
|
|
return nil
|
2020-03-12 12:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:28:38 +00:00
|
|
|
if err := s.beaconDB.SaveState(ctx, state, blockRoot); err != nil {
|
2020-03-12 12:58:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-27 20:28:38 +00:00
|
|
|
archivedIndex := state.Slot() / s.slotsPerArchivedPoint
|
|
|
|
if err := s.beaconDB.SaveArchivedPointRoot(ctx, blockRoot, archivedIndex); err != nil {
|
2020-03-12 12:58:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"slot": state.Slot(),
|
|
|
|
"blockRoot": hex.EncodeToString(bytesutil.Trunc(blockRoot[:]))}).Info("Saved full state on archived point")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
// This loads the cold state by block root.
|
2020-03-12 22:27:55 +00:00
|
|
|
func (s *State) loadColdStateByRoot(ctx context.Context, blockRoot [32]byte) (*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.loadColdStateByRoot")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
summary, err := s.stateSummary(ctx, blockRoot)
|
2020-03-12 22:27:55 +00:00
|
|
|
if err != nil {
|
2020-03-30 22:10:45 +00:00
|
|
|
return nil, errors.Wrap(err, "could not get state summary")
|
2020-03-12 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 20:25:37 +00:00
|
|
|
return s.loadColdStateBySlot(ctx, summary.Slot)
|
2020-03-12 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
// This loads a cold state by slot.
|
|
|
|
func (s *State) loadColdStateBySlot(ctx context.Context, slot uint64) (*state.BeaconState, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.loadColdStateBySlot")
|
2020-03-14 15:34:37 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2020-05-06 20:25:37 +00:00
|
|
|
if slot == 0 {
|
|
|
|
return s.beaconDB.GenesisState(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
archivedState, err := s.archivedState(ctx, slot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if archivedState == nil {
|
2020-05-18 19:46:06 +00:00
|
|
|
archivedRoot, err := s.archivedRoot(ctx, slot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 20:25:37 +00:00
|
|
|
archivedState, err = s.recoverStateByRoot(ctx, archivedRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if archivedState == nil {
|
|
|
|
return nil, errUnknownState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.processStateUpTo(ctx, archivedState, slot)
|
2020-03-12 21:04:24 +00:00
|
|
|
}
|