Skip to content

Commit e1037bd

Browse files
committed
Merge pull request bnb-chain#1724 from Gustav-Simonsson/get_work
rpc: return error code for eth_getWork when no work ready
2 parents 2d1ced8 + d9addf7 commit e1037bd

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎miner/remote_agent.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package miner
1818

1919
import (
20+
"errors"
2021
"math/big"
2122
"sync"
2223
"time"
@@ -90,7 +91,7 @@ func (a *RemoteAgent) GetHashRate() (tot int64) {
9091
return
9192
}
9293

93-
func (a *RemoteAgent) GetWork() [3]string {
94+
func (a *RemoteAgent) GetWork() ([3]string, error) {
9495
a.mu.Lock()
9596
defer a.mu.Unlock()
9697

@@ -110,9 +111,9 @@ func (a *RemoteAgent) GetWork() [3]string {
110111
res[2] = common.BytesToHash(n.Bytes()).Hex()
111112

112113
a.work[block.HashNoNonce()] = a.currentWork
114+
return res, nil
113115
}
114-
115-
return res
116+
return res, errors.New("No work available yet, don't panic.")
116117
}
117118

118119
// Returns true or false, but does not indicate if the PoW was correct

‎rpc/api/eth.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,12 @@ func (self *ethApi) GetLogs(req *shared.Request) (interface{}, error) {
563563

564564
func (self *ethApi) GetWork(req *shared.Request) (interface{}, error) {
565565
self.xeth.SetMining(true, 0)
566-
return self.xeth.RemoteMining().GetWork(), nil
566+
ret, err := self.xeth.RemoteMining().GetWork()
567+
if err != nil {
568+
return nil, shared.NewNotReadyError("mining work")
569+
} else {
570+
return ret, nil
571+
}
567572
}
568573

569574
func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) {

‎rpc/shared/errors.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ func NewNotImplementedError(method string) *NotImplementedError {
6464
}
6565
}
6666

67+
type NotReadyError struct {
68+
Resource string
69+
}
70+
71+
func (e *NotReadyError) Error() string {
72+
return fmt.Sprintf("%s not ready", e.Resource)
73+
}
74+
75+
func NewNotReadyError(resource string) *NotReadyError {
76+
return &NotReadyError{
77+
Resource: resource,
78+
}
79+
}
80+
6781
type DecodeParamError struct {
6882
err string
6983
}

‎rpc/shared/types.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func NewRpcResponse(id interface{}, jsonrpcver string, reply interface{}, err er
9292
case *NotImplementedError:
9393
jsonerr := &ErrorObject{-32601, err.Error()}
9494
response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
95+
case *NotReadyError:
96+
jsonerr := &ErrorObject{-32000, err.Error()}
97+
response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
9598
case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError:
9699
jsonerr := &ErrorObject{-32602, err.Error()}
97100
response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}

0 commit comments

Comments
 (0)