miner: fix concurrent map access in Miner.HashRate, close XFN-52 (#1634)

This commit is contained in:
Daniel Liu 2025-11-03 17:23:37 +08:00 committed by GitHub
parent f90a12d9f7
commit 76aa15b7d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View file

@ -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 {

View file

@ -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())