aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTaylor Gerring <taylor.gerring@gmail.com>2015-02-04 07:29:29 +0800
committerTaylor Gerring <taylor.gerring@gmail.com>2015-02-04 07:29:29 +0800
commit55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd (patch)
tree34ec5ce35049377792ccc33892191b76fc49069a
parent07590196a5ee1a63856f1653516da950dfa4966b (diff)
downloaddexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.tar
dexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.tar.gz
dexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.tar.bz2
dexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.tar.lz
dexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.tar.xz
dexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.tar.zst
dexon-55ed0ff07c6cf2dc8b422a3bf8d623a039ad6dbd.zip
Update RPC message format
-rw-r--r--rpc/http/server.go11
-rw-r--r--rpc/message.go25
-rw-r--r--rpc/ws/server.go13
3 files changed, 27 insertions, 22 deletions
diff --git a/rpc/http/server.go b/rpc/http/server.go
index 965727a4e..caa50d67c 100644
--- a/rpc/http/server.go
+++ b/rpc/http/server.go
@@ -84,6 +84,7 @@ func (s *RpcHttpServer) Start() {
}
func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
+ var jsonrpcver string = "2.0"
fn := func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -91,20 +92,22 @@ func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
reqParsed, reqerr := JSON.ParseRequestBody(req)
if reqerr != nil {
- JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest})
+ jsonerr := &rpc.RpcErrorObject{-32700, rpc.ErrorParseRequest}
+ JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
return
}
var response interface{}
reserr := api.GetRequestReply(&reqParsed, &response)
if reserr != nil {
- rpchttplogger.Errorln(reserr)
- JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
+ rpchttplogger.Warnln(reserr)
+ jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()}
+ JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: &reqParsed.ID, Error: jsonerr})
return
}
rpchttplogger.Debugf("Generated response: %T %s", response, response)
- JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response})
+ JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
}
return http.HandlerFunc(fn)
diff --git a/rpc/message.go b/rpc/message.go
index 26fac9d37..054b7a578 100644
--- a/rpc/message.go
+++ b/rpc/message.go
@@ -33,30 +33,29 @@ const (
ErrorDecodeArgs = "Error: Could not decode arguments"
)
-type ErrorResponse struct {
- Error bool `json:"error"`
- ErrorText string `json:"errorText"`
+type RpcRequest struct {
+ JsonRpc string `json:"jsonrpc"`
+ ID int `json:"id"`
+ Method string `json:"method"`
+ Params []json.RawMessage `json:"params"`
}
type RpcSuccessResponse struct {
ID int `json:"id"`
JsonRpc string `json:"jsonrpc"`
- Error bool `json:"error"`
Result interface{} `json:"result"`
}
type RpcErrorResponse struct {
- ID int `json:"id"`
- JsonRpc string `json:"jsonrpc"`
- Error bool `json:"error"`
- ErrorText string `json:"errortext"`
+ ID *int `json:"id"`
+ JsonRpc string `json:"jsonrpc"`
+ Error *RpcErrorObject `json:"error"`
}
-type RpcRequest struct {
- JsonRpc string `json:"jsonrpc"`
- ID int `json:"id"`
- Method string `json:"method"`
- Params []json.RawMessage `json:"params"`
+type RpcErrorObject struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ // Data interface{} `json:"data"`
}
func NewErrorResponse(msg string) error {
diff --git a/rpc/ws/server.go b/rpc/ws/server.go
index 85cacf80b..6be4e8ce9 100644
--- a/rpc/ws/server.go
+++ b/rpc/ws/server.go
@@ -28,6 +28,7 @@ import (
)
var wslogger = logger.NewLogger("RPC-WS")
+var JSON rpc.JsonWrapper
type WebSocketServer struct {
pipe *xeth.XEth
@@ -90,27 +91,29 @@ func (s *WebSocketServer) apiHandler(api *rpc.EthereumApi) http.Handler {
}
func sockHandler(api *rpc.EthereumApi) websocket.Handler {
+ var jsonrpcver string = "2.0"
fn := func(conn *websocket.Conn) {
for {
wslogger.Debugln("Handling request")
var reqParsed rpc.RpcRequest
if err := websocket.JSON.Receive(conn, &reqParsed); err != nil {
- wslogger.Debugln(rpc.ErrorParseRequest)
- websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest})
+ jsonerr := &rpc.RpcErrorObject{-32700, rpc.ErrorParseRequest}
+ JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
continue
}
var response interface{}
reserr := api.GetRequestReply(&reqParsed, &response)
if reserr != nil {
- wslogger.Errorln(reserr)
- websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
+ wslogger.Warnln(reserr)
+ jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()}
+ JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: &reqParsed.ID, Error: jsonerr})
continue
}
wslogger.Debugf("Generated response: %T %s", response, response)
- websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response})
+ JSON.Send(conn, &rpc.RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
}
}
return websocket.Handler(fn)