removed old rpc structure and added new inproc api client

This commit is contained in:
Bas van Kervel
2015-06-17 16:22:35 +02:00
parent 3e1d635f8d
commit a4a4e9fcf8
22 changed files with 393 additions and 4427 deletions

View File

@ -9,16 +9,32 @@ import (
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
"strings"
)
const (
jsonrpcver = "2.0"
maxHttpSizeReqLength = 1024 * 1024 // 1MB
)
var (
// List with all API's which are offered over the in proc interface by default
DefaultInProcApis = api.AllApis
// List with all API's which are offered over the IPC interface by default
DefaultIpcApis = api.AllApis
// List with API's which are offered over thr HTTP/RPC interface by default
DefaultHttpRpcApis = strings.Join([]string{
api.DbApiName, api.EthApiName, api.NetApiName, api.Web3ApiName,
}, ",")
)
type EthereumClient interface {
// Close underlaying connection
Close()
// Send request
Send(interface{}) error
// Receive response
Recv() (interface{}, error)
}

View File

@ -63,3 +63,27 @@ func StopHttp() {
httpListener = nil
}
}
type httpClient struct {
codec codec.ApiCoder
}
// Create a new in process client
func NewHttpClient(cfg HttpConfig, codec codec.Codec) *httpClient {
return &httpClient{
codec: codec.New(nil),
}
}
func (self *httpClient) Close() {
// do nothing
}
func (self *httpClient) Send(req interface{}) error {
return nil
}
func (self *httpClient) Recv() (interface{}, error) {
return nil, nil
}

View File

@ -90,7 +90,7 @@ func newStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
case <-stop:
w.Header().Set("Content-Type", "application/json")
err := fmt.Errorf("RPC service stopped")
response := shared.NewRpcResponse(-1, jsonrpcver, nil, err)
response := shared.NewRpcResponse(-1, api.JsonRpcVersion, nil, err)
httpSend(w, response)
default:
h.ServeHTTP(w, r)
@ -110,14 +110,14 @@ func httpSend(writer io.Writer, v interface{}) (n int, err error) {
return writer.Write(payload)
}
func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
func gethHttpHandler(codec codec.Codec, a api.EthereumApi) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Limit request size to resist DoS
if req.ContentLength > maxHttpSizeReqLength {
err := fmt.Errorf("Request too large")
response := shared.NewRpcErrorResponse(-1, jsonrpcver, -32700, err)
response := shared.NewRpcErrorResponse(-1, api.JsonRpcVersion, -32700, err)
httpSend(w, &response)
return
}
@ -126,7 +126,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
payload, err := ioutil.ReadAll(req.Body)
if err != nil {
err := fmt.Errorf("Could not read request body")
response := shared.NewRpcErrorResponse(-1, jsonrpcver, -32700, err)
response := shared.NewRpcErrorResponse(-1, api.JsonRpcVersion, -32700, err)
httpSend(w, &response)
return
}
@ -134,7 +134,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
c := codec.New(nil)
var rpcReq shared.Request
if err = c.Decode(payload, &rpcReq); err == nil {
reply, err := api.Execute(&rpcReq)
reply, err := a.Execute(&rpcReq)
res := shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
httpSend(w, &res)
return
@ -146,7 +146,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
resCount := 0
for i, rpcReq := range reqBatch {
reply, err := api.Execute(&rpcReq)
reply, err := a.Execute(&rpcReq)
if rpcReq.Id != nil { // this leaves nil entries in the response batch for later removal
resBatch[i] = shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
resCount += 1
@ -161,7 +161,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
// invalid request
err = fmt.Errorf("Could not decode request")
res := shared.NewRpcErrorResponse(-1, jsonrpcver, -32600, err)
res := shared.NewRpcErrorResponse(-1, api.JsonRpcVersion, -32600, err)
httpSend(w, res)
})
}

53
rpc/comms/inproc.go Normal file
View File

@ -0,0 +1,53 @@
package comms
import (
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/shared"
"fmt"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/xeth"
"github.com/ethereum/go-ethereum/eth"
)
type InProcClient struct {
api api.EthereumApi
codec codec.Codec
lastId interface{}
lastJsonrpc string
lastErr error
lastRes interface{}
}
// Create a new in process client
func NewInProcClient(codec codec.Codec) *InProcClient {
return &InProcClient{
codec: codec,
}
}
func (self *InProcClient) Close() {
// do nothing
}
// Need to setup api support
func (self *InProcClient) Initialize(xeth *xeth.XEth, eth *eth.Ethereum) {
if apis, err := api.ParseApiString(api.AllApis, self.codec, xeth, eth); err == nil {
self.api = api.Merge(apis...)
}
}
func (self *InProcClient) Send(req interface{}) error {
if r, ok := req.(*shared.Request); ok {
self.lastId = r.Id
self.lastJsonrpc = r.Jsonrpc
self.lastRes, self.lastErr = self.api.Execute(r)
return self.lastErr
}
return fmt.Errorf("Invalid request (%T)", req)
}
func (self *InProcClient) Recv() (interface{}, error) {
return self.lastRes, self.lastErr
//return *shared.NewRpcResponse(self.lastId, self.lastJsonrpc, self.lastRes, self.lastErr), nil
}

View File

@ -10,19 +10,19 @@ type IpcConfig struct {
}
type ipcClient struct {
c codec.ApiCoder
codec codec.ApiCoder
}
func (self *ipcClient) Close() {
self.c.Close()
self.codec.Close()
}
func (self *ipcClient) Send(req interface{}) error {
return self.c.WriteResponse(req)
return self.codec.WriteResponse(req)
}
func (self *ipcClient) Recv() (interface{}, error) {
return self.c.ReadResponse()
return self.codec.ReadResponse()
}
// Create a new IPC client, UNIX domain socket on posix, named pipe on Windows