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