Selaa lähdekoodia

Merge pull request #969 from Gustav-Simonsson/ethash_improve_hashrate_update2

Make read of ethash hashrate atomic and update ethash godep
Jeffrey Wilcke 10 vuotta sitten
vanhempi
commit
c7a13c9be8

+ 2 - 2
Godeps/Godeps.json

@@ -17,8 +17,8 @@
 		},
 		{
 			"ImportPath": "github.com/ethereum/ethash",
-			"Comment": "v23.1-204-g0401fdf",
-			"Rev": "0401fdf56a3bc8679f9560e542c3d1cf83020efe"
+			"Comment": "v23.1-206-gf0e6321",
+			"Rev": "f0e63218b721dc2f696920a92d5de1f6364e9bf7"
 		},
 		{
 			"ImportPath": "github.com/howeyc/fsnotify",

+ 15 - 7
Godeps/_workspace/src/github.com/ethereum/ethash/ethash.go

@@ -18,6 +18,7 @@ import (
 	"path/filepath"
 	"runtime"
 	"sync"
+	"sync/atomic"
 	"time"
 	"unsafe"
 
@@ -235,7 +236,7 @@ type Full struct {
 
 	test     bool // if set use a smaller DAG size
 	turbo    bool
-	hashRate int64
+	hashRate int32
 
 	mu      sync.Mutex // protects dag
 	current *dag       // current full DAG
@@ -265,6 +266,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
 	i := int64(0)
 	starti := i
 	start := time.Now().UnixNano()
+	previousHashrate := int32(0)
 
 	nonce = uint64(r.Int63())
 	hash := hashToH256(block.HashNoNonce())
@@ -272,14 +274,20 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
 	for {
 		select {
 		case <-stop:
-			pow.hashRate = 0
+			atomic.AddInt32(&pow.hashRate, -previousHashrate)
 			return 0, nil
 		default:
 			i++
 
-			elapsed := time.Now().UnixNano() - start
-			hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000
-			pow.hashRate = int64(hashes)
+			// we don't have to update hash rate on every nonce, so update after
+			// first nonce check and then after 2^X nonces
+			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))
 			result := h256ToHash(ret.result).Big()
@@ -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
 			if ret.success && result.Cmp(target) <= 0 {
 				mixDigest = C.GoBytes(unsafe.Pointer(&ret.mix_hash), C.int(32))
+				atomic.AddInt32(&pow.hashRate, -previousHashrate)
 				return nonce, mixDigest
 			}
 			nonce += 1
@@ -299,8 +308,7 @@ func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mi
 }
 
 func (pow *Full) GetHashrate() int64 {
-	// TODO: this needs to use an atomic operation.
-	return pow.hashRate
+	return int64(atomic.LoadInt32(&pow.hashRate))
 }
 
 func (pow *Full) Turbo(on bool) {

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 21 - 93
Godeps/_workspace/src/github.com/ethereum/ethash/src/python/core.c


+ 1 - 1
miner/miner.go

@@ -70,7 +70,7 @@ func (self *Miner) Register(agent Agent) {
 }
 
 func (self *Miner) HashRate() int64 {
-	return self.worker.HashRate()
+	return self.pow.GetHashrate()
 }
 
 func (self *Miner) SetExtra(extra []byte) {

+ 2 - 7
miner/worker.go

@@ -175,7 +175,6 @@ func (self *worker) stop() {
 func (self *worker) register(agent Agent) {
 	self.mu.Lock()
 	defer self.mu.Unlock()
-
 	self.agents = append(self.agents, agent)
 	agent.SetReturnCh(self.recv)
 }
@@ -459,13 +458,9 @@ func (self *worker) commitTransaction(tx *types.Transaction) error {
 	return nil
 }
 
+// TODO: remove or use
 func (self *worker) HashRate() int64 {
-	var tot int64
-	for _, agent := range self.agents {
-		tot += agent.GetHashRate()
-	}
-
-	return tot
+	return 0
 }
 
 // gasprice calculates a reduced gas price based on the pct

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä