Handle head state for init sync cache state (#4800)

* Don't save nil head state
* Update head
* Don't update head on new att
* Handle initial sync state in DB can be nil
* Don't update head if it's nil
* Merge branch 'master' of git+ssh://github.com/prysmaticlabs/prysm into handle-init-sync-cache-state
* Merge refs/heads/master into handle-init-sync-cache-state
* Update beacon-chain/blockchain/head.go
This commit is contained in:
terence tsao 2020-02-09 08:12:06 -08:00 committed by GitHub
parent 7e0d0502aa
commit bdcd06a708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,9 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"go.opencensus.io/trace"
)
@ -50,6 +52,13 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
return nil
}
// If the head state is not available, just return nil.
// There's nothing to cache
_, cached := s.initSyncState[headRoot]
if !cached && !s.beaconDB.HasState(ctx, headRoot) {
return nil
}
// Get the new head block from DB.
newHead, err := s.beaconDB.Block(ctx, headRoot)
if err != nil {
@ -59,10 +68,28 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
return errors.New("cannot save nil head block")
}
// Get the new head state from DB.
headState, err := s.beaconDB.State(ctx, headRoot)
if err != nil {
return errors.Wrap(err, "could not retrieve head state in DB")
// Get the new head state from cached state or DB.
var headState *state.BeaconState
var exists bool
if featureconfig.Get().InitSyncCacheState {
headState, exists = s.initSyncState[headRoot]
if !exists {
headState, err = s.beaconDB.State(ctx, headRoot)
if err != nil {
return errors.Wrap(err, "could not retrieve head state in DB")
}
if headState == nil {
return errors.New("cannot save nil head state")
}
}
} else {
headState, err = s.beaconDB.State(ctx, headRoot)
if err != nil {
return errors.Wrap(err, "could not retrieve head state in DB")
}
if headState == nil {
return errors.New("cannot save nil head state")
}
}
if headState == nil {
return errors.New("cannot save nil head state")