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: reduce mine period reset (waiting) time #730

Open
wants to merge 2 commits into
base: dev-upgrade
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/big"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -302,12 +303,14 @@ func (w *worker) update() {
if atomic.LoadInt32(&w.mining) == 1 {
w.commitNewWork()
}
timeout.Reset(time.Duration(minePeriod) * time.Second)
resetTime := getResetTime(w.chain, minePeriod)
timeout.Reset(resetTime)

// Handle ChainHeadEvent
case <-w.chainHeadCh:
w.commitNewWork()
timeout.Reset(time.Duration(minePeriod) * time.Second)
resetTime := getResetTime(w.chain, minePeriod)
timeout.Reset(resetTime)

// Handle ChainSideEvent
case <-w.chainSideCh:
Expand Down Expand Up @@ -354,6 +357,23 @@ func (w *worker) update() {
}
}

func getResetTime(chain *core.BlockChain, minePeriod int) time.Duration {
minePeriodDuration := time.Duration(minePeriod) * time.Second
currentBlockTime := chain.CurrentBlock().Time().Int64()
nowTime := time.Now().UnixMilli()
resetTime := time.Duration(currentBlockTime)*time.Second + minePeriodDuration - time.Duration(nowTime)*time.Millisecond
// in case the current block time is not very accurate
if resetTime > minePeriodDuration {
resetTime = minePeriodDuration
}
// in case the current block is too far in the past, the block time already is huge, we wait for mine period
if resetTime < 0 {
resetTime = minePeriodDuration
}
log.Info("Miner worker timer reset", "reset milliseconds", resetTime.Milliseconds(), "mine period sec", minePeriod, "current block time sec", currentBlockTime, "now time sec", fmt.Sprintf("%d.%d", nowTime/1000, nowTime%1000))
return resetTime
}

func (w *worker) wait() {
for {
mustCommitNewWork := true
Expand Down