miner: smart mining
Work is now handled and carried over multiple sessions. Previously one session only was assumed, potentially resulting in invalid (outdated) work * Larger work / result queue * Full validation option
This commit is contained in:
@ -20,7 +20,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
"github.com/ethereum/go-ethereum/pow"
|
||||
@ -29,10 +28,10 @@ import (
|
||||
type CpuAgent struct {
|
||||
mu sync.Mutex
|
||||
|
||||
workCh chan *types.Block
|
||||
workCh chan *Work
|
||||
quit chan struct{}
|
||||
quitCurrentOp chan struct{}
|
||||
returnCh chan<- *types.Block
|
||||
returnCh chan<- *Result
|
||||
|
||||
index int
|
||||
pow pow.PoW
|
||||
@ -47,9 +46,9 @@ func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
|
||||
return miner
|
||||
}
|
||||
|
||||
func (self *CpuAgent) Work() chan<- *types.Block { return self.workCh }
|
||||
func (self *CpuAgent) Pow() pow.PoW { return self.pow }
|
||||
func (self *CpuAgent) SetReturnCh(ch chan<- *types.Block) { self.returnCh = ch }
|
||||
func (self *CpuAgent) Work() chan<- *Work { return self.workCh }
|
||||
func (self *CpuAgent) Pow() pow.PoW { return self.pow }
|
||||
func (self *CpuAgent) SetReturnCh(ch chan<- *Result) { self.returnCh = ch }
|
||||
|
||||
func (self *CpuAgent) Stop() {
|
||||
self.mu.Lock()
|
||||
@ -65,7 +64,7 @@ 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.workCh = make(chan *types.Block, 1)
|
||||
self.workCh = make(chan *Work, 1)
|
||||
|
||||
go self.update()
|
||||
}
|
||||
@ -74,13 +73,13 @@ func (self *CpuAgent) update() {
|
||||
out:
|
||||
for {
|
||||
select {
|
||||
case block := <-self.workCh:
|
||||
case work := <-self.workCh:
|
||||
self.mu.Lock()
|
||||
if self.quitCurrentOp != nil {
|
||||
close(self.quitCurrentOp)
|
||||
}
|
||||
self.quitCurrentOp = make(chan struct{})
|
||||
go self.mine(block, self.quitCurrentOp)
|
||||
go self.mine(work, self.quitCurrentOp)
|
||||
self.mu.Unlock()
|
||||
case <-self.quit:
|
||||
self.mu.Lock()
|
||||
@ -106,13 +105,14 @@ done:
|
||||
}
|
||||
}
|
||||
|
||||
func (self *CpuAgent) mine(block *types.Block, stop <-chan struct{}) {
|
||||
func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {
|
||||
glog.V(logger.Debug).Infof("(re)started agent[%d]. mining...\n", self.index)
|
||||
|
||||
// Mine
|
||||
nonce, mixDigest := self.pow.Search(block, stop)
|
||||
nonce, mixDigest := self.pow.Search(work.Block, stop)
|
||||
if nonce != 0 {
|
||||
self.returnCh <- block.WithMiningResult(nonce, common.BytesToHash(mixDigest))
|
||||
block := work.Block.WithMiningResult(nonce, common.BytesToHash(mixDigest))
|
||||
self.returnCh <- &Result{work, block}
|
||||
} else {
|
||||
self.returnCh <- nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user