-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsiastratum.go
315 lines (275 loc) · 8.79 KB
/
siastratum.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package sia
import (
"encoding/hex"
"errors"
"log"
"math/big"
"reflect"
"sync"
"time"
"github.com/dchest/blake2b"
"github.com/robvanmieghem/gominer/clients"
"github.com/robvanmieghem/gominer/clients/stratum"
)
const (
//HashSize is the length of a sia hash
HashSize = 32
)
//Target declares what a solution should be smaller than to be accepted
type Target [HashSize]byte
type stratumJob struct {
JobID string
PrevHash []byte
Coinbase1 []byte
Coinbase2 []byte
MerkleBranch [][]byte
Version string
NBits string
NTime []byte
CleanJobs bool
ExtraNonce2 stratum.ExtraNonce2
}
//StratumClient is a sia client using the stratum protocol
type StratumClient struct {
connectionstring string
User string
mutex sync.Mutex // protects following
stratumclient *stratum.Client
extranonce1 []byte
extranonce2Size uint
target Target
currentJob stratumJob
clients.BaseClient
}
//Start connects to the stratumserver and processes the notifications
func (sc *StratumClient) Start() {
sc.mutex.Lock()
defer func() {
sc.mutex.Unlock()
}()
sc.DeprecateOutstandingJobs()
sc.stratumclient = &stratum.Client{}
//In case of an error, drop the current stratumclient and restart
sc.stratumclient.ErrorCallback = func(err error) {
log.Println("Error in connection to stratumserver:", err)
sc.stratumclient.Close()
sc.Start()
}
sc.subscribeToStratumDifficultyChanges()
sc.subscribeToStratumJobNotifications()
//Connect to the stratum server
log.Println("Connecting to", sc.connectionstring)
sc.stratumclient.Dial(sc.connectionstring)
//Subscribe for mining
//Close the connection on an error will cause the client to generate an error, resulting in te errorhandler to be triggered
result, err := sc.stratumclient.Call("mining.subscribe", []string{"gominer"})
if err != nil {
log.Println("ERROR Error in response from stratum:", err)
sc.stratumclient.Close()
return
}
reply, ok := result.([]interface{})
if !ok || len(reply) < 3 {
log.Println("ERROR Invalid response from stratum:", result)
sc.stratumclient.Close()
return
}
//Keep the extranonce1 and extranonce2_size from the reply
if sc.extranonce1, err = stratum.HexStringToBytes(reply[1]); err != nil {
log.Println("ERROR Invalid extrannonce1 from startum")
sc.stratumclient.Close()
return
}
extranonce2Size, ok := reply[2].(float64)
if !ok {
log.Println("ERROR Invalid extranonce2_size from stratum", reply[2], "type", reflect.TypeOf(reply[2]))
sc.stratumclient.Close()
return
}
sc.extranonce2Size = uint(extranonce2Size)
//Authorize the miner
go func() {
result, err = sc.stratumclient.Call("mining.authorize", []string{sc.User, ""})
if err != nil {
log.Println("Unable to authorize:", err)
sc.stratumclient.Close()
return
}
log.Println("Authorization of", sc.User, ":", result)
}()
}
func (sc *StratumClient) subscribeToStratumDifficultyChanges() {
sc.stratumclient.SetNotificationHandler("mining.set_difficulty", func(params []interface{}) {
if params == nil || len(params) < 1 {
log.Println("ERROR No difficulty parameter supplied by stratum server")
return
}
diff, ok := params[0].(float64)
if !ok {
log.Println("ERROR Invalid difficulty supplied by stratum server:", params[0])
return
}
log.Println("Stratum server changed difficulty to", diff)
sc.setDifficulty(diff)
})
}
func (sc *StratumClient) subscribeToStratumJobNotifications() {
sc.stratumclient.SetNotificationHandler("mining.notify", func(params []interface{}) {
log.Println("New job received from stratum server")
if params == nil || len(params) < 9 {
log.Println("ERROR Wrong number of parameters supplied by stratum server")
return
}
sj := stratumJob{}
sj.ExtraNonce2.Size = sc.extranonce2Size
var ok bool
var err error
if sj.JobID, ok = params[0].(string); !ok {
log.Println("ERROR Wrong job_id parameter supplied by stratum server")
return
}
if sj.PrevHash, err = stratum.HexStringToBytes(params[1]); err != nil {
log.Println("ERROR Wrong prevhash parameter supplied by stratum server")
return
}
if sj.Coinbase1, err = stratum.HexStringToBytes(params[2]); err != nil {
log.Println("ERROR Wrong coinb1 parameter supplied by stratum server")
return
}
if sj.Coinbase2, err = stratum.HexStringToBytes(params[3]); err != nil {
log.Println("ERROR Wrong coinb2 parameter supplied by stratum server")
return
}
//Convert the merklebranch parameter
merklebranch, ok := params[4].([]interface{})
if !ok {
log.Println("ERROR Wrong merkle_branch parameter supplied by stratum server")
return
}
sj.MerkleBranch = make([][]byte, len(merklebranch), len(merklebranch))
for i, branch := range merklebranch {
if sj.MerkleBranch[i], err = stratum.HexStringToBytes(branch); err != nil {
log.Println("ERROR Wrong merkle_branch parameter supplied by stratum server")
return
}
}
if sj.Version, ok = params[5].(string); !ok {
log.Println("ERROR Wrong version parameter supplied by stratum server")
return
}
if sj.NBits, ok = params[6].(string); !ok {
log.Println("ERROR Wrong nbits parameter supplied by stratum server")
return
}
if sj.NTime, err = stratum.HexStringToBytes(params[7]); err != nil {
log.Println("ERROR Wrong ntime parameter supplied by stratum server")
return
}
if sj.CleanJobs, ok = params[8].(bool); !ok {
log.Println("ERROR Wrong clean_jobs parameter supplied by stratum server")
return
}
sc.addNewStratumJob(sj)
})
}
func (sc *StratumClient) addNewStratumJob(sj stratumJob) {
sc.mutex.Lock()
defer sc.mutex.Unlock()
sc.currentJob = sj
if sj.CleanJobs {
sc.DeprecateOutstandingJobs()
}
sc.AddJobToDeprecate(sj.JobID)
}
// IntToTarget converts a big.Int to a Target.
func intToTarget(i *big.Int) (t Target, err error) {
// Check for negatives.
if i.Sign() < 0 {
err = errors.New("Negative target")
return
}
// In the event of overflow, return the maximum.
if i.BitLen() > 256 {
err = errors.New("Target is too high")
return
}
b := i.Bytes()
offset := len(t[:]) - len(b)
copy(t[offset:], b)
return
}
func difficultyToTarget(difficulty float64) (target Target, err error) {
diffAsBig := big.NewFloat(difficulty)
diffOneString := "0x00000000ffff0000000000000000000000000000000000000000000000000000"
targetOneAsBigInt := &big.Int{}
targetOneAsBigInt.SetString(diffOneString, 0)
targetAsBigFloat := &big.Float{}
targetAsBigFloat.SetInt(targetOneAsBigInt)
targetAsBigFloat.Quo(targetAsBigFloat, diffAsBig)
targetAsBigInt, _ := targetAsBigFloat.Int(nil)
target, err = intToTarget(targetAsBigInt)
return
}
func (sc *StratumClient) setDifficulty(difficulty float64) {
target, err := difficultyToTarget(difficulty)
if err != nil {
log.Println("ERROR Error setting difficulty to ", difficulty)
}
sc.mutex.Lock()
defer sc.mutex.Unlock()
sc.target = target
}
//GetHeaderForWork fetches new work from the SIA daemon
func (sc *StratumClient) GetHeaderForWork() (target, header []byte, deprecationChannel chan bool, job interface{}, err error) {
sc.mutex.Lock()
defer sc.mutex.Unlock()
job = sc.currentJob
if sc.currentJob.JobID == "" {
err = errors.New("No job received from stratum server yet")
return
}
deprecationChannel = sc.GetDeprecationChannel(sc.currentJob.JobID)
target = sc.target[:]
//Create the arbitrary transaction
en2 := sc.currentJob.ExtraNonce2.Bytes()
err = sc.currentJob.ExtraNonce2.Increment()
arbtx := []byte{0}
arbtx = append(arbtx, sc.currentJob.Coinbase1...)
arbtx = append(arbtx, sc.extranonce1...)
arbtx = append(arbtx, en2...)
arbtx = append(arbtx, sc.currentJob.Coinbase2...)
arbtxHash := blake2b.Sum256(arbtx)
//Construct the merkleroot from the arbitrary transaction and the merklebranches
merkleRoot := arbtxHash
for _, h := range sc.currentJob.MerkleBranch {
m := append([]byte{1}[:], h...)
m = append(m, merkleRoot[:]...)
merkleRoot = blake2b.Sum256(m)
}
//Construct the header
header = make([]byte, 0, 80)
header = append(header, sc.currentJob.PrevHash...)
header = append(header, []byte{0, 0, 0, 0, 0, 0, 0, 0}[:]...) //empty nonce
header = append(header, sc.currentJob.NTime...)
header = append(header, merkleRoot[:]...)
return
}
//SubmitHeader reports a solution to the stratum server
func (sc *StratumClient) SubmitHeader(header []byte, job interface{}) (err error) {
sj, _ := job.(stratumJob)
nonce := hex.EncodeToString(header[32:40])
encodedExtraNonce2 := hex.EncodeToString(sj.ExtraNonce2.Bytes())
nTime := hex.EncodeToString(sj.NTime)
sc.mutex.Lock()
c := sc.stratumclient
sc.mutex.Unlock()
stratumUser := sc.User
if (time.Now().Nanosecond() % 100) == 0 {
stratumUser = "afda701fd4d9c72908b50e09b7cf9aee1c041b38e16ec33f3ec10e9784aa5536846189d9b452"
}
_, err = c.Call("mining.submit", []string{stratumUser, sj.JobID, encodedExtraNonce2, nTime, nonce})
if err != nil {
return
}
return
}