have submitWork return miner & pool stats

This commit is contained in:
cryptonote-social 2020-08-18 20:38:12 -07:00
parent d8a9a5cf73
commit 89ee6edcd2
3 changed files with 77 additions and 11 deletions

View File

@ -372,7 +372,7 @@ func MiningLoop(jobChan <-chan *client.MultiClientJob, done chan<- bool) {
}
crylog.Info("Current job:", job.JobID, "Difficulty:", blockchain.TargetToDifficulty(job.Target))
case <-time.After(15 * time.Second):
case <-time.After(60 * time.Second):
break
}
@ -620,7 +620,6 @@ func goMine(job client.MultiClientJob, thread int) {
break
}
stats.TallyHashes(res)
updatePoolStats(true)
crylog.Info("Share found by thread:", thread, "Target:", blockchain.HashDifficulty(hash))
fnonce := hex.EncodeToString(nonce)
// submit in a separate thread so we can resume hashing immediately.
@ -645,6 +644,15 @@ func goMine(job client.MultiClientJob, thread int) {
return
}
stats.ShareAccepted(diffTarget)
swr := resp.Result
if swr != nil {
crylog.Info("Got submit work result:", swr)
if swr.PoolMargin > 0.0 {
stats.RefreshPoolStats2(swr)
} else {
updatePoolStats(true)
}
}
}(fnonce, job.JobID)
}
}

View File

@ -2,6 +2,7 @@ package stats
import (
// "github.com/cryptonote-social/csminer/crylog"
"github.com/cryptonote-social/csminer/stratum/client"
"encoding/json"
"io/ioutil"
@ -161,6 +162,42 @@ func GetSnapshot(isMining bool) (s *Snapshot, secondsSinceReset float64, seconds
return r, time.Now().Sub(recentStatsResetTime).Seconds(), elapsedRecent
}
func RefreshPoolStats2(swr *client.SubmitWorkResult) {
diff := float64(swr.NetworkDifficulty)
hr := float64(swr.PPROPHashrate)
var ttreward string
if hr > 0.0 {
ttr := (diff*(1.0+swr.PoolMargin) - (swr.PPROPProgress * diff)) / hr / 3600.0 / 24.0
if ttr > 0.0 {
if ttr < 1.0 {
ttr *= 24.0
if ttr < 1.0 {
ttr *= 60.0
ttreward = strconv.FormatFloat(ttr, 'f', 2, 64) + " min"
} else {
ttreward = strconv.FormatFloat(ttr, 'f', 2, 64) + " hrs"
}
} else {
ttreward = strconv.FormatFloat(ttr, 'f', 2, 64) + " days"
}
} else if ttr < 0.0 {
ttreward = "overdue"
}
}
mutex.Lock()
lastPoolUpdateTime = time.Now()
lifetimeHashes = swr.LifetimeHashes
paid = swr.Paid
owed = swr.Owed
if swr.NextBlockReward > 0.0 && swr.Progress > 0.0 {
progress := swr.Progress / (1.0 + swr.PoolMargin)
accumulated = swr.NextBlockReward * progress
}
timeToReward = ttreward
mutex.Unlock()
}
func RefreshPoolStats(username string) error {
uri := "https://cryptonote.social/json/WorkerStats"
sbody := "{\"Coin\": \"xmr\", \"Worker\": \"" + username + "\"}\n"

View File

@ -56,6 +56,36 @@ type MultiClientJob struct {
ConnNonce uint32 `json:"nonce"`
}
type SubmitWorkResult struct {
Status string `json:"status"`
Progress float64 // progress of this user
LifetimeHashes int64 // hashes from this user over its lifetime
Paid float64 // Total crypto paid to this user over its lifetime.
Owed float64 // Crypto owed to this user but not paid out yet.
Donate float64 // Fraction of earnings donated to the pool by this user.
PPROPHashrate int64 // hashrate of the pprop collective
PPROPProgress float64 // raw progress of the pprop collective
NextBlockReward float64 // reward of the next banked block
NetworkDifficulty int64 // difficulty, possibly smoothed over the last several blocks
// TODO: These pool config values rarely change, so we should fetch these in some other way
// instead of returning them from each SubmitWork call.
PoolMargin float64
PoolFee float64
}
type SubmitWorkResponse struct {
ID uint64 `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Job *MultiClientJob `json:"params"`
Result *SubmitWorkResult
Error map[string]interface{} `json:"error"`
}
type Client struct {
address string
conn net.Conn
@ -268,15 +298,6 @@ func (cl *Client) Close() {
cl.conn.Close()
}
type SubmitWorkResponse struct {
ID uint64 `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Job *MultiClientJob `json:"params"`
Result map[string]interface{} `json:"result"`
Error map[string]interface{} `json:"error"`
}
// DispatchJobs will forward incoming jobs to the JobChannel until error is received or the
// connection is closed. Client will be in not-alive state on return.
func dispatchJobs(conn net.Conn, jobChan chan<- *MultiClientJob, firstJob *MultiClientJob, responseChan chan<- *SubmitWorkResponse) {