github.com/hardtosaygoodbye/go-ethereum@v1.10.16-0.20220122011429-97003b9e6c15/miner/worker.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package miner
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"math/big"
    23  	"sync"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	mapset "github.com/deckarep/golang-set"
    28  	"github.com/hardtosaygoodbye/go-ethereum/common"
    29  	"github.com/hardtosaygoodbye/go-ethereum/consensus"
    30  	"github.com/hardtosaygoodbye/go-ethereum/consensus/misc"
    31  	"github.com/hardtosaygoodbye/go-ethereum/core"
    32  	"github.com/hardtosaygoodbye/go-ethereum/core/state"
    33  	"github.com/hardtosaygoodbye/go-ethereum/core/types"
    34  	"github.com/hardtosaygoodbye/go-ethereum/event"
    35  	"github.com/hardtosaygoodbye/go-ethereum/log"
    36  	"github.com/hardtosaygoodbye/go-ethereum/params"
    37  	"github.com/hardtosaygoodbye/go-ethereum/trie"
    38  )
    39  
    40  const (
    41  	// resultQueueSize is the size of channel listening to sealing result.
    42  	resultQueueSize = 10
    43  
    44  	// txChanSize is the size of channel listening to NewTxsEvent.
    45  	// The number is referenced from the size of tx pool.
    46  	txChanSize = 4096
    47  
    48  	// chainHeadChanSize is the size of channel listening to ChainHeadEvent.
    49  	chainHeadChanSize = 10
    50  
    51  	// chainSideChanSize is the size of channel listening to ChainSideEvent.
    52  	chainSideChanSize = 10
    53  
    54  	// resubmitAdjustChanSize is the size of resubmitting interval adjustment channel.
    55  	resubmitAdjustChanSize = 10
    56  
    57  	// miningLogAtDepth is the number of confirmations before logging successful mining.
    58  	miningLogAtDepth = 7
    59  
    60  	// minRecommitInterval is the minimal time interval to recreate the mining block with
    61  	// any newly arrived transactions.
    62  	minRecommitInterval = 1 * time.Second
    63  
    64  	// maxRecommitInterval is the maximum time interval to recreate the mining block with
    65  	// any newly arrived transactions.
    66  	maxRecommitInterval = 15 * time.Second
    67  
    68  	// intervalAdjustRatio is the impact a single interval adjustment has on sealing work
    69  	// resubmitting interval.
    70  	intervalAdjustRatio = 0.1
    71  
    72  	// intervalAdjustBias is applied during the new resubmit interval calculation in favor of
    73  	// increasing upper limit or decreasing lower limit so that the limit can be reachable.
    74  	intervalAdjustBias = 200 * 1000.0 * 1000.0
    75  
    76  	// staleThreshold is the maximum depth of the acceptable stale block.
    77  	staleThreshold = 7
    78  )
    79  
    80  // environment is the worker's current environment and holds all of the current state information.
    81  type environment struct {
    82  	signer types.Signer
    83  
    84  	state     *state.StateDB // apply state changes here
    85  	ancestors mapset.Set     // ancestor set (used for checking uncle parent validity)
    86  	family    mapset.Set     // family set (used for checking uncle invalidity)
    87  	uncles    mapset.Set     // uncle set
    88  	tcount    int            // tx count in cycle
    89  	gasPool   *core.GasPool  // available gas used to pack transactions
    90  
    91  	header   *types.Header
    92  	txs      []*types.Transaction
    93  	receipts []*types.Receipt
    94  }
    95  
    96  // task contains all information for consensus engine sealing and result submitting.
    97  type task struct {
    98  	receipts  []*types.Receipt
    99  	state     *state.StateDB
   100  	block     *types.Block
   101  	createdAt time.Time
   102  }
   103  
   104  const (
   105  	commitInterruptNone int32 = iota
   106  	commitInterruptNewHead
   107  	commitInterruptResubmit
   108  )
   109  
   110  // newWorkReq represents a request for new sealing work submitting with relative interrupt notifier.
   111  type newWorkReq struct {
   112  	interrupt *int32
   113  	noempty   bool
   114  	timestamp int64
   115  }
   116  
   117  // intervalAdjust represents a resubmitting interval adjustment.
   118  type intervalAdjust struct {
   119  	ratio float64
   120  	inc   bool
   121  }
   122  
   123  // worker is the main object which takes care of submitting new work to consensus engine
   124  // and gathering the sealing result.
   125  type worker struct {
   126  	config      *Config
   127  	chainConfig *params.ChainConfig
   128  	engine      consensus.Engine
   129  	eth         Backend
   130  	chain       *core.BlockChain
   131  	merger      *consensus.Merger
   132  
   133  	// Feeds
   134  	pendingLogsFeed event.Feed
   135  
   136  	// Subscriptions
   137  	mux          *event.TypeMux
   138  	txsCh        chan core.NewTxsEvent
   139  	txsSub       event.Subscription
   140  	chainHeadCh  chan core.ChainHeadEvent
   141  	chainHeadSub event.Subscription
   142  	chainSideCh  chan core.ChainSideEvent
   143  	chainSideSub event.Subscription
   144  
   145  	// Channels
   146  	newWorkCh          chan *newWorkReq
   147  	taskCh             chan *task
   148  	resultCh           chan *types.Block
   149  	startCh            chan struct{}
   150  	exitCh             chan struct{}
   151  	resubmitIntervalCh chan time.Duration
   152  	resubmitAdjustCh   chan *intervalAdjust
   153  
   154  	wg sync.WaitGroup
   155  
   156  	current      *environment                 // An environment for current running cycle.
   157  	localUncles  map[common.Hash]*types.Block // A set of side blocks generated locally as the possible uncle blocks.
   158  	remoteUncles map[common.Hash]*types.Block // A set of side blocks as the possible uncle blocks.
   159  	unconfirmed  *unconfirmedBlocks           // A set of locally mined blocks pending canonicalness confirmations.
   160  
   161  	mu       sync.RWMutex // The lock used to protect the coinbase and extra fields
   162  	coinbase common.Address
   163  	extra    []byte
   164  
   165  	pendingMu    sync.RWMutex
   166  	pendingTasks map[common.Hash]*task
   167  
   168  	snapshotMu       sync.RWMutex // The lock used to protect the snapshots below
   169  	snapshotBlock    *types.Block
   170  	snapshotReceipts types.Receipts
   171  	snapshotState    *state.StateDB
   172  
   173  	// atomic status counters
   174  	running int32 // The indicator whether the consensus engine is running or not.
   175  	newTxs  int32 // New arrival transaction count since last sealing work submitting.
   176  
   177  	// noempty is the flag used to control whether the feature of pre-seal empty
   178  	// block is enabled. The default value is false(pre-seal is enabled by default).
   179  	// But in some special scenario the consensus engine will seal blocks instantaneously,
   180  	// in this case this feature will add all empty blocks into canonical chain
   181  	// non-stop and no real transaction will be included.
   182  	noempty uint32
   183  
   184  	// External functions
   185  	isLocalBlock func(header *types.Header) bool // Function used to determine whether the specified block is mined by local miner.
   186  
   187  	// Test hooks
   188  	newTaskHook  func(*task)                        // Method to call upon receiving a new sealing task.
   189  	skipSealHook func(*task) bool                   // Method to decide whether skipping the sealing.
   190  	fullTaskHook func()                             // Method to call before pushing the full sealing task.
   191  	resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval.
   192  }
   193  
   194  func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, isLocalBlock func(header *types.Header) bool, init bool, merger *consensus.Merger) *worker {
   195  	worker := &worker{
   196  		config:             config,
   197  		chainConfig:        chainConfig,
   198  		engine:             engine,
   199  		eth:                eth,
   200  		mux:                mux,
   201  		chain:              eth.BlockChain(),
   202  		merger:             merger,
   203  		isLocalBlock:       isLocalBlock,
   204  		localUncles:        make(map[common.Hash]*types.Block),
   205  		remoteUncles:       make(map[common.Hash]*types.Block),
   206  		unconfirmed:        newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth),
   207  		pendingTasks:       make(map[common.Hash]*task),
   208  		txsCh:              make(chan core.NewTxsEvent, txChanSize),
   209  		chainHeadCh:        make(chan core.ChainHeadEvent, chainHeadChanSize),
   210  		chainSideCh:        make(chan core.ChainSideEvent, chainSideChanSize),
   211  		newWorkCh:          make(chan *newWorkReq),
   212  		taskCh:             make(chan *task),
   213  		resultCh:           make(chan *types.Block, resultQueueSize),
   214  		exitCh:             make(chan struct{}),
   215  		startCh:            make(chan struct{}, 1),
   216  		resubmitIntervalCh: make(chan time.Duration),
   217  		resubmitAdjustCh:   make(chan *intervalAdjust, resubmitAdjustChanSize),
   218  	}
   219  	// Subscribe NewTxsEvent for tx pool
   220  	worker.txsSub = eth.TxPool().SubscribeNewTxsEvent(worker.txsCh)
   221  	// Subscribe events for blockchain
   222  	worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh)
   223  	worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh)
   224  
   225  	// Sanitize recommit interval if the user-specified one is too short.
   226  	recommit := worker.config.Recommit
   227  	if recommit < minRecommitInterval {
   228  		log.Warn("Sanitizing miner recommit interval", "provided", recommit, "updated", minRecommitInterval)
   229  		recommit = minRecommitInterval
   230  	}
   231  
   232  	worker.wg.Add(4)
   233  	go worker.mainLoop()
   234  	go worker.newWorkLoop(recommit)
   235  	go worker.resultLoop()
   236  	go worker.taskLoop()
   237  
   238  	// Submit first work to initialize pending state.
   239  	if init {
   240  		worker.startCh <- struct{}{}
   241  	}
   242  	return worker
   243  }
   244  
   245  // setEtherbase sets the etherbase used to initialize the block coinbase field.
   246  func (w *worker) setEtherbase(addr common.Address) {
   247  	w.mu.Lock()
   248  	defer w.mu.Unlock()
   249  	w.coinbase = addr
   250  }
   251  
   252  func (w *worker) setGasCeil(ceil uint64) {
   253  	w.mu.Lock()
   254  	defer w.mu.Unlock()
   255  	w.config.GasCeil = ceil
   256  }
   257  
   258  // setExtra sets the content used to initialize the block extra field.
   259  func (w *worker) setExtra(extra []byte) {
   260  	w.mu.Lock()
   261  	defer w.mu.Unlock()
   262  	w.extra = extra
   263  }
   264  
   265  // setRecommitInterval updates the interval for miner sealing work recommitting.
   266  func (w *worker) setRecommitInterval(interval time.Duration) {
   267  	w.resubmitIntervalCh <- interval
   268  }
   269  
   270  // disablePreseal disables pre-sealing mining feature
   271  func (w *worker) disablePreseal() {
   272  	atomic.StoreUint32(&w.noempty, 1)
   273  }
   274  
   275  // enablePreseal enables pre-sealing mining feature
   276  func (w *worker) enablePreseal() {
   277  	atomic.StoreUint32(&w.noempty, 0)
   278  }
   279  
   280  // pending returns the pending state and corresponding block.
   281  func (w *worker) pending() (*types.Block, *state.StateDB) {
   282  	// return a snapshot to avoid contention on currentMu mutex
   283  	w.snapshotMu.RLock()
   284  	defer w.snapshotMu.RUnlock()
   285  	if w.snapshotState == nil {
   286  		return nil, nil
   287  	}
   288  	return w.snapshotBlock, w.snapshotState.Copy()
   289  }
   290  
   291  // pendingBlock returns pending block.
   292  func (w *worker) pendingBlock() *types.Block {
   293  	// return a snapshot to avoid contention on currentMu mutex
   294  	w.snapshotMu.RLock()
   295  	defer w.snapshotMu.RUnlock()
   296  	return w.snapshotBlock
   297  }
   298  
   299  // pendingBlockAndReceipts returns pending block and corresponding receipts.
   300  func (w *worker) pendingBlockAndReceipts() (*types.Block, types.Receipts) {
   301  	// return a snapshot to avoid contention on currentMu mutex
   302  	w.snapshotMu.RLock()
   303  	defer w.snapshotMu.RUnlock()
   304  	return w.snapshotBlock, w.snapshotReceipts
   305  }
   306  
   307  // start sets the running status as 1 and triggers new work submitting.
   308  func (w *worker) start() {
   309  	atomic.StoreInt32(&w.running, 1)
   310  	w.startCh <- struct{}{}
   311  }
   312  
   313  // stop sets the running status as 0.
   314  func (w *worker) stop() {
   315  	atomic.StoreInt32(&w.running, 0)
   316  }
   317  
   318  // isRunning returns an indicator whether worker is running or not.
   319  func (w *worker) isRunning() bool {
   320  	return atomic.LoadInt32(&w.running) == 1
   321  }
   322  
   323  // close terminates all background threads maintained by the worker.
   324  // Note the worker does not support being closed multiple times.
   325  func (w *worker) close() {
   326  	atomic.StoreInt32(&w.running, 0)
   327  	close(w.exitCh)
   328  	w.wg.Wait()
   329  }
   330  
   331  // recalcRecommit recalculates the resubmitting interval upon feedback.
   332  func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) time.Duration {
   333  	var (
   334  		prevF = float64(prev.Nanoseconds())
   335  		next  float64
   336  	)
   337  	if inc {
   338  		next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias)
   339  		max := float64(maxRecommitInterval.Nanoseconds())
   340  		if next > max {
   341  			next = max
   342  		}
   343  	} else {
   344  		next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias)
   345  		min := float64(minRecommit.Nanoseconds())
   346  		if next < min {
   347  			next = min
   348  		}
   349  	}
   350  	return time.Duration(int64(next))
   351  }
   352  
   353  // newWorkLoop is a standalone goroutine to submit new mining work upon received events.
   354  func (w *worker) newWorkLoop(recommit time.Duration) {
   355  	defer w.wg.Done()
   356  	var (
   357  		interrupt   *int32
   358  		minRecommit = recommit // minimal resubmit interval specified by user.
   359  		timestamp   int64      // timestamp for each round of mining.
   360  	)
   361  
   362  	timer := time.NewTimer(0)
   363  	defer timer.Stop()
   364  	<-timer.C // discard the initial tick
   365  
   366  	// commit aborts in-flight transaction execution with given signal and resubmits a new one.
   367  	commit := func(noempty bool, s int32) {
   368  		if interrupt != nil {
   369  			atomic.StoreInt32(interrupt, s)
   370  		}
   371  		interrupt = new(int32)
   372  		select {
   373  		case w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty, timestamp: timestamp}:
   374  		case <-w.exitCh:
   375  			return
   376  		}
   377  		timer.Reset(recommit)
   378  		atomic.StoreInt32(&w.newTxs, 0)
   379  	}
   380  	// clearPending cleans the stale pending tasks.
   381  	clearPending := func(number uint64) {
   382  		w.pendingMu.Lock()
   383  		for h, t := range w.pendingTasks {
   384  			if t.block.NumberU64()+staleThreshold <= number {
   385  				delete(w.pendingTasks, h)
   386  			}
   387  		}
   388  		w.pendingMu.Unlock()
   389  	}
   390  
   391  	for {
   392  		select {
   393  		case <-w.startCh:
   394  			clearPending(w.chain.CurrentBlock().NumberU64())
   395  			timestamp = time.Now().Unix()
   396  			commit(false, commitInterruptNewHead)
   397  
   398  		case head := <-w.chainHeadCh:
   399  			clearPending(head.Block.NumberU64())
   400  			timestamp = time.Now().Unix()
   401  			commit(false, commitInterruptNewHead)
   402  
   403  		case <-timer.C:
   404  			// If mining is running resubmit a new work cycle periodically to pull in
   405  			// higher priced transactions. Disable this overhead for pending blocks.
   406  			if w.isRunning() && (w.chainConfig.Clique == nil || w.chainConfig.Clique.Period > 0) {
   407  				// Short circuit if no new transaction arrives.
   408  				if atomic.LoadInt32(&w.newTxs) == 0 {
   409  					timer.Reset(recommit)
   410  					continue
   411  				}
   412  				commit(true, commitInterruptResubmit)
   413  			}
   414  
   415  		case interval := <-w.resubmitIntervalCh:
   416  			// Adjust resubmit interval explicitly by user.
   417  			if interval < minRecommitInterval {
   418  				log.Warn("Sanitizing miner recommit interval", "provided", interval, "updated", minRecommitInterval)
   419  				interval = minRecommitInterval
   420  			}
   421  			log.Info("Miner recommit interval update", "from", minRecommit, "to", interval)
   422  			minRecommit, recommit = interval, interval
   423  
   424  			if w.resubmitHook != nil {
   425  				w.resubmitHook(minRecommit, recommit)
   426  			}
   427  
   428  		case adjust := <-w.resubmitAdjustCh:
   429  			// Adjust resubmit interval by feedback.
   430  			if adjust.inc {
   431  				before := recommit
   432  				target := float64(recommit.Nanoseconds()) / adjust.ratio
   433  				recommit = recalcRecommit(minRecommit, recommit, target, true)
   434  				log.Trace("Increase miner recommit interval", "from", before, "to", recommit)
   435  			} else {
   436  				before := recommit
   437  				recommit = recalcRecommit(minRecommit, recommit, float64(minRecommit.Nanoseconds()), false)
   438  				log.Trace("Decrease miner recommit interval", "from", before, "to", recommit)
   439  			}
   440  
   441  			if w.resubmitHook != nil {
   442  				w.resubmitHook(minRecommit, recommit)
   443  			}
   444  
   445  		case <-w.exitCh:
   446  			return
   447  		}
   448  	}
   449  }
   450  
   451  // mainLoop is a standalone goroutine to regenerate the sealing task based on the received event.
   452  func (w *worker) mainLoop() {
   453  	defer w.wg.Done()
   454  	defer w.txsSub.Unsubscribe()
   455  	defer w.chainHeadSub.Unsubscribe()
   456  	defer w.chainSideSub.Unsubscribe()
   457  	defer func() {
   458  		if w.current != nil && w.current.state != nil {
   459  			w.current.state.StopPrefetcher()
   460  		}
   461  	}()
   462  
   463  	for {
   464  		select {
   465  		case req := <-w.newWorkCh:
   466  			w.commitNewWork(req.interrupt, req.noempty, req.timestamp)
   467  
   468  		case ev := <-w.chainSideCh:
   469  			// Short circuit for duplicate side blocks
   470  			if _, exist := w.localUncles[ev.Block.Hash()]; exist {
   471  				continue
   472  			}
   473  			if _, exist := w.remoteUncles[ev.Block.Hash()]; exist {
   474  				continue
   475  			}
   476  			// Add side block to possible uncle block set depending on the author.
   477  			if w.isLocalBlock != nil && w.isLocalBlock(ev.Block.Header()) {
   478  				w.localUncles[ev.Block.Hash()] = ev.Block
   479  			} else {
   480  				w.remoteUncles[ev.Block.Hash()] = ev.Block
   481  			}
   482  			// If our mining block contains less than 2 uncle blocks,
   483  			// add the new uncle block if valid and regenerate a mining block.
   484  			if w.isRunning() && w.current != nil && w.current.uncles.Cardinality() < 2 {
   485  				start := time.Now()
   486  				if err := w.commitUncle(w.current, ev.Block.Header()); err == nil {
   487  					var uncles []*types.Header
   488  					w.current.uncles.Each(func(item interface{}) bool {
   489  						hash, ok := item.(common.Hash)
   490  						if !ok {
   491  							return false
   492  						}
   493  						uncle, exist := w.localUncles[hash]
   494  						if !exist {
   495  							uncle, exist = w.remoteUncles[hash]
   496  						}
   497  						if !exist {
   498  							return false
   499  						}
   500  						uncles = append(uncles, uncle.Header())
   501  						return false
   502  					})
   503  					w.commit(uncles, nil, true, start)
   504  				}
   505  			}
   506  
   507  		case ev := <-w.txsCh:
   508  			// Apply transactions to the pending state if we're not mining.
   509  			//
   510  			// Note all transactions received may not be continuous with transactions
   511  			// already included in the current mining block. These transactions will
   512  			// be automatically eliminated.
   513  			if !w.isRunning() && w.current != nil {
   514  				// If block is already full, abort
   515  				if gp := w.current.gasPool; gp != nil && gp.Gas() < params.TxGas {
   516  					continue
   517  				}
   518  				w.mu.RLock()
   519  				coinbase := w.coinbase
   520  				w.mu.RUnlock()
   521  
   522  				txs := make(map[common.Address]types.Transactions)
   523  				for _, tx := range ev.Txs {
   524  					acc, _ := types.Sender(w.current.signer, tx)
   525  					txs[acc] = append(txs[acc], tx)
   526  				}
   527  				txset := types.NewTransactionsByPriceAndNonce(w.current.signer, txs, w.current.header.BaseFee)
   528  				tcount := w.current.tcount
   529  				w.commitTransactions(txset, coinbase, nil)
   530  				// Only update the snapshot if any new transactons were added
   531  				// to the pending block
   532  				if tcount != w.current.tcount {
   533  					w.updateSnapshot()
   534  				}
   535  			} else {
   536  				// Special case, if the consensus engine is 0 period clique(dev mode),
   537  				// submit mining work here since all empty submission will be rejected
   538  				// by clique. Of course the advance sealing(empty submission) is disabled.
   539  				if w.chainConfig.Clique != nil && w.chainConfig.Clique.Period == 0 {
   540  					w.commitNewWork(nil, true, time.Now().Unix())
   541  				}
   542  			}
   543  			atomic.AddInt32(&w.newTxs, int32(len(ev.Txs)))
   544  
   545  		// System stopped
   546  		case <-w.exitCh:
   547  			return
   548  		case <-w.txsSub.Err():
   549  			return
   550  		case <-w.chainHeadSub.Err():
   551  			return
   552  		case <-w.chainSideSub.Err():
   553  			return
   554  		}
   555  	}
   556  }
   557  
   558  // taskLoop is a standalone goroutine to fetch sealing task from the generator and
   559  // push them to consensus engine.
   560  func (w *worker) taskLoop() {
   561  	defer w.wg.Done()
   562  	var (
   563  		stopCh chan struct{}
   564  		prev   common.Hash
   565  	)
   566  
   567  	// interrupt aborts the in-flight sealing task.
   568  	interrupt := func() {
   569  		if stopCh != nil {
   570  			close(stopCh)
   571  			stopCh = nil
   572  		}
   573  	}
   574  	for {
   575  		select {
   576  		case task := <-w.taskCh:
   577  			if w.newTaskHook != nil {
   578  				w.newTaskHook(task)
   579  			}
   580  			// Reject duplicate sealing work due to resubmitting.
   581  			sealHash := w.engine.SealHash(task.block.Header())
   582  			if sealHash == prev {
   583  				continue
   584  			}
   585  			// Interrupt previous sealing operation
   586  			interrupt()
   587  			stopCh, prev = make(chan struct{}), sealHash
   588  
   589  			if w.skipSealHook != nil && w.skipSealHook(task) {
   590  				continue
   591  			}
   592  			w.pendingMu.Lock()
   593  			w.pendingTasks[sealHash] = task
   594  			w.pendingMu.Unlock()
   595  
   596  			if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh); err != nil {
   597  				log.Warn("Block sealing failed", "err", err)
   598  				w.pendingMu.Lock()
   599  				delete(w.pendingTasks, sealHash)
   600  				w.pendingMu.Unlock()
   601  			}
   602  		case <-w.exitCh:
   603  			interrupt()
   604  			return
   605  		}
   606  	}
   607  }
   608  
   609  // resultLoop is a standalone goroutine to handle sealing result submitting
   610  // and flush relative data to the database.
   611  func (w *worker) resultLoop() {
   612  	defer w.wg.Done()
   613  	for {
   614  		select {
   615  		case block := <-w.resultCh:
   616  			// Short circuit when receiving empty result.
   617  			if block == nil {
   618  				continue
   619  			}
   620  			// Short circuit when receiving duplicate result caused by resubmitting.
   621  			if w.chain.HasBlock(block.Hash(), block.NumberU64()) {
   622  				continue
   623  			}
   624  			var (
   625  				sealhash = w.engine.SealHash(block.Header())
   626  				hash     = block.Hash()
   627  			)
   628  			w.pendingMu.RLock()
   629  			task, exist := w.pendingTasks[sealhash]
   630  			w.pendingMu.RUnlock()
   631  			if !exist {
   632  				log.Error("Block found but no relative pending task", "number", block.Number(), "sealhash", sealhash, "hash", hash)
   633  				continue
   634  			}
   635  			// Different block could share same sealhash, deep copy here to prevent write-write conflict.
   636  			var (
   637  				receipts = make([]*types.Receipt, len(task.receipts))
   638  				logs     []*types.Log
   639  			)
   640  			for i, taskReceipt := range task.receipts {
   641  				receipt := new(types.Receipt)
   642  				receipts[i] = receipt
   643  				*receipt = *taskReceipt
   644  
   645  				// add block location fields
   646  				receipt.BlockHash = hash
   647  				receipt.BlockNumber = block.Number()
   648  				receipt.TransactionIndex = uint(i)
   649  
   650  				// Update the block hash in all logs since it is now available and not when the
   651  				// receipt/log of individual transactions were created.
   652  				receipt.Logs = make([]*types.Log, len(taskReceipt.Logs))
   653  				for i, taskLog := range taskReceipt.Logs {
   654  					log := new(types.Log)
   655  					receipt.Logs[i] = log
   656  					*log = *taskLog
   657  					log.BlockHash = hash
   658  				}
   659  				logs = append(logs, receipt.Logs...)
   660  			}
   661  			// Commit block and state to database.
   662  			_, err := w.chain.WriteBlockAndSetHead(block, receipts, logs, task.state, true)
   663  			if err != nil {
   664  				log.Error("Failed writing block to chain", "err", err)
   665  				continue
   666  			}
   667  			log.Info("Successfully sealed new block", "number", block.Number(), "sealhash", sealhash, "hash", hash,
   668  				"elapsed", common.PrettyDuration(time.Since(task.createdAt)))
   669  
   670  			// Broadcast the block and announce chain insertion event
   671  			w.mux.Post(core.NewMinedBlockEvent{Block: block})
   672  
   673  			// Insert the block into the set of pending ones to resultLoop for confirmations
   674  			w.unconfirmed.Insert(block.NumberU64(), block.Hash())
   675  
   676  		case <-w.exitCh:
   677  			return
   678  		}
   679  	}
   680  }
   681  
   682  // makeCurrent creates a new environment for the current cycle.
   683  func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error {
   684  	// Retrieve the parent state to execute on top and start a prefetcher for
   685  	// the miner to speed block sealing up a bit
   686  	state, err := w.chain.StateAt(parent.Root())
   687  	if err != nil {
   688  		return err
   689  	}
   690  	state.StartPrefetcher("miner")
   691  
   692  	env := &environment{
   693  		signer:    types.MakeSigner(w.chainConfig, header.Number),
   694  		state:     state,
   695  		ancestors: mapset.NewSet(),
   696  		family:    mapset.NewSet(),
   697  		uncles:    mapset.NewSet(),
   698  		header:    header,
   699  	}
   700  	// when 08 is processed ancestors contain 07 (quick block)
   701  	for _, ancestor := range w.chain.GetBlocksFromHash(parent.Hash(), 7) {
   702  		for _, uncle := range ancestor.Uncles() {
   703  			env.family.Add(uncle.Hash())
   704  		}
   705  		env.family.Add(ancestor.Hash())
   706  		env.ancestors.Add(ancestor.Hash())
   707  	}
   708  	// Keep track of transactions which return errors so they can be removed
   709  	env.tcount = 0
   710  
   711  	// Swap out the old work with the new one, terminating any leftover prefetcher
   712  	// processes in the mean time and starting a new one.
   713  	if w.current != nil && w.current.state != nil {
   714  		w.current.state.StopPrefetcher()
   715  	}
   716  	w.current = env
   717  	return nil
   718  }
   719  
   720  // commitUncle adds the given block to uncle block set, returns error if failed to add.
   721  func (w *worker) commitUncle(env *environment, uncle *types.Header) error {
   722  	hash := uncle.Hash()
   723  	if env.uncles.Contains(hash) {
   724  		return errors.New("uncle not unique")
   725  	}
   726  	if env.header.ParentHash == uncle.ParentHash {
   727  		return errors.New("uncle is sibling")
   728  	}
   729  	if !env.ancestors.Contains(uncle.ParentHash) {
   730  		return errors.New("uncle's parent unknown")
   731  	}
   732  	if env.family.Contains(hash) {
   733  		return errors.New("uncle already included")
   734  	}
   735  	env.uncles.Add(uncle.Hash())
   736  	return nil
   737  }
   738  
   739  // updateSnapshot updates pending snapshot block and state.
   740  // Note this function assumes the current variable is thread safe.
   741  func (w *worker) updateSnapshot() {
   742  	w.snapshotMu.Lock()
   743  	defer w.snapshotMu.Unlock()
   744  
   745  	var uncles []*types.Header
   746  	w.current.uncles.Each(func(item interface{}) bool {
   747  		hash, ok := item.(common.Hash)
   748  		if !ok {
   749  			return false
   750  		}
   751  		uncle, exist := w.localUncles[hash]
   752  		if !exist {
   753  			uncle, exist = w.remoteUncles[hash]
   754  		}
   755  		if !exist {
   756  			return false
   757  		}
   758  		uncles = append(uncles, uncle.Header())
   759  		return false
   760  	})
   761  
   762  	w.snapshotBlock = types.NewBlock(
   763  		w.current.header,
   764  		w.current.txs,
   765  		uncles,
   766  		w.current.receipts,
   767  		trie.NewStackTrie(nil),
   768  	)
   769  	w.snapshotReceipts = copyReceipts(w.current.receipts)
   770  	w.snapshotState = w.current.state.Copy()
   771  }
   772  
   773  func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Address) ([]*types.Log, error) {
   774  	snap := w.current.state.Snapshot()
   775  
   776  	receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &coinbase, w.current.gasPool, w.current.state, w.current.header, tx, &w.current.header.GasUsed, *w.chain.GetVMConfig())
   777  	if err != nil {
   778  		w.current.state.RevertToSnapshot(snap)
   779  		return nil, err
   780  	}
   781  	w.current.txs = append(w.current.txs, tx)
   782  	w.current.receipts = append(w.current.receipts, receipt)
   783  
   784  	return receipt.Logs, nil
   785  }
   786  
   787  func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coinbase common.Address, interrupt *int32) bool {
   788  	// Short circuit if current is nil
   789  	if w.current == nil {
   790  		return true
   791  	}
   792  
   793  	gasLimit := w.current.header.GasLimit
   794  	if w.current.gasPool == nil {
   795  		w.current.gasPool = new(core.GasPool).AddGas(gasLimit)
   796  	}
   797  
   798  	var coalescedLogs []*types.Log
   799  
   800  	for {
   801  		// In the following three cases, we will interrupt the execution of the transaction.
   802  		// (1) new head block event arrival, the interrupt signal is 1
   803  		// (2) worker start or restart, the interrupt signal is 1
   804  		// (3) worker recreate the mining block with any newly arrived transactions, the interrupt signal is 2.
   805  		// For the first two cases, the semi-finished work will be discarded.
   806  		// For the third case, the semi-finished work will be submitted to the consensus engine.
   807  		if interrupt != nil && atomic.LoadInt32(interrupt) != commitInterruptNone {
   808  			// Notify resubmit loop to increase resubmitting interval due to too frequent commits.
   809  			if atomic.LoadInt32(interrupt) == commitInterruptResubmit {
   810  				ratio := float64(gasLimit-w.current.gasPool.Gas()) / float64(gasLimit)
   811  				if ratio < 0.1 {
   812  					ratio = 0.1
   813  				}
   814  				w.resubmitAdjustCh <- &intervalAdjust{
   815  					ratio: ratio,
   816  					inc:   true,
   817  				}
   818  			}
   819  			return atomic.LoadInt32(interrupt) == commitInterruptNewHead
   820  		}
   821  		// If we don't have enough gas for any further transactions then we're done
   822  		if w.current.gasPool.Gas() < params.TxGas {
   823  			log.Trace("Not enough gas for further transactions", "have", w.current.gasPool, "want", params.TxGas)
   824  			break
   825  		}
   826  		// Retrieve the next transaction and abort if all done
   827  		tx := txs.Peek()
   828  		if tx == nil {
   829  			break
   830  		}
   831  		// Error may be ignored here. The error has already been checked
   832  		// during transaction acceptance is the transaction pool.
   833  		//
   834  		// We use the eip155 signer regardless of the current hf.
   835  		from, _ := types.Sender(w.current.signer, tx)
   836  		// Check whether the tx is replay protected. If we're not in the EIP155 hf
   837  		// phase, start ignoring the sender until we do.
   838  		if tx.Protected() && !w.chainConfig.IsEIP155(w.current.header.Number) {
   839  			log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", w.chainConfig.EIP155Block)
   840  
   841  			txs.Pop()
   842  			continue
   843  		}
   844  		// Start executing the transaction
   845  		w.current.state.Prepare(tx.Hash(), w.current.tcount)
   846  
   847  		logs, err := w.commitTransaction(tx, coinbase)
   848  		switch {
   849  		case errors.Is(err, core.ErrGasLimitReached):
   850  			// Pop the current out-of-gas transaction without shifting in the next from the account
   851  			log.Trace("Gas limit exceeded for current block", "sender", from)
   852  			txs.Pop()
   853  
   854  		case errors.Is(err, core.ErrNonceTooLow):
   855  			// New head notification data race between the transaction pool and miner, shift
   856  			log.Trace("Skipping transaction with low nonce", "sender", from, "nonce", tx.Nonce())
   857  			txs.Shift()
   858  
   859  		case errors.Is(err, core.ErrNonceTooHigh):
   860  			// Reorg notification data race between the transaction pool and miner, skip account =
   861  			log.Trace("Skipping account with hight nonce", "sender", from, "nonce", tx.Nonce())
   862  			txs.Pop()
   863  
   864  		case errors.Is(err, nil):
   865  			// Everything ok, collect the logs and shift in the next transaction from the same account
   866  			coalescedLogs = append(coalescedLogs, logs...)
   867  			w.current.tcount++
   868  			txs.Shift()
   869  
   870  		case errors.Is(err, core.ErrTxTypeNotSupported):
   871  			// Pop the unsupported transaction without shifting in the next from the account
   872  			log.Trace("Skipping unsupported transaction type", "sender", from, "type", tx.Type())
   873  			txs.Pop()
   874  
   875  		default:
   876  			// Strange error, discard the transaction and get the next in line (note, the
   877  			// nonce-too-high clause will prevent us from executing in vain).
   878  			log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err)
   879  			txs.Shift()
   880  		}
   881  	}
   882  
   883  	if !w.isRunning() && len(coalescedLogs) > 0 {
   884  		// We don't push the pendingLogsEvent while we are mining. The reason is that
   885  		// when we are mining, the worker will regenerate a mining block every 3 seconds.
   886  		// In order to avoid pushing the repeated pendingLog, we disable the pending log pushing.
   887  
   888  		// make a copy, the state caches the logs and these logs get "upgraded" from pending to mined
   889  		// logs by filling in the block hash when the block was mined by the local miner. This can
   890  		// cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed.
   891  		cpy := make([]*types.Log, len(coalescedLogs))
   892  		for i, l := range coalescedLogs {
   893  			cpy[i] = new(types.Log)
   894  			*cpy[i] = *l
   895  		}
   896  		w.pendingLogsFeed.Send(cpy)
   897  	}
   898  	// Notify resubmit loop to decrease resubmitting interval if current interval is larger
   899  	// than the user-specified one.
   900  	if interrupt != nil {
   901  		w.resubmitAdjustCh <- &intervalAdjust{inc: false}
   902  	}
   903  	return false
   904  }
   905  
   906  // commitNewWork generates several new sealing tasks based on the parent block.
   907  func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64) {
   908  	w.mu.RLock()
   909  	defer w.mu.RUnlock()
   910  
   911  	tstart := time.Now()
   912  	parent := w.chain.CurrentBlock()
   913  
   914  	if parent.Time() >= uint64(timestamp) {
   915  		timestamp = int64(parent.Time() + 1)
   916  	}
   917  	num := parent.Number()
   918  	header := &types.Header{
   919  		ParentHash: parent.Hash(),
   920  		Number:     num.Add(num, common.Big1),
   921  		GasLimit:   core.CalcGasLimit(parent.GasLimit(), w.config.GasCeil),
   922  		Extra:      w.extra,
   923  		Time:       uint64(timestamp),
   924  	}
   925  	// Set baseFee and GasLimit if we are on an EIP-1559 chain
   926  	if w.chainConfig.IsLondon(header.Number) {
   927  		header.BaseFee = misc.CalcBaseFee(w.chainConfig, parent.Header())
   928  		if !w.chainConfig.IsLondon(parent.Number()) {
   929  			parentGasLimit := parent.GasLimit() * params.ElasticityMultiplier
   930  			header.GasLimit = core.CalcGasLimit(parentGasLimit, w.config.GasCeil)
   931  		}
   932  	}
   933  	// Only set the coinbase if our consensus engine is running (avoid spurious block rewards)
   934  	if w.isRunning() {
   935  		if w.coinbase == (common.Address{}) {
   936  			log.Error("Refusing to mine without etherbase")
   937  			return
   938  		}
   939  		header.Coinbase = w.coinbase
   940  	}
   941  	if err := w.engine.Prepare(w.chain, header); err != nil {
   942  		log.Error("Failed to prepare header for mining", "err", err)
   943  		return
   944  	}
   945  	// If we are care about TheDAO hard-fork check whether to override the extra-data or not
   946  	if daoBlock := w.chainConfig.DAOForkBlock; daoBlock != nil {
   947  		// Check whether the block is among the fork extra-override range
   948  		limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
   949  		if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
   950  			// Depending whether we support or oppose the fork, override differently
   951  			if w.chainConfig.DAOForkSupport {
   952  				header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
   953  			} else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
   954  				header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
   955  			}
   956  		}
   957  	}
   958  	// Could potentially happen if starting to mine in an odd state.
   959  	err := w.makeCurrent(parent, header)
   960  	if err != nil {
   961  		log.Error("Failed to create mining context", "err", err)
   962  		return
   963  	}
   964  	// Create the current work task and check any fork transitions needed
   965  	env := w.current
   966  	if w.chainConfig.DAOForkSupport && w.chainConfig.DAOForkBlock != nil && w.chainConfig.DAOForkBlock.Cmp(header.Number) == 0 {
   967  		misc.ApplyDAOHardFork(env.state)
   968  	}
   969  	// Accumulate the uncles for the current block
   970  	uncles := make([]*types.Header, 0, 2)
   971  	commitUncles := func(blocks map[common.Hash]*types.Block) {
   972  		// Clean up stale uncle blocks first
   973  		for hash, uncle := range blocks {
   974  			if uncle.NumberU64()+staleThreshold <= header.Number.Uint64() {
   975  				delete(blocks, hash)
   976  			}
   977  		}
   978  		for hash, uncle := range blocks {
   979  			if len(uncles) == 2 {
   980  				break
   981  			}
   982  			if err := w.commitUncle(env, uncle.Header()); err != nil {
   983  				log.Trace("Possible uncle rejected", "hash", hash, "reason", err)
   984  			} else {
   985  				log.Debug("Committing new uncle to block", "hash", hash)
   986  				uncles = append(uncles, uncle.Header())
   987  			}
   988  		}
   989  	}
   990  	// Prefer to locally generated uncle
   991  	commitUncles(w.localUncles)
   992  	commitUncles(w.remoteUncles)
   993  
   994  	// Create an empty block based on temporary copied state for
   995  	// sealing in advance without waiting block execution finished.
   996  	if !noempty && atomic.LoadUint32(&w.noempty) == 0 {
   997  		w.commit(uncles, nil, false, tstart)
   998  	}
   999  
  1000  	// Fill the block with all available pending transactions.
  1001  	pending := w.eth.TxPool().Pending(true)
  1002  	// Short circuit if there is no available pending transactions.
  1003  	// But if we disable empty precommit already, ignore it. Since
  1004  	// empty block is necessary to keep the liveness of the network.
  1005  	if len(pending) == 0 && atomic.LoadUint32(&w.noempty) == 0 {
  1006  		w.updateSnapshot()
  1007  		return
  1008  	}
  1009  	// Split the pending transactions into locals and remotes
  1010  	localTxs, remoteTxs := make(map[common.Address]types.Transactions), pending
  1011  	for _, account := range w.eth.TxPool().Locals() {
  1012  		if txs := remoteTxs[account]; len(txs) > 0 {
  1013  			delete(remoteTxs, account)
  1014  			localTxs[account] = txs
  1015  		}
  1016  	}
  1017  	if len(localTxs) > 0 {
  1018  		txs := types.NewTransactionsByPriceAndNonce(w.current.signer, localTxs, header.BaseFee)
  1019  		if w.commitTransactions(txs, w.coinbase, interrupt) {
  1020  			return
  1021  		}
  1022  	}
  1023  	if len(remoteTxs) > 0 {
  1024  		txs := types.NewTransactionsByPriceAndNonce(w.current.signer, remoteTxs, header.BaseFee)
  1025  		if w.commitTransactions(txs, w.coinbase, interrupt) {
  1026  			return
  1027  		}
  1028  	}
  1029  	w.commit(uncles, w.fullTaskHook, true, tstart)
  1030  }
  1031  
  1032  // commit runs any post-transaction state modifications, assembles the final block
  1033  // and commits new work if consensus engine is running.
  1034  func (w *worker) commit(uncles []*types.Header, interval func(), update bool, start time.Time) error {
  1035  	// Deep copy receipts here to avoid interaction between different tasks.
  1036  	receipts := copyReceipts(w.current.receipts)
  1037  	s := w.current.state.Copy()
  1038  	block, err := w.engine.FinalizeAndAssemble(w.chain, w.current.header, s, w.current.txs, uncles, receipts)
  1039  	if err != nil {
  1040  		return err
  1041  	}
  1042  	if w.isRunning() {
  1043  		if interval != nil {
  1044  			interval()
  1045  		}
  1046  		// If we're post merge, just ignore
  1047  		td, ttd := w.chain.GetTd(block.ParentHash(), block.NumberU64()-1), w.chain.Config().TerminalTotalDifficulty
  1048  		if td != nil && ttd != nil && td.Cmp(ttd) >= 0 {
  1049  			return nil
  1050  		}
  1051  		select {
  1052  		case w.taskCh <- &task{receipts: receipts, state: s, block: block, createdAt: time.Now()}:
  1053  			w.unconfirmed.Shift(block.NumberU64() - 1)
  1054  			log.Info("Commit new mining work", "number", block.Number(), "sealhash", w.engine.SealHash(block.Header()),
  1055  				"uncles", len(uncles), "txs", w.current.tcount,
  1056  				"gas", block.GasUsed(), "fees", totalFees(block, receipts),
  1057  				"elapsed", common.PrettyDuration(time.Since(start)))
  1058  
  1059  		case <-w.exitCh:
  1060  			log.Info("Worker has exited")
  1061  		}
  1062  	}
  1063  	if update {
  1064  		w.updateSnapshot()
  1065  	}
  1066  	return nil
  1067  }
  1068  
  1069  // copyReceipts makes a deep copy of the given receipts.
  1070  func copyReceipts(receipts []*types.Receipt) []*types.Receipt {
  1071  	result := make([]*types.Receipt, len(receipts))
  1072  	for i, l := range receipts {
  1073  		cpy := *l
  1074  		result[i] = &cpy
  1075  	}
  1076  	return result
  1077  }
  1078  
  1079  // postSideBlock fires a side chain event, only use it for testing.
  1080  func (w *worker) postSideBlock(event core.ChainSideEvent) {
  1081  	select {
  1082  	case w.chainSideCh <- event:
  1083  	case <-w.exitCh:
  1084  	}
  1085  }
  1086  
  1087  // totalFees computes total consumed miner fees in ETH. Block transactions and receipts have to have the same order.
  1088  func totalFees(block *types.Block, receipts []*types.Receipt) *big.Float {
  1089  	feesWei := new(big.Int)
  1090  	for i, tx := range block.Transactions() {
  1091  		minerFee, _ := tx.EffectiveGasTip(block.BaseFee())
  1092  		feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), minerFee))
  1093  	}
  1094  	return new(big.Float).Quo(new(big.Float).SetInt(feesWei), new(big.Float).SetInt(big.NewInt(params.Ether)))
  1095  }