Skip to content
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(lib/babe): ensure the slot time is correct before build a block #2648

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/babe/epoch_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ func (h *epochHandler) run(ctx context.Context, errCh chan<- error) {
authoringSlots := getAuthoringSlots(h.slotToPreRuntimeDigest)

type slotWithTimer struct {
timer *time.Timer
slotNum uint64
startTime time.Time
timer *time.Timer
slotNum uint64
}

slotTimeTimers := make([]*slotWithTimer, 0, len(authoringSlots))
Expand All @@ -90,10 +91,15 @@ func (h *epochHandler) run(ctx context.Context, errCh chan<- error) {
}

startTime := getSlotStartTime(authoringSlot, h.constants.slotDuration)
waitTime := startTime.Sub(time.Now())
timer := time.NewTimer(waitTime)

slotTimeTimers = append(slotTimeTimers, &slotWithTimer{
timer: time.NewTimer(time.Until(startTime)),
slotNum: authoringSlot,
timer: timer,
slotNum: authoringSlot,
startTime: startTime,
})

logger.Debugf("start time of slot %d: %v", authoringSlot, startTime)
}

Expand All @@ -115,6 +121,15 @@ func (h *epochHandler) run(ctx context.Context, errCh chan<- error) {
case <-ctx.Done():
return
case <-swt.timer.C:
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
// we must do a time correction as the slot timer sometimes is triggered
// before the time defined in the constructor due to an inconsistency
// of the language -> https://github.com/golang/go/issues/17696
qdm12 marked this conversation as resolved.
Show resolved Hide resolved

diff := time.Since(swt.startTime)
if diff < 0 {
time.Sleep(-diff)
}

if _, has := h.slotToPreRuntimeDigest[swt.slotNum]; !has {
// this should never happen
panic(fmt.Sprintf("no VRF proof for authoring slot! slot=%d", swt.slotNum))
Expand Down