github.com/samgwo/go-ethereum@v1.8.2-0.20180302101319-49bcb5fbd55e/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  	"fmt"
    22  	"math/big"
    23  	"sync"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/consensus"
    29  	"github.com/ethereum/go-ethereum/consensus/misc"
    30  	"github.com/ethereum/go-ethereum/core"
    31  	"github.com/ethereum/go-ethereum/core/state"
    32  	"github.com/ethereum/go-ethereum/core/types"
    33  	"github.com/ethereum/go-ethereum/core/vm"
    34  	"github.com/ethereum/go-ethereum/ethdb"
    35  	"github.com/ethereum/go-ethereum/event"
    36  	"github.com/ethereum/go-ethereum/log"
    37  	"github.com/ethereum/go-ethereum/params"
    38  	"gopkg.in/fatih/set.v0"
    39  )
    40  
    41  const (
    42  	resultQueueSize  = 10
    43  	miningLogAtDepth = 5
    44  
    45  	// txChanSize is the size of channel listening to TxPreEvent.
    46  	// The number is referenced from the size of tx pool.
    47  	txChanSize = 4096
    48  	// chainHeadChanSize is the size of channel listening to ChainHeadEvent.
    49  	chainHeadChanSize = 10
    50  	// chainSideChanSize is the size of channel listening to ChainSideEvent.
    51  	chainSideChanSize = 10
    52  )
    53  
    54  // Agent can register themself with the worker
    55  type Agent interface {
    56  	Work() chan<- *Work
    57  	SetReturnCh(chan<- *Result)
    58  	Stop()
    59  	Start()
    60  	GetHashRate() int64
    61  }
    62  
    63  // Work is the workers current environment and holds
    64  // all of the current state information
    65  type Work struct {
    66  	config *params.ChainConfig
    67  	signer types.Signer
    68  
    69  	state     *state.StateDB // apply state changes here
    70  	ancestors *set.Set       // ancestor set (used for checking uncle parent validity)
    71  	family    *set.Set       // family set (used for checking uncle invalidity)
    72  	uncles    *set.Set       // uncle set
    73  	tcount    int            // tx count in cycle
    74  
    75  	Block *types.Block // the new block
    76  
    77  	header   *types.Header
    78  	txs      []*types.Transaction
    79  	receipts []*types.Receipt
    80  
    81  	createdAt time.Time
    82  }
    83  
    84  type Result struct {
    85  	Work  *Work
    86  	Block *types.Block
    87  }
    88  
    89  // worker is the main object which takes care of applying messages to the new state
    90  type worker struct {
    91  	config *params.ChainConfig
    92  	engine consensus.Engine
    93  
    94  	mu sync.Mutex
    95  
    96  	// update loop
    97  	mux          *event.TypeMux
    98  	txCh         chan core.TxPreEvent
    99  	txSub        event.Subscription
   100  	chainHeadCh  chan core.ChainHeadEvent
   101  	chainHeadSub event.Subscription
   102  	chainSideCh  chan core.ChainSideEvent
   103  	chainSideSub event.Subscription
   104  	wg           sync.WaitGroup
   105  
   106  	agents map[Agent]struct{}
   107  	recv   chan *Result
   108  
   109  	eth     Backend
   110  	chain   *core.BlockChain
   111  	proc    core.Validator
   112  	chainDb ethdb.Database
   113  
   114  	coinbase common.Address
   115  	extra    []byte
   116  
   117  	currentMu sync.Mutex
   118  	current   *Work
   119  
   120  	uncleMu        sync.Mutex
   121  	possibleUncles map[common.Hash]*types.Block
   122  
   123  	unconfirmed *unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations
   124  
   125  	// atomic status counters
   126  	mining int32
   127  	atWork int32
   128  }
   129  
   130  func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker {
   131  	worker := &worker{
   132  		config:         config,
   133  		engine:         engine,
   134  		eth:            eth,
   135  		mux:            mux,
   136  		txCh:           make(chan core.TxPreEvent, txChanSize),
   137  		chainHeadCh:    make(chan core.ChainHeadEvent, chainHeadChanSize),
   138  		chainSideCh:    make(chan core.ChainSideEvent, chainSideChanSize),
   139  		chainDb:        eth.ChainDb(),
   140  		recv:           make(chan *Result, resultQueueSize),
   141  		chain:          eth.BlockChain(),
   142  		proc:           eth.BlockChain().Validator(),
   143  		possibleUncles: make(map[common.Hash]*types.Block),
   144  		coinbase:       coinbase,
   145  		agents:         make(map[Agent]struct{}),
   146  		unconfirmed:    newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth),
   147  	}
   148  	// Subscribe TxPreEvent for tx pool
   149  	worker.txSub = eth.TxPool().SubscribeTxPreEvent(worker.txCh)
   150  	// Subscribe events for blockchain
   151  	worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh)
   152  	worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh)
   153  	go worker.update()
   154  
   155  	go worker.wait()
   156  	worker.commitNewWork()
   157  
   158  	return worker
   159  }
   160  
   161  func (self *worker) setEtherbase(addr common.Address) {
   162  	self.mu.Lock()
   163  	defer self.mu.Unlock()
   164  	self.coinbase = addr
   165  }
   166  
   167  func (self *worker) setExtra(extra []byte) {
   168  	self.mu.Lock()
   169  	defer self.mu.Unlock()
   170  	self.extra = extra
   171  }
   172  
   173  func (self *worker) pending() (*types.Block, *state.StateDB) {
   174  	self.currentMu.Lock()
   175  	defer self.currentMu.Unlock()
   176  
   177  	if atomic.LoadInt32(&self.mining) == 0 {
   178  		return types.NewBlock(
   179  			self.current.header,
   180  			self.current.txs,
   181  			nil,
   182  			self.current.receipts,
   183  		), self.current.state.Copy()
   184  	}
   185  	return self.current.Block, self.current.state.Copy()
   186  }
   187  
   188  func (self *worker) pendingBlock() *types.Block {
   189  	self.currentMu.Lock()
   190  	defer self.currentMu.Unlock()
   191  
   192  	if atomic.LoadInt32(&self.mining) == 0 {
   193  		return types.NewBlock(
   194  			self.current.header,
   195  			self.current.txs,
   196  			nil,
   197  			self.current.receipts,
   198  		)
   199  	}
   200  	return self.current.Block
   201  }
   202  
   203  func (self *worker) start() {
   204  	self.mu.Lock()
   205  	defer self.mu.Unlock()
   206  
   207  	atomic.StoreInt32(&self.mining, 1)
   208  
   209  	// spin up agents
   210  	for agent := range self.agents {
   211  		agent.Start()
   212  	}
   213  }
   214  
   215  func (self *worker) stop() {
   216  	self.wg.Wait()
   217  
   218  	self.mu.Lock()
   219  	defer self.mu.Unlock()
   220  	if atomic.LoadInt32(&self.mining) == 1 {
   221  		for agent := range self.agents {
   222  			agent.Stop()
   223  		}
   224  	}
   225  	atomic.StoreInt32(&self.mining, 0)
   226  	atomic.StoreInt32(&self.atWork, 0)
   227  }
   228  
   229  func (self *worker) register(agent Agent) {
   230  	self.mu.Lock()
   231  	defer self.mu.Unlock()
   232  	self.agents[agent] = struct{}{}
   233  	agent.SetReturnCh(self.recv)
   234  }
   235  
   236  func (self *worker) unregister(agent Agent) {
   237  	self.mu.Lock()
   238  	defer self.mu.Unlock()
   239  	delete(self.agents, agent)
   240  	agent.Stop()
   241  }
   242  
   243  func (self *worker) update() {
   244  	defer self.txSub.Unsubscribe()
   245  	defer self.chainHeadSub.Unsubscribe()
   246  	defer self.chainSideSub.Unsubscribe()
   247  
   248  	for {
   249  		// A real event arrived, process interesting content
   250  		select {
   251  		// Handle ChainHeadEvent
   252  		case <-self.chainHeadCh:
   253  			self.commitNewWork()
   254  
   255  		// Handle ChainSideEvent
   256  		case ev := <-self.chainSideCh:
   257  			self.uncleMu.Lock()
   258  			self.possibleUncles[ev.Block.Hash()] = ev.Block
   259  			self.uncleMu.Unlock()
   260  
   261  		// Handle TxPreEvent
   262  		case ev := <-self.txCh:
   263  			// Apply transaction to the pending state if we're not mining
   264  			if atomic.LoadInt32(&self.mining) == 0 {
   265  				self.currentMu.Lock()
   266  				acc, _ := types.Sender(self.current.signer, ev.Tx)
   267  				txs := map[common.Address]types.Transactions{acc: {ev.Tx}}
   268  				txset := types.NewTransactionsByPriceAndNonce(self.current.signer, txs)
   269  
   270  				self.current.commitTransactions(self.mux, txset, self.chain, self.coinbase)
   271  				self.currentMu.Unlock()
   272  			} else {
   273  				// If we're mining, but nothing is being processed, wake on new transactions
   274  				if self.config.Clique != nil && self.config.Clique.Period == 0 {
   275  					self.commitNewWork()
   276  				}
   277  			}
   278  
   279  		// System stopped
   280  		case <-self.txSub.Err():
   281  			return
   282  		case <-self.chainHeadSub.Err():
   283  			return
   284  		case <-self.chainSideSub.Err():
   285  			return
   286  		}
   287  	}
   288  }
   289  
   290  func (self *worker) wait() {
   291  	for {
   292  		mustCommitNewWork := true
   293  		for result := range self.recv {
   294  			atomic.AddInt32(&self.atWork, -1)
   295  
   296  			if result == nil {
   297  				continue
   298  			}
   299  			block := result.Block
   300  			work := result.Work
   301  
   302  			// Update the block hash in all logs since it is now available and not when the
   303  			// receipt/log of individual transactions were created.
   304  			for _, r := range work.receipts {
   305  				for _, l := range r.Logs {
   306  					l.BlockHash = block.Hash()
   307  				}
   308  			}
   309  			for _, log := range work.state.Logs() {
   310  				log.BlockHash = block.Hash()
   311  			}
   312  			stat, err := self.chain.WriteBlockWithState(block, work.receipts, work.state)
   313  			if err != nil {
   314  				log.Error("Failed writing block to chain", "err", err)
   315  				continue
   316  			}
   317  			// check if canon block and write transactions
   318  			if stat == core.CanonStatTy {
   319  				// implicit by posting ChainHeadEvent
   320  				mustCommitNewWork = false
   321  			}
   322  			// Broadcast the block and announce chain insertion event
   323  			self.mux.Post(core.NewMinedBlockEvent{Block: block})
   324  			var (
   325  				events []interface{}
   326  				logs   = work.state.Logs()
   327  			)
   328  			events = append(events, core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
   329  			if stat == core.CanonStatTy {
   330  				events = append(events, core.ChainHeadEvent{Block: block})
   331  			}
   332  			self.chain.PostChainEvents(events, logs)
   333  
   334  			// Insert the block into the set of pending ones to wait for confirmations
   335  			self.unconfirmed.Insert(block.NumberU64(), block.Hash())
   336  
   337  			if mustCommitNewWork {
   338  				self.commitNewWork()
   339  			}
   340  		}
   341  	}
   342  }
   343  
   344  // push sends a new work task to currently live miner agents.
   345  func (self *worker) push(work *Work) {
   346  	if atomic.LoadInt32(&self.mining) != 1 {
   347  		return
   348  	}
   349  	for agent := range self.agents {
   350  		atomic.AddInt32(&self.atWork, 1)
   351  		if ch := agent.Work(); ch != nil {
   352  			ch <- work
   353  		}
   354  	}
   355  }
   356  
   357  // makeCurrent creates a new environment for the current cycle.
   358  func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error {
   359  	state, err := self.chain.StateAt(parent.Root())
   360  	if err != nil {
   361  		return err
   362  	}
   363  	work := &Work{
   364  		config:    self.config,
   365  		signer:    types.NewEIP155Signer(self.config.ChainId),
   366  		state:     state,
   367  		ancestors: set.New(),
   368  		family:    set.New(),
   369  		uncles:    set.New(),
   370  		header:    header,
   371  		createdAt: time.Now(),
   372  	}
   373  
   374  	// when 08 is processed ancestors contain 07 (quick block)
   375  	for _, ancestor := range self.chain.GetBlocksFromHash(parent.Hash(), 7) {
   376  		for _, uncle := range ancestor.Uncles() {
   377  			work.family.Add(uncle.Hash())
   378  		}
   379  		work.family.Add(ancestor.Hash())
   380  		work.ancestors.Add(ancestor.Hash())
   381  	}
   382  
   383  	// Keep track of transactions which return errors so they can be removed
   384  	work.tcount = 0
   385  	self.current = work
   386  	return nil
   387  }
   388  
   389  func (self *worker) commitNewWork() {
   390  	self.mu.Lock()
   391  	defer self.mu.Unlock()
   392  	self.uncleMu.Lock()
   393  	defer self.uncleMu.Unlock()
   394  	self.currentMu.Lock()
   395  	defer self.currentMu.Unlock()
   396  
   397  	tstart := time.Now()
   398  	parent := self.chain.CurrentBlock()
   399  
   400  	tstamp := tstart.Unix()
   401  	if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) >= 0 {
   402  		tstamp = parent.Time().Int64() + 1
   403  	}
   404  	// this will ensure we're not going off too far in the future
   405  	if now := time.Now().Unix(); tstamp > now+1 {
   406  		wait := time.Duration(tstamp-now) * time.Second
   407  		log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait))
   408  		time.Sleep(wait)
   409  	}
   410  
   411  	num := parent.Number()
   412  	header := &types.Header{
   413  		ParentHash: parent.Hash(),
   414  		Number:     num.Add(num, common.Big1),
   415  		GasLimit:   core.CalcGasLimit(parent),
   416  		Extra:      self.extra,
   417  		Time:       big.NewInt(tstamp),
   418  	}
   419  	// Only set the coinbase if we are mining (avoid spurious block rewards)
   420  	if atomic.LoadInt32(&self.mining) == 1 {
   421  		header.Coinbase = self.coinbase
   422  	}
   423  	if err := self.engine.Prepare(self.chain, header); err != nil {
   424  		log.Error("Failed to prepare header for mining", "err", err)
   425  		return
   426  	}
   427  	// If we are care about TheDAO hard-fork check whether to override the extra-data or not
   428  	if daoBlock := self.config.DAOForkBlock; daoBlock != nil {
   429  		// Check whether the block is among the fork extra-override range
   430  		limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
   431  		if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
   432  			// Depending whether we support or oppose the fork, override differently
   433  			if self.config.DAOForkSupport {
   434  				header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
   435  			} else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
   436  				header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
   437  			}
   438  		}
   439  	}
   440  	// Could potentially happen if starting to mine in an odd state.
   441  	err := self.makeCurrent(parent, header)
   442  	if err != nil {
   443  		log.Error("Failed to create mining context", "err", err)
   444  		return
   445  	}
   446  	// Create the current work task and check any fork transitions needed
   447  	work := self.current
   448  	if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 {
   449  		misc.ApplyDAOHardFork(work.state)
   450  	}
   451  	pending, err := self.eth.TxPool().Pending()
   452  	if err != nil {
   453  		log.Error("Failed to fetch pending transactions", "err", err)
   454  		return
   455  	}
   456  	txs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending)
   457  	work.commitTransactions(self.mux, txs, self.chain, self.coinbase)
   458  
   459  	// compute uncles for the new block.
   460  	var (
   461  		uncles    []*types.Header
   462  		badUncles []common.Hash
   463  	)
   464  	for hash, uncle := range self.possibleUncles {
   465  		if len(uncles) == 2 {
   466  			break
   467  		}
   468  		if err := self.commitUncle(work, uncle.Header()); err != nil {
   469  			log.Trace("Bad uncle found and will be removed", "hash", hash)
   470  			log.Trace(fmt.Sprint(uncle))
   471  
   472  			badUncles = append(badUncles, hash)
   473  		} else {
   474  			log.Debug("Committing new uncle to block", "hash", hash)
   475  			uncles = append(uncles, uncle.Header())
   476  		}
   477  	}
   478  	for _, hash := range badUncles {
   479  		delete(self.possibleUncles, hash)
   480  	}
   481  	// Create the new block to seal with the consensus engine
   482  	if work.Block, err = self.engine.Finalize(self.chain, header, work.state, work.txs, uncles, work.receipts); err != nil {
   483  		log.Error("Failed to finalize block for sealing", "err", err)
   484  		return
   485  	}
   486  	// We only care about logging if we're actually mining.
   487  	if atomic.LoadInt32(&self.mining) == 1 {
   488  		log.Info("Commit new mining work", "number", work.Block.Number(), "txs", work.tcount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart)))
   489  		self.unconfirmed.Shift(work.Block.NumberU64() - 1)
   490  	}
   491  	self.push(work)
   492  }
   493  
   494  func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
   495  	hash := uncle.Hash()
   496  	if work.uncles.Has(hash) {
   497  		return fmt.Errorf("uncle not unique")
   498  	}
   499  	if !work.ancestors.Has(uncle.ParentHash) {
   500  		return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4])
   501  	}
   502  	if work.family.Has(hash) {
   503  		return fmt.Errorf("uncle already in family (%x)", hash)
   504  	}
   505  	work.uncles.Add(uncle.Hash())
   506  	return nil
   507  }
   508  
   509  func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, bc *core.BlockChain, coinbase common.Address) {
   510  	gp := new(core.GasPool).AddGas(env.header.GasLimit)
   511  
   512  	var coalescedLogs []*types.Log
   513  
   514  	for {
   515  		// If we don't have enough gas for any further transactions then we're done
   516  		if gp.Gas() < params.TxGas {
   517  			log.Trace("Not enough gas for further transactions", "gp", gp)
   518  			break
   519  		}
   520  		// Retrieve the next transaction and abort if all done
   521  		tx := txs.Peek()
   522  		if tx == nil {
   523  			break
   524  		}
   525  		// Error may be ignored here. The error has already been checked
   526  		// during transaction acceptance is the transaction pool.
   527  		//
   528  		// We use the eip155 signer regardless of the current hf.
   529  		from, _ := types.Sender(env.signer, tx)
   530  		// Check whether the tx is replay protected. If we're not in the EIP155 hf
   531  		// phase, start ignoring the sender until we do.
   532  		if tx.Protected() && !env.config.IsEIP155(env.header.Number) {
   533  			log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", env.config.EIP155Block)
   534  
   535  			txs.Pop()
   536  			continue
   537  		}
   538  		// Start executing the transaction
   539  		env.state.Prepare(tx.Hash(), common.Hash{}, env.tcount)
   540  
   541  		err, logs := env.commitTransaction(tx, bc, coinbase, gp)
   542  		switch err {
   543  		case core.ErrGasLimitReached:
   544  			// Pop the current out-of-gas transaction without shifting in the next from the account
   545  			log.Trace("Gas limit exceeded for current block", "sender", from)
   546  			txs.Pop()
   547  
   548  		case core.ErrNonceTooLow:
   549  			// New head notification data race between the transaction pool and miner, shift
   550  			log.Trace("Skipping transaction with low nonce", "sender", from, "nonce", tx.Nonce())
   551  			txs.Shift()
   552  
   553  		case core.ErrNonceTooHigh:
   554  			// Reorg notification data race between the transaction pool and miner, skip account =
   555  			log.Trace("Skipping account with hight nonce", "sender", from, "nonce", tx.Nonce())
   556  			txs.Pop()
   557  
   558  		case nil:
   559  			// Everything ok, collect the logs and shift in the next transaction from the same account
   560  			coalescedLogs = append(coalescedLogs, logs...)
   561  			env.tcount++
   562  			txs.Shift()
   563  
   564  		default:
   565  			// Strange error, discard the transaction and get the next in line (note, the
   566  			// nonce-too-high clause will prevent us from executing in vain).
   567  			log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err)
   568  			txs.Shift()
   569  		}
   570  	}
   571  
   572  	if len(coalescedLogs) > 0 || env.tcount > 0 {
   573  		// make a copy, the state caches the logs and these logs get "upgraded" from pending to mined
   574  		// logs by filling in the block hash when the block was mined by the local miner. This can
   575  		// cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed.
   576  		cpy := make([]*types.Log, len(coalescedLogs))
   577  		for i, l := range coalescedLogs {
   578  			cpy[i] = new(types.Log)
   579  			*cpy[i] = *l
   580  		}
   581  		go func(logs []*types.Log, tcount int) {
   582  			if len(logs) > 0 {
   583  				mux.Post(core.PendingLogsEvent{Logs: logs})
   584  			}
   585  			if tcount > 0 {
   586  				mux.Post(core.PendingStateEvent{})
   587  			}
   588  		}(cpy, env.tcount)
   589  	}
   590  }
   591  
   592  func (env *Work) commitTransaction(tx *types.Transaction, bc *core.BlockChain, coinbase common.Address, gp *core.GasPool) (error, []*types.Log) {
   593  	snap := env.state.Snapshot()
   594  
   595  	receipt, _, err := core.ApplyTransaction(env.config, bc, &coinbase, gp, env.state, env.header, tx, &env.header.GasUsed, vm.Config{})
   596  	if err != nil {
   597  		env.state.RevertToSnapshot(snap)
   598  		return err, nil
   599  	}
   600  	env.txs = append(env.txs, tx)
   601  	env.receipts = append(env.receipts, receipt)
   602  
   603  	return nil, receipt.Logs
   604  }