From 829201382b67e95ab31fca887234d1858c11c810 Mon Sep 17 00:00:00 2001
From: Gustav Simonsson <gustav.simonsson@gmail.com>
Date: Wed, 26 Aug 2015 12:46:50 +0200
Subject: [PATCH] rpc: return error code for eth_getWork when no work ready

---
 miner/remote_agent.go |  8 +++++---
 rpc/api/eth.go        |  7 ++++++-
 rpc/shared/errors.go  | 14 ++++++++++++++
 rpc/shared/types.go   |  3 +++
 4 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/miner/remote_agent.go b/miner/remote_agent.go
index 5c672a6e0..5ccadb896 100644
--- a/miner/remote_agent.go
+++ b/miner/remote_agent.go
@@ -17,6 +17,7 @@
 package miner
 
 import (
+	"errors"
 	"math/big"
 	"sync"
 	"time"
@@ -90,7 +91,7 @@ func (a *RemoteAgent) GetHashRate() (tot int64) {
 	return
 }
 
-func (a *RemoteAgent) GetWork() [3]string {
+func (a *RemoteAgent) GetWork() ([3]string, error) {
 	a.mu.Lock()
 	defer a.mu.Unlock()
 
@@ -110,9 +111,10 @@ func (a *RemoteAgent) GetWork() [3]string {
 		res[2] = common.BytesToHash(n.Bytes()).Hex()
 
 		a.work[block.HashNoNonce()] = a.currentWork
+		return res, nil
+	} else {
+		return res, errors.New("No work available yet, don't panic.")
 	}
-
-	return res
 }
 
 // Returns true or false, but does not indicate if the PoW was correct
diff --git a/rpc/api/eth.go b/rpc/api/eth.go
index 5199bd966..253b6d5cf 100644
--- a/rpc/api/eth.go
+++ b/rpc/api/eth.go
@@ -563,7 +563,12 @@ func (self *ethApi) GetLogs(req *shared.Request) (interface{}, error) {
 
 func (self *ethApi) GetWork(req *shared.Request) (interface{}, error) {
 	self.xeth.SetMining(true, 0)
-	return self.xeth.RemoteMining().GetWork(), nil
+	ret, err := self.xeth.RemoteMining().GetWork()
+	if err != nil {
+		return nil, shared.NewNotReadyError("getWork")
+	} else {
+		return ret, nil
+	}
 }
 
 func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) {
diff --git a/rpc/shared/errors.go b/rpc/shared/errors.go
index 85af1bb2f..d43858a40 100644
--- a/rpc/shared/errors.go
+++ b/rpc/shared/errors.go
@@ -64,6 +64,20 @@ func NewNotImplementedError(method string) *NotImplementedError {
 	}
 }
 
+type NotReadyError struct {
+	Method string
+}
+
+func (e *NotReadyError) Error() string {
+	return fmt.Sprintf("%s method not ready", e.Method)
+}
+
+func NewNotReadyError(method string) *NotReadyError {
+	return &NotReadyError{
+		Method: method,
+	}
+}
+
 type DecodeParamError struct {
 	err string
 }
diff --git a/rpc/shared/types.go b/rpc/shared/types.go
index 659b74bf6..f58924976 100644
--- a/rpc/shared/types.go
+++ b/rpc/shared/types.go
@@ -94,6 +94,9 @@ func NewRpcResponse(id interface{}, jsonrpcver string, reply interface{}, err er
 	case *NotImplementedError:
 		jsonerr := &ErrorObject{-32601, err.Error()}
 		response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
+	case *NotReadyError:
+		jsonerr := &ErrorObject{-32000, err.Error()}
+		response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
 	case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError:
 		jsonerr := &ErrorObject{-32602, err.Error()}
 		response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
-- 
GitLab