-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor next slot cache #12233
refactor next slot cache #12233
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -671,9 +671,7 @@ func (s *Service) fillMissingPayloadIDRoutine(ctx context.Context, stateFeed *ev | |
for { | ||
select { | ||
case <-ticker.C(): | ||
if err := s.fillMissingBlockPayloadId(ctx); err != nil { | ||
log.WithError(err).Error("Could not fill missing payload ID") | ||
} | ||
s.lateBlockTasks(ctx) | ||
|
||
case <-ctx.Done(): | ||
log.Debug("Context closed, exiting routine") | ||
|
@@ -683,11 +681,13 @@ func (s *Service) fillMissingPayloadIDRoutine(ctx context.Context, stateFeed *ev | |
}() | ||
} | ||
|
||
// fillMissingBlockPayloadId is called 4 seconds into the slot and calls FCU if we are proposing next slot | ||
// and the cache has been missed | ||
func (s *Service) fillMissingBlockPayloadId(ctx context.Context) error { | ||
// lateBlockTasks is called 4 seconds into the slot and performs tasks | ||
// related to late blocks. It emits a MissedSlot state feed event. | ||
// It calls FCU and sets the right attributes if we are proposing next slot | ||
// it also updates the next slot cache to deal with skipped slots. | ||
func (s *Service) lateBlockTasks(ctx context.Context) { | ||
if s.CurrentSlot() == s.HeadSlot() { | ||
return nil | ||
return | ||
} | ||
s.cfg.StateNotifier.StateFeed().Send(&feed.Event{ | ||
Type: statefeed.MissedSlot, | ||
|
@@ -697,21 +697,31 @@ func (s *Service) fillMissingBlockPayloadId(ctx context.Context) error { | |
_, id, has := s.cfg.ProposerSlotIndexCache.GetProposerPayloadIDs(s.CurrentSlot()+1, [32]byte{} /* head root */) | ||
// There exists proposer for next slot, but we haven't called fcu w/ payload attribute yet. | ||
if !has || id != [8]byte{} { | ||
return nil | ||
return | ||
} | ||
s.headLock.RLock() | ||
headBlock, err := s.headBlock() | ||
if err != nil { | ||
s.headLock.RUnlock() | ||
return err | ||
log.WithError(err).Debug("could not perform late block tasks: failed to retrieve head block") | ||
return | ||
} | ||
headState := s.headState(ctx) | ||
headRoot := s.headRoot() | ||
headState := s.headState(ctx) | ||
s.headLock.RUnlock() | ||
_, err = s.notifyForkchoiceUpdate(ctx, ¬ifyForkchoiceUpdateArg{ | ||
headState: headState, | ||
headRoot: headRoot, | ||
headBlock: headBlock.Block(), | ||
}) | ||
return err | ||
if err != nil { | ||
log.WithError(err).Debug("could not perform late block tasks: failed to update forkchoice with engine") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if these |
||
} | ||
lastRoot, lastState := transition.LastCachedState() | ||
if lastState == nil { | ||
lastRoot, lastState = headRoot[:], headState | ||
} | ||
if err = transition.UpdateNextSlotCache(ctx, lastRoot, lastState); err != nil { | ||
log.WithError(err).Debug("could not update next slot state cache") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,25 +147,15 @@ func ProcessSlotsUsingNextSlotCache( | |
ctx, span := trace.StartSpan(ctx, "core.state.ProcessSlotsUsingNextSlotCache") | ||
defer span.End() | ||
|
||
// Check whether the parent state has been advanced by 1 slot in next slot cache. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very simple function that was overly commented making it less readable. I removed some useless variables and most comments. |
||
nextSlotState, err := NextSlotState(ctx, parentRoot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cachedStateExists := nextSlotState != nil && !nextSlotState.IsNil() | ||
// If the next slot state is not nil (i.e. cache hit). | ||
// We replace next slot state with parent state. | ||
if cachedStateExists { | ||
nextSlotState := NextSlotState(parentRoot) | ||
if nextSlotState != nil { | ||
parentState = nextSlotState | ||
} | ||
|
||
// In the event our cached state has advanced our | ||
// state to the desired slot, we exit early. | ||
if cachedStateExists && parentState.Slot() == slot { | ||
if parentState.Slot() == slot { | ||
return parentState, nil | ||
} | ||
// Since next slot cache only advances state by 1 slot, | ||
// we check if there's more slots that need to process. | ||
|
||
var err error | ||
parentState, err = ProcessSlots(ctx, parentState, slot) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not process slots") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason you switched the ordering from
state then root
toroot and then state
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no reason, was going to use the cached state as head state, but that ended up being too hard to do in one PR,