-
Notifications
You must be signed in to change notification settings - Fork 104
/
DownSessionETH.go
589 lines (499 loc) · 15.2 KB
/
DownSessionETH.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package main
import (
"bufio"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"strings"
"github.com/golang/glog"
)
type DownSessionETH struct {
id string // 打印日志用的连接标识符
manager *SessionManager // 会话管理器
upSession EventInterface // 所属的服务器会话
sessionID uint16 // 会话ID
clientConn net.Conn // 到矿机的TCP连接
clientReader *bufio.Reader // 读取矿机发送的内容
readLoopRunning bool // TCP读循环是否在运行
jobDiff uint64 // 挖矿任务难度
extraNonce uint32 // 由矿池指定
hasExtraNonce bool // 是否获得了ExtraNonce
isFirstJob bool // 是否为第一个任务
jobIDQueue *JobIDQueueETH // job id 队列
ethGetWorkID interface{} // 矿机eth_getWork请求的id
stat AuthorizeStat // 认证状态
protocol StratumProtocol // 挖矿协议
rpcVersion int // JSON-RPC版本
clientAgent string // 挖矿软件名称
fullName string // 完整的矿工名
subAccountName string // 子账户名部分
workerName string // 矿机名部分
eventLoopRunning bool // 消息循环是否在运行
eventChannel chan interface{} // 消息通道
}
// NewDownSessionETH 创建一个新的 Stratum 会话
func NewDownSessionETH(manager *SessionManager, clientConn net.Conn, sessionID uint16) (down *DownSessionETH) {
down = new(DownSessionETH)
down.manager = manager
down.sessionID = sessionID
down.clientConn = clientConn
down.clientReader = bufio.NewReader(clientConn)
down.stat = StatConnected
down.eventChannel = make(chan interface{}, manager.config.Advanced.MessageQueueSize.MinerSession)
down.jobIDQueue = NewJobqueueETH(EthereumJobIDQueueSize)
down.ethGetWorkID = 0
down.id = fmt.Sprintf("miner#%d (%s) ", down.sessionID, down.clientConn.RemoteAddr())
glog.Info(down.id, "miner connected")
return
}
func (down *DownSessionETH) SessionID() uint16 {
return down.sessionID
}
func (down *DownSessionETH) SubAccountName() string {
return down.subAccountName
}
func (down *DownSessionETH) Stat() AuthorizeStat {
return down.stat
}
func (down *DownSessionETH) Init() {
go down.handleRequest()
down.handleEvent()
}
func (down *DownSessionETH) Run() {
down.handleEvent()
}
func (down *DownSessionETH) close() {
if down.upSession != nil && down.stat != StatExit {
go down.upSession.SendEvent(EventDownSessionBroken{down.sessionID})
}
down.eventLoopRunning = false
down.stat = StatDisconnected
down.clientConn.Close()
// release down id
down.manager.sessionIDManager.FreeSessionID(down.sessionID)
}
func (down *DownSessionETH) writeJSONRequest(jsonData *JSONRPCRequest) (int, error) {
bytes, err := jsonData.ToJSONBytesLineWithVersion(down.rpcVersion)
if err != nil {
return 0, err
}
if glog.V(10) {
glog.Info(down.id, "writeJSONRequest: ", string(bytes))
}
return down.clientConn.Write(bytes)
}
func (down *DownSessionETH) writeJSONResponse(jsonData *JSONRPCResponse) (int, error) {
bytes, err := jsonData.ToJSONBytesLineWithVersion(down.rpcVersion)
if err != nil {
return 0, err
}
if glog.V(12) {
glog.Info(down.id, "writeJSONResponse: ", string(bytes))
}
return down.clientConn.Write(bytes)
}
func (down *DownSessionETH) stratumHandleRequest(request *JSONRPCLineETH, requestJSON []byte) (result interface{}, err *StratumError) {
switch request.Method {
case "mining.subscribe":
if down.stat != StatConnected {
err = StratumErrDuplicateSubscribed
return
}
result, err = down.parseSubscribeRequest(request)
if err == nil {
down.stat = StatSubScribed
}
return
case "eth_submitLogin":
down.protocol = ProtocolETHProxy
down.rpcVersion = 2
down.stat = StatSubScribed
fallthrough
case "mining.authorize":
if down.stat != StatSubScribed {
err = StratumErrNeedSubscribed
return
}
result, err = down.parseAuthorizeRequest(request)
if err == nil {
down.stat = StatAuthorized
// 让 Init() 函数返回
down.eventLoopRunning = false
down.id += fmt.Sprintf("<%s> ", down.fullName)
glog.Info(down.id, "miner authorized")
}
return
case "eth_submitWork":
fallthrough
case "mining.submit":
result, err = down.parseMiningSubmit(request)
if err != nil {
glog.Warning(down.id, "stratum error: ", err, "; ", string(requestJSON))
}
return
case "mining.extranonce.subscribe":
fallthrough
case "eth_submitHashrate":
result = true
return
case "eth_getWork":
result, err = down.parseEthGetWork(request)
return
// ignore unimplemented methods
case "mining.multi_version":
fallthrough
case "mining.suggest_difficulty":
// If no response, the miner may wait indefinitely
err = StratumErrIllegalParams
return
default:
glog.Warning(down.id, "unknown request: ", string(requestJSON))
// If no response, the miner may wait indefinitely
err = StratumErrIllegalParams
return
}
}
func (down *DownSessionETH) parseEthGetWork(request *JSONRPCLineETH) (result interface{}, err *StratumError) {
down.ethGetWorkID = request.ID
return
}
func (down *DownSessionETH) parseMiningSubmit(request *JSONRPCLineETH) (result interface{}, err *StratumError) {
if down.stat != StatAuthorized {
err = StratumErrNeedAuthorized
// there must be something wrong, send reconnect command
down.sendReconnectRequest()
return
}
if down.upSession == nil {
err = StratumErrJobNotFound
return
}
var powHash, mixHash, nonce string
switch down.protocol {
case ProtocolLegacyStratum:
if len(request.Params) < 5 {
err = StratumErrTooFewParams
return
}
powHash, _ = request.Params[3].(string)
if len(powHash) < 1 {
powHash, _ = request.Params[1].(string)
}
nonce, _ = request.Params[2].(string)
mixHash, _ = request.Params[4].(string)
case ProtocolETHProxy:
if len(request.Params) < 3 {
err = StratumErrTooFewParams
return
}
nonce, _ = request.Params[0].(string)
powHash, _ = request.Params[1].(string)
mixHash, _ = request.Params[2].(string)
case ProtocolEthereumStratum:
if len(request.Params) < 3 {
err = StratumErrTooFewParams
return
}
powHash, _ = request.Params[1].(string)
nonce, _ = request.Params[2].(string)
}
var msg ExMessageSubmitShareETH
msg.SessionID = down.sessionID
powHash = HexRemovePrefix(powHash)
if IsFakeJobIDETH(powHash) {
msg.IsFakeJob = true
}
msg.JobID = down.jobIDQueue.Find(powHash)
if msg.JobID == nil {
err = StratumErrJobNotFound
return
}
if len(nonce) < 1 {
err = StratumErrIllegalParams
return
}
var convErr error
msg.Nonce, convErr = Hex2Uint64(nonce)
if convErr != nil {
err = StratumErrIllegalParams
return
}
if len(mixHash) > 0 {
msg.MixHash, _ = Hex2Bin(mixHash)
BinReverse(msg.MixHash) // btcpool使用小端字节序
}
go down.upSession.SendEvent(EventSubmitShareETH{request.ID, &msg})
return
}
func (down *DownSessionETH) sendReconnectRequest() {
var reconnect JSONRPCRequest
reconnect.Method = "client.reconnect"
reconnect.Params = JSONRPCArray{}
bytes, err := reconnect.ToJSONBytesLineWithVersion(down.rpcVersion)
if err != nil {
glog.Error(down.id, "failed to convert client.reconnect request to JSON: ", err.Error(), "; ", reconnect)
return
}
go down.SendEvent(EventSendBytes{bytes})
}
func (down *DownSessionETH) parseSubscribeRequest(request *JSONRPCLineETH) (result interface{}, err *StratumError) {
down.protocol = ProtocolLegacyStratum
down.rpcVersion = 1
result = true
if len(request.Params) >= 1 {
down.clientAgent, _ = request.Params[0].(string)
}
if len(request.Params) >= 2 {
// message example: {"id":1,"method":"mining.subscribe","params":["ethminer 0.15.0rc1","EthereumStratum/1.0.0"]}
protocol, ok := request.Params[1].(string)
// 判断是否为"EthereumStratum/xxx"
if ok && strings.HasPrefix(strings.ToLower(protocol), EthereumStratumPrefix) {
down.protocol = ProtocolEthereumStratum
sessionIDString := Uint16ToHex(down.sessionID)
// message example: {"id":1,"jsonrpc":"2.0","result":[["mining.notify","0001","EthereumStratum/1.0.0"],"0001"],"error":null}
// Add a padding nonce prefix. BTCAgent doesn't need a nonce prefix, but gminer has problems when it's missing.
result = JSONRPCArray{JSONRPCArray{"mining.notify", sessionIDString, EthereumStratumVersion}, "00"}
}
}
return
}
func (down *DownSessionETH) parseAuthorizeRequest(request *JSONRPCLineETH) (result interface{}, err *StratumError) {
if len(request.Params) < 1 {
err = StratumErrTooFewParams
return
}
fullWorkerName, ok := request.Params[0].(string)
if !ok {
err = StratumErrWorkerNameMustBeString
return
}
if len(request.Worker) > 0 {
fullWorkerName = fullWorkerName + "." + request.Worker
}
// 矿工名
down.fullName = FilterWorkerName(StripEthAddrFromFullName(fullWorkerName))
// 截取“.”之前的做为子账户名,“.”及之后的做矿机名
pos := strings.IndexByte(down.fullName, '.')
if pos >= 0 {
down.subAccountName = down.fullName[:pos]
down.workerName = down.fullName[pos+1:]
} else {
down.subAccountName = down.fullName
down.workerName = ""
}
if len(down.manager.config.FixedWorkerName) > 0 {
down.workerName = down.manager.config.FixedWorkerName
down.fullName = down.subAccountName + "." + down.workerName
} else if down.manager.config.UseIpAsWorkerName {
down.workerName = IPAsWorkerName(down.manager.config.IpWorkerNameFormat, down.clientConn.RemoteAddr().String())
down.fullName = down.subAccountName + "." + down.workerName
}
if down.manager.config.MultiUserMode {
if len(down.subAccountName) < 1 {
err = StratumErrSubAccountNameEmpty
return
}
} else {
down.subAccountName = ""
}
if down.workerName == "" {
down.workerName = down.fullName
if down.workerName == "" {
down.workerName = DefaultWorkerName
down.fullName = down.subAccountName + "." + down.workerName
}
}
// 获取矿机名成功
result = true
err = nil
return
}
func (down *DownSessionETH) setUpSession(e EventSetUpSession) {
down.hasExtraNonce = false
down.isFirstJob = true
down.upSession = e.Session
down.upSession.SendEvent(EventAddDownSession{down})
}
func (down *DownSessionETH) handleRequest() {
down.readLoopRunning = true
for down.readLoopRunning {
jsonBytes, err := down.clientReader.ReadBytes('\n')
if err != nil {
glog.Error(down.id, "failed to read request from miner: ", err.Error())
down.connBroken()
return
}
if glog.V(11) {
glog.Info(down.id, "handleRequest: ", string(jsonBytes), ", len=", len(jsonBytes), ", hex=", hex.EncodeToString(jsonBytes))
}
rpcData, err := NewJSONRPCLineETH(jsonBytes)
// ignore the json decode error
if err != nil {
glog.Warning(down.id, "failed to decode JSON from miner: ", err.Error(), "; ", string(jsonBytes), ", len=", len(jsonBytes), ", hex=", hex.EncodeToString(jsonBytes))
continue
}
down.SendEvent(EventRecvJSONRPCETH{rpcData, jsonBytes})
}
}
func (down *DownSessionETH) recvJSONRPC(e EventRecvJSONRPCETH) {
// stat will be changed in stratumHandleRequest
result, stratumErr := down.stratumHandleRequest(e.RPCData, e.JSONBytes)
// 两个均为空说明没有想要返回的响应
if result != nil || stratumErr != nil {
var response JSONRPCResponse
response.ID = e.RPCData.ID
response.Result = result
response.Error = stratumErr.ToJSONRPCArray(nil)
_, err := down.writeJSONResponse(&response)
if err != nil {
glog.Error(down.id, "failed to send response to miner: ", err.Error())
down.close()
return
}
}
}
func (down *DownSessionETH) sendJob(e EventStratumJobETH) {
// 还没有获得 extra nonce,不应该给矿机发送任务
if !down.hasExtraNonce {
return
}
powHash := e.Job.PoWHash(down.extraNonce)
seedHash := hex.EncodeToString(e.Job.SeedHash)
target := DiffToTargetETH(down.jobDiff)
height := e.Job.Height()
isClean := down.isFirstJob || e.Job.IsClean
down.isFirstJob = false
down.jobIDQueue.Add(powHash, e.Job.JobID)
var job interface{}
switch down.protocol {
case ProtocolLegacyStratum:
job = JSONRPCJobETH{nil, "mining.notify", JSONRPCArray{
powHash,
powHash,
seedHash,
target,
isClean,
}, height}
case ProtocolETHProxy:
job = JSONRPC2JobETH{down.ethGetWorkID, "2.0", JSONRPCArray{
HexAddPrefix(powHash),
HexAddPrefix(seedHash),
HexAddPrefix(target),
}, height}
down.ethGetWorkID = 0
case ProtocolEthereumStratum:
job = JSONRPCJobETH{nil, "mining.notify", JSONRPCArray{
powHash,
seedHash,
powHash,
isClean,
}, height}
}
jsonBytes, err := json.Marshal(job)
if err != nil {
glog.Error(down.id, "failed to convert mining job to JSON: ", err, ": ", job)
}
jsonBytes = append(jsonBytes, '\n')
if glog.V(12) {
glog.Info(down.id, "sendJob: ", string(jsonBytes))
}
_, err = down.clientConn.Write(jsonBytes)
if err != nil {
glog.Error(down.id, "failed to send job to miner: ", err.Error())
down.close()
}
}
func (down *DownSessionETH) SendEvent(event interface{}) {
down.eventChannel <- event
}
func (down *DownSessionETH) connBroken() {
down.readLoopRunning = false
down.SendEvent(EventConnBroken{})
}
func (down *DownSessionETH) sendBytes(e EventSendBytes) {
if glog.V(12) {
glog.Info(down.id, "sendBytes: ", string(e.Content))
}
_, err := down.clientConn.Write(e.Content)
if err != nil {
glog.Error(down.id, "failed to send notify to miner: ", err.Error())
down.close()
}
}
func (down *DownSessionETH) submitResponse(e EventSubmitResponse) {
var response JSONRPCResponse
response.ID = e.ID
if e.Status.IsAccepted() {
response.Result = true
} else {
response.Error = e.Status.ToJSONRPCArray(nil)
}
_, err := down.writeJSONResponse(&response)
if err != nil {
glog.Error(down.id, "failed to send share response to miner: ", err.Error())
down.close()
}
}
func (down *DownSessionETH) setDifficulty(e EventSetDifficulty) {
if down.protocol == ProtocolEthereumStratum && down.jobDiff != e.Difficulty {
diff := float64(e.Difficulty) / 4294967296.0
var request JSONRPCRequest
request.Method = "mining.set_difficulty"
request.Params = JSONRPCArray{diff}
_, err := down.writeJSONRequest(&request)
if err != nil {
glog.Error(down.id, "failed to send difficulty to miner: ", err.Error())
down.close()
}
return
}
down.jobDiff = e.Difficulty
}
func (down *DownSessionETH) setExtraNonce(e EventSetExtraNonce) {
if e.ExtraNonce == EthereumInvalidExtraNonce {
glog.Error(down.id, "pool server is full and cannot allocate an extra nonce")
down.close()
return
}
down.extraNonce = e.ExtraNonce
down.hasExtraNonce = true
}
func (down *DownSessionETH) exit() {
down.stat = StatExit
down.close()
}
func (down *DownSessionETH) poolNotReady() {
glog.Warning(down.id, "pool connection not ready")
down.exit()
}
func (down *DownSessionETH) handleEvent() {
down.eventLoopRunning = true
for down.eventLoopRunning {
event := <-down.eventChannel
switch e := event.(type) {
case EventSetUpSession:
down.setUpSession(e)
case EventRecvJSONRPCETH:
down.recvJSONRPC(e)
case EventStratumJobETH:
down.sendJob(e)
case EventSendBytes:
down.sendBytes(e)
case EventSubmitResponse:
down.submitResponse(e)
case EventSetDifficulty:
down.setDifficulty(e)
case EventSetExtraNonce:
down.setExtraNonce(e)
case EventConnBroken:
down.close()
case EventExit:
down.exit()
case EventPoolNotReady:
down.poolNotReady()
default:
glog.Error(down.id, "unknown event: ", e)
}
}
}