aboutsummaryrefslogtreecommitdiffstats
path: root/miner/worker.go
diff options
context:
space:
mode:
Diffstat (limited to 'miner/worker.go')
-rw-r--r--miner/worker.go163
1 files changed, 67 insertions, 96 deletions
diff --git a/miner/worker.go b/miner/worker.go
index 09cf6b6aa..1676036d8 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -17,6 +17,7 @@
package miner
import (
+ "bytes"
"fmt"
"math/big"
"sync"
@@ -33,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
+ "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow"
"gopkg.in/fatih/set.v0"
)
@@ -58,21 +60,19 @@ type uint64RingBuffer struct {
next int //where is the next insertion? assert 0 <= next < len(ints)
}
-// environment is the workers current environment and holds
+// Work is the workers current environment and holds
// all of the current state information
type Work struct {
- config *core.ChainConfig
- state *state.StateDB // apply state changes here
- ancestors *set.Set // ancestor set (used for checking uncle parent validity)
- family *set.Set // family set (used for checking uncle invalidity)
- uncles *set.Set // uncle set
- remove *set.Set // tx which will be removed
- tcount int // tx count in cycle
- ignoredTransactors *set.Set
- lowGasTransactors *set.Set
- ownedAccounts *set.Set
- lowGasTxs types.Transactions
- localMinedBlocks *uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion)
+ config *core.ChainConfig
+ state *state.StateDB // apply state changes here
+ ancestors *set.Set // ancestor set (used for checking uncle parent validity)
+ family *set.Set // family set (used for checking uncle invalidity)
+ uncles *set.Set // uncle set
+ tcount int // tx count in cycle
+ ownedAccounts *set.Set
+ lowGasTxs types.Transactions
+ failedTxs types.Transactions
+ localMinedBlocks *uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion)
Block *types.Block // the new block
@@ -103,7 +103,7 @@ type worker struct {
recv chan *Result
pow pow.PoW
- eth core.Backend
+ eth Backend
chain *core.BlockChain
proc core.Validator
chainDb ethdb.Database
@@ -128,11 +128,11 @@ type worker struct {
fullValidation bool
}
-func newWorker(config *core.ChainConfig, coinbase common.Address, eth core.Backend) *worker {
+func newWorker(config *core.ChainConfig, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker {
worker := &worker{
config: config,
eth: eth,
- mux: eth.EventMux(),
+ mux: mux,
chainDb: eth.ChainDb(),
recv: make(chan *Result, resultQueueSize),
gasPrice: new(big.Int),
@@ -234,7 +234,12 @@ func (self *worker) update() {
// Apply transaction to the pending state if we're not mining
if atomic.LoadInt32(&self.mining) == 0 {
self.currentMu.Lock()
- self.current.commitTransactions(self.mux, types.Transactions{ev.Tx}, self.gasPrice, self.chain)
+
+ acc, _ := ev.Tx.From()
+ txs := map[common.Address]types.Transactions{acc: types.Transactions{ev.Tx}}
+ txset := types.NewTransactionsByPriceAndNonce(txs)
+
+ self.current.commitTransactions(self.mux, txset, self.gasPrice, self.chain)
self.currentMu.Unlock()
}
}
@@ -381,10 +386,7 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error
accounts := self.eth.AccountManager().Accounts()
// Keep track of transactions which return errors so they can be removed
- work.remove = set.New()
work.tcount = 0
- work.ignoredTransactors = set.New()
- work.lowGasTransactors = set.New()
work.ownedAccounts = accountAddressesSet(accounts)
if self.current != nil {
work.localMinedBlocks = self.current.localMinedBlocks
@@ -468,7 +470,19 @@ func (self *worker) commitNewWork() {
Extra: self.extra,
Time: big.NewInt(tstamp),
}
-
+ // If we are care about TheDAO hard-fork check whether to override the extra-data or not
+ if daoBlock := self.config.DAOForkBlock; daoBlock != nil {
+ // Check whether the block is among the fork extra-override range
+ limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
+ if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
+ // Depending whether we support or oppose the fork, override differently
+ if self.config.DAOForkSupport {
+ header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
+ } else if bytes.Compare(header.Extra, params.DAOForkBlockExtra) == 0 {
+ header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
+ }
+ }
+ }
previous := self.current
// Could potentially happen if starting to mine in an odd state.
err := self.makeCurrent(parent, header)
@@ -476,46 +490,16 @@ func (self *worker) commitNewWork() {
glog.V(logger.Info).Infoln("Could not create new env for mining, retrying on next block.")
return
}
+ // Create the current work task and check any fork transitions needed
work := self.current
-
- /* //approach 1
- transactions := self.eth.TxPool().GetTransactions()
- sort.Sort(types.TxByNonce(transactions))
- */
-
- //approach 2
- transactions := self.eth.TxPool().GetTransactions()
- types.SortByPriceAndNonce(transactions)
-
- /* // approach 3
- // commit transactions for this run.
- txPerOwner := make(map[common.Address]types.Transactions)
- // Sort transactions by owner
- for _, tx := range self.eth.TxPool().GetTransactions() {
- from, _ := tx.From() // we can ignore the sender error
- txPerOwner[from] = append(txPerOwner[from], tx)
+ if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 {
+ core.ApplyDAOHardFork(work.state)
}
- var (
- singleTxOwner types.Transactions
- multiTxOwner types.Transactions
- )
- // Categorise transactions by
- // 1. 1 owner tx per block
- // 2. multi txs owner per block
- for _, txs := range txPerOwner {
- if len(txs) == 1 {
- singleTxOwner = append(singleTxOwner, txs[0])
- } else {
- multiTxOwner = append(multiTxOwner, txs...)
- }
- }
- sort.Sort(types.TxByPrice(singleTxOwner))
- sort.Sort(types.TxByNonce(multiTxOwner))
- transactions := append(singleTxOwner, multiTxOwner...)
- */
+ txs := types.NewTransactionsByPriceAndNonce(self.eth.TxPool().Pending())
+ work.commitTransactions(self.mux, txs, self.gasPrice, self.chain)
- work.commitTransactions(self.mux, transactions, self.gasPrice, self.chain)
- self.eth.TxPool().RemoveTransactions(work.lowGasTxs)
+ self.eth.TxPool().RemoveBatch(work.lowGasTxs)
+ self.eth.TxPool().RemoveBatch(work.failedTxs)
// compute uncles for the new block.
var (
@@ -573,65 +557,52 @@ func (self *worker) commitUncle(work *Work, uncle *types.Header) error {
return nil
}
-func (env *Work) commitTransactions(mux *event.TypeMux, transactions types.Transactions, gasPrice *big.Int, bc *core.BlockChain) {
+func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, gasPrice *big.Int, bc *core.BlockChain) {
gp := new(core.GasPool).AddGas(env.header.GasLimit)
var coalescedLogs vm.Logs
- for _, tx := range transactions {
+
+ for {
+ // Retrieve the next transaction and abort if all done
+ tx := txs.Peek()
+ if tx == nil {
+ break
+ }
// Error may be ignored here. The error has already been checked
// during transaction acceptance is the transaction pool.
from, _ := tx.From()
- // Check if it falls within margin. Txs from owned accounts are always processed.
+ // Ignore any transactions (and accounts subsequently) with low gas limits
if tx.GasPrice().Cmp(gasPrice) < 0 && !env.ownedAccounts.Has(from) {
- // ignore the transaction and transactor. We ignore the transactor
- // because nonce will fail after ignoring this transaction so there's
- // no point
- env.lowGasTransactors.Add(from)
+ // Pop the current low-priced transaction without shifting in the next from the account
+ glog.V(logger.Info).Infof("Transaction (%x) below gas price (tx=%v ask=%v). All sequential txs from this address(%x) will be ignored\n", tx.Hash().Bytes()[:4], common.CurrencyToString(tx.GasPrice()), common.CurrencyToString(gasPrice), from[:4])
- glog.V(logger.Info).Infof("transaction(%x) below gas price (tx=%v ask=%v). All sequential txs from this address(%x) will be ignored\n", tx.Hash().Bytes()[:4], common.CurrencyToString(tx.GasPrice()), common.CurrencyToString(gasPrice), from[:4])
- }
-
- // Continue with the next transaction if the transaction sender is included in
- // the low gas tx set. This will also remove the tx and all sequential transaction
- // from this transactor
- if env.lowGasTransactors.Has(from) {
- // add tx to the low gas set. This will be removed at the end of the run
- // owned accounts are ignored
- if !env.ownedAccounts.Has(from) {
- env.lowGasTxs = append(env.lowGasTxs, tx)
- }
- continue
- }
+ env.lowGasTxs = append(env.lowGasTxs, tx)
+ txs.Pop()
- // Move on to the next transaction when the transactor is in ignored transactions set
- // This may occur when a transaction hits the gas limit. When a gas limit is hit and
- // the transaction is processed (that could potentially be included in the block) it
- // will throw a nonce error because the previous transaction hasn't been processed.
- // Therefor we need to ignore any transaction after the ignored one.
- if env.ignoredTransactors.Has(from) {
continue
}
-
- env.state.StartRecord(tx.Hash(), common.Hash{}, 0)
+ // Start executing the transaction
+ env.state.StartRecord(tx.Hash(), common.Hash{}, env.tcount)
err, logs := env.commitTransaction(tx, bc, gp)
switch {
case core.IsGasLimitErr(err):
- // ignore the transactor so no nonce errors will be thrown for this account
- // next time the worker is run, they'll be picked up again.
- env.ignoredTransactors.Add(from)
-
+ // Pop the current out-of-gas transaction without shifting in the next from the account
glog.V(logger.Detail).Infof("Gas limit reached for (%x) in this block. Continue to try smaller txs\n", from[:4])
+ txs.Pop()
+
case err != nil:
- env.remove.Add(tx.Hash())
+ // Pop the current failed transaction without shifting in the next from the account
+ glog.V(logger.Detail).Infof("Transaction (%x) failed, will be removed: %v\n", tx.Hash().Bytes()[:4], err)
+ env.failedTxs = append(env.failedTxs, tx)
+ txs.Pop()
- if glog.V(logger.Detail) {
- glog.Infof("TX (%x) failed, will be removed: %v\n", tx.Hash().Bytes()[:4], err)
- }
default:
- env.tcount++
+ // Everything ok, collect the logs and shift in the next transaction from the same account
coalescedLogs = append(coalescedLogs, logs...)
+ env.tcount++
+ txs.Shift()
}
}
if len(coalescedLogs) > 0 || env.tcount > 0 {