@@ -111,6 +111,7 @@ const (
111
111
type newWorkReq struct {
112
112
interrupt * int32
113
113
noempty bool
114
+ timestamp int64
114
115
}
115
116
116
117
// intervalAdjust represents a resubmitting interval adjustment.
@@ -285,6 +286,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
285
286
var (
286
287
interrupt * int32
287
288
minRecommit = recommit // minimal resubmit interval specified by user.
289
+ timestamp int64 // timestamp for each round of mining.
288
290
)
289
291
290
292
timer := time .NewTimer (0 )
@@ -296,7 +298,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
296
298
atomic .StoreInt32 (interrupt , s )
297
299
}
298
300
interrupt = new (int32 )
299
- w .newWorkCh <- & newWorkReq {interrupt : interrupt , noempty : noempty }
301
+ w .newWorkCh <- & newWorkReq {interrupt : interrupt , noempty : noempty , timestamp : timestamp }
300
302
timer .Reset (recommit )
301
303
atomic .StoreInt32 (& w .newTxs , 0 )
302
304
}
@@ -336,10 +338,12 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
336
338
select {
337
339
case <- w .startCh :
338
340
clearPending (w .chain .CurrentBlock ().NumberU64 ())
341
+ timestamp = time .Now ().Unix ()
339
342
commit (false , commitInterruptNewHead )
340
343
341
344
case head := <- w .chainHeadCh :
342
345
clearPending (head .Block .NumberU64 ())
346
+ timestamp = time .Now ().Unix ()
343
347
commit (false , commitInterruptNewHead )
344
348
345
349
case <- timer .C :
@@ -398,7 +402,7 @@ func (w *worker) mainLoop() {
398
402
for {
399
403
select {
400
404
case req := <- w .newWorkCh :
401
- w .commitNewWork (req .interrupt , req .noempty )
405
+ w .commitNewWork (req .interrupt , req .noempty , req . timestamp )
402
406
403
407
case ev := <- w .chainSideCh :
404
408
if _ , exist := w .possibleUncles [ev .Block .Hash ()]; exist {
@@ -450,7 +454,7 @@ func (w *worker) mainLoop() {
450
454
} else {
451
455
// If we're mining, but nothing is being processed, wake on new transactions
452
456
if w .config .Clique != nil && w .config .Clique .Period == 0 {
453
- w .commitNewWork (nil , false )
457
+ w .commitNewWork (nil , false , time . Now (). Unix () )
454
458
}
455
459
}
456
460
atomic .AddInt32 (& w .newTxs , int32 (len (ev .Txs )))
@@ -793,20 +797,19 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin
793
797
}
794
798
795
799
// 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 ) {
797
801
w .mu .RLock ()
798
802
defer w .mu .RUnlock ()
799
803
800
804
tstart := time .Now ()
801
805
parent := w .chain .CurrentBlock ()
802
806
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
806
809
}
807
810
// 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
810
813
log .Info ("Mining too far in the future" , "wait" , common .PrettyDuration (wait ))
811
814
time .Sleep (wait )
812
815
}
@@ -817,7 +820,7 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool) {
817
820
Number : num .Add (num , common .Big1 ),
818
821
GasLimit : core .CalcGasLimit (parent , w .gasFloor , w .gasCeil ),
819
822
Extra : w .extra ,
820
- Time : big .NewInt (tstamp ),
823
+ Time : big .NewInt (timestamp ),
821
824
}
822
825
// Only set the coinbase if our consensus engine is running (avoid spurious block rewards)
823
826
if w .isRunning () {
0 commit comments