Commit c7a13c9b authored by Jeffrey Wilcke's avatar Jeffrey Wilcke

Merge pull request #969 from Gustav-Simonsson/ethash_improve_hashrate_update2

Make read of ethash hashrate atomic and update ethash godep
parents 2b06fe3e b24f16fa
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
}, },
{ {
"ImportPath": "github.com/ethereum/ethash", "ImportPath": "github.com/ethereum/ethash",
"Comment": "v23.1-204-g0401fdf", "Comment": "v23.1-206-gf0e6321",
"Rev": "0401fdf56a3bc8679f9560e542c3d1cf83020efe" "Rev": "f0e63218b721dc2f696920a92d5de1f6364e9bf7"
}, },
{ {
"ImportPath": "github.com/howeyc/fsnotify", "ImportPath": "github.com/howeyc/fsnotify",
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync" "sync"
"sync/atomic"
"time" "time"
"unsafe" "unsafe"
...@@ -235,7 +236,7 @@ type Full struct { ...@@ -235,7 +236,7 @@ type Full struct {
test bool // if set use a smaller DAG size test bool // if set use a smaller DAG size
turbo bool turbo bool
hashRate int64 hashRate int32
mu sync.Mutex // protects dag mu sync.Mutex // protects dag
current *dag // current full DAG current *dag // current full DAG
...@@ -265,6 +266,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi ...@@ -265,6 +266,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
i := int64(0) i := int64(0)
starti := i starti := i
start := time.Now().UnixNano() start := time.Now().UnixNano()
previousHashrate := int32(0)
nonce = uint64(r.Int63()) nonce = uint64(r.Int63())
hash := hashToH256(block.HashNoNonce()) hash := hashToH256(block.HashNoNonce())
...@@ -272,14 +274,20 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi ...@@ -272,14 +274,20 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
for { for {
select { select {
case <-stop: case <-stop:
pow.hashRate = 0 atomic.AddInt32(&pow.hashRate, -previousHashrate)
return 0, nil return 0, nil
default: default:
i++ i++
elapsed := time.Now().UnixNano() - start // we don't have to update hash rate on every nonce, so update after
hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000 // first nonce check and then after 2^X nonces
pow.hashRate = int64(hashes) if i == 2 || ((i % (1 << 16)) == 0) {
elapsed := time.Now().UnixNano() - start
hashes := (float64(1e9) / float64(elapsed)) * float64(i-starti)
hashrateDiff := int32(hashes) - previousHashrate
previousHashrate = int32(hashes)
atomic.AddInt32(&pow.hashRate, hashrateDiff)
}
ret := C.ethash_full_compute(dag.ptr, hash, C.uint64_t(nonce)) ret := C.ethash_full_compute(dag.ptr, hash, C.uint64_t(nonce))
result := h256ToHash(ret.result).Big() result := h256ToHash(ret.result).Big()
...@@ -287,6 +295,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi ...@@ -287,6 +295,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
// TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining // TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining
if ret.success && result.Cmp(target) <= 0 { if ret.success && result.Cmp(target) <= 0 {
mixDigest = C.GoBytes(unsafe.Pointer(&ret.mix_hash), C.int(32)) mixDigest = C.GoBytes(unsafe.Pointer(&ret.mix_hash), C.int(32))
atomic.AddInt32(&pow.hashRate, -previousHashrate)
return nonce, mixDigest return nonce, mixDigest
} }
nonce += 1 nonce += 1
...@@ -299,8 +308,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi ...@@ -299,8 +308,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
} }
func (pow *Full) GetHashrate() int64 { func (pow *Full) GetHashrate() int64 {
// TODO: this needs to use an atomic operation. return int64(atomic.LoadInt32(&pow.hashRate))
return pow.hashRate
} }
func (pow *Full) Turbo(on bool) { func (pow *Full) Turbo(on bool) {
......
...@@ -70,7 +70,7 @@ func (self *Miner) Register(agent Agent) { ...@@ -70,7 +70,7 @@ func (self *Miner) Register(agent Agent) {
} }
func (self *Miner) HashRate() int64 { func (self *Miner) HashRate() int64 {
return self.worker.HashRate() return self.pow.GetHashrate()
} }
func (self *Miner) SetExtra(extra []byte) { func (self *Miner) SetExtra(extra []byte) {
......
...@@ -175,7 +175,6 @@ func (self *worker) stop() { ...@@ -175,7 +175,6 @@ func (self *worker) stop() {
func (self *worker) register(agent Agent) { func (self *worker) register(agent Agent) {
self.mu.Lock() self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
self.agents = append(self.agents, agent) self.agents = append(self.agents, agent)
agent.SetReturnCh(self.recv) agent.SetReturnCh(self.recv)
} }
...@@ -459,13 +458,9 @@ func (self *worker) commitTransaction(tx *types.Transaction) error { ...@@ -459,13 +458,9 @@ func (self *worker) commitTransaction(tx *types.Transaction) error {
return nil return nil
} }
// TODO: remove or use
func (self *worker) HashRate() int64 { func (self *worker) HashRate() int64 {
var tot int64 return 0
for _, agent := range self.agents {
tot += agent.GetHashRate()
}
return tot
} }
// gasprice calculates a reduced gas price based on the pct // gasprice calculates a reduced gas price based on the pct
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment