diff options
author | Paweł Bylica <pawel.bylica@imapp.pl> | 2015-02-24 01:39:05 +0800 |
---|---|---|
committer | Paweł Bylica <pawel.bylica@imapp.pl> | 2015-02-24 01:39:05 +0800 |
commit | 114c3b4efe7f30ab7be0bec013210e7b4c3d08d7 (patch) | |
tree | 5230f6fee87dcbac36e1d71d6ab731b55eab8268 /miner | |
parent | b9894c1d0979b9f3e8428b1dc230f1ece106f676 (diff) | |
parent | dd086791acf477da7641c168f82de70ed0b2dca6 (diff) | |
download | go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.tar go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.tar.gz go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.tar.bz2 go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.tar.lz go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.tar.xz go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.tar.zst go-tangerine-114c3b4efe7f30ab7be0bec013210e7b4c3d08d7.zip |
Merge remote-tracking branch 'upstream/develop' into evmjit
Diffstat (limited to 'miner')
-rw-r--r-- | miner/agent.go | 76 | ||||
-rw-r--r-- | miner/miner.go | 248 | ||||
-rw-r--r-- | miner/worker.go | 137 |
3 files changed, 206 insertions, 255 deletions
diff --git a/miner/agent.go b/miner/agent.go new file mode 100644 index 000000000..9046f5d5a --- /dev/null +++ b/miner/agent.go @@ -0,0 +1,76 @@ +package miner + +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/pow" +) + +type CpuMiner struct { + c chan *types.Block + quit chan struct{} + quitCurrentOp chan struct{} + returnCh chan<- Work + + index int + pow pow.PoW +} + +func NewCpuMiner(index int, pow pow.PoW) *CpuMiner { + miner := &CpuMiner{ + pow: pow, + index: index, + } + + return miner +} + +func (self *CpuMiner) Work() chan<- *types.Block { return self.c } +func (self *CpuMiner) Pow() pow.PoW { return self.pow } +func (self *CpuMiner) SetWorkCh(ch chan<- Work) { self.returnCh = ch } + +func (self *CpuMiner) Stop() { + close(self.quit) + close(self.quitCurrentOp) +} + +func (self *CpuMiner) Start() { + self.quit = make(chan struct{}) + self.quitCurrentOp = make(chan struct{}, 1) + self.c = make(chan *types.Block, 1) + + go self.update() +} + +func (self *CpuMiner) update() { +out: + for { + select { + case block := <-self.c: + self.quitCurrentOp <- struct{}{} + + go self.mine(block) + case <-self.quit: + break out + } + } + +done: + // Empty channel + for { + select { + case <-self.c: + default: + close(self.c) + + break done + } + } +} + +func (self *CpuMiner) mine(block *types.Block) { + minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index) + nonce := self.pow.Search(block, self.quitCurrentOp) + if nonce != nil { + self.returnCh <- Work{block.Number().Uint64(), nonce} + } +} diff --git a/miner/miner.go b/miner/miner.go index 63c8e22a7..0cc2361c8 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -1,263 +1,61 @@ -/* - This file is part of go-ethereum - - go-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - go-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. -*/ -/** - * @authors - * Jeffrey Wilcke <i@jev.io> - * @date 2014 - * - */ - package miner import ( "math/big" - "sort" - - "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/pow" - "github.com/ethereum/go-ethereum/pow/ezp" - "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/pow/ezp" ) -type LocalTx struct { - To []byte `json:"to"` - Data []byte `json:"data"` - Gas string `json:"gas"` - GasPrice string `json:"gasPrice"` - Value string `json:"value"` -} - -func (self *LocalTx) Sign(key []byte) *types.Transaction { - return nil -} - var minerlogger = logger.NewLogger("MINER") type Miner struct { - eth *eth.Ethereum - events event.Subscription - - uncles []*types.Header - localTxs map[int]*LocalTx - localTxId int - - pow pow.PoW - quitCh chan struct{} - powQuitCh chan struct{} - - Coinbase []byte - - mining bool + worker *worker MinAcceptedGasPrice *big.Int Extra string -} - -func New(coinbase []byte, eth *eth.Ethereum) *Miner { - return &Miner{ - eth: eth, - powQuitCh: make(chan struct{}), - pow: ezp.New(), - mining: false, - localTxs: make(map[int]*LocalTx), - MinAcceptedGasPrice: big.NewInt(10000000000000), - Coinbase: coinbase, - } -} -func (self *Miner) GetPow() pow.PoW { - return self.pow + Coinbase []byte + mining bool } -func (self *Miner) AddLocalTx(tx *LocalTx) int { - minerlogger.Infof("Added local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value) +func New(coinbase []byte, eth core.Backend, minerThreads int) *Miner { + miner := &Miner{ + Coinbase: coinbase, + worker: newWorker(coinbase, eth), + } - self.localTxId++ - self.localTxs[self.localTxId] = tx - self.eth.EventMux().Post(tx) + for i := 0; i < minerThreads; i++ { + miner.worker.register(NewCpuMiner(i, ezp.New())) + } - return self.localTxId + return miner } -func (self *Miner) RemoveLocalTx(id int) { - if tx := self.localTxs[id]; tx != nil { - minerlogger.Infof("Removed local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value) - } - self.eth.EventMux().Post(&LocalTx{}) - - delete(self.localTxs, id) +func (self *Miner) Mining() bool { + return self.mining } func (self *Miner) Start() { - if self.mining { - return - } - - minerlogger.Infoln("Starting mining operations") self.mining = true - self.quitCh = make(chan struct{}) - self.powQuitCh = make(chan struct{}) - mux := self.eth.EventMux() - self.events = mux.Subscribe(core.NewBlockEvent{}, core.TxPreEvent{}, &LocalTx{}) + self.worker.start() - go self.update() - go self.mine() + self.worker.commitNewWork() } func (self *Miner) Stop() { - if !self.mining { - return - } - self.mining = false - minerlogger.Infoln("Stopping mining operations") - - self.events.Unsubscribe() - - close(self.quitCh) - close(self.powQuitCh) -} - -func (self *Miner) Mining() bool { - return self.mining -} - -func (self *Miner) update() { -out: - for { - select { - case event := <-self.events.Chan(): - switch event := event.(type) { - case core.NewBlockEvent: - block := event.Block - if self.eth.ChainManager().HasBlock(block.Hash()) { - self.reset() - self.eth.TxPool().RemoveSet(block.Transactions()) - go self.mine() - } else if true { - // do uncle stuff - } - case core.TxPreEvent, *LocalTx: - self.reset() - go self.mine() - } - case <-self.quitCh: - break out - } - } + self.worker.stop() } -func (self *Miner) reset() { - close(self.powQuitCh) - self.powQuitCh = make(chan struct{}) -} - -func (self *Miner) mine() { - var ( - blockProcessor = self.eth.BlockProcessor() - chainMan = self.eth.ChainManager() - block = chainMan.NewBlock(self.Coinbase) - state = state.New(block.Root(), self.eth.Db()) - ) - block.Header().Extra = self.Extra - - // Apply uncles - block.SetUncles(self.uncles) - - parent := chainMan.GetBlock(block.ParentHash()) - coinbase := state.GetOrNewStateObject(block.Coinbase()) - coinbase.SetGasPool(core.CalcGasLimit(parent, block)) - - transactions := self.finiliseTxs() - - // Accumulate all valid transactions and apply them to the new state - // Error may be ignored. It's not important during mining - receipts, txs, _, erroneous, err := blockProcessor.ApplyTransactions(coinbase, state, block, transactions, true) - if err != nil { - minerlogger.Debugln(err) +func (self *Miner) HashRate() int64 { + var tot int64 + for _, agent := range self.worker.agents { + tot += agent.Pow().GetHashrate() } - self.eth.TxPool().RemoveSet(erroneous) - - block.SetTransactions(txs) - block.SetReceipts(receipts) - - // Accumulate the rewards included for this block - blockProcessor.AccumulateRewards(state, block, parent) - - state.Update(ethutil.Big0) - block.SetRoot(state.Root()) - - minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions)) - - // Find a valid nonce - nonce := self.pow.Search(block, self.powQuitCh) - if nonce != nil { - block.Header().Nonce = nonce - err := chainMan.InsertChain(types.Blocks{block}) - if err != nil { - minerlogger.Infoln(err) - } else { - self.eth.EventMux().Post(core.NewMinedBlockEvent{block}) - - minerlogger.Infof("🔨 Mined block %x\n", block.Hash()) - minerlogger.Infoln(block) - } - - go self.mine() - } -} - -func (self *Miner) finiliseTxs() types.Transactions { - // Sort the transactions by nonce in case of odd network propagation - actualSize := len(self.localTxs) // See copy below - txs := make(types.Transactions, actualSize+self.eth.TxPool().Size()) - - state := self.eth.ChainManager().TransState() - // XXX This has to change. Coinbase is, for new, same as key. - key := self.eth.KeyManager() - for i, ltx := range self.localTxs { - tx := types.NewTransactionMessage(ltx.To, ethutil.Big(ltx.Value), ethutil.Big(ltx.Gas), ethutil.Big(ltx.GasPrice), ltx.Data) - tx.SetNonce(state.GetNonce(self.Coinbase)) - state.SetNonce(self.Coinbase, tx.Nonce()+1) - - tx.Sign(key.PrivateKey()) - - txs[i] = tx - } - - // Faster than append - for _, tx := range self.eth.TxPool().GetTransactions() { - if tx.GasPrice().Cmp(self.MinAcceptedGasPrice) >= 0 { - txs[actualSize] = tx - actualSize++ - } - } - - newTransactions := make(types.Transactions, actualSize) - copy(newTransactions, txs[:actualSize]) - sort.Sort(types.TxByNonce{newTransactions}) - return newTransactions + return tot } diff --git a/miner/worker.go b/miner/worker.go index ea8f2e8b5..1f3a52ab5 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -4,10 +4,10 @@ import ( "fmt" "math/big" "sort" + "sync" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/pow" @@ -24,7 +24,7 @@ type environment struct { uncles *set.Set } -func env(block *types.Block, eth *eth.Ethereum) *environment { +func env(block *types.Block, eth core.Backend) *environment { state := state.New(block.Root(), eth.Db()) env := &environment{ totalUsedGas: new(big.Int), @@ -41,60 +41,134 @@ func env(block *types.Block, eth *eth.Ethereum) *environment { return env } +type Work struct { + Number uint64 + Nonce []byte +} + type Agent interface { - Comms() chan<- *types.Block + Work() chan<- *types.Block + SetWorkCh(chan<- Work) + Stop() + Start() + Pow() pow.PoW } type worker struct { - agents []chan<- *types.Block + mu sync.Mutex + agents []Agent + recv chan Work mux *event.TypeMux quit chan struct{} pow pow.PoW - eth *eth.Ethereum + eth core.Backend chain *core.ChainManager proc *core.BlockProcessor coinbase []byte current *environment + + mining bool +} + +func newWorker(coinbase []byte, eth core.Backend) *worker { + return &worker{ + eth: eth, + mux: eth.EventMux(), + recv: make(chan Work), + chain: eth.ChainManager(), + proc: eth.BlockProcessor(), + coinbase: coinbase, + } +} + +func (self *worker) start() { + self.mining = true + + self.quit = make(chan struct{}) + + // spin up agents + for _, agent := range self.agents { + agent.Start() + } + + go self.update() + go self.wait() } -func (self *worker) register(agent chan<- *types.Block) { +func (self *worker) stop() { + self.mining = false + + close(self.quit) +} + +func (self *worker) register(agent Agent) { self.agents = append(self.agents, agent) + agent.SetWorkCh(self.recv) } func (self *worker) update() { - events := self.mux.Subscribe(core.NewBlockEvent{}, core.TxPreEvent{}, &LocalTx{}) + events := self.mux.Subscribe(core.ChainEvent{}, core.NewMinedBlockEvent{}) out: for { select { case event := <-events.Chan(): - switch event := event.(type) { - case core.NewBlockEvent: - if self.eth.ChainManager().HasBlock(event.Block.Hash()) { - } - case core.TxPreEvent: - if err := self.commitTransaction(event.Tx); err != nil { - self.commit() + switch ev := event.(type) { + case core.ChainEvent: + if self.current.block != ev.Block { + self.commitNewWork() } + case core.NewMinedBlockEvent: + self.commitNewWork() } case <-self.quit: + // stop all agents + for _, agent := range self.agents { + agent.Stop() + } break out } } + + events.Unsubscribe() } -func (self *worker) commit() { - self.current.state.Update(ethutil.Big0) - self.current.block.SetRoot(self.current.state.Root()) +func (self *worker) wait() { + for { + for work := range self.recv { + block := self.current.block + if block.Number().Uint64() == work.Number && block.Nonce() == nil { + self.current.block.Header().Nonce = work.Nonce + + if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { + self.mux.Post(core.NewMinedBlockEvent{self.current.block}) + } else { + self.commitNewWork() + } + } + break + } + } +} - for _, agent := range self.agents { - agent <- self.current.block +func (self *worker) push() { + if self.mining { + self.current.block.Header().GasUsed = self.current.totalUsedGas + self.current.block.SetRoot(self.current.state.Root()) + + // push new work to agents + for _, agent := range self.agents { + agent.Work() <- self.current.block + } } } func (self *worker) commitNewWork() { + self.mu.Lock() + defer self.mu.Unlock() + self.current = env(self.chain.NewBlock(self.coinbase), self.eth) parent := self.chain.GetBlock(self.current.block.ParentHash()) self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block)) @@ -102,25 +176,31 @@ func (self *worker) commitNewWork() { transactions := self.eth.TxPool().GetTransactions() sort.Sort(types.TxByNonce{transactions}) + minerlogger.Infof("committing new work with %d txs\n", len(transactions)) // Keep track of transactions which return errors so they can be removed var remove types.Transactions +gasLimit: for _, tx := range transactions { err := self.commitTransaction(tx) switch { case core.IsNonceErr(err): + // Remove invalid transactions remove = append(remove, tx) - case core.IsGasLimitErr(err): - // ignore - default: + case state.IsGasLimitErr(err): + // Break on gas limit + break gasLimit + } + + if err != nil { minerlogger.Infoln(err) - remove = append(remove, tx) } } self.eth.TxPool().RemoveSet(remove) self.current.coinbase.AddAmount(core.BlockReward) - self.commit() + self.current.state.Update(ethutil.Big0) + self.push() } var ( @@ -153,15 +233,12 @@ func (self *worker) commitUncle(uncle *types.Header) error { } func (self *worker) commitTransaction(tx *types.Transaction) error { - snapshot := self.current.state.Copy() - receipt, txGas, err := self.proc.ApplyTransaction(self.current.coinbase, self.current.state, self.current.block, tx, self.current.totalUsedGas, true) - if err != nil { - self.current.state.Set(snapshot) - + //fmt.Printf("proc %x %v\n", tx.Hash()[:3], tx.Nonce()) + receipt, _, err := self.proc.ApplyTransaction(self.current.coinbase, self.current.state, self.current.block, tx, self.current.totalUsedGas, true) + if err != nil && (core.IsNonceErr(err) || state.IsGasLimitErr(err)) { return err } - self.current.totalUsedGas.Add(self.current.totalUsedGas, txGas) self.current.block.AddTransaction(tx) self.current.block.AddReceipt(receipt) |