Skip to content

Commit dbd1e8c

Browse files
terencechainnisdas
andauthored
Only cache check point state if it hasn't existed in other cache (#7037)
* Only cache epoch boundary state in the event of skipped epoch boundary slot * checkpoint * remove lods * checkppoint again * Cache: fill if empty * Don't include the sync fixes * Update sync utils * Avoid extra process slots Co-authored-by: nisdas <nishdas93@gmail.com>
1 parent bde3073 commit dbd1e8c

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

beacon-chain/blockchain/process_attestation_helpers.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (*stateTrie.BeaconState, error) {
2121
s.checkpointStateLock.Lock()
2222
defer s.checkpointStateLock.Unlock()
23+
2324
cachedState, err := s.checkpointState.StateByCheckpoint(c)
2425
if err != nil {
2526
return nil, errors.Wrap(err, "could not get cached checkpoint state")
@@ -39,12 +40,21 @@ func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (*sta
3940
if err != nil {
4041
return nil, errors.Wrapf(err, "could not process slots up to %d", helpers.StartSlot(c.Epoch))
4142
}
43+
if err := s.checkpointState.AddCheckpointState(c, baseState); err != nil {
44+
return nil, errors.Wrap(err, "could not saved checkpoint state to cache")
45+
}
46+
return baseState, nil
4247
}
4348

44-
if err := s.checkpointState.AddCheckpointState(c, baseState); err != nil {
45-
return nil, errors.Wrap(err, "could not saved checkpoint state to cache")
49+
has, err := s.stateGen.HasState(ctx, bytesutil.ToBytes32(c.Root))
50+
if err != nil {
51+
return nil, err
52+
}
53+
if !has {
54+
if err := s.checkpointState.AddCheckpointState(c, baseState); err != nil {
55+
return nil, errors.Wrap(err, "could not saved checkpoint state to cache")
56+
}
4657
}
47-
4858
return baseState, nil
4959

5060
}

beacon-chain/blockchain/process_block_helpers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ func (s *Service) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) e
7878
if !s.stateGen.StateSummaryExists(ctx, parentRoot) && !s.beaconDB.HasBlock(ctx, parentRoot) {
7979
return errors.New("could not reconstruct parent state")
8080
}
81-
if !s.stateGen.HasState(ctx, parentRoot) {
81+
has, err := s.stateGen.HasState(ctx, parentRoot)
82+
if err != nil {
83+
return err
84+
}
85+
if !has {
8286
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
8387
return errors.Wrap(err, "could not save initial sync blocks")
8488
}

beacon-chain/state/stategen/hot.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ import (
1414
)
1515

1616
// HasState returns true if the state exists in cache or in DB.
17-
func (s *State) HasState(ctx context.Context, blockRoot [32]byte) bool {
17+
func (s *State) HasState(ctx context.Context, blockRoot [32]byte) (bool, error) {
1818
if s.hotStateCache.Has(blockRoot) {
19-
return true
19+
return true, nil
2020
}
21-
22-
return s.beaconDB.HasState(ctx, blockRoot)
21+
_, has, err := s.epochBoundaryStateCache.getByRoot(blockRoot)
22+
if err != nil {
23+
return false, err
24+
}
25+
if has {
26+
return true, nil
27+
}
28+
return s.beaconDB.HasState(ctx, blockRoot), nil
2329
}
2430

2531
// SaveStateSummary saves the relevant state summary for a block and its corresponding state slot in the

0 commit comments

Comments
 (0)