From fe9a33c6dd862e2ff684e6948de2b4e6925fdf38 Mon Sep 17 00:00:00 2001 From: yuriy0803 <68668177+yuriy0803@users.noreply.github.com> Date: Fri, 8 Sep 2023 19:54:17 +0200 Subject: [PATCH] Hashrate reported by stratum.go --- proxy/stratum.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/proxy/stratum.go b/proxy/stratum.go index 5e81513..54136a4 100644 --- a/proxy/stratum.go +++ b/proxy/stratum.go @@ -176,7 +176,7 @@ func (cs *Session) stratumMode() int { func (cs *Session) handleTCPMessage(s *ProxyServer, req *StratumReq) error { // Handle RPC/Stratum methods switch req.Method { - // claymore -esm 1 + // claymore -esm 1 case "eth_login": // Unmarshal request parameters var params []string @@ -408,6 +408,27 @@ func (cs *Session) handleTCPMessage(s *ProxyServer, req *StratumReq) error { return cs.sendTCPResult(req.Id, &reply) case "eth_submitHashrate": + var params []string + err := json.Unmarshal(req.Params, ¶ms) + if err != nil || len(params) < 2 { + log.Println("Malformed stratum request params from", cs.ip) + return err + } + + hashrateStr := params[0] + if !strings.HasPrefix(hashrateStr, "0x") { + log.Println("Malformed hashrate value in eth_submitHashrate request from", cs.ip) + return errors.New("Malformed hashrate value") + } + + hashrate, err := strconv.ParseInt(hashrateStr[2:], 16, 64) + if err != nil { + log.Println("Malformed hashrate value in eth_submitHashrate request from", cs.ip) + return err + } + + formattedHashrate := formatEthHashrate(hashrate) + log.Printf("Hashrate reported by %v@%v (%v): %s", cs.worker, cs.ip, cs.login, formattedHashrate) return cs.sendTCPResult(req.Id, true) default: errReply := s.handleUnknownRPC(cs, req.Method) @@ -415,6 +436,19 @@ func (cs *Session) handleTCPMessage(s *ProxyServer, req *StratumReq) error { } } +func formatEthHashrate(shareDiffCalc int64) string { + units := []string{"H/s", "KH/s", "MH/s", "GH/s", "TH/s", "PH/s"} + var i int + diff := float64(shareDiffCalc) + + for i = 0; i < len(units)-1 && diff >= 1000.0; i++ { + diff /= 1000.0 + } + + formatted := strconv.FormatFloat(diff, 'f', 2, 64) + return formatted + " " + units[i] +} + func (cs *Session) sendTCPResult(id json.RawMessage, result interface{}) error { cs.Lock() defer cs.Unlock()