Commit 941018b5 authored by gary rong's avatar gary rong Committed by Péter Szilágyi

miner: seperate state, receipts for different mining work (#17323)

parent a72ba5a5
......@@ -27,10 +27,10 @@ import (
type CpuAgent struct {
mu sync.Mutex
workCh chan *Work
taskCh chan *Package
returnCh chan<- *Package
stop chan struct{}
quitCurrentOp chan struct{}
returnCh chan<- *Result
chain consensus.ChainReader
engine consensus.Engine
......@@ -43,13 +43,17 @@ func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine) *CpuAgent
chain: chain,
engine: engine,
stop: make(chan struct{}, 1),
workCh: make(chan *Work, 1),
taskCh: make(chan *Package, 1),
}
return agent
}
func (self *CpuAgent) Work() chan<- *Work { return self.workCh }
func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch }
func (self *CpuAgent) AssignTask(p *Package) {
if atomic.LoadInt32(&self.started) == 1 {
self.taskCh <- p
}
}
func (self *CpuAgent) DeliverTo(ch chan<- *Package) { self.returnCh = ch }
func (self *CpuAgent) Start() {
if !atomic.CompareAndSwapInt32(&self.started, 0, 1) {
......@@ -67,7 +71,7 @@ done:
// Empty work channel
for {
select {
case <-self.workCh:
case <-self.taskCh:
default:
break done
}
......@@ -78,13 +82,13 @@ func (self *CpuAgent) update() {
out:
for {
select {
case work := <-self.workCh:
case p := <-self.taskCh:
self.mu.Lock()
if self.quitCurrentOp != nil {
close(self.quitCurrentOp)
}
self.quitCurrentOp = make(chan struct{})
go self.mine(work, self.quitCurrentOp)
go self.mine(p, self.quitCurrentOp)
self.mu.Unlock()
case <-self.stop:
self.mu.Lock()
......@@ -98,10 +102,11 @@ out:
}
}
func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {
if result, err := self.engine.Seal(self.chain, work.Block, stop); result != nil {
log.Info("Successfully sealed new block", "number", result.Number(), "hash", result.Hash())
self.returnCh <- &Result{work, result}
func (self *CpuAgent) mine(p *Package, stop <-chan struct{}) {
var err error
if p.Block, err = self.engine.Seal(self.chain, p.Block, stop); p.Block != nil {
log.Info("Successfully sealed new block", "number", p.Block.Number(), "hash", p.Block.Hash())
self.returnCh <- p
} else {
if err != nil {
log.Warn("Block sealing failed", "err", err)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment