Skip to content

Commit

Permalink
[bugFix] hashrate calculation and payment export
Browse files Browse the repository at this point in the history
  • Loading branch information
Command M committed Sep 21, 2019
1 parent f9c7570 commit f943c67
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
22 changes: 17 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (db *database) putDayShare(login string, diff int64) {
func (db *database) putTmpShare(login, agent string, diff int64) {
z := redis.Z{
Score: float64(time.Now().UnixNano()),
Member: strconv.FormatInt(diff, 10) + ":" + strconv.FormatInt(time.Now().Unix(), 10),
Member: strconv.FormatInt(diff, 10) + ":" + strconv.FormatInt(time.Now().UnixNano(), 16),
}
_, err := db.client.ZAdd("tmp:"+login+":"+agent, z).Result()
if err != nil {
Expand Down Expand Up @@ -161,11 +161,12 @@ func (db *database) setMinerAgentStatus(login, agent string, diff int64, status

strUnixNano, _ := db.client.HGet("user:"+login, "lastShare").Result()
lastShareTime, _ := strconv.ParseInt(strUnixNano, 10, 64)
// H = D / ΔT
if time.Now().UnixNano() == lastShareTime {
realtimeHashrate := diff * 1e9 / (1) / int64(db.conf.Node.BlockTime)
realtimeHashrate := float64(diff*1e9) / (1)
status["realtime_hashrate"] = realtimeHashrate
} else {
realtimeHashrate := diff * 1e9 / (time.Now().UnixNano() - lastShareTime) / int64(db.conf.Node.BlockTime)
realtimeHashrate := float64(diff*1e9) / float64(time.Now().UnixNano()-lastShareTime)
status["realtime_hashrate"] = realtimeHashrate
}

Expand All @@ -185,7 +186,7 @@ func (db *database) setMinerAgentStatus(login, agent string, diff int64, status
}
sum = sum + int64(i)
}
averageHashrate := sum * 1e9 / (10 * time.Minute.Nanoseconds()) / int64(db.conf.Node.BlockTime)
averageHashrate := float64(sum*1e9) / float64(10*time.Minute.Nanoseconds())
status["average_hashrate"] = averageHashrate

agents := make(map[string]interface{})
Expand Down Expand Up @@ -246,7 +247,7 @@ func (db *database) calcRevenueToday(totalRevenue uint64) {
for miner, shares := range allMinersSharesTable {
allMinersRevenueTable[miner] = shares / totalShare * totalRevenue

payment := db.client.HGet("user:"+miner, "payment")
payment, _ := db.client.HGet("user:"+miner, "payment").Result()
_, _ = fmt.Fprintf(f, "%s %d %s\n", miner, allMinersSharesTable[miner], payment)

date, _ := strconv.ParseFloat(time.Now().Format("20190102"), 10)
Expand Down
50 changes: 50 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"testing"
)

func TestPutTmpShare(t *testing.T) {
//conf := parseConfig()
//db := initDB(conf)
//login := "123"
//agent := "321"
//ch := time.After(10 * time.Minute)
//for {
// select {
// case <-ch:
// goto SUM
// default:
// time.Sleep(10*time.Second)
// db.putTmpShare(login, agent, 5000)
// }
//}
//
//SUM:
//
//db.client.ZRemRangeByScore("tmp:"+login+":"+agent, "-inf", fmt.Sprint("(", time.Now().UnixNano()-10*time.Minute.Nanoseconds()))
//l, err := db.client.ZRangeWithScores("tmp:"+login+":"+agent, 0, -1).Result()
//if err != nil {
// logger.Error(err)
//}
//
//logger.Info(l)
//
//var sum int64
//for _, z := range l {
// str := z.Member.(string)
// li := strings.Split(str, ":")
// logger.Info(li)
// i, err := strconv.Atoi(li[0])
// if err != nil {
// logger.Error(err)
// }
// sum = sum + int64(i)
//}
//logger.Info(sum)
//logger.Info(time.Minute.Seconds())
//logger.Info(int64(db.conf.Node.BlockTime))
////H = D / ΔT
//averageHashrate := sum / (10 * int64(time.Minute.Seconds()))
//logger.Info(averageHashrate)
}

0 comments on commit f943c67

Please sign in to comment.