aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBas van Kervel <basvankervel@gmail.com>2015-05-18 21:13:58 +0800
committerBas van Kervel <basvankervel@gmail.com>2015-05-18 21:13:58 +0800
commit7778740315f322a9a31219090f180374a5620ec0 (patch)
tree0762859d40ad6e0d954efcc988a3ecebdb573dab
parent67d44519ce4344d5ee5f7cf37dd785ead70e8453 (diff)
downloadgo-tangerine-7778740315f322a9a31219090f180374a5620ec0.tar.gz
go-tangerine-7778740315f322a9a31219090f180374a5620ec0.tar.zst
go-tangerine-7778740315f322a9a31219090f180374a5620ec0.zip
fixed race condition in miner
-rw-r--r--miner/agent.go19
1 files changed, 7 insertions, 12 deletions
diff --git a/miner/agent.go b/miner/agent.go
index da2a2008d..b4c776c65 100644
--- a/miner/agent.go
+++ b/miner/agent.go
@@ -40,7 +40,6 @@ func (self *CpuAgent) Stop() {
defer self.mu.Unlock()
close(self.quit)
- close(self.quitCurrentOp)
}
func (self *CpuAgent) Start() {
@@ -50,7 +49,6 @@ func (self *CpuAgent) Start() {
self.quit = make(chan struct{})
// creating current op ch makes sure we're not closing a nil ch
// later on
- self.quitCurrentOp = make(chan struct{})
self.workCh = make(chan *types.Block, 1)
go self.update()
@@ -62,10 +60,12 @@ out:
select {
case block := <-self.workCh:
self.mu.Lock()
- close(self.quitCurrentOp)
+ if self.quitCurrentOp != nil {
+ close(self.quitCurrentOp)
+ }
+ self.quitCurrentOp = make(chan struct{})
+ go self.mine(block, self.quitCurrentOp)
self.mu.Unlock()
-
- go self.mine(block)
case <-self.quit:
break out
}
@@ -84,16 +84,11 @@ done:
}
}
-func (self *CpuAgent) mine(block *types.Block) {
+func (self *CpuAgent) mine(block *types.Block, stop <- chan struct{}) {
glog.V(logger.Debug).Infof("(re)started agent[%d]. mining...\n", self.index)
- // Reset the channel
- self.mu.Lock()
- self.quitCurrentOp = make(chan struct{})
- self.mu.Unlock()
-
// Mine
- nonce, mixDigest := self.pow.Search(block, self.quitCurrentOp)
+ nonce, mixDigest := self.pow.Search(block, stop)
if nonce != 0 {
block.SetNonce(nonce)
block.Header().MixDigest = common.BytesToHash(mixDigest)