Skip to content

Commit

Permalink
Save init sync cached blocks before regenerate state (#5501)
Browse files Browse the repository at this point in the history
* Revert "Add WaitForSynced to beacon node for validator startup (#5366)"

This reverts commit 1224e75.
* Merge branch 'master' of github.com:prysmaticlabs/prysm
* Revert "Add Separate Network Config (#5454)"

This reverts commit 28733f2.
* Save blocks to db before generating state
* Revert "Revert "Add Separate Network Config (#5454)""

This reverts commit d43c2b7.
* Revert "Revert "Add WaitForSynced to beacon node for validator startup (#5366)""

This reverts commit 927d8d9.
  • Loading branch information
terencechain authored Apr 18, 2020
1 parent fb26177 commit c3217ab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions beacon-chain/blockchain/init_sync_process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot
return nil, err
}
if preState == nil {
if !s.stateGen.HasState(ctx, startRoot) {
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
return nil, errors.Wrap(err, "could not save initial sync blocks")
}
s.clearInitSyncBlocks()
}
preState, err = s.stateGen.StateByRoot(ctx, startRoot)
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions beacon-chain/blockchain/process_attestation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (*sta

var baseState *stateTrie.BeaconState
if !featureconfig.Get().DisableNewStateMgmt {
if !s.stateGen.HasState(ctx, bytesutil.ToBytes32(c.Root)) {
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
return nil, errors.Wrap(err, "could not save initial sync blocks")
}
s.clearInitSyncBlocks()
}
baseState, err = s.stateGen.StateByRoot(ctx, bytesutil.ToBytes32(c.Root))
if err != nil {
return nil, errors.Wrapf(err, "could not get pre state for slot %d", helpers.StartSlot(c.Epoch))
Expand Down Expand Up @@ -129,6 +135,12 @@ func (s *Service) verifyAttestation(ctx context.Context, baseState *stateTrie.Be
var aState *stateTrie.BeaconState
var err error
if !featureconfig.Get().DisableNewStateMgmt {
if !s.stateGen.HasState(ctx, bytesutil.ToBytes32(a.Data.BeaconBlockRoot)) {
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
return nil, errors.Wrap(err, "could not save initial sync blocks")
}
s.clearInitSyncBlocks()
}
aState, err = s.stateGen.StateByRoot(ctx, bytesutil.ToBytes32(a.Data.BeaconBlockRoot))
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/blockchain/process_block_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func (s *Service) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) (
if !s.stateGen.StateSummaryExists(ctx, parentRoot) && !s.beaconDB.HasBlock(ctx, parentRoot) {
return nil, errors.New("could not reconstruct parent state")
}
if !s.stateGen.HasState(ctx, parentRoot) {
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
return nil, errors.Wrap(err, "could not save initial sync blocks")
}
s.clearInitSyncBlocks()
}
preState, err := s.stateGen.StateByRoot(ctx, parentRoot)
if err != nil {
return nil, errors.Wrapf(err, "could not get pre state for slot %d", b.Slot)
Expand Down
9 changes: 9 additions & 0 deletions beacon-chain/state/stategen/hot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"go.opencensus.io/trace"
)

// HasState returns true if the state exists in cache or in DB.
func (s *State) HasState(ctx context.Context, blockRoot [32]byte) bool {
if s.hotStateCache.Has(blockRoot) {
return true
}

return s.beaconDB.HasState(ctx, blockRoot)
}

// 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.
Expand Down

0 comments on commit c3217ab

Please sign in to comment.