Skip to content

Commit 6674ea8

Browse files
committed
Merge pull request bnb-chain#911 from karalabe/whisper-graceful-fail
rpc, xeth: fix bnb-chain#881, gracefully handle offline whisper
2 parents 00280e6 + 0ad5898 commit 6674ea8

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎rpc/api.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,18 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
439439
*reply = newHexData(res)
440440

441441
case "shh_version":
442+
// Short circuit if whisper is not running
443+
if api.xeth().Whisper() == nil {
444+
return NewNotAvailableError(req.Method, "whisper offline")
445+
}
442446
// Retrieves the currently running whisper protocol version
443447
*reply = api.xeth().WhisperVersion()
444448

445449
case "shh_post":
450+
// Short circuit if whisper is not running
451+
if api.xeth().Whisper() == nil {
452+
return NewNotAvailableError(req.Method, "whisper offline")
453+
}
446454
// Injects a new message into the whisper network
447455
args := new(WhisperMessageArgs)
448456
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -455,10 +463,18 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
455463
*reply = true
456464

457465
case "shh_newIdentity":
466+
// Short circuit if whisper is not running
467+
if api.xeth().Whisper() == nil {
468+
return NewNotAvailableError(req.Method, "whisper offline")
469+
}
458470
// Creates a new whisper identity to use for sending/receiving messages
459471
*reply = api.xeth().Whisper().NewIdentity()
460472

461473
case "shh_hasIdentity":
474+
// Short circuit if whisper is not running
475+
if api.xeth().Whisper() == nil {
476+
return NewNotAvailableError(req.Method, "whisper offline")
477+
}
462478
// Checks if an identity if owned or not
463479
args := new(WhisperIdentityArgs)
464480
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -467,6 +483,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
467483
*reply = api.xeth().Whisper().HasIdentity(args.Identity)
468484

469485
case "shh_newFilter":
486+
// Short circuit if whisper is not running
487+
if api.xeth().Whisper() == nil {
488+
return NewNotAvailableError(req.Method, "whisper offline")
489+
}
470490
// Create a new filter to watch and match messages with
471491
args := new(WhisperFilterArgs)
472492
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -476,6 +496,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
476496
*reply = newHexNum(big.NewInt(int64(id)).Bytes())
477497

478498
case "shh_uninstallFilter":
499+
// Short circuit if whisper is not running
500+
if api.xeth().Whisper() == nil {
501+
return NewNotAvailableError(req.Method, "whisper offline")
502+
}
479503
// Remove an existing filter watching messages
480504
args := new(FilterIdArgs)
481505
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -484,6 +508,10 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
484508
*reply = api.xeth().UninstallWhisperFilter(args.Id)
485509

486510
case "shh_getFilterChanges":
511+
// Short circuit if whisper is not running
512+
if api.xeth().Whisper() == nil {
513+
return NewNotAvailableError(req.Method, "whisper offline")
514+
}
487515
// Retrieve all the new messages arrived since the last request
488516
args := new(FilterIdArgs)
489517
if err := json.Unmarshal(req.Params, &args); err != nil {
@@ -492,12 +520,17 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
492520
*reply = api.xeth().WhisperMessagesChanged(args.Id)
493521

494522
case "shh_getMessages":
523+
// Short circuit if whisper is not running
524+
if api.xeth().Whisper() == nil {
525+
return NewNotAvailableError(req.Method, "whisper offline")
526+
}
495527
// Retrieve all the cached messages matching a specific, existing filter
496528
args := new(FilterIdArgs)
497529
if err := json.Unmarshal(req.Params, &args); err != nil {
498530
return err
499531
}
500532
*reply = api.xeth().WhisperMessages(args.Id)
533+
501534
case "eth_hashrate":
502535
*reply = newHexNum(api.xeth().HashRate())
503536

‎rpc/http.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} {
116116
switch reserr.(type) {
117117
case nil:
118118
response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply}
119-
case *NotImplementedError:
119+
case *NotImplementedError, *NotAvailableError:
120120
jsonerr := &RpcErrorObject{-32601, reserr.Error()}
121121
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
122122
case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError:

‎rpc/types.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ func NewNotImplementedError(method string) *NotImplementedError {
209209
}
210210
}
211211

212+
type NotAvailableError struct {
213+
Method string
214+
Reason string
215+
}
216+
217+
func (e *NotAvailableError) Error() string {
218+
return fmt.Sprintf("%s method not available: %s", e.Method, e.Reason)
219+
}
220+
221+
func NewNotAvailableError(method string, reason string) *NotAvailableError {
222+
return &NotAvailableError{
223+
Method: method,
224+
Reason: reason,
225+
}
226+
}
227+
212228
type DecodeParamError struct {
213229
err string
214230
}

‎xeth/xeth.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ func New(eth *eth.Ethereum, frontend Frontend) *XEth {
7979
xeth := &XEth{
8080
backend: eth,
8181
frontend: frontend,
82-
whisper: NewWhisper(eth.Whisper()),
8382
quit: make(chan struct{}),
8483
filterManager: filter.NewFilterManager(eth.EventMux()),
8584
logQueue: make(map[int]*logQueue),
@@ -88,6 +87,9 @@ func New(eth *eth.Ethereum, frontend Frontend) *XEth {
8887
messages: make(map[int]*whisperFilter),
8988
agent: miner.NewRemoteAgent(),
9089
}
90+
if eth.Whisper() != nil {
91+
xeth.whisper = NewWhisper(eth.Whisper())
92+
}
9193
eth.Miner().Register(xeth.agent)
9294
if frontend == nil {
9395
xeth.frontend = dummyFrontend{}

0 commit comments

Comments
 (0)