Skip to content

Commit

Permalink
fix(lib/babe): ensure the slot time is correct before build a block (#…
Browse files Browse the repository at this point in the history
…2648)

* fix: ensure the slot time is correct before build a block

* chore: sleep is inevitable
  • Loading branch information
EclesioMeloJunior committed Jul 8, 2022
1 parent 422e7b3 commit 78c03b6
Showing 1 changed file with 19 additions and 4 deletions.
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:
// 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

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

0 comments on commit 78c03b6

Please sign in to comment.