From 6eca90d53184792e106f85d6324f2f44f97bfa82 Mon Sep 17 00:00:00 2001 From: yuriy0803 <68668177+yuriy0803@users.noreply.github.com> Date: Fri, 12 May 2023 13:01:22 +0200 Subject: [PATCH] support geth --miner.notify option to push mining jobs ./geth --miner.notify http://127.0.0.1:8888/etc api.json https://github.com/yuriy0803/open-etc-pool-friends/blob/master/api.json#L12 --- proxy/blocks.go | 12 ++++++--- proxy/proxy.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/proxy/blocks.go b/proxy/blocks.go index 936eedd..5ebea55 100644 --- a/proxy/blocks.go +++ b/proxy/blocks.go @@ -12,7 +12,7 @@ import ( "github.com/yuriy0803/open-etc-pool-friends/util" ) -const maxBacklog = 3 +const maxBacklog = 10 type heightDiffPair struct { diff *big.Int @@ -59,9 +59,13 @@ func (s *ProxyServer) fetchBlockTemplate() { return } // No need to update, we have fresh job - if t != nil && t.Header == reply[0] { - return - } + if t != nil { + if t.Header == reply[0] { + return + } + if _, ok := t.headers[reply[0]]; ok { + return + } pendingReply.Difficulty = util.ToHex(s.config.Proxy.Difficulty) diff --git a/proxy/proxy.go b/proxy/proxy.go index e1ef082..4849ad3 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -175,6 +175,7 @@ func (s *ProxyServer) Start() { r := mux.NewRouter() r.Handle("/{login:0x[0-9a-fA-F]{40}}/{id:[0-9a-zA-Z-_]{1,200}}", s) r.Handle("/{login:0x[0-9a-fA-F]{40}}", s) + r.HandleFunc("/etc", s.MiningNotify) srv := &http.Server{ Addr: s.config.Proxy.Listen, Handler: r, @@ -361,3 +362,73 @@ func (s *ProxyServer) isSick() bool { func (s *ProxyServer) markOk() { atomic.StoreInt64(&s.failsCount, 0) } + +func (s *ProxyServer) MiningNotify(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "405 method not allowed.", http.StatusMethodNotAllowed) + return + } + + body := make([]byte, r.ContentLength) + r.Body.Read(body) + + var reply []string + err := json.Unmarshal(body, &reply) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + w.WriteHeader(http.StatusOK) + + t := s.currentBlockTemplate() + // No need to update, we have fresh job + if t != nil { + if t.Header == reply[0] { + return + } + if _, ok := t.headers[reply[0]]; ok { + return + } + } + diff := util.TargetHexToDiff(reply[2]) + height, err := strconv.ParseUint(strings.Replace(reply[3], "0x", "", -1), 16, 64) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + pendingReply := &rpc.GetBlockReplyPart{ + Difficulty: util.ToHex(s.config.Proxy.Difficulty), + Number: reply[3], + } + + newTemplate := BlockTemplate{ + Header: reply[0], + Seed: reply[1], + Target: reply[2], + Height: height, + Difficulty: diff, + GetPendingBlockCache: pendingReply, + headers: make(map[string]heightDiffPair), + } + // Copy job backlog and add current one + newTemplate.headers[reply[0]] = heightDiffPair{ + diff: diff, + height: height, + } + if t != nil { + for k, v := range t.headers { + if v.height > height-maxBacklog { + newTemplate.headers[k] = v + } + } + } + s.blockTemplate.Store(&newTemplate) + + log.Printf("New block notified at height %d / %s / %d", height, reply[0][0:10], diff) + + // Stratum + if s.config.Proxy.Stratum.Enabled { + go s.broadcastNewJobs() + } +}