-
Notifications
You must be signed in to change notification settings - Fork 105
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
fix: duty scheduler indices change & reorg bleeps #1725
Open
olegshmuelov
wants to merge
15
commits into
stage
Choose a base branch
from
fix/duty-scheduler-indices-change-and-reorg
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
adece06
draft
olegshmuelov a265022
tmp postfork
olegshmuelov 33e76c8
Revert "tmp postfork"
olegshmuelov caeeb87
tmp postfork
olegshmuelov 276ab6d
renaming
olegshmuelov 2c15442
add more tests
olegshmuelov f52c444
Revert "tmp postfork"
olegshmuelov c4694e8
optimization
olegshmuelov 00b3aa8
dont run in parallel
olegshmuelov c7d6778
optimization
olegshmuelov 7edf712
fix data race
olegshmuelov 6cee156
fix #1714
olegshmuelov c142342
Merge branch 'stage' into fix/duty-scheduler-indices-change-and-reorg
olegshmuelov 9fa010d
adapt to stage
olegshmuelov d337488
remove non relevant comment
olegshmuelov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,31 +35,7 @@ func (h *AttesterHandler) Name() string { | |
return spectypes.BNRoleAttester.String() | ||
} | ||
|
||
// HandleDuties manages the duty lifecycle, handling different cases: | ||
// | ||
// On First Run: | ||
// 1. Fetch duties for the current epoch. | ||
// 2. If necessary, fetch duties for the next epoch. | ||
// 3. Execute duties. | ||
// | ||
// On Re-org: | ||
// | ||
// If the previous dependent root changed: | ||
// 1. Fetch duties for the current epoch. | ||
// 2. Execute duties. | ||
// If the current dependent root changed: | ||
// 1. Execute duties. | ||
// 2. If necessary, fetch duties for the next epoch. | ||
// | ||
// On Indices Change: | ||
// 1. Execute duties. | ||
// 2. ResetEpoch duties for the current epoch. | ||
// 3. Fetch duties for the current epoch. | ||
// 4. If necessary, fetch duties for the next epoch. | ||
// | ||
// On Ticker event: | ||
// 1. Execute duties. | ||
// 2. If necessary, fetch duties for the next epoch. | ||
// HandleDuties manages the duty lifecycle | ||
func (h *AttesterHandler) HandleDuties(ctx context.Context) { | ||
h.logger.Info("starting duty handler") | ||
defer h.logger.Info("duty handler exited") | ||
|
@@ -75,67 +51,38 @@ func (h *AttesterHandler) HandleDuties(ctx context.Context) { | |
case <-next: | ||
slot := h.ticker.Slot() | ||
next = h.ticker.Next() | ||
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(slot) | ||
buildStr := fmt.Sprintf("e%v-s%v-#%v", currentEpoch, slot, slot%32+1) | ||
h.logger.Debug("🛠 ticker event", zap.String("epoch_slot_pos", buildStr)) | ||
|
||
h.processExecution(currentEpoch, slot) | ||
if h.indicesChanged { | ||
h.duties.ResetEpoch(currentEpoch) | ||
h.indicesChanged = false | ||
} | ||
h.processFetching(ctx, currentEpoch, slot) | ||
|
||
slotsPerEpoch := h.network.Beacon.SlotsPerEpoch() | ||
|
||
// If we have reached the mid-point of the epoch, fetch the duties for the next epoch in the next slot. | ||
// This allows us to set them up at a time when the beacon node should be less busy. | ||
if uint64(slot)%slotsPerEpoch == slotsPerEpoch/2-1 { | ||
h.fetchNextEpoch = true | ||
} | ||
|
||
// last slot of epoch | ||
if uint64(slot)%slotsPerEpoch == slotsPerEpoch-1 { | ||
h.duties.ResetEpoch(currentEpoch - 1) | ||
epoch := h.network.Beacon.EstimatedEpochAtSlot(slot) | ||
tickerID := fields.FormatSlotTickerID(epoch, slot) | ||
h.logger.Debug("🛠 ticker event", fields.SlotTickerID(tickerID)) | ||
|
||
if !h.network.PastAlanForkAtEpoch(epoch) { | ||
h.processExecution(epoch, slot) | ||
if h.indicesChanged { | ||
h.duties.Reset(epoch) | ||
h.indicesChanged = false | ||
} | ||
h.processFetching(ctx, epoch, slot) | ||
h.processSlotTransition(epoch, slot) | ||
} | ||
|
||
case reorgEvent := <-h.reorg: | ||
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(reorgEvent.Slot) | ||
buildStr := fmt.Sprintf("e%v-s%v-#%v", currentEpoch, reorgEvent.Slot, reorgEvent.Slot%32+1) | ||
h.logger.Info("🔀 reorg event received", zap.String("epoch_slot_pos", buildStr), zap.Any("event", reorgEvent)) | ||
|
||
// reset current epoch duties | ||
if reorgEvent.Previous { | ||
h.duties.ResetEpoch(currentEpoch) | ||
h.fetchCurrentEpoch = true | ||
if h.shouldFetchNexEpoch(reorgEvent.Slot) { | ||
h.duties.ResetEpoch(currentEpoch + 1) | ||
h.fetchNextEpoch = true | ||
} | ||
epoch := h.network.Beacon.EstimatedEpochAtSlot(reorgEvent.Slot) | ||
tickerID := fields.FormatSlotTickerID(epoch, reorgEvent.Slot) | ||
h.logger.Info("🔀 reorg event received", fields.SlotTickerID(tickerID), zap.Any("event", reorgEvent)) | ||
|
||
h.processFetching(ctx, currentEpoch, reorgEvent.Slot) | ||
} else if reorgEvent.Current { | ||
// reset & re-fetch next epoch duties if in appropriate slot range, | ||
// otherwise they will be fetched by the appropriate slot tick. | ||
if h.shouldFetchNexEpoch(reorgEvent.Slot) { | ||
h.duties.ResetEpoch(currentEpoch + 1) | ||
h.fetchNextEpoch = true | ||
} | ||
if !h.network.PastAlanForkAtEpoch(epoch) { | ||
h.processReorg(ctx, epoch, reorgEvent) | ||
} | ||
|
||
case <-h.indicesChange: | ||
slot := h.network.Beacon.EstimatedCurrentSlot() | ||
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(slot) | ||
buildStr := fmt.Sprintf("e%v-s%v-#%v", currentEpoch, slot, slot%32+1) | ||
h.logger.Info("🔁 indices change received", zap.String("epoch_slot_pos", buildStr)) | ||
|
||
h.indicesChanged = true | ||
h.fetchCurrentEpoch = true | ||
epoch := h.network.Beacon.EstimatedEpochAtSlot(slot) | ||
tickerID := fields.FormatSlotTickerID(epoch, slot) | ||
h.logger.Info("🔁 indices change received", fields.SlotTickerID(tickerID)) | ||
|
||
// reset next epoch duties if in appropriate slot range | ||
if h.shouldFetchNexEpoch(slot) { | ||
h.duties.ResetEpoch(currentEpoch + 1) | ||
h.fetchNextEpoch = true | ||
if !h.network.PastAlanForkAtEpoch(epoch) { | ||
h.indicesChanged = true | ||
h.processIndicesChange(epoch, slot) | ||
} | ||
} | ||
} | ||
|
@@ -177,27 +124,61 @@ func (h *AttesterHandler) processExecution(epoch phase0.Epoch, slot phase0.Slot) | |
return | ||
} | ||
|
||
if !h.network.PastAlanForkAtEpoch(h.network.Beacon.EstimatedEpochAtSlot(slot)) { | ||
toExecute := make([]*genesisspectypes.Duty, 0, len(duties)*2) | ||
for _, d := range duties { | ||
if h.shouldExecute(d) { | ||
toExecute = append(toExecute, h.toGenesisSpecDuty(d, genesisspectypes.BNRoleAttester)) | ||
toExecute = append(toExecute, h.toGenesisSpecDuty(d, genesisspectypes.BNRoleAggregator)) | ||
} | ||
toExecute := make([]*genesisspectypes.Duty, 0, len(duties)*2) | ||
for _, d := range duties { | ||
if h.shouldExecute(d) { | ||
toExecute = append(toExecute, h.toGenesisSpecDuty(d, genesisspectypes.BNRoleAttester)) | ||
toExecute = append(toExecute, h.toGenesisSpecDuty(d, genesisspectypes.BNRoleAggregator)) | ||
} | ||
} | ||
|
||
h.dutiesExecutor.ExecuteGenesisDuties(h.logger, toExecute) | ||
return | ||
h.dutiesExecutor.ExecuteGenesisDuties(h.logger, toExecute) | ||
} | ||
|
||
func (h *AttesterHandler) processIndicesChange(epoch phase0.Epoch, slot phase0.Slot) { | ||
h.fetchCurrentEpoch = true | ||
|
||
// reset next epoch duties if in appropriate slot range | ||
if h.shouldFetchNexEpoch(slot) { | ||
h.duties.Reset(epoch + 1) | ||
h.fetchNextEpoch = true | ||
} | ||
} | ||
|
||
toExecute := make([]*spectypes.ValidatorDuty, 0, len(duties)) | ||
for _, d := range duties { | ||
if h.shouldExecute(d) { | ||
toExecute = append(toExecute, h.toSpecDuty(d, spectypes.BNRoleAggregator)) | ||
func (h *AttesterHandler) processReorg(ctx context.Context, epoch phase0.Epoch, reorgEvent ReorgEvent) { | ||
// reset current epoch duties | ||
if reorgEvent.Previous { | ||
h.duties.Reset(epoch) | ||
h.fetchCurrentEpoch = true | ||
if h.shouldFetchNexEpoch(reorgEvent.Slot) { | ||
h.duties.Reset(epoch + 1) | ||
h.fetchNextEpoch = true | ||
} | ||
|
||
h.processFetching(ctx, epoch, reorgEvent.Slot) | ||
} else if reorgEvent.Current { | ||
// reset & re-fetch next epoch duties if in appropriate slot range, | ||
// otherwise they will be fetched by the appropriate slot tick. | ||
if h.shouldFetchNexEpoch(reorgEvent.Slot) { | ||
h.duties.Reset(epoch + 1) | ||
h.fetchNextEpoch = true | ||
} | ||
} | ||
} | ||
|
||
func (h *AttesterHandler) processSlotTransition(epoch phase0.Epoch, slot phase0.Slot) { | ||
slotsPerEpoch := h.network.Beacon.SlotsPerEpoch() | ||
|
||
h.dutiesExecutor.ExecuteDuties(h.logger, toExecute) | ||
// If we have reached the mid-point of the epoch, fetch the duties for the next epoch in the next slot. | ||
// This allows us to set them up at a time when the beacon node should be less busy. | ||
if uint64(slot)%slotsPerEpoch == slotsPerEpoch/2-1 { | ||
h.fetchNextEpoch = true | ||
} | ||
|
||
// last slot of epoch | ||
if uint64(slot)%slotsPerEpoch == slotsPerEpoch-1 { | ||
h.duties.Reset(epoch - 1) | ||
} | ||
Comment on lines
+182
to
+185
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. Would be nice to have helper |
||
} | ||
|
||
func (h *AttesterHandler) fetchAndProcessDuties(ctx context.Context, epoch phase0.Epoch) error { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can use
if h.shouldFetchNexEpoch(...
here ? I guess applies to other Duties changed in this PR too.