aboutsummaryrefslogtreecommitdiffstats
path: root/miner/agent.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <geffobscura@gmail.com>2015-07-12 02:45:59 +0800
committerJeffrey Wilcke <geffobscura@gmail.com>2015-07-16 02:37:12 +0800
commite870e61bc95cf40ca9956b2eb887976ef60dfd9c (patch)
tree3a83461327f0eee3d3afdc8d7de83a632f904e9e /miner/agent.go
parentcecc9cdd2f799ddaf189d62d9d43892f7dc82ebc (diff)
downloaddexon-e870e61bc95cf40ca9956b2eb887976ef60dfd9c.tar.gz
dexon-e870e61bc95cf40ca9956b2eb887976ef60dfd9c.tar.zst
dexon-e870e61bc95cf40ca9956b2eb887976ef60dfd9c.zip
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
Diffstat (limited to 'miner/agent.go')
-rw-r--r--miner/agent.go24
1 files changed, 12 insertions, 12 deletions
diff --git a/miner/agent.go b/miner/agent.go
index 8455ed36e..370e611f8 100644
--- a/miner/agent.go
+++ b/miner/agent.go
@@ -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
}