diff options
author | obscuren <geffobscura@gmail.com> | 2014-05-22 06:25:34 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-05-22 06:25:34 +0800 |
commit | 4e1c6a8a22924d06a2a972c024891cebcf8ea054 (patch) | |
tree | 2a93a5beae1b98d33d13ed9ef3f8f7d6c62e3056 /ethminer/miner.go | |
parent | f8f84ef0953534f44c3ce7c9630c569b1059e4bd (diff) | |
download | go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.tar go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.tar.gz go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.tar.bz2 go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.tar.lz go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.tar.xz go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.tar.zst go-tangerine-4e1c6a8a22924d06a2a972c024891cebcf8ea054.zip |
Added start / stopping methods
Diffstat (limited to 'ethminer/miner.go')
-rw-r--r-- | ethminer/miner.go | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/ethminer/miner.go b/ethminer/miner.go index 2e31dcead..166f7bc2f 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -8,21 +8,22 @@ import ( ) type Miner struct { - pow ethchain.PoW - ethereum ethchain.EthManager - coinbase []byte - reactChan chan ethutil.React - txs []*ethchain.Transaction - uncles []*ethchain.Block - block *ethchain.Block - powChan chan []byte - quitChan chan ethutil.React + pow ethchain.PoW + ethereum ethchain.EthManager + coinbase []byte + reactChan chan ethutil.React + txs []*ethchain.Transaction + uncles []*ethchain.Block + block *ethchain.Block + powChan chan []byte + powQuitChan chan ethutil.React + quitChan chan bool } func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { - reactChan := make(chan ethutil.React, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in - powChan := make(chan []byte, 1) // This is the channel that receives valid sha hases for a given block - quitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread + reactChan := make(chan ethutil.React, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in + powChan := make(chan []byte, 1) // This is the channel that receives valid sha hases for a given block + powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread ethereum.Reactor().Subscribe("newBlock", reactChan) ethereum.Reactor().Subscribe("newTx:pre", reactChan) @@ -32,16 +33,17 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { // listen to the reactor events inside of the pow itself // The miner overseer will never get the reactor events themselves // Only after the miner will find the sha - ethereum.Reactor().Subscribe("newBlock", quitChan) - ethereum.Reactor().Subscribe("newTx:pre", quitChan) + ethereum.Reactor().Subscribe("newBlock", powQuitChan) + ethereum.Reactor().Subscribe("newTx:pre", powQuitChan) miner := Miner{ - pow: ðchain.EasyPow{}, - ethereum: ethereum, - coinbase: coinbase, - reactChan: reactChan, - powChan: powChan, - quitChan: quitChan, + pow: ðchain.EasyPow{}, + ethereum: ethereum, + coinbase: coinbase, + reactChan: reactChan, + powChan: powChan, + powQuitChan: powQuitChan, + quitChan: make(chan bool), } // Insert initial TXs in our little miner 'pool' @@ -56,8 +58,11 @@ func (miner *Miner) Start() { go miner.listener() } func (miner *Miner) listener() { +out: for { select { + case <-miner.quitChan: + break out case chanMessage := <-miner.reactChan: if block, ok := chanMessage.Resource.(*ethchain.Block); ok { //ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor") @@ -112,6 +117,11 @@ func (miner *Miner) listener() { } } +func (self *Miner) Stop() { + self.powQuitChan <- ethutil.React{} + self.quitChan <- true +} + func (self *Miner) mineNewBlock() { stateManager := self.ethereum.StateManager() @@ -138,7 +148,7 @@ func (self *Miner) mineNewBlock() { ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions") // Find a valid nonce - self.block.Nonce = self.pow.Search(self.block, self.quitChan) + self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) if self.block.Nonce != nil { err := self.ethereum.StateManager().Process(self.block, true) if err != nil { |