@@ -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.
8287type 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