Merge branch 'miner' into develop
This commit is contained in:
		@@ -117,7 +117,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	refresh();
 | 
						refresh();
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<!--
 | 
					<!--
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -289,6 +289,7 @@ ApplicationWindow {
 | 
				
			|||||||
            styleColor: "#797979"
 | 
					            styleColor: "#797979"
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
        Label {
 | 
					        Label {
 | 
				
			||||||
            //y: 6
 | 
					            //y: 6
 | 
				
			||||||
            objectName: "miningLabel"
 | 
					            objectName: "miningLabel"
 | 
				
			||||||
@@ -307,6 +308,7 @@ ApplicationWindow {
 | 
				
			|||||||
            anchors.right: peerGroup.left
 | 
					            anchors.right: peerGroup.left
 | 
				
			||||||
            anchors.rightMargin: 5
 | 
					            anchors.rightMargin: 5
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
						*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ProgressBar {
 | 
					        ProgressBar {
 | 
				
			||||||
            visible: false
 | 
					            visible: false
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,30 @@ Rectangle {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	color: "#00000000"
 | 
						color: "#00000000"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Label {
 | 
				
			||||||
 | 
						    visible: false
 | 
				
			||||||
 | 
					            id: lastBlockLabel
 | 
				
			||||||
 | 
					            objectName: "lastBlockLabel"
 | 
				
			||||||
 | 
					            text: "---"
 | 
				
			||||||
 | 
					            font.pixelSize: 10
 | 
				
			||||||
 | 
					            anchors.right: peerGroup.left
 | 
				
			||||||
 | 
					            anchors.rightMargin: 5
 | 
				
			||||||
 | 
						    onTextChanged: {
 | 
				
			||||||
 | 
							//menuItem.secondaryTitle = text
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Label {
 | 
				
			||||||
 | 
					            objectName: "miningLabel"
 | 
				
			||||||
 | 
					            visible: false
 | 
				
			||||||
 | 
					            font.pixelSize: 10
 | 
				
			||||||
 | 
					            anchors.right: lastBlockLabel.left
 | 
				
			||||||
 | 
					            anchors.rightMargin: 5
 | 
				
			||||||
 | 
						    onTextChanged: {
 | 
				
			||||||
 | 
							menuItem.secondaryTitle = text
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ColumnLayout {
 | 
						ColumnLayout {
 | 
				
			||||||
		spacing: 10
 | 
							spacing: 10
 | 
				
			||||||
		anchors.fill: parent
 | 
							anchors.fill: parent
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -452,7 +452,7 @@ func (gui *Gui) update() {
 | 
				
			|||||||
		case <-generalUpdateTicker.C:
 | 
							case <-generalUpdateTicker.C:
 | 
				
			||||||
			statusText := "#" + gui.eth.ChainManager().CurrentBlock().Number().String()
 | 
								statusText := "#" + gui.eth.ChainManager().CurrentBlock().Number().String()
 | 
				
			||||||
			lastBlockLabel.Set("text", statusText)
 | 
								lastBlockLabel.Set("text", statusText)
 | 
				
			||||||
			miningLabel.Set("text", "Mining @ "+strconv.FormatInt(gui.uiLib.miner.GetPow().GetHashrate(), 10)+"Khash")
 | 
								miningLabel.Set("text", "Mining @ "+strconv.FormatInt(gui.uiLib.miner.HashRate(), 10)+"/Khash")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			/*
 | 
								/*
 | 
				
			||||||
				blockLength := gui.eth.BlockPool().BlocksProcessed
 | 
									blockLength := gui.eth.BlockPool().BlocksProcessed
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -209,6 +209,8 @@ func (self *UiLib) Call(params map[string]interface{}) (string, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
 | 
					func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
 | 
				
			||||||
 | 
						return 0
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
		return self.miner.AddLocalTx(&miner.LocalTx{
 | 
							return self.miner.AddLocalTx(&miner.LocalTx{
 | 
				
			||||||
			To:       ethutil.Hex2Bytes(to),
 | 
								To:       ethutil.Hex2Bytes(to),
 | 
				
			||||||
			Data:     ethutil.Hex2Bytes(data),
 | 
								Data:     ethutil.Hex2Bytes(data),
 | 
				
			||||||
@@ -216,10 +218,11 @@ func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) in
 | 
				
			|||||||
			GasPrice: gasPrice,
 | 
								GasPrice: gasPrice,
 | 
				
			||||||
			Value:    value,
 | 
								Value:    value,
 | 
				
			||||||
		}) - 1
 | 
							}) - 1
 | 
				
			||||||
 | 
						*/
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *UiLib) RemoveLocalTransaction(id int) {
 | 
					func (self *UiLib) RemoveLocalTransaction(id int) {
 | 
				
			||||||
	self.miner.RemoveLocalTx(id)
 | 
						//self.miner.RemoveLocalTx(id)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *UiLib) SetGasPrice(price string) {
 | 
					func (self *UiLib) SetGasPrice(price string) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,6 +50,7 @@ func NewBlockProcessor(db ethutil.Database, txpool *TxPool, chainManager *ChainM
 | 
				
			|||||||
	sm := &BlockProcessor{
 | 
						sm := &BlockProcessor{
 | 
				
			||||||
		db:  db,
 | 
							db:  db,
 | 
				
			||||||
		mem: make(map[string]*big.Int),
 | 
							mem: make(map[string]*big.Int),
 | 
				
			||||||
 | 
							//Pow:      ðash.Ethash{},
 | 
				
			||||||
		Pow:      ezp.New(),
 | 
							Pow:      ezp.New(),
 | 
				
			||||||
		bc:       chainManager,
 | 
							bc:       chainManager,
 | 
				
			||||||
		eventMux: eventMux,
 | 
							eventMux: eventMux,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,11 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var chainlogger = logger.NewLogger("CHAIN")
 | 
					var chainlogger = logger.NewLogger("CHAIN")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ChainEvent struct {
 | 
				
			||||||
 | 
						Block *types.Block
 | 
				
			||||||
 | 
						Td    *big.Int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type StateQuery interface {
 | 
					type StateQuery interface {
 | 
				
			||||||
	GetAccount(addr []byte) *state.StateObject
 | 
						GetAccount(addr []byte) *state.StateObject
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -175,6 +180,9 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block {
 | 
				
			|||||||
		ethutil.BigPow(2, 32),
 | 
							ethutil.BigPow(2, 32),
 | 
				
			||||||
		nil,
 | 
							nil,
 | 
				
			||||||
		"")
 | 
							"")
 | 
				
			||||||
 | 
						block.SetUncles(nil)
 | 
				
			||||||
 | 
						block.SetTransactions(nil)
 | 
				
			||||||
 | 
						block.SetReceipts(nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	parent := bc.currentBlock
 | 
						parent := bc.currentBlock
 | 
				
			||||||
	if parent != nil {
 | 
						if parent != nil {
 | 
				
			||||||
@@ -385,8 +393,9 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
 | 
				
			|||||||
				self.setTotalDifficulty(td)
 | 
									self.setTotalDifficulty(td)
 | 
				
			||||||
				self.insert(block)
 | 
									self.insert(block)
 | 
				
			||||||
				self.transState = state.New(cblock.Root(), self.db) //state.New(cblock.Trie().Copy())
 | 
									self.transState = state.New(cblock.Root(), self.db) //state.New(cblock.Trie().Copy())
 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									self.eventMux.Post(ChainEvent{block, td})
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		self.mu.Unlock()
 | 
							self.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										73
									
								
								miner/agent.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								miner/agent.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,73 @@
 | 
				
			|||||||
 | 
					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{
 | 
				
			||||||
 | 
							c:             make(chan *types.Block, 1),
 | 
				
			||||||
 | 
							quit:          make(chan struct{}),
 | 
				
			||||||
 | 
							quitCurrentOp: make(chan struct{}, 1),
 | 
				
			||||||
 | 
							pow:           pow,
 | 
				
			||||||
 | 
							index:         index,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						go miner.update()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return miner
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (self *CpuMiner) Work() chan<- *types.Block { return self.c }
 | 
				
			||||||
 | 
					func (self *CpuMiner) Pow() pow.PoW              { return self.pow }
 | 
				
			||||||
 | 
					func (self *CpuMiner) SetNonceCh(ch chan<- Work) { self.returnCh = ch }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (self *CpuMiner) Stop() {
 | 
				
			||||||
 | 
						close(self.quit)
 | 
				
			||||||
 | 
						close(self.quitCurrentOp)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (self *CpuMiner) update() {
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case block := <-self.c:
 | 
				
			||||||
 | 
								// make sure it's open
 | 
				
			||||||
 | 
								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}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										254
									
								
								miner/miner.go
									
									
									
									
									
								
							
							
						
						
									
										254
									
								
								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
 | 
					package miner
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"math/big"
 | 
						"math/big"
 | 
				
			||||||
	"sort"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/ethereum/go-ethereum/eth"
 | 
						"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/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")
 | 
					var minerlogger = logger.NewLogger("MINER")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Miner struct {
 | 
					type Miner struct {
 | 
				
			||||||
	eth    *eth.Ethereum
 | 
						worker *worker
 | 
				
			||||||
	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
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	MinAcceptedGasPrice *big.Int
 | 
						MinAcceptedGasPrice *big.Int
 | 
				
			||||||
	Extra               string
 | 
						Extra               string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						coinbase []byte
 | 
				
			||||||
 | 
						mining   bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func New(coinbase []byte, eth *eth.Ethereum) *Miner {
 | 
					func New(coinbase []byte, eth *eth.Ethereum) *Miner {
 | 
				
			||||||
	return &Miner{
 | 
						miner := &Miner{
 | 
				
			||||||
		eth:                 eth,
 | 
							coinbase: coinbase,
 | 
				
			||||||
		powQuitCh:           make(chan struct{}),
 | 
							worker:   newWorker(coinbase, eth),
 | 
				
			||||||
		pow:                 ezp.New(),
 | 
					 | 
				
			||||||
		mining:              false,
 | 
					 | 
				
			||||||
		localTxs:            make(map[int]*LocalTx),
 | 
					 | 
				
			||||||
		MinAcceptedGasPrice: big.NewInt(10000000000000),
 | 
					 | 
				
			||||||
		Coinbase:            coinbase,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *Miner) GetPow() pow.PoW {
 | 
						for i := 0; i < 4; i++ {
 | 
				
			||||||
	return self.pow
 | 
							miner.worker.register(NewCpuMiner(i, ezp.New()))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *Miner) AddLocalTx(tx *LocalTx) int {
 | 
						return miner
 | 
				
			||||||
	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)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return self.localTxId
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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) 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{})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	go self.update()
 | 
					 | 
				
			||||||
	go self.mine()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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 {
 | 
					func (self *Miner) Mining() bool {
 | 
				
			||||||
	return self.mining
 | 
						return self.mining
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *Miner) update() {
 | 
					func (self *Miner) Start() {
 | 
				
			||||||
out:
 | 
						self.mining = true
 | 
				
			||||||
	for {
 | 
					
 | 
				
			||||||
		select {
 | 
						self.worker.start()
 | 
				
			||||||
		case event := <-self.events.Chan():
 | 
					
 | 
				
			||||||
			switch event := event.(type) {
 | 
						self.worker.commitNewWork()
 | 
				
			||||||
			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() {
 | 
					func (self *Miner) Stop() {
 | 
				
			||||||
	close(self.powQuitCh)
 | 
						self.mining = false
 | 
				
			||||||
	self.powQuitCh = make(chan struct{})
 | 
					
 | 
				
			||||||
 | 
						self.worker.stop()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *Miner) mine() {
 | 
					func (self *Miner) HashRate() int64 {
 | 
				
			||||||
	var (
 | 
						var tot int64
 | 
				
			||||||
		blockProcessor = self.eth.BlockProcessor()
 | 
						for _, agent := range self.worker.agents {
 | 
				
			||||||
		chainMan       = self.eth.ChainManager()
 | 
							tot += agent.Pow().GetHashrate()
 | 
				
			||||||
		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)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	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()
 | 
						return tot
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,12 +41,21 @@ func env(block *types.Block, eth *eth.Ethereum) *environment {
 | 
				
			|||||||
	return env
 | 
						return env
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Work struct {
 | 
				
			||||||
 | 
						Number uint64
 | 
				
			||||||
 | 
						Nonce  []byte
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Agent interface {
 | 
					type Agent interface {
 | 
				
			||||||
	Comms() chan<- *types.Block
 | 
						Work() chan<- *types.Block
 | 
				
			||||||
 | 
						SetNonceCh(chan<- Work)
 | 
				
			||||||
 | 
						Stop()
 | 
				
			||||||
 | 
						Pow() pow.PoW
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type worker struct {
 | 
					type worker struct {
 | 
				
			||||||
	agents []chan<- *types.Block
 | 
						agents []Agent
 | 
				
			||||||
 | 
						recv   chan Work
 | 
				
			||||||
	mux    *event.TypeMux
 | 
						mux    *event.TypeMux
 | 
				
			||||||
	quit   chan struct{}
 | 
						quit   chan struct{}
 | 
				
			||||||
	pow    pow.PoW
 | 
						pow    pow.PoW
 | 
				
			||||||
@@ -57,40 +66,85 @@ type worker struct {
 | 
				
			|||||||
	coinbase []byte
 | 
						coinbase []byte
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	current *environment
 | 
						current *environment
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mining bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *worker) register(agent chan<- *types.Block) {
 | 
					func newWorker(coinbase []byte, eth *eth.Ethereum) *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{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						go self.update()
 | 
				
			||||||
 | 
						go self.wait()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (self *worker) stop() {
 | 
				
			||||||
 | 
						self.mining = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						close(self.quit)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (self *worker) register(agent Agent) {
 | 
				
			||||||
	self.agents = append(self.agents, agent)
 | 
						self.agents = append(self.agents, agent)
 | 
				
			||||||
 | 
						agent.SetNonceCh(self.recv)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *worker) update() {
 | 
					func (self *worker) update() {
 | 
				
			||||||
	events := self.mux.Subscribe(core.NewBlockEvent{}, core.TxPreEvent{}, &LocalTx{})
 | 
						events := self.mux.Subscribe(core.ChainEvent{}, core.TxPreEvent{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
out:
 | 
					out:
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		select {
 | 
							select {
 | 
				
			||||||
		case event := <-events.Chan():
 | 
							case event := <-events.Chan():
 | 
				
			||||||
			switch event := event.(type) {
 | 
								switch event := event.(type) {
 | 
				
			||||||
			case core.NewBlockEvent:
 | 
								case core.ChainEvent:
 | 
				
			||||||
				if self.eth.ChainManager().HasBlock(event.Block.Hash()) {
 | 
									self.commitNewWork()
 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			case core.TxPreEvent:
 | 
								case core.TxPreEvent:
 | 
				
			||||||
				if err := self.commitTransaction(event.Tx); err != nil {
 | 
									if err := self.commitTransaction(event.Tx); err != nil {
 | 
				
			||||||
					self.commit()
 | 
										self.push()
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		case <-self.quit:
 | 
							case <-self.quit:
 | 
				
			||||||
			break out
 | 
								break out
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						events.Unsubscribe()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (self *worker) commit() {
 | 
					func (self *worker) wait() {
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							for work := range self.recv {
 | 
				
			||||||
 | 
								if self.current.block.Number().Uint64() == work.Number {
 | 
				
			||||||
 | 
									self.current.block.Header().Nonce = work.Nonce
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									self.chain.InsertChain(types.Blocks{self.current.block})
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								break
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (self *worker) push() {
 | 
				
			||||||
 | 
						if self.mining {
 | 
				
			||||||
		self.current.state.Update(ethutil.Big0)
 | 
							self.current.state.Update(ethutil.Big0)
 | 
				
			||||||
		self.current.block.SetRoot(self.current.state.Root())
 | 
							self.current.block.SetRoot(self.current.state.Root())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for _, agent := range self.agents {
 | 
							for _, agent := range self.agents {
 | 
				
			||||||
		agent <- self.current.block
 | 
								agent.Work() <- self.current.block
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -110,7 +164,8 @@ func (self *worker) commitNewWork() {
 | 
				
			|||||||
		case core.IsNonceErr(err):
 | 
							case core.IsNonceErr(err):
 | 
				
			||||||
			remove = append(remove, tx)
 | 
								remove = append(remove, tx)
 | 
				
			||||||
		case core.IsGasLimitErr(err):
 | 
							case core.IsGasLimitErr(err):
 | 
				
			||||||
			// ignore
 | 
								// Break on gas limit
 | 
				
			||||||
 | 
								break
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			minerlogger.Infoln(err)
 | 
								minerlogger.Infoln(err)
 | 
				
			||||||
			remove = append(remove, tx)
 | 
								remove = append(remove, tx)
 | 
				
			||||||
@@ -120,7 +175,7 @@ func (self *worker) commitNewWork() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	self.current.coinbase.AddAmount(core.BlockReward)
 | 
						self.current.coinbase.AddAmount(core.BlockReward)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	self.commit()
 | 
						self.push()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,4 +6,5 @@ type Block interface {
 | 
				
			|||||||
	Difficulty() *big.Int
 | 
						Difficulty() *big.Int
 | 
				
			||||||
	HashNoNonce() []byte
 | 
						HashNoNonce() []byte
 | 
				
			||||||
	N() []byte
 | 
						N() []byte
 | 
				
			||||||
 | 
						Number() *big.Int
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,7 +21,7 @@ type EasyPow struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func New() *EasyPow {
 | 
					func New() *EasyPow {
 | 
				
			||||||
	return &EasyPow{turbo: false}
 | 
						return &EasyPow{turbo: true}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (pow *EasyPow) GetHashrate() int64 {
 | 
					func (pow *EasyPow) GetHashrate() int64 {
 | 
				
			||||||
@@ -36,27 +36,34 @@ func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) []byte {
 | 
				
			|||||||
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
 | 
						r := rand.New(rand.NewSource(time.Now().UnixNano()))
 | 
				
			||||||
	hash := block.HashNoNonce()
 | 
						hash := block.HashNoNonce()
 | 
				
			||||||
	diff := block.Difficulty()
 | 
						diff := block.Difficulty()
 | 
				
			||||||
	i := int64(0)
 | 
						//i := int64(0)
 | 
				
			||||||
 | 
						// TODO fix offset
 | 
				
			||||||
 | 
						i := rand.Int63()
 | 
				
			||||||
 | 
						starti := i
 | 
				
			||||||
	start := time.Now().UnixNano()
 | 
						start := time.Now().UnixNano()
 | 
				
			||||||
	t := time.Now()
 | 
					
 | 
				
			||||||
 | 
						// Make sure stop is empty
 | 
				
			||||||
 | 
					empty:
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case <-stop:
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								break empty
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		select {
 | 
							select {
 | 
				
			||||||
		case <-stop:
 | 
							case <-stop:
 | 
				
			||||||
			powlogger.Infoln("Breaking from mining")
 | 
					 | 
				
			||||||
			pow.HashRate = 0
 | 
								pow.HashRate = 0
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			i++
 | 
								i++
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if time.Since(t) > (1 * time.Second) {
 | 
					 | 
				
			||||||
			elapsed := time.Now().UnixNano() - start
 | 
								elapsed := time.Now().UnixNano() - start
 | 
				
			||||||
				hashes := ((float64(1e9) / float64(elapsed)) * float64(i)) / 1000
 | 
								hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000
 | 
				
			||||||
			pow.HashRate = int64(hashes)
 | 
								pow.HashRate = int64(hashes)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				t = time.Now()
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes())
 | 
								sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes())
 | 
				
			||||||
			if verify(hash, diff, sha) {
 | 
								if verify(hash, diff, sha) {
 | 
				
			||||||
				return sha
 | 
									return sha
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,7 +60,6 @@ func (self *Object) Storage() (storage map[string]string) {
 | 
				
			|||||||
		rlp.Decode(bytes.NewReader(it.Value), &data)
 | 
							rlp.Decode(bytes.NewReader(it.Value), &data)
 | 
				
			||||||
		storage[toHex(it.Key)] = toHex(data)
 | 
							storage[toHex(it.Key)] = toHex(data)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	self.StateObject.Trie().PrintRoot()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user