Improved miner recovery

* In case of uncle mining (e.g. same TS) the miner would stop if all
  threads happened to mine a potential uncle
pull/564/head
obscuren 10 years ago
parent a1cae93d78
commit 950b4a68c8
  1. 34
      miner/worker.go

@ -64,6 +64,7 @@ type worker struct {
mux *event.TypeMux mux *event.TypeMux
quit chan struct{} quit chan struct{}
pow pow.PoW pow pow.PoW
atWork int
eth core.Backend eth core.Backend
chain *core.ChainManager chain *core.ChainManager
@ -106,6 +107,7 @@ func (self *worker) start() {
func (self *worker) stop() { func (self *worker) stop() {
self.mining = false self.mining = false
self.atWork = 0
close(self.quit) close(self.quit)
} }
@ -116,7 +118,7 @@ func (self *worker) register(agent Agent) {
} }
func (self *worker) update() { func (self *worker) update() {
events := self.mux.Subscribe(core.ChainHeadEvent{}, core.NewMinedBlockEvent{}, core.ChainSideEvent{}) events := self.mux.Subscribe(core.ChainHeadEvent{}, core.ChainSideEvent{})
timer := time.NewTicker(2 * time.Second) timer := time.NewTicker(2 * time.Second)
@ -127,13 +129,15 @@ out:
switch ev := event.(type) { switch ev := event.(type) {
case core.ChainHeadEvent: case core.ChainHeadEvent:
self.commitNewWork() self.commitNewWork()
case core.NewMinedBlockEvent:
//self.commitNewWork()
case core.ChainSideEvent: case core.ChainSideEvent:
self.uncleMu.Lock() self.uncleMu.Lock()
self.possibleUncles[ev.Block.Hash()] = ev.Block self.possibleUncles[ev.Block.Hash()] = ev.Block
self.uncleMu.Unlock() self.uncleMu.Unlock()
} }
if self.atWork == 0 {
self.commitNewWork()
}
case <-self.quit: case <-self.quit:
// stop all agents // stop all agents
for _, agent := range self.agents { for _, agent := range self.agents {
@ -148,17 +152,14 @@ out:
events.Unsubscribe() events.Unsubscribe()
} }
func (self *worker) addUncle(uncle *types.Block) {
}
func (self *worker) wait() { func (self *worker) wait() {
for { for {
for block := range self.recv { for block := range self.recv {
// Someone Successfully Mined! if err := self.chain.InsertChain(types.Blocks{block}); err == nil {
//block := self.current.block for _, uncle := range block.Uncles() {
//if block.Number().Uint64() == work.Number && block.Nonce() == 0 { delete(self.possibleUncles, uncle.Hash())
//self.current.block.SetNonce(work.Nonce) }
//self.current.block.Header().MixDigest = common.BytesToHash(work.MixDigest) self.mux.Post(core.NewMinedBlockEvent{block})
jsonlogger.LogJson(&logger.EthMinerNewBlock{ jsonlogger.LogJson(&logger.EthMinerNewBlock{
BlockHash: block.Hash().Hex(), BlockHash: block.Hash().Hex(),
@ -166,18 +167,10 @@ func (self *worker) wait() {
ChainHeadHash: block.ParentHeaderHash.Hex(), ChainHeadHash: block.ParentHeaderHash.Hex(),
BlockPrevHash: block.ParentHeaderHash.Hex(), BlockPrevHash: block.ParentHeaderHash.Hex(),
}) })
if err := self.chain.InsertChain(types.Blocks{block}); err == nil {
for _, uncle := range block.Uncles() {
delete(self.possibleUncles, uncle.Hash())
}
self.mux.Post(core.NewMinedBlockEvent{block})
} else { } else {
self.commitNewWork() self.commitNewWork()
} }
//} self.atWork--
break
} }
} }
} }
@ -190,6 +183,7 @@ func (self *worker) push() {
// push new work to agents // push new work to agents
for _, agent := range self.agents { for _, agent := range self.agents {
agent.Work() <- self.current.block.Copy() agent.Work() <- self.current.block.Copy()
self.atWork++
} }
} }
} }

Loading…
Cancel
Save