Skip to content

Commit

Permalink
ETH: fix protocol detail issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SwimmingTiger committed Jan 4, 2022
1 parent c58c861 commit dc51783
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 30 additions & 5 deletions DownSessionETH.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type DownSessionETH struct {
hasExtraNonce bool // 是否获得了ExtraNonce
isFirstJob bool // 是否为第一个任务
jobIDQueue *JobIDQueueETH // job id 队列
ethGetWorkID interface{} // 矿机eth_getWork请求的id

stat AuthorizeStat // 认证状态
protocol StratumProtocol // 挖矿协议
Expand All @@ -51,6 +52,7 @@ func NewDownSessionETH(manager *SessionManager, clientConn net.Conn, sessionID u
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())

Expand Down Expand Up @@ -158,6 +160,16 @@ func (down *DownSessionETH) stratumHandleRequest(request *JSONRPCLineETH, reques
}
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
Expand All @@ -175,6 +187,11 @@ func (down *DownSessionETH) stratumHandleRequest(request *JSONRPCLineETH, reques
}
}

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
Expand All @@ -197,7 +214,10 @@ func (down *DownSessionETH) parseMiningSubmit(request *JSONRPCLineETH) (result i
err = StratumErrTooFewParams
return
}
powHash, _ = request.Params[1].(string)
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:
Expand Down Expand Up @@ -409,10 +429,12 @@ func (down *DownSessionETH) sendJob(e EventStratumJobETH) {

powHash := e.Job.PoWHash(down.extraNonce)
seedHash := hex.EncodeToString(e.Job.SeedHash)
isClean := down.isFirstJob || e.Job.IsClean
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{}
Expand All @@ -426,11 +448,12 @@ func (down *DownSessionETH) sendJob(e EventStratumJobETH) {
isClean,
}, height}
case ProtocolETHProxy:
job = JSONRPC2JobETH{0, "2.0", JSONRPCArray{
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,
Expand Down Expand Up @@ -501,8 +524,10 @@ func (down *DownSessionETH) setDifficulty(e EventSetDifficulty) {
request.Params = JSONRPCArray{diff}

_, err := down.writeJSONRequest(&request)
glog.Error(down.id, "failed to send difficulty to miner: ", err.Error())
down.close()
if err != nil {
glog.Error(down.id, "failed to send difficulty to miner: ", err.Error())
down.close()
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion StratumJobETH.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type JSONRPCJobETH struct {
}

type JSONRPC2JobETH struct {
ID int `json:"id"`
ID interface{} `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result []interface{} `json:"result"`
Height uint64 `json:"height"`
Expand Down

0 comments on commit dc51783

Please sign in to comment.