diff --git a/miner/miner.go b/miner/miner.go index f48df9491d..8dfe8e762c 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -145,15 +145,7 @@ func (m *Miner) HashRate() (tot int64) { if pow, ok := m.engine.(consensus.PoW); ok { tot += int64(pow.Hashrate()) } - // do we care this might race? is it worth we're rewriting some - // aspects of the worker/locking up agents so we can get an accurate - // hashrate? - for agent := range m.worker.agents { - if _, ok := agent.(*CpuAgent); !ok { - tot += agent.GetHashRate() - } - } - return + return tot + m.worker.getHashrate() } func (m *Miner) SetExtra(extra []byte) error { diff --git a/miner/worker.go b/miner/worker.go index 6dff4fb191..81a1acb6be 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -379,6 +379,19 @@ func (w *worker) update() { } } +func (w *worker) getHashrate() int64 { + w.mu.Lock() + defer w.mu.Unlock() + + total := int64(0) + for agent := range w.agents { + if _, ok := agent.(*CpuAgent); !ok { + total += agent.GetHashRate() + } + } + return total +} + func getResetTime(chain *core.BlockChain, minePeriod int) time.Duration { minePeriodDuration := time.Duration(minePeriod) * time.Second currentBlockTime := int64(chain.CurrentBlock().Time())