-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_getwork.go
149 lines (118 loc) · 2.63 KB
/
handle_getwork.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/hex"
"strconv"
"strings"
"time"
"xelis-mining-proxy/log"
"xelis-mining-proxy/xelisutil"
"github.com/xelis-project/xelis-go-sdk/getwork"
)
// Getwork client
var clGw *getwork.Getwork
var sharesToPool chan xelisutil.PacketC2S_Submit
func getworkClientHandler() {
func() {
prefix := strings.Split(Cfg.PoolUrl, ":")[0]
if prefix != "ws" && prefix != "wss" {
Cfg.PoolUrl = "ws://" + Cfg.PoolUrl
}
}()
log.Debug("getwork pool url", Cfg.PoolUrl)
for {
log.Info("Starting a new connection to the pool")
sharesToPool = make(chan xelisutil.PacketC2S_Submit, 1)
var err error
clGw, err = getwork.NewGetwork(Cfg.PoolUrl+"/getwork", Cfg.WalletAddress, "xelis-mining-proxy v"+VERSION)
if err != nil {
log.Err(err)
time.Sleep(time.Second)
continue
}
go recvSharesGw(clGw)
go readAcceptGw()
go readRejectGw()
readjobsGw(clGw)
// close(sharesToPool)
log.Debug("pool connection closed, starting a new one")
time.Sleep(time.Second)
}
}
func recvSharesGw(clGw *getwork.Getwork) {
log.Debug("recvShares started")
for {
share, ok := <-sharesToPool
if !ok {
log.Warn("sharesToPool chan closed")
clGw.Close()
return
}
log.Info("Share found, submitting to pool")
bm := share.BlockMiner
log.Debugf("%x", bm)
log.Debug(bm.String())
err := clGw.SubmitBlock(share.BlockMiner.String())
if err != nil {
log.Err("failed to submit share to pool:", err)
clGw.Close()
return
}
}
}
func readjobsGw(clGw *getwork.Getwork) {
for {
job, ok := <-clGw.Job
if !ok {
return
}
tmpl, err := hex.DecodeString(job.Template)
if err != nil {
log.Err(err)
clGw.Close()
return
}
diff, err := strconv.ParseUint(job.Difficulty, 10, 64)
if err != nil {
log.Err(err)
clGw.Close()
return
}
if len(tmpl) != xelisutil.BLOCKMINER_LENGTH {
log.Errf("template %x length is invalid", tmpl)
clGw.Close()
return
}
log.Debug("new job from GetWork")
bm := xelisutil.BlockMiner(tmpl)
mutCurJob.Lock()
curJob = Job{
Blob: bm,
Diff: diff,
Target: xelisutil.GetTargetBytes(diff),
}
mutCurJob.Unlock()
log.Infof("new job with difficulty %d", diff)
log.Debugf("new job: diff %d, blob %x", diff, tmpl)
log.Debugf("blob public key %x", xelisutil.BlockMiner(tmpl).GetPublickey())
go sendJobToWebsocket(diff, tmpl)
go stratumServer.sendJobs(diff, bm)
}
}
func readAcceptGw() {
for {
accept, ok := <-clGw.AcceptedBlock
if !ok {
return
}
log.Info("share accepted:", accept)
}
}
func readRejectGw() {
for {
reject, ok := <-clGw.RejectedBlock
if !ok {
return
}
log.Err("share rejected:", reject)
}
}