aboutsummaryrefslogtreecommitdiffstats
path: root/miner
diff options
context:
space:
mode:
authorJeffrey Wilcke <jeffrey@ethereum.org>2015-05-12 03:11:27 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2015-05-12 03:11:27 +0800
commit0bedf1c3760a1042171fdfde0f119acb683c43ec (patch)
tree2251978416ad615ee8ca5e465a5bf9ed5b74efed /miner
parent59bc5412327469fd13534ee22eaeda91d79286f4 (diff)
parent48bd48876c02d1a08690b9604df09ef4bcf77838 (diff)
downloaddexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.tar
dexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.tar.gz
dexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.tar.bz2
dexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.tar.lz
dexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.tar.xz
dexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.tar.zst
dexon-0bedf1c3760a1042171fdfde0f119acb683c43ec.zip
Merge pull request #918 from obscuren/cpu_miner_fixes
cmd/geth, miner, backend, xeth: Fixed miner threads to be settable
Diffstat (limited to 'miner')
-rw-r--r--miner/agent.go22
-rw-r--r--miner/miner.go33
-rw-r--r--miner/worker.go7
3 files changed, 33 insertions, 29 deletions
diff --git a/miner/agent.go b/miner/agent.go
index b2f89aaab..939f63fef 100644
--- a/miner/agent.go
+++ b/miner/agent.go
@@ -10,7 +10,7 @@ import (
"github.com/ethereum/go-ethereum/pow"
)
-type CpuMiner struct {
+type CpuAgent struct {
chMu sync.Mutex
c chan *types.Block
quit chan struct{}
@@ -21,8 +21,8 @@ type CpuMiner struct {
pow pow.PoW
}
-func NewCpuMiner(index int, pow pow.PoW) *CpuMiner {
- miner := &CpuMiner{
+func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
+ miner := &CpuAgent{
pow: pow,
index: index,
}
@@ -30,16 +30,16 @@ func NewCpuMiner(index int, pow pow.PoW) *CpuMiner {
return miner
}
-func (self *CpuMiner) Work() chan<- *types.Block { return self.c }
-func (self *CpuMiner) Pow() pow.PoW { return self.pow }
-func (self *CpuMiner) SetReturnCh(ch chan<- *types.Block) { self.returnCh = ch }
+func (self *CpuAgent) Work() chan<- *types.Block { return self.c }
+func (self *CpuAgent) Pow() pow.PoW { return self.pow }
+func (self *CpuAgent) SetReturnCh(ch chan<- *types.Block) { self.returnCh = ch }
-func (self *CpuMiner) Stop() {
+func (self *CpuAgent) Stop() {
close(self.quit)
close(self.quitCurrentOp)
}
-func (self *CpuMiner) Start() {
+func (self *CpuAgent) Start() {
self.quit = make(chan struct{})
self.quitCurrentOp = make(chan struct{}, 1)
self.c = make(chan *types.Block, 1)
@@ -47,7 +47,7 @@ func (self *CpuMiner) Start() {
go self.update()
}
-func (self *CpuMiner) update() {
+func (self *CpuAgent) update() {
out:
for {
select {
@@ -76,7 +76,7 @@ done:
}
}
-func (self *CpuMiner) mine(block *types.Block) {
+func (self *CpuAgent) mine(block *types.Block) {
glog.V(logger.Debug).Infof("(re)started agent[%d]. mining...\n", self.index)
// Reset the channel
@@ -95,6 +95,6 @@ func (self *CpuMiner) mine(block *types.Block) {
}
}
-func (self *CpuMiner) GetHashRate() int64 {
+func (self *CpuAgent) GetHashRate() int64 {
return self.pow.GetHashrate()
}
diff --git a/miner/miner.go b/miner/miner.go
index efe6d3051..09342e250 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -23,16 +23,8 @@ type Miner struct {
pow pow.PoW
}
-func New(eth core.Backend, pow pow.PoW, minerThreads int) *Miner {
- // note: minerThreads is currently ignored because
- // ethash is not thread safe.
- miner := &Miner{eth: eth, pow: pow, worker: newWorker(common.Address{}, eth)}
- for i := 0; i < minerThreads; i++ {
- miner.worker.register(NewCpuMiner(i, pow))
- }
- miner.threads = minerThreads
-
- return miner
+func New(eth core.Backend, pow pow.PoW) *Miner {
+ return &Miner{eth: eth, pow: pow, worker: newWorker(common.Address{}, eth)}
}
func (self *Miner) Mining() bool {
@@ -48,15 +40,27 @@ func (m *Miner) SetGasPrice(price *big.Int) {
m.worker.gasPrice = price
}
-func (self *Miner) Start(coinbase common.Address) {
- glog.V(logger.Info).Infoln("Starting mining operation")
+func (self *Miner) Start(coinbase common.Address, threads int) {
self.mining = true
+
+ for i := 0; i < threads; i++ {
+ self.worker.register(NewCpuAgent(i, self.pow))
+ }
+ self.threads = threads
+
+ glog.V(logger.Info).Infof("Starting mining operation (CPU=%d TOT=%d)\n", threads, len(self.worker.agents))
+
self.worker.coinbase = coinbase
self.worker.start()
self.worker.commitNewWork()
}
+func (self *Miner) Stop() {
+ self.worker.stop()
+ self.mining = false
+}
+
func (self *Miner) Register(agent Agent) {
if self.mining {
agent.Start()
@@ -65,11 +69,6 @@ func (self *Miner) Register(agent Agent) {
self.worker.register(agent)
}
-func (self *Miner) Stop() {
- self.mining = false
- self.worker.stop()
-}
-
func (self *Miner) HashRate() int64 {
return self.worker.HashRate()
}
diff --git a/miner/worker.go b/miner/worker.go
index e3dbae717..d801a9839 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -141,7 +141,6 @@ func (self *worker) start() {
for _, agent := range self.agents {
agent.Start()
}
-
}
func (self *worker) stop() {
@@ -149,10 +148,16 @@ func (self *worker) stop() {
defer self.mu.Unlock()
if atomic.LoadInt32(&self.mining) == 1 {
+ var keep []Agent
// stop all agents
for _, agent := range self.agents {
agent.Stop()
+ // keep all that's not a cpu agent
+ if _, ok := agent.(*CpuAgent); !ok {
+ keep = append(keep, agent)
+ }
}
+ self.agents = keep
}
atomic.StoreInt32(&self.mining, 0)