Improve ethash hash rate update

* Add hash rate tracking for each mining thread
* Do not use pow.GetHashrate but track it in each agent instead
This commit is contained in:
Gustav Simonsson 2015-05-13 17:39:31 +02:00
parent 7cb0e24245
commit 59ff88444a
9 changed files with 20 additions and 17 deletions

View file

@ -14,7 +14,7 @@ import (
// So we can generate blocks easily // So we can generate blocks easily
type FakePow struct{} type FakePow struct{}
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte) { func (f FakePow) Search(block pow.Block, stop <-chan struct{}, prevHashRate *uint64) (uint64, []byte) {
return 0, nil return 0, nil
} }
func (f FakePow) Verify(block pow.Block) bool { return true } func (f FakePow) Verify(block pow.Block) bool { return true }

View file

@ -17,14 +17,16 @@ type CpuAgent struct {
quitCurrentOp chan struct{} quitCurrentOp chan struct{}
returnCh chan<- *types.Block returnCh chan<- *types.Block
index int index int
pow pow.PoW prevHashRate *uint64
pow pow.PoW
} }
func NewCpuAgent(index int, pow pow.PoW) *CpuAgent { func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
miner := &CpuAgent{ miner := &CpuAgent{
pow: pow, pow: pow,
index: index, index: index,
prevHashRate: new(uint64),
} }
return miner return miner
@ -85,7 +87,7 @@ func (self *CpuAgent) mine(block *types.Block) {
self.chMu.Unlock() self.chMu.Unlock()
// Mine // Mine
nonce, mixDigest := self.pow.Search(block, self.quitCurrentOp) nonce, mixDigest := self.pow.Search(block, self.quitCurrentOp, self.prevHashRate)
if nonce != 0 { if nonce != 0 {
block.SetNonce(nonce) block.SetNonce(nonce)
block.Header().MixDigest = common.BytesToHash(mixDigest) block.Header().MixDigest = common.BytesToHash(mixDigest)
@ -95,6 +97,6 @@ func (self *CpuAgent) mine(block *types.Block) {
} }
} }
func (self *CpuAgent) GetHashRate() int64 { func (self *CpuAgent) GetHashRate() uint64 {
return self.pow.GetHashrate() return *self.prevHashRate
} }

View file

@ -54,6 +54,7 @@ func (self *Miner) Start(coinbase common.Address, threads int) {
self.worker.coinbase = coinbase self.worker.coinbase = coinbase
self.worker.start() self.worker.start()
self.worker.commitNewWork() self.worker.commitNewWork()
} }
func (self *Miner) Stop() { func (self *Miner) Stop() {
@ -69,7 +70,7 @@ func (self *Miner) Register(agent Agent) {
self.worker.register(agent) self.worker.register(agent)
} }
func (self *Miner) HashRate() int64 { func (self *Miner) HashRate() uint64 {
return self.worker.HashRate() return self.worker.HashRate()
} }

View file

@ -42,7 +42,7 @@ func (a *RemoteAgent) Stop() {
close(a.workCh) close(a.workCh)
} }
func (a *RemoteAgent) GetHashRate() int64 { return 0 } func (a *RemoteAgent) GetHashRate() uint64 { return 0 }
func (a *RemoteAgent) run() { func (a *RemoteAgent) run() {
out: out:

View file

@ -35,7 +35,7 @@ type Agent interface {
SetReturnCh(chan<- *types.Block) SetReturnCh(chan<- *types.Block)
Stop() Stop()
Start() Start()
GetHashRate() int64 GetHashRate() uint64
} }
// environment is the workers current environment and holds // environment is the workers current environment and holds
@ -453,8 +453,8 @@ func (self *worker) commitTransaction(tx *types.Transaction) error {
return nil return nil
} }
func (self *worker) HashRate() int64 { func (self *worker) HashRate() uint64 {
var tot int64 var tot uint64
for _, agent := range self.agents { for _, agent := range self.agents {
tot += agent.GetHashRate() tot += agent.GetHashRate()
} }

View file

@ -44,7 +44,7 @@ func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
resChan <- 0 resChan <- 0
} }
func (dag *Dagger) Search(hash, diff *big.Int) (uint64, []byte) { func (dag *Dagger) Search(hash, diff *big.Int, prevHashRate *uint64) (uint64, []byte) {
// TODO fix multi threading. Somehow it results in the wrong nonce // TODO fix multi threading. Somehow it results in the wrong nonce
amountOfRoutines := 1 amountOfRoutines := 1

View file

@ -32,7 +32,7 @@ func (pow *EasyPow) Turbo(on bool) {
pow.turbo = on pow.turbo = on
} }
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte) { func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}, prevHashRate *uint64) (uint64, []byte) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
hash := block.HashNoNonce() hash := block.HashNoNonce()
diff := block.Difficulty() diff := block.Difficulty()

View file

@ -1,7 +1,7 @@
package pow package pow
type PoW interface { type PoW interface {
Search(block Block, stop <-chan struct{}) (uint64, []byte) Search(block Block, stop <-chan struct{}, prevHashRate *uint64) (uint64, []byte)
Verify(block Block) bool Verify(block Block) bool
GetHashrate() int64 GetHashrate() int64
Turbo(bool) Turbo(bool)

View file

@ -405,7 +405,7 @@ func (self *XEth) IsMining() bool {
return self.backend.IsMining() return self.backend.IsMining()
} }
func (self *XEth) HashRate() int64 { func (self *XEth) HashRate() uint64 {
return self.backend.Miner().HashRate() return self.backend.Miner().HashRate()
} }