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 ( ...@@ -27,10 +27,10 @@ import (
type CpuAgent struct { type CpuAgent struct {
mu sync.Mutex mu sync.Mutex
workCh chan *Work taskCh chan *Package
returnCh chan<- *Package
stop chan struct{} stop chan struct{}
quitCurrentOp chan struct{} quitCurrentOp chan struct{}
returnCh chan<- *Result
chain consensus.ChainReader chain consensus.ChainReader
engine consensus.Engine engine consensus.Engine
...@@ -43,13 +43,17 @@ func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine) *CpuAgent ...@@ -43,13 +43,17 @@ func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine) *CpuAgent
chain: chain, chain: chain,
engine: engine, engine: engine,
stop: make(chan struct{}, 1), stop: make(chan struct{}, 1),
workCh: make(chan *Work, 1), taskCh: make(chan *Package, 1),
} }
return agent return agent
} }
func (self *CpuAgent) Work() chan<- *Work { return self.workCh } func (self *CpuAgent) AssignTask(p *Package) {
func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch } if atomic.LoadInt32(&self.started) == 1 {
self.taskCh <- p
}
}
func (self *CpuAgent) DeliverTo(ch chan<- *Package) { self.returnCh = ch }
func (self *CpuAgent) Start() { func (self *CpuAgent) Start() {
if !atomic.CompareAndSwapInt32(&self.started, 0, 1) { if !atomic.CompareAndSwapInt32(&self.started, 0, 1) {
...@@ -67,7 +71,7 @@ done: ...@@ -67,7 +71,7 @@ done:
// Empty work channel // Empty work channel
for { for {
select { select {
case <-self.workCh: case <-self.taskCh:
default: default:
break done break done
} }
...@@ -78,13 +82,13 @@ func (self *CpuAgent) update() { ...@@ -78,13 +82,13 @@ func (self *CpuAgent) update() {
out: out:
for { for {
select { select {
case work := <-self.workCh: case p := <-self.taskCh:
self.mu.Lock() self.mu.Lock()
if self.quitCurrentOp != nil { if self.quitCurrentOp != nil {
close(self.quitCurrentOp) close(self.quitCurrentOp)
} }
self.quitCurrentOp = make(chan struct{}) self.quitCurrentOp = make(chan struct{})
go self.mine(work, self.quitCurrentOp) go self.mine(p, self.quitCurrentOp)
self.mu.Unlock() self.mu.Unlock()
case <-self.stop: case <-self.stop:
self.mu.Lock() self.mu.Lock()
...@@ -98,10 +102,11 @@ out: ...@@ -98,10 +102,11 @@ out:
} }
} }
func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) { func (self *CpuAgent) mine(p *Package, stop <-chan struct{}) {
if result, err := self.engine.Seal(self.chain, work.Block, stop); result != nil { var err error
log.Info("Successfully sealed new block", "number", result.Number(), "hash", result.Hash()) if p.Block, err = self.engine.Seal(self.chain, p.Block, stop); p.Block != nil {
self.returnCh <- &Result{work, result} log.Info("Successfully sealed new block", "number", p.Block.Number(), "hash", p.Block.Hash())
self.returnCh <- p
} else { } else {
if err != nil { if err != nil {
log.Warn("Block sealing failed", "err", err) 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