aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-05-11 16:53:53 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-05-11 16:53:53 +0800
commit0ad5898c0f9b0d777818d89356b74606f4b3c988 (patch)
treedce4e31dedd9e92f26c5203d0f62dc4ae2d59044 /rpc
parent00280e62e3c422b2824a0280015b7b78578ab16d (diff)
downloadgo-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.tar
go-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.tar.gz
go-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.tar.bz2
go-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.tar.lz
go-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.tar.xz
go-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.tar.zst
go-tangerine-0ad5898c0f9b0d777818d89356b74606f4b3c988.zip
rpc, xeth: fix #881, gracefully handle offline whisper
Diffstat (limited to 'rpc')
-rw-r--r--rpc/api.go33
-rw-r--r--rpc/http.go2
-rw-r--r--rpc/types.go16
3 files changed, 50 insertions, 1 deletions
diff --git a/rpc/api.go b/rpc/api.go
index 6ba0d93e2..6a629ce8e 100644
--- a/rpc/api.go
+++ b/rpc/api.go
@@ -439,10 +439,18 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
*reply = newHexData(res)
case "shh_version":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Retrieves the currently running whisper protocol version
*reply = api.xeth().WhisperVersion()
case "shh_post":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Injects a new message into the whisper network
args := new(WhisperMessageArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -455,10 +463,18 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
*reply = true
case "shh_newIdentity":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Creates a new whisper identity to use for sending/receiving messages
*reply = api.xeth().Whisper().NewIdentity()
case "shh_hasIdentity":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Checks if an identity if owned or not
args := new(WhisperIdentityArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -467,6 +483,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
*reply = api.xeth().Whisper().HasIdentity(args.Identity)
case "shh_newFilter":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Create a new filter to watch and match messages with
args := new(WhisperFilterArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -476,6 +496,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
*reply = newHexNum(big.NewInt(int64(id)).Bytes())
case "shh_uninstallFilter":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Remove an existing filter watching messages
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -484,6 +508,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
*reply = api.xeth().UninstallWhisperFilter(args.Id)
case "shh_getFilterChanges":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Retrieve all the new messages arrived since the last request
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -492,12 +520,17 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
*reply = api.xeth().WhisperMessagesChanged(args.Id)
case "shh_getMessages":
+ // Short circuit if whisper is not running
+ if api.xeth().Whisper() == nil {
+ return NewNotAvailableError(req.Method, "whisper offline")
+ }
// Retrieve all the cached messages matching a specific, existing filter
args := new(FilterIdArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
*reply = api.xeth().WhisperMessages(args.Id)
+
case "eth_hashrate":
*reply = newHexNum(api.xeth().HashRate())
diff --git a/rpc/http.go b/rpc/http.go
index 4760601d8..c5bb10c80 100644
--- a/rpc/http.go
+++ b/rpc/http.go
@@ -116,7 +116,7 @@ func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} {
switch reserr.(type) {
case nil:
response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply}
- case *NotImplementedError:
+ case *NotImplementedError, *NotAvailableError:
jsonerr := &RpcErrorObject{-32601, reserr.Error()}
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError:
diff --git a/rpc/types.go b/rpc/types.go
index 1784759a4..e6eb4f856 100644
--- a/rpc/types.go
+++ b/rpc/types.go
@@ -209,6 +209,22 @@ func NewNotImplementedError(method string) *NotImplementedError {
}
}
+type NotAvailableError struct {
+ Method string
+ Reason string
+}
+
+func (e *NotAvailableError) Error() string {
+ return fmt.Sprintf("%s method not available: %s", e.Method, e.Reason)
+}
+
+func NewNotAvailableError(method string, reason string) *NotAvailableError {
+ return &NotAvailableError{
+ Method: method,
+ Reason: reason,
+ }
+}
+
type DecodeParamError struct {
err string
}