From b09268c56f41cff5c5a815fdfa4e82f393333053 Mon Sep 17 00:00:00 2001 From: Sammy Libre Date: Tue, 6 Dec 2016 20:03:42 +0500 Subject: [PATCH] Fix stats --- go-pool/stratum/api.go | 2 +- go-pool/stratum/miner.go | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/go-pool/stratum/api.go b/go-pool/stratum/api.go index c020c02..21dd958 100644 --- a/go-pool/stratum/api.go +++ b/go-pool/stratum/api.go @@ -30,7 +30,7 @@ func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) { stats["height"] = t.Height stats["diff"] = t.Difficulty roundShares := atomic.LoadInt64(&s.roundShares) - stats["variance"] = roundShares / t.Difficulty + stats["variance"] = float64(roundShares) / float64(t.Difficulty) stats["template"] = true } json.NewEncoder(w).Encode(stats) diff --git a/go-pool/stratum/miner.go b/go-pool/stratum/miner.go index e75c552..a5675df 100644 --- a/go-pool/stratum/miner.go +++ b/go-pool/stratum/miner.go @@ -60,7 +60,8 @@ func (job *Job) submit(nonce string) bool { func NewMiner(login, pass string, diff int64, ip string) *Miner { id := util.Random() - miner := &Miner{Id: id, Login: login, Pass: pass, Difficulty: diff, IP: ip} + shares := make(map[int64]int64) + miner := &Miner{Id: id, Login: login, Pass: pass, Difficulty: diff, IP: ip, shares: shares} target, targetHex := util.GetTargetHex(diff) miner.Target = target miner.TargetHex = targetHex @@ -104,16 +105,16 @@ func (m *Miner) getLastBeat() int64 { } func (m *Miner) storeShare(diff int64) { - now := util.MakeTimestamp() + now := util.MakeTimestamp() / 1000 m.Lock() m.shares[now] += diff m.Unlock() } -func (m *Miner) hashrate(hashrateWindow time.Duration) float64 { - now := util.MakeTimestamp() +func (m *Miner) hashrate(estimationWindow time.Duration) float64 { + now := util.MakeTimestamp() / 1000 totalShares := int64(0) - window := int64(hashrateWindow / time.Millisecond) + window := int64(estimationWindow / time.Second) boundary := now - m.startedAt if boundary > window { @@ -122,7 +123,7 @@ func (m *Miner) hashrate(hashrateWindow time.Duration) float64 { m.Lock() for k, v := range m.shares { - if k < now-86400000 { + if k < now-86400 { delete(m.shares, k) } else if k >= now-window { totalShares += v @@ -171,8 +172,12 @@ func (m *Miner) processShare(s *StratumServer, e *Endpoint, job *Job, t *BlockTe } hashDiff := util.GetHashDifficulty(hashBytes).Int64() // FIXME: Will return max int64 value if overflows - atomic.AddInt64(&s.roundShares, hashDiff) + atomic.AddInt64(&s.roundShares, e.config.Difficulty) + atomic.AddUint64(&m.validShares, 1) + m.storeShare(e.config.Difficulty) + block := hashDiff >= t.Difficulty + if block { _, err := s.rpc.SubmitBlock(hex.EncodeToString(shareBuff)) if err != nil {