Update RPC message format
This commit is contained in:
parent
07590196a5
commit
55ed0ff07c
@ -84,6 +84,7 @@ func (s *RpcHttpServer) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
|
func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
|
||||||
|
var jsonrpcver string = "2.0"
|
||||||
fn := func(w http.ResponseWriter, req *http.Request) {
|
fn := func(w http.ResponseWriter, req *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
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)
|
reqParsed, reqerr := JSON.ParseRequestBody(req)
|
||||||
if reqerr != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var response interface{}
|
var response interface{}
|
||||||
reserr := api.GetRequestReply(&reqParsed, &response)
|
reserr := api.GetRequestReply(&reqParsed, &response)
|
||||||
if reserr != nil {
|
if reserr != nil {
|
||||||
rpchttplogger.Errorln(reserr)
|
rpchttplogger.Warnln(reserr)
|
||||||
JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
|
jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()}
|
||||||
|
JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: &reqParsed.ID, Error: jsonerr})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rpchttplogger.Debugf("Generated response: %T %s", response, response)
|
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)
|
return http.HandlerFunc(fn)
|
||||||
|
@ -33,25 +33,6 @@ const (
|
|||||||
ErrorDecodeArgs = "Error: Could not decode arguments"
|
ErrorDecodeArgs = "Error: Could not decode arguments"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ErrorResponse struct {
|
|
||||||
Error bool `json:"error"`
|
|
||||||
ErrorText string `json:"errorText"`
|
|
||||||
}
|
|
||||||
|
|
||||||
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"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RpcRequest struct {
|
type RpcRequest struct {
|
||||||
JsonRpc string `json:"jsonrpc"`
|
JsonRpc string `json:"jsonrpc"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
@ -59,6 +40,24 @@ type RpcRequest struct {
|
|||||||
Params []json.RawMessage `json:"params"`
|
Params []json.RawMessage `json:"params"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RpcSuccessResponse struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
JsonRpc string `json:"jsonrpc"`
|
||||||
|
Result interface{} `json:"result"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RpcErrorResponse struct {
|
||||||
|
ID *int `json:"id"`
|
||||||
|
JsonRpc string `json:"jsonrpc"`
|
||||||
|
Error *RpcErrorObject `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RpcErrorObject struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
// Data interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewErrorResponse(msg string) error {
|
func NewErrorResponse(msg string) error {
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var wslogger = logger.NewLogger("RPC-WS")
|
var wslogger = logger.NewLogger("RPC-WS")
|
||||||
|
var JSON rpc.JsonWrapper
|
||||||
|
|
||||||
type WebSocketServer struct {
|
type WebSocketServer struct {
|
||||||
pipe *xeth.XEth
|
pipe *xeth.XEth
|
||||||
@ -90,27 +91,29 @@ func (s *WebSocketServer) apiHandler(api *rpc.EthereumApi) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sockHandler(api *rpc.EthereumApi) websocket.Handler {
|
func sockHandler(api *rpc.EthereumApi) websocket.Handler {
|
||||||
|
var jsonrpcver string = "2.0"
|
||||||
fn := func(conn *websocket.Conn) {
|
fn := func(conn *websocket.Conn) {
|
||||||
for {
|
for {
|
||||||
wslogger.Debugln("Handling request")
|
wslogger.Debugln("Handling request")
|
||||||
var reqParsed rpc.RpcRequest
|
var reqParsed rpc.RpcRequest
|
||||||
|
|
||||||
if err := websocket.JSON.Receive(conn, &reqParsed); err != nil {
|
if err := websocket.JSON.Receive(conn, &reqParsed); err != nil {
|
||||||
wslogger.Debugln(rpc.ErrorParseRequest)
|
jsonerr := &rpc.RpcErrorObject{-32700, rpc.ErrorParseRequest}
|
||||||
websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest})
|
JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var response interface{}
|
var response interface{}
|
||||||
reserr := api.GetRequestReply(&reqParsed, &response)
|
reserr := api.GetRequestReply(&reqParsed, &response)
|
||||||
if reserr != nil {
|
if reserr != nil {
|
||||||
wslogger.Errorln(reserr)
|
wslogger.Warnln(reserr)
|
||||||
websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
|
jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()}
|
||||||
|
JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: &reqParsed.ID, Error: jsonerr})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
wslogger.Debugf("Generated response: %T %s", response, response)
|
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)
|
return websocket.Handler(fn)
|
||||||
|
Loading…
Reference in New Issue
Block a user