diff options
author | Bas van Kervel <bas@ethdev.com> | 2016-03-14 16:38:54 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2016-04-12 17:02:39 +0800 |
commit | aa9fff3e68b1def0a9a22009c233150bf9ba481f (patch) | |
tree | 926c241574d6d80dfe4ffd6d2e447a9f7f84dc8b /node | |
parent | 7e02105672cda92889a78db864a5701d78f45eb2 (diff) | |
download | go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.gz go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.bz2 go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.lz go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.xz go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.tar.zst go-tangerine-aa9fff3e68b1def0a9a22009c233150bf9ba481f.zip |
rpc: various fixes/enhancements
rpc: be less restrictive on the request id
rpc: improved documentation
console: upgrade web3.js to version 0.16.0
rpc: cache http connections
rpc: rename wsDomains parameter to wsOrigins
Diffstat (limited to 'node')
-rw-r--r-- | node/api.go | 47 | ||||
-rw-r--r-- | node/config.go | 4 | ||||
-rw-r--r-- | node/node.go | 20 | ||||
-rw-r--r-- | node/node_test.go | 2 |
4 files changed, 60 insertions, 13 deletions
diff --git a/node/api.go b/node/api.go index 8a2578410..f199a8d3d 100644 --- a/node/api.go +++ b/node/api.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" + "github.com/ethereum/go-ethereum/rpc" "github.com/rcrowley/go-metrics" ) @@ -58,14 +59,33 @@ func (api *PrivateAdminAPI) AddPeer(url string) (bool, error) { } // StartRPC starts the HTTP RPC API server. -func (api *PrivateAdminAPI) StartRPC(host string, port int, cors string, apis string) (bool, error) { +func (api *PrivateAdminAPI) StartRPC(host *string, port *rpc.HexNumber, cors *string, apis *string) (bool, error) { api.node.lock.Lock() defer api.node.lock.Unlock() if api.node.httpHandler != nil { return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint) } - if err := api.node.startHTTP(fmt.Sprintf("%s:%d", host, port), api.node.rpcAPIs, strings.Split(apis, ","), cors); err != nil { + + if host == nil { + host = &api.node.httpHost + } + if port == nil { + port = rpc.NewHexNumber(api.node.httpPort) + } + if cors == nil { + cors = &api.node.httpCors + } + + modules := api.node.httpWhitelist + if apis != nil { + modules = nil + for _, m := range strings.Split(*apis, ",") { + modules = append(modules, strings.TrimSpace(m)) + } + } + + if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, port.Int()), api.node.rpcAPIs, modules, *cors); err != nil { return false, err } return true, nil @@ -84,14 +104,33 @@ func (api *PrivateAdminAPI) StopRPC() (bool, error) { } // StartWS starts the websocket RPC API server. -func (api *PrivateAdminAPI) StartWS(host string, port int, cors string, apis string) (bool, error) { +func (api *PrivateAdminAPI) StartWS(host *string, port *rpc.HexNumber, allowedOrigins *string, apis *string) (bool, error) { api.node.lock.Lock() defer api.node.lock.Unlock() if api.node.wsHandler != nil { return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint) } - if err := api.node.startWS(fmt.Sprintf("%s:%d", host, port), api.node.rpcAPIs, strings.Split(apis, ","), cors); err != nil { + + if host == nil { + host = &api.node.wsHost + } + if port == nil { + port = rpc.NewHexNumber(api.node.wsPort) + } + if allowedOrigins == nil { + allowedOrigins = &api.node.wsOrigins + } + + modules := api.node.wsWhitelist + if apis != nil { + modules = nil + for _, m := range strings.Split(*apis, ",") { + modules = append(modules, strings.TrimSpace(m)) + } + } + + if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, port.Int()), api.node.rpcAPIs, modules, *allowedOrigins); err != nil { return false, err } return true, nil diff --git a/node/config.go b/node/config.go index 301ec636e..23a745c2c 100644 --- a/node/config.go +++ b/node/config.go @@ -127,10 +127,10 @@ type Config struct { // ephemeral nodes). WSPort int - // WSDomains is the list of domain to accept websocket requests from. Please be + // WSOrigins is the list of domain to accept websocket requests from. Please be // aware that the server can only act upon the HTTP request the client sends and // cannot verify the validity of the request header. - WSDomains string + WSOrigins string // WSModules is a list of API modules to expose via the websocket RPC interface. // If the module list is empty, all RPC API endpoints designated public will be diff --git a/node/node.go b/node/node.go index 62cc3895b..18c3f91d8 100644 --- a/node/node.go +++ b/node/node.go @@ -62,15 +62,19 @@ type Node struct { ipcListener net.Listener // IPC RPC listener socket to serve API requests ipcHandler *rpc.Server // IPC RPC request handler to process the API requests + httpHost string // HTTP hostname + httpPort int // HTTP post httpEndpoint string // HTTP endpoint (interface + port) to listen at (empty = HTTP disabled) httpWhitelist []string // HTTP RPC modules to allow through this endpoint httpCors string // HTTP RPC Cross-Origin Resource Sharing header httpListener net.Listener // HTTP RPC listener socket to server API requests httpHandler *rpc.Server // HTTP RPC request handler to process the API requests + wsHost string // Websocket host + wsPort int // Websocket post wsEndpoint string // Websocket endpoint (interface + port) to listen at (empty = websocket disabled) wsWhitelist []string // Websocket RPC modules to allow through this endpoint - wsDomains string // Websocket RPC allowed origin domains + wsOrigins string // Websocket RPC allowed origin domains wsListener net.Listener // Websocket RPC listener socket to server API requests wsHandler *rpc.Server // Websocket RPC request handler to process the API requests @@ -110,12 +114,16 @@ func New(conf *Config) (*Node, error) { }, serviceFuncs: []ServiceConstructor{}, ipcEndpoint: conf.IPCEndpoint(), + httpHost: conf.HTTPHost, + httpPort: conf.HTTPPort, httpEndpoint: conf.HTTPEndpoint(), httpWhitelist: conf.HTTPModules, httpCors: conf.HTTPCors, + wsHost: conf.WSHost, + wsPort: conf.WSPort, wsEndpoint: conf.WSEndpoint(), wsWhitelist: conf.WSModules, - wsDomains: conf.WSDomains, + wsOrigins: conf.WSOrigins, eventmux: new(event.TypeMux), }, nil } @@ -231,7 +239,7 @@ func (n *Node) startRPC(services map[reflect.Type]Service) error { n.stopInProc() return err } - if err := n.startWS(n.wsEndpoint, apis, n.wsWhitelist, n.wsDomains); err != nil { + if err := n.startWS(n.wsEndpoint, apis, n.wsWhitelist, n.wsOrigins); err != nil { n.stopHTTP() n.stopIPC() n.stopInProc() @@ -383,7 +391,7 @@ func (n *Node) stopHTTP() { } // startWS initializes and starts the websocket RPC endpoint. -func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, cors string) error { +func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrigins string) error { // Short circuit if the WS endpoint isn't being exposed if endpoint == "" { return nil @@ -411,14 +419,14 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, cors s if listener, err = net.Listen("tcp", endpoint); err != nil { return err } - go rpc.NewWSServer(cors, handler).Serve(listener) + go rpc.NewWSServer(wsOrigins, handler).Serve(listener) glog.V(logger.Info).Infof("WebSocket endpoint opened: ws://%s", endpoint) // All listeners booted successfully n.wsEndpoint = endpoint n.wsListener = listener n.wsHandler = handler - n.wsDomains = cors + n.wsOrigins = wsOrigins return nil } diff --git a/node/node_test.go b/node/node_test.go index 532115d3c..372fc6b10 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -554,7 +554,7 @@ func TestAPIGather(t *testing.T) { {"multi.v2.nested_theOneMethod", "multi.v2.nested"}, } for i, test := range tests { - if err := client.Send(rpc.JSONRequest{Id: new(int64), Version: "2.0", Method: test.Method}); err != nil { + if err := client.Send(rpc.JSONRequest{Id: []byte("1"), Version: "2.0", Method: test.Method}); err != nil { t.Fatalf("test %d: failed to send API request: %v", i, err) } reply := new(rpc.JSONSuccessResponse) |