Skip to content

Commit cef1a86

Browse files
authored
miner: discard interrupted blocks (#24638)
During mining, when a new head arrives and interrupts the block building, the block being built should not be commited (but discarded). Committing the interrupted block introduces unnecessary delay, and possibly causes miner to mine on the previous head, which could result in higher uncle rate.
1 parent 33d7a46 commit cef1a86

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

eth/catalyst/api.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/ethereum/go-ethereum/common/hexutil"
2929
"github.com/ethereum/go-ethereum/core/beacon"
3030
"github.com/ethereum/go-ethereum/core/rawdb"
31-
"github.com/ethereum/go-ethereum/core/types"
3231
"github.com/ethereum/go-ethereum/eth"
3332
"github.com/ethereum/go-ethereum/log"
3433
"github.com/ethereum/go-ethereum/node"
@@ -349,11 +348,3 @@ func (api *ConsensusAPI) assembleBlock(parentHash common.Hash, params *beacon.Pa
349348
}
350349
return beacon.BlockToExecutableData(block), nil
351350
}
352-
353-
// Used in tests to add a the list of transactions from a block to the tx pool.
354-
func (api *ConsensusAPI) insertTransactions(txs types.Transactions) error {
355-
for _, tx := range txs {
356-
api.eth.TxPool().AddLocal(tx)
357-
}
358-
return nil
359-
}

eth/catalyst/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) {
108108
api := NewConsensusAPI(ethservice)
109109

110110
// Put the 10th block's tx in the pool and produce a new block
111-
api.insertTransactions(blocks[9].Transactions())
111+
api.eth.TxPool().AddRemotesSync(blocks[9].Transactions())
112112
blockParams := beacon.PayloadAttributesV1{
113113
Timestamp: blocks[8].Time() + 5,
114114
}

miner/worker.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ const (
7777
staleThreshold = 7
7878
)
7979

80+
var (
81+
errBlockInterruptedByNewHead = errors.New("new head arrived while building block")
82+
errBlockInterruptedByRecommit = errors.New("recommit interrupt while building block")
83+
)
84+
8085
// environment is the worker's current environment and holds all
8186
// information of the sealing block generation.
8287
type environment struct {
@@ -841,7 +846,7 @@ func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*
841846
return receipt.Logs, nil
842847
}
843848

844-
func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByPriceAndNonce, interrupt *int32) bool {
849+
func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByPriceAndNonce, interrupt *int32) error {
845850
gasLimit := env.header.GasLimit
846851
if env.gasPool == nil {
847852
env.gasPool = new(core.GasPool).AddGas(gasLimit)
@@ -866,8 +871,9 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP
866871
ratio: ratio,
867872
inc: true,
868873
}
874+
return errBlockInterruptedByRecommit
869875
}
870-
return atomic.LoadInt32(interrupt) == commitInterruptNewHead
876+
return errBlockInterruptedByNewHead
871877
}
872878
// If we don't have enough gas for any further transactions then we're done
873879
if env.gasPool.Gas() < params.TxGas {
@@ -951,7 +957,7 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP
951957
if interrupt != nil {
952958
w.resubmitAdjustCh <- &intervalAdjust{inc: false}
953959
}
954-
return false
960+
return nil
955961
}
956962

957963
// generateParams wraps various of settings for generating sealing task.
@@ -1050,7 +1056,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
10501056
// fillTransactions retrieves the pending transactions from the txpool and fills them
10511057
// into the given sealing block. The transaction selection and ordering strategy can
10521058
// be customized with the plugin in the future.
1053-
func (w *worker) fillTransactions(interrupt *int32, env *environment) {
1059+
func (w *worker) fillTransactions(interrupt *int32, env *environment) error {
10541060
// Split the pending transactions into locals and remotes
10551061
// Fill the block with all available pending transactions.
10561062
pending := w.eth.TxPool().Pending(true)
@@ -1063,16 +1069,17 @@ func (w *worker) fillTransactions(interrupt *int32, env *environment) {
10631069
}
10641070
if len(localTxs) > 0 {
10651071
txs := types.NewTransactionsByPriceAndNonce(env.signer, localTxs, env.header.BaseFee)
1066-
if w.commitTransactions(env, txs, interrupt) {
1067-
return
1072+
if err := w.commitTransactions(env, txs, interrupt); err != nil {
1073+
return err
10681074
}
10691075
}
10701076
if len(remoteTxs) > 0 {
10711077
txs := types.NewTransactionsByPriceAndNonce(env.signer, remoteTxs, env.header.BaseFee)
1072-
if w.commitTransactions(env, txs, interrupt) {
1073-
return
1078+
if err := w.commitTransactions(env, txs, interrupt); err != nil {
1079+
return err
10741080
}
10751081
}
1082+
return nil
10761083
}
10771084

10781085
// generateWork generates a sealing block based on the given parameters.
@@ -1084,6 +1091,7 @@ func (w *worker) generateWork(params *generateParams) (*types.Block, error) {
10841091
defer work.discard()
10851092

10861093
w.fillTransactions(nil, work)
1094+
10871095
return w.engine.FinalizeAndAssemble(w.chain, work.header, work.state, work.txs, work.unclelist(), work.receipts)
10881096
}
10891097

@@ -1113,8 +1121,14 @@ func (w *worker) commitWork(interrupt *int32, noempty bool, timestamp int64) {
11131121
if !noempty && atomic.LoadUint32(&w.noempty) == 0 {
11141122
w.commit(work.copy(), nil, false, start)
11151123
}
1124+
11161125
// Fill pending transactions from the txpool
1117-
w.fillTransactions(interrupt, work)
1126+
err = w.fillTransactions(interrupt, work)
1127+
if errors.Is(err, errBlockInterruptedByNewHead) {
1128+
work.discard()
1129+
return
1130+
}
1131+
11181132
w.commit(work.copy(), w.fullTaskHook, true, start)
11191133

11201134
// Swap out the old work with the new one, terminating any leftover

0 commit comments

Comments
 (0)