aboutsummaryrefslogtreecommitdiffstats
path: root/miner/worker.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-03-27 00:45:03 +0800
committerobscuren <geffobscura@gmail.com>2015-03-27 00:45:03 +0800
commitd36501a6e54e1c794af0c7109e937f7f7c74de79 (patch)
tree5b32e606558a99e4a516757c924a679fdcb2716f /miner/worker.go
parentd0fa0a234d19468b513d9144f442b8be77ccf898 (diff)
downloadgo-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.tar
go-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.tar.gz
go-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.tar.bz2
go-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.tar.lz
go-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.tar.xz
go-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.tar.zst
go-tangerine-d36501a6e54e1c794af0c7109e937f7f7c74de79.zip
Fixed miner
* Miners could stall because the worker wasn't aware the miner was done
Diffstat (limited to 'miner/worker.go')
-rw-r--r--miner/worker.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/miner/worker.go b/miner/worker.go
index e0287ea8d..e3680dea3 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -5,6 +5,7 @@ import (
"math/big"
"sort"
"sync"
+ "sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common"
@@ -58,13 +59,14 @@ type Agent interface {
}
type worker struct {
- mu sync.Mutex
+ mu sync.Mutex
+
agents []Agent
recv chan *types.Block
mux *event.TypeMux
quit chan struct{}
pow pow.PoW
- atWork int
+ atWork int64
eth core.Backend
chain *core.ChainManager
@@ -107,7 +109,7 @@ func (self *worker) start() {
func (self *worker) stop() {
self.mining = false
- self.atWork = 0
+ atomic.StoreInt64(&self.atWork, 0)
close(self.quit)
}
@@ -135,9 +137,6 @@ out:
self.uncleMu.Unlock()
}
- if self.atWork == 0 {
- self.commitNewWork()
- }
case <-self.quit:
// stop all agents
for _, agent := range self.agents {
@@ -146,6 +145,11 @@ out:
break out
case <-timer.C:
minerlogger.Infoln("Hash rate:", self.HashRate(), "Khash")
+
+ // XXX In case all mined a possible uncle
+ if atomic.LoadInt64(&self.atWork) == 0 {
+ self.commitNewWork()
+ }
}
}
@@ -155,6 +159,12 @@ out:
func (self *worker) wait() {
for {
for block := range self.recv {
+ atomic.AddInt64(&self.atWork, -1)
+
+ if block == nil {
+ continue
+ }
+
if err := self.chain.InsertChain(types.Blocks{block}); err == nil {
for _, uncle := range block.Uncles() {
delete(self.possibleUncles, uncle.Hash())
@@ -170,7 +180,6 @@ func (self *worker) wait() {
} else {
self.commitNewWork()
}
- self.atWork--
}
}
}
@@ -182,8 +191,9 @@ func (self *worker) push() {
// push new work to agents
for _, agent := range self.agents {
+ atomic.AddInt64(&self.atWork, 1)
+
agent.Work() <- self.current.block.Copy()
- self.atWork++
}
}
}