Skip to content

Commit f0242ee

Browse files
rjl493456442karalabe
authored andcommitted
miner: keep the timestamp for resubmitted mining block (#17547)
1 parent a4bc2c3 commit f0242ee

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

miner/worker.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const (
111111
type newWorkReq struct {
112112
interrupt *int32
113113
noempty bool
114+
timestamp int64
114115
}
115116

116117
// intervalAdjust represents a resubmitting interval adjustment.
@@ -285,6 +286,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
285286
var (
286287
interrupt *int32
287288
minRecommit = recommit // minimal resubmit interval specified by user.
289+
timestamp int64 // timestamp for each round of mining.
288290
)
289291

290292
timer := time.NewTimer(0)
@@ -296,7 +298,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
296298
atomic.StoreInt32(interrupt, s)
297299
}
298300
interrupt = new(int32)
299-
w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty}
301+
w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty, timestamp: timestamp}
300302
timer.Reset(recommit)
301303
atomic.StoreInt32(&w.newTxs, 0)
302304
}
@@ -336,10 +338,12 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
336338
select {
337339
case <-w.startCh:
338340
clearPending(w.chain.CurrentBlock().NumberU64())
341+
timestamp = time.Now().Unix()
339342
commit(false, commitInterruptNewHead)
340343

341344
case head := <-w.chainHeadCh:
342345
clearPending(head.Block.NumberU64())
346+
timestamp = time.Now().Unix()
343347
commit(false, commitInterruptNewHead)
344348

345349
case <-timer.C:
@@ -398,7 +402,7 @@ func (w *worker) mainLoop() {
398402
for {
399403
select {
400404
case req := <-w.newWorkCh:
401-
w.commitNewWork(req.interrupt, req.noempty)
405+
w.commitNewWork(req.interrupt, req.noempty, req.timestamp)
402406

403407
case ev := <-w.chainSideCh:
404408
if _, exist := w.possibleUncles[ev.Block.Hash()]; exist {
@@ -450,7 +454,7 @@ func (w *worker) mainLoop() {
450454
} else {
451455
// If we're mining, but nothing is being processed, wake on new transactions
452456
if w.config.Clique != nil && w.config.Clique.Period == 0 {
453-
w.commitNewWork(nil, false)
457+
w.commitNewWork(nil, false, time.Now().Unix())
454458
}
455459
}
456460
atomic.AddInt32(&w.newTxs, int32(len(ev.Txs)))
@@ -793,20 +797,19 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin
793797
}
794798

795799
// commitNewWork generates several new sealing tasks based on the parent block.
796-
func (w *worker) commitNewWork(interrupt *int32, noempty bool) {
800+
func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) {
797801
w.mu.RLock()
798802
defer w.mu.RUnlock()
799803

800804
tstart := time.Now()
801805
parent := w.chain.CurrentBlock()
802806

803-
tstamp := tstart.Unix()
804-
if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) >= 0 {
805-
tstamp = parent.Time().Int64() + 1
807+
if parent.Time().Cmp(new(big.Int).SetInt64(timestamp)) >= 0 {
808+
timestamp = parent.Time().Int64() + 1
806809
}
807810
// this will ensure we're not going off too far in the future
808-
if now := time.Now().Unix(); tstamp > now+1 {
809-
wait := time.Duration(tstamp-now) * time.Second
811+
if now := time.Now().Unix(); timestamp > now+1 {
812+
wait := time.Duration(timestamp-now) * time.Second
810813
log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait))
811814
time.Sleep(wait)
812815
}
@@ -817,7 +820,7 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool) {
817820
Number: num.Add(num, common.Big1),
818821
GasLimit: core.CalcGasLimit(parent, w.gasFloor, w.gasCeil),
819822
Extra: w.extra,
820-
Time: big.NewInt(tstamp),
823+
Time: big.NewInt(timestamp),
821824
}
822825
// Only set the coinbase if our consensus engine is running (avoid spurious block rewards)
823826
if w.isRunning() {

0 commit comments

Comments
 (0)