Skip to content

Commit 466ff66

Browse files
authored
miner: reset ctx timeout before commit tx on new tx notif (#1434)
* miner: reset ctx before commit transactions in main loop * miner: log error in tx interrupt * miner: typo * worker: handle commit interrupt for new tx processing
1 parent d274e62 commit 466ff66

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

miner/worker.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ func (w *worker) mainLoop() {
615615
if gp := w.current.gasPool; gp != nil && gp.Gas() < params.TxGas {
616616
continue
617617
}
618+
// If we don't have time to execute (i.e. we're past header timestamp), abort
619+
delay := time.Until(time.Unix(int64(w.current.header.Time), 0))
620+
if delay <= 0 {
621+
continue
622+
}
618623
txs := make(map[common.Address][]*txpool.LazyTransaction, len(ev.Txs))
619624
for _, tx := range ev.Txs {
620625
acc, _ := types.Sender(w.current.signer, tx)
@@ -634,7 +639,14 @@ func (w *worker) mainLoop() {
634639

635640
tcount := w.current.tcount
636641

642+
w.interruptCtx = resetAndCopyInterruptCtx(w.interruptCtx)
643+
stopFn := func() {}
644+
if w.interruptCommitFlag {
645+
w.interruptCtx, stopFn = getInterruptTimer(w.interruptCtx, w.current.header.Number.Uint64(), w.current.header.Time)
646+
w.interruptCtx = vm.PutCache(w.interruptCtx, w.interruptedTxCache)
647+
}
637648
w.commitTransactions(w.current, plainTxs, blobTxs, nil, new(uint256.Int))
649+
stopFn()
638650

639651
// Only update the snapshot if any new transactons were added
640652
// to the pending block
@@ -939,7 +951,7 @@ mainloop:
939951
select {
940952
case <-w.interruptCtx.Done():
941953
txCommitInterruptCounter.Inc(1)
942-
log.Warn("Tx Level Interrupt", "hash", lastTxHash)
954+
log.Warn("Tx Level Interrupt", "hash", lastTxHash, "err", w.interruptCtx.Err())
943955
break mainloop
944956
default:
945957
}
@@ -1046,7 +1058,7 @@ mainloop:
10461058

10471059
logs, err := w.commitTransaction(env, tx)
10481060

1049-
// Check if we have a `delay` set in interrup context. It's only set during tests.
1061+
// Check if we have a `delay` set in interrupt context. It's only set during tests.
10501062
if w.interruptCtx != nil {
10511063
if delay := w.interruptCtx.Value(vm.InterruptCtxDelayKey); delay != nil {
10521064
// nolint : durationcheck
@@ -1428,8 +1440,7 @@ func (w *worker) commitWork(interrupt *atomic.Int32, noempty bool, timestamp int
14281440
}()
14291441

14301442
if !noempty && w.interruptCommitFlag {
1431-
block := w.chain.GetBlockByHash(w.chain.CurrentBlock().Hash())
1432-
w.interruptCtx, stopFn = getInterruptTimer(w.interruptCtx, work, block)
1443+
w.interruptCtx, stopFn = getInterruptTimer(w.interruptCtx, work.header.Number.Uint64(), work.header.Time)
14331444
w.interruptCtx = vm.PutCache(w.interruptCtx, w.interruptedTxCache)
14341445
}
14351446

@@ -1497,16 +1508,14 @@ func resetAndCopyInterruptCtx(interruptCtx context.Context) context.Context {
14971508
return newCtx
14981509
}
14991510

1500-
func getInterruptTimer(interruptCtx context.Context, work *environment, current *types.Block) (context.Context, func()) {
1501-
delay := time.Until(time.Unix(int64(work.header.Time), 0))
1502-
1511+
func getInterruptTimer(interruptCtx context.Context, number, timestamp uint64) (context.Context, func()) {
1512+
delay := time.Until(time.Unix(int64(timestamp), 0))
15031513
interruptCtx, cancel := context.WithTimeout(interruptCtx, delay)
1504-
blockNumber := current.NumberU64() + 1
15051514

15061515
go func() {
15071516
<-interruptCtx.Done()
15081517
if interruptCtx.Err() != context.Canceled {
1509-
log.Info("Commit Interrupt. Pre-committing the current block", "block", blockNumber)
1518+
log.Info("Commit Interrupt. Pre-committing the current block", "block", number)
15101519
cancel()
15111520
}
15121521
}()

0 commit comments

Comments
 (0)