mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
678347e64b
* Make `archivedRoot` handle missing root case * Regression tests * Fixed existing tests * Removed debug log * Merge refs/heads/master into handle-missing-archived-index * Merge refs/heads/master into handle-missing-archived-index * More comments on the look back * Merge branch 'handle-missing-archived-index' of github.com:prysmaticlabs/prysm into handle-missing-archived-index * Merge refs/heads/master into handle-missing-archived-index
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package stategen
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/sirupsen/logrus"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
// 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 {
|
|
return nil
|
|
}
|
|
|
|
if err := s.beaconDB.SaveState(ctx, state, blockRoot); err != nil {
|
|
return err
|
|
}
|
|
archivedIndex := state.Slot() / s.slotsPerArchivedPoint
|
|
if err := s.beaconDB.SaveArchivedPointRoot(ctx, blockRoot, archivedIndex); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.WithFields(logrus.Fields{
|
|
"slot": state.Slot(),
|
|
"blockRoot": hex.EncodeToString(bytesutil.Trunc(blockRoot[:]))}).Info("Saved full state on archived point")
|
|
|
|
return nil
|
|
}
|
|
|
|
// This loads the cold state by block root.
|
|
func (s *State) loadColdStateByRoot(ctx context.Context, blockRoot [32]byte) (*state.BeaconState, error) {
|
|
ctx, span := trace.StartSpan(ctx, "stateGen.loadColdStateByRoot")
|
|
defer span.End()
|
|
|
|
summary, err := s.stateSummary(ctx, blockRoot)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not get state summary")
|
|
}
|
|
|
|
return s.loadColdStateBySlot(ctx, summary.Slot)
|
|
}
|
|
|
|
// 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")
|
|
defer span.End()
|
|
|
|
if slot == 0 {
|
|
return s.beaconDB.GenesisState(ctx)
|
|
}
|
|
|
|
archivedState, err := s.archivedState(ctx, slot)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if archivedState == nil {
|
|
archivedRoot, err := s.archivedRoot(ctx, slot)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
archivedState, err = s.recoverStateByRoot(ctx, archivedRoot)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if archivedState == nil {
|
|
return nil, errUnknownState
|
|
}
|
|
}
|
|
|
|
return s.processStateUpTo(ctx, archivedState, slot)
|
|
}
|