Compare commits

...

4 Commits

Author SHA1 Message Date
terence tsao
6208f93ad9 Use read lock 2023-04-05 09:33:08 -07:00
terence tsao
f8e17eaf07 Refactor the getAttPreState function to handle cases where epochStartSlot is less than or equal to baseState's slot 2023-04-05 07:45:35 -07:00
terence tsao
844d42799c Add condition 2023-04-05 07:13:46 -07:00
terence tsao
4902660f30 Use next slot cache for attestation gossip 2023-04-05 06:56:45 -07:00
3 changed files with 11 additions and 8 deletions

View File

@@ -42,9 +42,11 @@ func (s *Service) getAttPreState(ctx context.Context, c *ethpb.Checkpoint) (stat
if err != nil {
return nil, err
}
baseState, err = transition.ProcessSlotsIfPossible(ctx, baseState, epochStartSlot)
if err != nil {
return nil, errors.Wrapf(err, "could not process slots up to epoch %d", c.Epoch)
if epochStartSlot > baseState.Slot() {
baseState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, baseState, c.Root, epochStartSlot)
if err != nil {
return nil, errors.Wrapf(err, "could not process slots up to epoch %d", c.Epoch)
}
}
// Sharing the same state across caches is perfectly fine here, the fetching

View File

@@ -13,7 +13,7 @@ import (
)
type nextSlotCache struct {
sync.Mutex
sync.RWMutex
prevRoot []byte
lastRoot []byte
prevState state.BeaconState
@@ -37,8 +37,8 @@ var (
// It returns the last updated state if it matches. Otherwise it returns the previously
// updated state if it matches its root. If no root matches it returns nil
func NextSlotState(root []byte) state.BeaconState {
nsc.Lock()
defer nsc.Unlock()
nsc.RLock()
defer nsc.RUnlock()
if bytes.Equal(root, nsc.lastRoot) {
nextSlotCacheHit.Inc()
return nsc.lastState.Copy()
@@ -74,8 +74,8 @@ func UpdateNextSlotCache(ctx context.Context, root []byte, state state.BeaconSta
// LastCachedState returns the last cached state and root in the cache
func LastCachedState() ([]byte, state.BeaconState) {
nsc.Lock()
defer nsc.Unlock()
nsc.RLock()
defer nsc.RUnlock()
if nsc.lastState == nil {
return nil, nil
}

View File

@@ -160,6 +160,7 @@ func ProcessSlotsUsingNextSlotCache(
if err != nil {
return nil, errors.Wrap(err, "could not process slots")
}
return parentState, nil
}