aboutsummaryrefslogtreecommitdiffstats
path: root/miner/miner.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-02-21 01:13:46 +0800
committerobscuren <geffobscura@gmail.com>2015-02-21 01:13:46 +0800
commitbd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca (patch)
tree46ab5943fd5e26198067aeec4a44287452eb2a32 /miner/miner.go
parent771bfe9e78f9952002a71cccc8d41c8c544fdfcb (diff)
parentd586a633ff005ac01c9f1eb33552d147cf6c883e (diff)
downloadgo-tangerine-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.gz
go-tangerine-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.zst
go-tangerine-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.zip
Merge branch 'release/0.9.0'
Diffstat (limited to 'miner/miner.go')
-rw-r--r--miner/miner.go246
1 files changed, 23 insertions, 223 deletions
diff --git a/miner/miner.go b/miner/miner.go
index f63096b63..0cc2361c8 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -1,261 +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"
- "github.com/ethereum/go-ethereum/ethutil"
- "github.com/ethereum/go-ethereum/pow"
- "github.com/ethereum/go-ethereum/pow/ezp"
"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/wire"
+ "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.Blocks
- localTxs map[int]*LocalTx
- localTxId int
+ worker *worker
- pow pow.PoW
- quitCh chan struct{}
- powQuitCh chan struct{}
+ MinAcceptedGasPrice *big.Int
+ Extra string
Coinbase []byte
-
- mining bool
-
- MinAcceptedGasPrice *big.Int
+ mining bool
}
-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 New(coinbase []byte, eth core.Backend, minerThreads int) *Miner {
+ miner := &Miner{
+ Coinbase: coinbase,
+ worker: newWorker(coinbase, eth),
}
-}
-
-func (self *Miner) GetPow() pow.PoW {
- return self.pow
-}
-
-func (self *Miner) AddLocalTx(tx *LocalTx) int {
- minerlogger.Infof("Added local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value)
- 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)
+ self.worker.stop()
}
-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
- }
- }
-}
-
-func (self *Miner) reset() {
- close(self.powQuitCh)
- self.powQuitCh = make(chan struct{})
-}
-
-func (self *Miner) mine() {
- var (
- blockManager = self.eth.BlockManager()
- chainMan = self.eth.ChainManager()
- block = chainMan.NewBlock(self.Coinbase)
- )
-
- // Apply uncles
- if len(self.uncles) > 0 {
- block.SetUncles(self.uncles)
- }
-
- parent := chainMan.GetBlock(block.PrevHash)
- coinbase := block.State().GetOrNewStateObject(block.Coinbase)
- coinbase.SetGasPool(block.CalcGasLimit(parent))
-
- 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 := blockManager.ApplyTransactions(coinbase, block.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
- blockManager.AccumelateRewards(block.State(), block, parent)
-
- block.State().Update(ethutil.Big0)
-
- 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.Nonce = nonce
- err := chainMan.InsertChain(types.Blocks{block})
- if err != nil {
- minerlogger.Infoln(err)
- } else {
- self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val})
-
- 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().CurrentTransactions() {
- 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
}