Skip to content
Merged
Changes from 1 commit
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
25 changes: 20 additions & 5 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,11 +91,17 @@ 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,
startTime: startTime,
slotNum: authoringSlot,
})
logger.Debugf("start time of slot %d: %v", authoringSlot, startTime)

logger.Debugf("start time of slot %d: %v", authoringSlot, startTime.UnixMilli())
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
}

defer func() {
Expand All @@ -115,6 +122,14 @@ func (h *epochHandler) run(ctx context.Context, errCh chan<- error) {
case <-ctx.Done():
return
case <-swt.timer.C:
Comment thread
qdm12 marked this conversation as 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
Comment thread
qdm12 marked this conversation as resolved.
diff := time.Now().Sub(swt.startTime).Nanoseconds()
if diff < 0 {
time.Sleep(-time.Duration(diff))
}
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated
Comment thread
EclesioMeloJunior marked this conversation as resolved.
Outdated

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