Added start / stopping methods

This commit is contained in:
obscuren 2014-05-22 00:25:34 +02:00
parent f8f84ef095
commit 4e1c6a8a22

@ -16,13 +16,14 @@ type Miner struct {
uncles []*ethchain.Block uncles []*ethchain.Block
block *ethchain.Block block *ethchain.Block
powChan chan []byte powChan chan []byte
quitChan chan ethutil.React powQuitChan chan ethutil.React
quitChan chan bool
} }
func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { 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 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 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 powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread
ethereum.Reactor().Subscribe("newBlock", reactChan) ethereum.Reactor().Subscribe("newBlock", reactChan)
ethereum.Reactor().Subscribe("newTx:pre", reactChan) ethereum.Reactor().Subscribe("newTx:pre", reactChan)
@ -32,8 +33,8 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
// listen to the reactor events inside of the pow itself // listen to the reactor events inside of the pow itself
// The miner overseer will never get the reactor events themselves // The miner overseer will never get the reactor events themselves
// Only after the miner will find the sha // Only after the miner will find the sha
ethereum.Reactor().Subscribe("newBlock", quitChan) ethereum.Reactor().Subscribe("newBlock", powQuitChan)
ethereum.Reactor().Subscribe("newTx:pre", quitChan) ethereum.Reactor().Subscribe("newTx:pre", powQuitChan)
miner := Miner{ miner := Miner{
pow: &ethchain.EasyPow{}, pow: &ethchain.EasyPow{},
@ -41,7 +42,8 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
coinbase: coinbase, coinbase: coinbase,
reactChan: reactChan, reactChan: reactChan,
powChan: powChan, powChan: powChan,
quitChan: quitChan, powQuitChan: powQuitChan,
quitChan: make(chan bool),
} }
// Insert initial TXs in our little miner 'pool' // Insert initial TXs in our little miner 'pool'
@ -56,8 +58,11 @@ func (miner *Miner) Start() {
go miner.listener() go miner.listener()
} }
func (miner *Miner) listener() { func (miner *Miner) listener() {
out:
for { for {
select { select {
case <-miner.quitChan:
break out
case chanMessage := <-miner.reactChan: case chanMessage := <-miner.reactChan:
if block, ok := chanMessage.Resource.(*ethchain.Block); ok { if block, ok := chanMessage.Resource.(*ethchain.Block); ok {
//ethutil.Config.Log.Infoln("[MINER] Got new block via Reactor") //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() { func (self *Miner) mineNewBlock() {
stateManager := self.ethereum.StateManager() 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") ethutil.Config.Log.Infoln("[MINER] Mining on block. Includes", len(self.txs), "transactions")
// Find a valid nonce // 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 { if self.block.Nonce != nil {
err := self.ethereum.StateManager().Process(self.block, true) err := self.ethereum.StateManager().Process(self.block, true)
if err != nil { if err != nil {