aboutsummaryrefslogtreecommitdiffstats
path: root/node
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-02-05 17:33:24 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-02-05 17:33:24 +0800
commitba7c125153ce1be30985784a18edf38645406d03 (patch)
tree92c1e2b8ec6ef0d4b39381148b3497e311e80b95 /node
parent212828963172b3921827df15abdc8602480e947d (diff)
parent188ab928c3f2a2eac5ee0f7ac42cbf2f35568bcd (diff)
downloadgo-tangerine-ba7c125153ce1be30985784a18edf38645406d03.tar.gz
go-tangerine-ba7c125153ce1be30985784a18edf38645406d03.tar.zst
go-tangerine-ba7c125153ce1be30985784a18edf38645406d03.zip
Merge pull request #2168 from karalabe/move-rpc-into-node
cmd, common, node, rpc: move IPC into the node itself
Diffstat (limited to 'node')
-rw-r--r--node/config.go40
-rw-r--r--node/config_test.go32
-rw-r--r--node/node.go100
-rw-r--r--node/node_example_test.go1
-rw-r--r--node/node_test.go101
-rw-r--r--node/service_test.go2
-rw-r--r--node/utils_test.go14
7 files changed, 269 insertions, 21 deletions
diff --git a/node/config.go b/node/config.go
index 93f0ba79d..d3eb1c78b 100644
--- a/node/config.go
+++ b/node/config.go
@@ -23,7 +23,10 @@ import (
"net"
"os"
"path/filepath"
+ "runtime"
+ "strings"
+ "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
@@ -49,6 +52,12 @@ type Config struct {
// in memory.
DataDir string
+ // IpcPath is the requested location to place the IPC endpoint. If the path is
+ // a simple file name, it is placed inside the data directory (or on the root
+ // pipe path on Windows), whereas if it's a resolvable path name (absolute or
+ // relative), then that specific path is enforced. An empty path disables IPC.
+ IpcPath string
+
// This field should be a valid secp256k1 private key that will be used for both
// remote peer identification as well as network traffic encryption. If no key
// is configured, the preset one is loaded from the data dir, generating it if
@@ -90,6 +99,37 @@ type Config struct {
MaxPendingPeers int
}
+// IpcEndpoint resolves an IPC endpoint based on a configured value, taking into
+// account the set data folders as well as the designated platform we're currently
+// running on.
+func (c *Config) IpcEndpoint() string {
+ // Short circuit if IPC has not been enabled
+ if c.IpcPath == "" {
+ return ""
+ }
+ // On windows we can only use plain top-level pipes
+ if runtime.GOOS == "windows" {
+ if strings.HasPrefix(c.IpcPath, `\\.\pipe\`) {
+ return c.IpcPath
+ }
+ return `\\.\pipe\` + c.IpcPath
+ }
+ // Resolve names into the data directory full paths otherwise
+ if filepath.Base(c.IpcPath) == c.IpcPath {
+ if c.DataDir == "" {
+ return filepath.Join(os.TempDir(), c.IpcPath)
+ }
+ return filepath.Join(c.DataDir, c.IpcPath)
+ }
+ return c.IpcPath
+}
+
+// DefaultIpcEndpoint returns the IPC path used by default.
+func DefaultIpcEndpoint() string {
+ config := &Config{DataDir: common.DefaultDataDir(), IpcPath: common.DefaultIpcSocket()}
+ return config.IpcEndpoint()
+}
+
// NodeKey retrieves the currently configured private key of the node, checking
// first any manually set key, falling back to the one found in the configured
// data folder. If no key can be found, a new one is generated.
diff --git a/node/config_test.go b/node/config_test.go
index f59f3c0fe..efb864ce4 100644
--- a/node/config_test.go
+++ b/node/config_test.go
@@ -21,6 +21,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
+ "runtime"
"testing"
"github.com/ethereum/go-ethereum/crypto"
@@ -60,6 +61,37 @@ func TestDatadirCreation(t *testing.T) {
}
}
+// Tests that IPC paths are correctly resolved to valid endpoints of different
+// platforms.
+func TestIpcPathResolution(t *testing.T) {
+ var tests = []struct {
+ DataDir string
+ IpcPath string
+ Windows bool
+ Endpoint string
+ }{
+ {"", "", false, ""},
+ {"data", "", false, ""},
+ {"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
+ {"data", "geth.ipc", false, "data/geth.ipc"},
+ {"data", "./geth.ipc", false, "./geth.ipc"},
+ {"data", "/geth.ipc", false, "/geth.ipc"},
+ {"", "", true, ``},
+ {"data", "", true, ``},
+ {"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
+ {"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
+ {"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
+ }
+ for i, test := range tests {
+ // Only run when platform/test match
+ if (runtime.GOOS == "windows") == test.Windows {
+ if endpoint := (&Config{DataDir: test.DataDir, IpcPath: test.IpcPath}).IpcEndpoint(); endpoint != test.Endpoint {
+ t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
+ }
+ }
+ }
+}
+
// Tests that node keys can be correctly created, persisted, loaded and/or made
// ephemeral.
func TestNodeKeyPersistency(t *testing.T) {
diff --git a/node/node.go b/node/node.go
index 4af111a41..e3fc03360 100644
--- a/node/node.go
+++ b/node/node.go
@@ -19,6 +19,7 @@ package node
import (
"errors"
+ "net"
"os"
"path/filepath"
"reflect"
@@ -27,6 +28,8 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/debug"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)
@@ -52,6 +55,10 @@ type Node struct {
serviceFuncs []ServiceConstructor // Service constructors (in dependency order)
services map[reflect.Type]Service // Currently running services
+ ipcEndpoint string // IPC endpoint to listen at (empty = IPC disabled)
+ ipcListener net.Listener // IPC RPC listener socket to serve API requests
+ ipcHandler *rpc.Server // IPC RPC request handler to process the API requests
+
stop chan struct{} // Channel to wait for termination notifications
lock sync.RWMutex
}
@@ -87,6 +94,7 @@ func New(conf *Config) (*Node, error) {
MaxPendingPeers: conf.MaxPendingPeers,
},
serviceFuncs: []ServiceConstructor{},
+ ipcEndpoint: conf.IpcEndpoint(),
eventmux: new(event.TypeMux),
}, nil
}
@@ -164,6 +172,14 @@ func (n *Node) Start() error {
// Mark the service started for potential cleanup
started = append(started, kind)
}
+ // Lastly start the configured RPC interfaces
+ if err := n.startRPC(services); err != nil {
+ for _, service := range services {
+ service.Stop()
+ }
+ running.Stop()
+ return err
+ }
// Finish initializing the startup
n.services = services
n.server = running
@@ -172,6 +188,58 @@ func (n *Node) Start() error {
return nil
}
+// startRPC initializes and starts the IPC RPC endpoints.
+func (n *Node) startRPC(services map[reflect.Type]Service) error {
+ // Gather and register all the APIs exposed by the services
+ apis := n.apis()
+ for _, service := range services {
+ apis = append(apis, service.APIs()...)
+ }
+ ipcHandler := rpc.NewServer()
+ for _, api := range apis {
+ if err := ipcHandler.RegisterName(api.Namespace, api.Service); err != nil {
+ return err
+ }
+ glog.V(logger.Debug).Infof("Register %T under namespace '%s'", api.Service, api.Namespace)
+ }
+ // All APIs registered, start the IPC and HTTP listeners
+ var (
+ ipcListener net.Listener
+ err error
+ )
+ if n.ipcEndpoint != "" {
+ if ipcListener, err = rpc.CreateIPCListener(n.ipcEndpoint); err != nil {
+ return err
+ }
+ go func() {
+ glog.V(logger.Info).Infof("IPC endpoint opened: %s", n.ipcEndpoint)
+ defer glog.V(logger.Info).Infof("IPC endpoint closed: %s", n.ipcEndpoint)
+
+ for {
+ conn, err := ipcListener.Accept()
+ if err != nil {
+ // Terminate if the listener was closed
+ n.lock.RLock()
+ closed := n.ipcListener == nil
+ n.lock.RUnlock()
+ if closed {
+ return
+ }
+ // Not closed, just some error; report and continue
+ glog.V(logger.Error).Infof("IPC accept failed: %v", err)
+ continue
+ }
+ go ipcHandler.ServeCodec(rpc.NewJSONCodec(conn))
+ }
+ }()
+ }
+ // All listeners booted successfully
+ n.ipcListener = ipcListener
+ n.ipcHandler = ipcHandler
+
+ return nil
+}
+
// Stop terminates a running node along with all it's services. In the node was
// not started, an error is returned.
func (n *Node) Stop() error {
@@ -182,7 +250,15 @@ func (n *Node) Stop() error {
if n.server == nil {
return ErrNodeStopped
}
- // Otherwise terminate all the services and the P2P server too
+ // Otherwise terminate the API, all services and the P2P server too
+ if n.ipcListener != nil {
+ n.ipcListener.Close()
+ n.ipcListener = nil
+ }
+ if n.ipcHandler != nil {
+ n.ipcHandler.Stop()
+ n.ipcHandler = nil
+ }
failure := &StopError{
Services: make(map[reflect.Type]error),
}
@@ -261,18 +337,20 @@ func (n *Node) DataDir() string {
return n.datadir
}
+// IpcEndpoint retrieves the current IPC endpoint used by the protocol stack.
+func (n *Node) IpcEndpoint() string {
+ return n.ipcEndpoint
+}
+
// EventMux retrieves the event multiplexer used by all the network services in
// the current protocol stack.
func (n *Node) EventMux() *event.TypeMux {
return n.eventmux
}
-// APIs returns the collection of RPC descriptor this node offers. This method
-// is just a quick placeholder passthrough for the RPC update, which in the next
-// step will be fully integrated into the node itself.
-func (n *Node) APIs() []rpc.API {
- // Define all the APIs owned by the node itself
- apis := []rpc.API{
+// apis returns the collection of RPC descriptors this node offers.
+func (n *Node) apis() []rpc.API {
+ return []rpc.API{
{
Namespace: "admin",
Version: "1.0",
@@ -298,7 +376,13 @@ func (n *Node) APIs() []rpc.API {
Public: true,
},
}
- // Inject all the APIs owned by various services
+}
+
+// APIs returns the collection of RPC descriptor this node offers. This method
+// is just a quick placeholder passthrough for the RPC update, which in the next
+// step will be fully integrated into the node itself.
+func (n *Node) APIs() []rpc.API {
+ apis := n.apis()
for _, api := range n.services {
apis = append(apis, api.APIs()...)
}
diff --git a/node/node_example_test.go b/node/node_example_test.go
index 5ff5d06a6..01ff683c0 100644
--- a/node/node_example_test.go
+++ b/node/node_example_test.go
@@ -31,6 +31,7 @@ import (
//
// The following methods are needed to implement a node.Service:
// - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on
+// - APIs() []rpc.API - api methods the service wants to expose on rpc channels
// - Start() error - method invoked when the node is ready to start the service
// - Stop() error - method invoked when the node terminates the service
type SampleService struct{}
diff --git a/node/node_test.go b/node/node_test.go
index ef096fc91..53dcbcf74 100644
--- a/node/node_test.go
+++ b/node/node_test.go
@@ -18,27 +18,34 @@ package node
import (
"errors"
+ "fmt"
"io/ioutil"
+ "math/rand"
"os"
"reflect"
"testing"
+ "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/rpc"
)
var (
testNodeKey, _ = crypto.GenerateKey()
+)
- testNodeConfig = &Config{
+func testNodeConfig() *Config {
+ return &Config{
+ IpcPath: fmt.Sprintf("test-%d.ipc", rand.Int63()),
PrivateKey: testNodeKey,
Name: "test node",
}
-)
+}
// Tests that an empty protocol stack can be started, restarted and stopped.
func TestNodeLifeCycle(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -101,7 +108,7 @@ func TestNodeUsedDataDir(t *testing.T) {
// Tests whether services can be registered and duplicates caught.
func TestServiceRegistry(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -133,7 +140,7 @@ func TestServiceRegistry(t *testing.T) {
// Tests that registered services get started and stopped correctly.
func TestServiceLifeCycle(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -183,7 +190,7 @@ func TestServiceLifeCycle(t *testing.T) {
// Tests that services are restarted cleanly as new instances.
func TestServiceRestarts(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -231,7 +238,7 @@ func TestServiceRestarts(t *testing.T) {
// Tests that if a service fails to initialize itself, none of the other services
// will be allowed to even start.
func TestServiceConstructionAbortion(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -278,7 +285,7 @@ func TestServiceConstructionAbortion(t *testing.T) {
// Tests that if a service fails to start, all others started before it will be
// shut down.
func TestServiceStartupAbortion(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -331,7 +338,7 @@ func TestServiceStartupAbortion(t *testing.T) {
// Tests that even if a registered service fails to shut down cleanly, it does
// not influece the rest of the shutdown invocations.
func TestServiceTerminationGuarantee(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -406,7 +413,7 @@ func TestServiceTerminationGuarantee(t *testing.T) {
// TestServiceRetrieval tests that individual services can be retrieved.
func TestServiceRetrieval(t *testing.T) {
// Create a simple stack and register two service types
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -441,7 +448,7 @@ func TestServiceRetrieval(t *testing.T) {
// Tests that all protocols defined by individual services get launched.
func TestProtocolGather(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
@@ -494,3 +501,75 @@ func TestProtocolGather(t *testing.T) {
}
}
}
+
+// Tests that all APIs defined by individual services get exposed.
+func TestAPIGather(t *testing.T) {
+ stack, err := New(testNodeConfig())
+ if err != nil {
+ t.Fatalf("failed to create protocol stack: %v", err)
+ }
+ // Register a batch of services with some configured APIs
+ calls := make(chan string, 1)
+
+ services := map[string]struct {
+ APIs []rpc.API
+ Maker InstrumentingWrapper
+ }{
+ "Zero APIs": {[]rpc.API{}, InstrumentedServiceMakerA},
+ "Single API": {[]rpc.API{
+ {"single", "1", &OneMethodApi{fun: func() { calls <- "single.v1" }}, true},
+ }, InstrumentedServiceMakerB},
+ "Many APIs": {[]rpc.API{
+ {"multi", "1", &OneMethodApi{fun: func() { calls <- "multi.v1" }}, true},
+ {"multi.v2", "2", &OneMethodApi{fun: func() { calls <- "multi.v2" }}, true},
+ {"multi.v2.nested", "2", &OneMethodApi{fun: func() { calls <- "multi.v2.nested" }}, true},
+ }, InstrumentedServiceMakerC},
+ }
+ for id, config := range services {
+ config := config
+ constructor := func(*ServiceContext) (Service, error) {
+ return &InstrumentedService{apis: config.APIs}, nil
+ }
+ if err := stack.Register(config.Maker(constructor)); err != nil {
+ t.Fatalf("service %s: registration failed: %v", id, err)
+ }
+ }
+ // Start the services and ensure all API start successfully
+ if err := stack.Start(); err != nil {
+ t.Fatalf("failed to start protocol stack: %v", err)
+ }
+ defer stack.Stop()
+
+ // Connect to the RPC server and verify the various registered endpoints
+ ipcClient, err := rpc.NewIPCClient(stack.IpcEndpoint())
+ if err != nil {
+ t.Fatalf("failed to connect to the IPC API server: %v", err)
+ }
+
+ tests := []struct {
+ Method string
+ Result string
+ }{
+ {"single_theOneMethod", "single.v1"},
+ {"multi_theOneMethod", "multi.v1"},
+ {"multi.v2_theOneMethod", "multi.v2"},
+ {"multi.v2.nested_theOneMethod", "multi.v2.nested"},
+ }
+ for i, test := range tests {
+ if err := ipcClient.Send(rpc.JSONRequest{Id: new(int64), Version: "2.0", Method: test.Method}); err != nil {
+ t.Fatalf("test %d: failed to send API request: %v", i, err)
+ }
+ reply := new(rpc.JSONSuccessResponse)
+ if err := ipcClient.Recv(reply); err != nil {
+ t.Fatalf("test %d: failed to read API reply: %v", i, err)
+ }
+ select {
+ case result := <-calls:
+ if result != test.Result {
+ t.Errorf("test %d: result mismatch: have %s, want %s", i, result, test.Result)
+ }
+ case <-time.After(time.Second):
+ t.Fatalf("test %d: rpc execution timeout", i)
+ }
+ }
+}
diff --git a/node/service_test.go b/node/service_test.go
index 1dfb61f73..cfe7fe5dc 100644
--- a/node/service_test.go
+++ b/node/service_test.go
@@ -63,7 +63,7 @@ func TestContextDatabases(t *testing.T) {
// Tests that already constructed services can be retrieves by later ones.
func TestContextServices(t *testing.T) {
- stack, err := New(testNodeConfig)
+ stack, err := New(testNodeConfig())
if err != nil {
t.Fatalf("failed to create protocol stack: %v", err)
}
diff --git a/node/utils_test.go b/node/utils_test.go
index 7755605ae..7cdfc2b3a 100644
--- a/node/utils_test.go
+++ b/node/utils_test.go
@@ -52,6 +52,7 @@ func NewNoopServiceD(*ServiceContext) (Service, error) { return new(NoopServiceD
// methods can be instrumented both return value as well as event hook wise.
type InstrumentedService struct {
protocols []p2p.Protocol
+ apis []rpc.API
start error
stop error
@@ -70,7 +71,7 @@ func (s *InstrumentedService) Protocols() []p2p.Protocol {
}
func (s *InstrumentedService) APIs() []rpc.API {
- return nil
+ return s.apis
}
func (s *InstrumentedService) Start(server *p2p.Server) error {
@@ -121,3 +122,14 @@ func InstrumentedServiceMakerB(base ServiceConstructor) ServiceConstructor {
func InstrumentedServiceMakerC(base ServiceConstructor) ServiceConstructor {
return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceC{}))
}
+
+// OneMethodApi is a single-method API handler to be returned by test services.
+type OneMethodApi struct {
+ fun func()
+}
+
+func (api *OneMethodApi) TheOneMethod() {
+ if api.fun != nil {
+ api.fun()
+ }
+}