Merge branch 'rpcfrontier' into conversion

This commit is contained in:
obscuren 2015-03-18 18:42:30 +01:00
commit 5f35e6778f
2 changed files with 65 additions and 67 deletions

View File

@ -1,6 +1,9 @@
package rpc package rpc
import ( import (
"encoding/json"
"io"
"io/ioutil"
"net/http" "net/http"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
@ -16,54 +19,83 @@ const (
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API. // JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
var json JsonWrapper
api := NewEthereumApi(pipe, dataDir) api := NewEthereumApi(pipe, dataDir)
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// TODO this needs to be configurable
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
rpchttplogger.DebugDetailln("Handling request") // Limit request size to resist DoS
if req.ContentLength > maxSizeReqLength { if req.ContentLength > maxSizeReqLength {
jsonerr := &RpcErrorObject{-32700, "Request too large"} jsonerr := &RpcErrorObject{-32700, "Request too large"}
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
return return
} }
reqParsed, reqerr := json.ParseRequestBody(req) // Read request body
switch reqerr.(type) { defer req.Body.Close()
case nil: body, err := ioutil.ReadAll(req.Body)
break if err != nil {
case *DecodeParamError, *InsufficientParamsError, *ValidationError: jsonerr := &RpcErrorObject{-32700, "Could not read request body"}
jsonerr := &RpcErrorObject{-32602, reqerr.Error()} Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) }
return
default: // Try to parse the request as a single
jsonerr := &RpcErrorObject{-32700, "Could not parse request"} var reqSingle RpcRequest
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) if err := json.Unmarshal(body, &reqSingle); err == nil {
response := RpcResponse(api, &reqSingle)
Send(w, &response)
return return
} }
var response interface{} // Try to parse the request to batch
reserr := api.GetRequestReply(&reqParsed, &response) var reqBatch []RpcRequest
if err := json.Unmarshal(body, &reqBatch); err == nil {
// Build response batch
resBatch := make([]*interface{}, len(reqBatch))
for i, request := range reqBatch {
response := RpcResponse(api, &request)
resBatch[i] = response
}
Send(w, resBatch)
return
}
// Not a batch or single request, error
jsonerr := &RpcErrorObject{-32600, "Could not decode request"}
Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
})
}
func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} {
var reply, response interface{}
reserr := api.GetRequestReply(request, &reply)
switch reserr.(type) { switch reserr.(type) {
case nil: case nil:
break response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply}
case *NotImplementedError: case *NotImplementedError:
jsonerr := &RpcErrorObject{-32601, reserr.Error()} jsonerr := &RpcErrorObject{-32601, reserr.Error()}
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
return
case *DecodeParamError, *InsufficientParamsError, *ValidationError: case *DecodeParamError, *InsufficientParamsError, *ValidationError:
jsonerr := &RpcErrorObject{-32602, reserr.Error()} jsonerr := &RpcErrorObject{-32602, reserr.Error()}
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
return
default: default:
jsonerr := &RpcErrorObject{-32603, reserr.Error()} jsonerr := &RpcErrorObject{-32603, reserr.Error()}
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
return
} }
rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
json.Send(w, &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Result: response}) return &response
}) }
func Send(writer io.Writer, v interface{}) (n int, err error) {
var payload []byte
payload, err = json.MarshalIndent(v, "", "\t")
if err != nil {
rpclogger.Fatalln("Error marshalling JSON", err)
return 0, err
}
rpclogger.DebugDetailf("Sending payload: %s", payload)
return writer.Write(payload)
} }

View File

@ -19,9 +19,7 @@ package rpc
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"math/big" "math/big"
"net/http"
"reflect" "reflect"
"time" "time"
@ -33,8 +31,6 @@ import (
var rpclogger = logger.NewLogger("RPC") var rpclogger = logger.NewLogger("RPC")
type JsonWrapper struct{}
// Unmarshal state is a helper method which has the ability to decode messsages // Unmarshal state is a helper method which has the ability to decode messsages
// that use the `defaultBlock` (https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter) // that use the `defaultBlock` (https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter)
// For example a `call`: [{to: "0x....", data:"0x..."}, "latest"]. The first argument is the transaction // For example a `call`: [{to: "0x....", data:"0x..."}, "latest"]. The first argument is the transaction
@ -94,36 +90,6 @@ func UnmarshalRawMessages(b []byte, iface interface{}, number *int64) (err error
return nil return nil
} }
func (self JsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error) {
var payload []byte
payload, err = json.MarshalIndent(v, "", "\t")
if err != nil {
rpclogger.Fatalln("Error marshalling JSON", err)
return 0, err
}
rpclogger.DebugDetailf("Sending payload: %s", payload)
return writer.Write(payload)
}
func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) {
var reqParsed RpcRequest
// Convert JSON to native types
d := json.NewDecoder(req.Body)
defer req.Body.Close()
err := d.Decode(&reqParsed)
if err != nil {
rpclogger.Errorln("Error decoding JSON: ", err)
return reqParsed, err
}
rpclogger.DebugDetailf("Parsed request: %s", reqParsed)
return reqParsed, nil
}
func i2hex(n int) string { func i2hex(n int) string {
return common.ToHex(big.NewInt(int64(n)).Bytes()) return common.ToHex(big.NewInt(int64(n)).Bytes())
} }