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