Skip to content

Commit 8fbf29a

Browse files
Ruterijagdeep sidhu
authored andcommitted
miner: discard interrupted blocks (ethereum#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 c1be38d commit 8fbf29a

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
@@ -78,6 +78,11 @@ const (
7878
staleThreshold = 7
7979
)
8080

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

852-
func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByPriceAndNonce, interrupt *int32) bool {
857+
func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByPriceAndNonce, interrupt *int32) error {
853858
gasLimit := env.header.GasLimit
854859
if env.gasPool == nil {
855860
env.gasPool = new(core.GasPool).AddGas(gasLimit)
@@ -874,8 +879,9 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP
874879
ratio: ratio,
875880
inc: true,
876881
}
882+
return errBlockInterruptedByRecommit
877883
}
878-
return atomic.LoadInt32(interrupt) == commitInterruptNewHead
884+
return errBlockInterruptedByNewHead
879885
}
880886
// If we don't have enough gas for any further transactions then we're done
881887
if env.gasPool.Gas() < params.TxGas {
@@ -959,7 +965,7 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP
959965
if interrupt != nil {
960966
w.resubmitAdjustCh <- &intervalAdjust{inc: false}
961967
}
962-
return false
968+
return nil
963969
}
964970

965971
// generateParams wraps various of settings for generating sealing task.
@@ -1058,7 +1064,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
10581064
// fillTransactions retrieves the pending transactions from the txpool and fills them
10591065
// into the given sealing block. The transaction selection and ordering strategy can
10601066
// be customized with the plugin in the future.
1061-
func (w *worker) fillTransactions(interrupt *int32, env *environment) {
1067+
func (w *worker) fillTransactions(interrupt *int32, env *environment) error {
10621068
// Split the pending transactions into locals and remotes
10631069
// Fill the block with all available pending transactions.
10641070
pending := w.eth.TxPool().Pending(true)
@@ -1071,16 +1077,17 @@ func (w *worker) fillTransactions(interrupt *int32, env *environment) {
10711077
}
10721078
if len(localTxs) > 0 {
10731079
txs := types.NewTransactionsByPriceAndNonce(env.signer, localTxs, env.header.BaseFee)
1074-
if w.commitTransactions(env, txs, interrupt) {
1075-
return
1080+
if err := w.commitTransactions(env, txs, interrupt); err != nil {
1081+
return err
10761082
}
10771083
}
10781084
if len(remoteTxs) > 0 {
10791085
txs := types.NewTransactionsByPriceAndNonce(env.signer, remoteTxs, env.header.BaseFee)
1080-
if w.commitTransactions(env, txs, interrupt) {
1081-
return
1086+
if err := w.commitTransactions(env, txs, interrupt); err != nil {
1087+
return err
10821088
}
10831089
}
1090+
return nil
10841091
}
10851092

10861093
// generateWork generates a sealing block based on the given parameters.
@@ -1092,6 +1099,7 @@ func (w *worker) generateWork(params *generateParams) (*types.Block, error) {
10921099
defer work.discard()
10931100

10941101
w.fillTransactions(nil, work)
1102+
10951103
return w.engine.FinalizeAndAssemble(w.chain, work.header, work.state, work.txs, work.unclelist(), work.receipts)
10961104
}
10971105

@@ -1121,8 +1129,14 @@ func (w *worker) commitWork(interrupt *int32, noempty bool, timestamp int64) {
11211129
if !noempty && atomic.LoadUint32(&w.noempty) == 0 {
11221130
w.commit(work.copy(), nil, false, start)
11231131
}
1132+
11241133
// Fill pending transactions from the txpool
1125-
w.fillTransactions(interrupt, work)
1134+
err = w.fillTransactions(interrupt, work)
1135+
if errors.Is(err, errBlockInterruptedByNewHead) {
1136+
work.discard()
1137+
return
1138+
}
1139+
11261140
w.commit(work.copy(), w.fullTaskHook, true, start)
11271141

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

0 commit comments

Comments
 (0)