Browse Source

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
master
yuriy0803 3 years ago
parent
commit
6eca90d531
  1. 12
      proxy/blocks.go
  2. 71
      proxy/proxy.go

12
proxy/blocks.go

@ -12,7 +12,7 @@ import (
"github.com/yuriy0803/open-etc-pool-friends/util" "github.com/yuriy0803/open-etc-pool-friends/util"
) )
const maxBacklog = 3 const maxBacklog = 10
type heightDiffPair struct { type heightDiffPair struct {
diff *big.Int diff *big.Int
@ -59,9 +59,13 @@ func (s *ProxyServer) fetchBlockTemplate() {
return return
} }
// No need to update, we have fresh job // No need to update, we have fresh job
if t != nil && t.Header == reply[0] { if t != nil {
return if t.Header == reply[0] {
} return
}
if _, ok := t.headers[reply[0]]; ok {
return
}
pendingReply.Difficulty = util.ToHex(s.config.Proxy.Difficulty) pendingReply.Difficulty = util.ToHex(s.config.Proxy.Difficulty)

71
proxy/proxy.go

@ -175,6 +175,7 @@ func (s *ProxyServer) Start() {
r := mux.NewRouter() 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}}/{id:[0-9a-zA-Z-_]{1,200}}", s)
r.Handle("/{login:0x[0-9a-fA-F]{40}}", s) r.Handle("/{login:0x[0-9a-fA-F]{40}}", s)
r.HandleFunc("/etc", s.MiningNotify)
srv := &http.Server{ srv := &http.Server{
Addr: s.config.Proxy.Listen, Addr: s.config.Proxy.Listen,
Handler: r, Handler: r,
@ -361,3 +362,73 @@ func (s *ProxyServer) isSick() bool {
func (s *ProxyServer) markOk() { func (s *ProxyServer) markOk() {
atomic.StoreInt64(&s.failsCount, 0) 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()
}
}

Loading…
Cancel
Save