Merge pull request #1031 from bas-vk/issue1012

fixed race condition in miner
pull/1032/head
Jeffrey Wilcke 10 years ago
commit 677796b351
  1. 25
      miner/agent.go

@ -40,7 +40,6 @@ func (self *CpuAgent) Stop() {
defer self.mu.Unlock() defer self.mu.Unlock()
close(self.quit) close(self.quit)
close(self.quitCurrentOp)
} }
func (self *CpuAgent) Start() { func (self *CpuAgent) Start() {
@ -50,7 +49,6 @@ func (self *CpuAgent) Start() {
self.quit = make(chan struct{}) self.quit = make(chan struct{})
// creating current op ch makes sure we're not closing a nil ch // creating current op ch makes sure we're not closing a nil ch
// later on // later on
self.quitCurrentOp = make(chan struct{})
self.workCh = make(chan *types.Block, 1) self.workCh = make(chan *types.Block, 1)
go self.update() go self.update()
@ -62,11 +60,19 @@ out:
select { select {
case block := <-self.workCh: case block := <-self.workCh:
self.mu.Lock() 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() self.mu.Unlock()
go self.mine(block)
case <-self.quit: case <-self.quit:
self.mu.Lock()
if self.quitCurrentOp != nil {
close(self.quitCurrentOp)
self.quitCurrentOp = nil
}
self.mu.Unlock()
break out break out
} }
} }
@ -84,16 +90,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) 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 // Mine
nonce, mixDigest := self.pow.Search(block, self.quitCurrentOp) nonce, mixDigest := self.pow.Search(block, stop)
if nonce != 0 { if nonce != 0 {
block.SetNonce(nonce) block.SetNonce(nonce)
block.Header().MixDigest = common.BytesToHash(mixDigest) block.Header().MixDigest = common.BytesToHash(mixDigest)

Loading…
Cancel
Save