Skip to content

Commit 188ab92

Browse files
committed
cmd, common, node, rpc: move IPC into the node itself
1 parent 3274db1 commit 188ab92

14 files changed

Lines changed: 292 additions & 156 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
502502
unlockAccount(ctx, accman, trimmed, i, passwords)
503503
}
504504
}
505-
// Start auxiliary services if enabled.
506-
if !ctx.GlobalBool(utils.IPCDisabledFlag.Name) {
507-
if err := utils.StartIPC(stack, ctx); err != nil {
508-
utils.Fatalf("Failed to start IPC: %v", err)
509-
}
510-
}
505+
// Start auxiliary services if enabled
511506
if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
512507
if err := utils.StartRPC(stack, ctx); err != nil {
513508
utils.Fatalf("Failed to start RPC: %v", err)

‎cmd/geth/monitorcmd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ import (
2828

2929
"github.com/codegangsta/cli"
3030
"github.com/ethereum/go-ethereum/cmd/utils"
31-
"github.com/ethereum/go-ethereum/common"
31+
"github.com/ethereum/go-ethereum/node"
3232
"github.com/ethereum/go-ethereum/rpc"
3333
"github.com/gizak/termui"
3434
)
3535

3636
var (
3737
monitorCommandAttachFlag = cli.StringFlag{
3838
Name: "attach",
39-
Value: "ipc:" + common.DefaultIpcPath(),
39+
Value: "ipc:" + node.DefaultIpcEndpoint(),
4040
Usage: "API endpoint to attach to",
4141
}
4242
monitorCommandRowsFlag = cli.IntFlag{

‎cmd/gethrpctest/main.go‎

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,20 @@
1818
package main
1919

2020
import (
21+
"errors"
2122
"flag"
2223
"io/ioutil"
2324
"log"
2425
"os"
2526
"os/signal"
26-
"path/filepath"
27-
"runtime"
28-
29-
"errors"
3027

3128
"github.com/ethereum/go-ethereum/accounts"
32-
"github.com/ethereum/go-ethereum/common"
3329
"github.com/ethereum/go-ethereum/crypto"
3430
"github.com/ethereum/go-ethereum/eth"
3531
"github.com/ethereum/go-ethereum/ethdb"
3632
"github.com/ethereum/go-ethereum/logger"
3733
"github.com/ethereum/go-ethereum/logger/glog"
3834
"github.com/ethereum/go-ethereum/node"
39-
"github.com/ethereum/go-ethereum/rpc"
4035
"github.com/ethereum/go-ethereum/tests"
4136
"github.com/ethereum/go-ethereum/whisper"
4237
)
@@ -89,11 +84,6 @@ func main() {
8984
}
9085
log.Println("Initial test suite passed...")
9186

92-
if err := StartIPC(stack); err != nil {
93-
log.Fatalf("Failed to start IPC interface: %v\n", err)
94-
}
95-
log.Println("IPC Interface started, accepting requests...")
96-
9787
// Start the RPC interface and wait until terminated
9888
if err := StartRPC(stack); err != nil {
9989
log.Fatalf("Failed to start RPC interface: %v", err)
@@ -109,7 +99,7 @@ func main() {
10999
// keystore path and initial pre-state.
110100
func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) {
111101
// Create a networkless protocol stack
112-
stack, err := node.New(&node.Config{NoDiscovery: true})
102+
stack, err := node.New(&node.Config{IpcPath: node.DefaultIpcEndpoint(), NoDiscovery: true})
113103
if err != nil {
114104
return nil, err
115105
}
@@ -194,50 +184,3 @@ func StartRPC(stack *node.Node) error {
194184
glog.V(logger.Error).Infof("Unable to start RPC-HTTP interface, could not find admin API")
195185
return errors.New("Unable to start RPC-HTTP interface")
196186
}
197-
198-
// StartIPC initializes an IPC interface to the given protocol stack.
199-
func StartIPC(stack *node.Node) error {
200-
var ethereum *eth.Ethereum
201-
if err := stack.Service(&ethereum); err != nil {
202-
return err
203-
}
204-
205-
endpoint := `\\.\pipe\geth.ipc`
206-
if runtime.GOOS != "windows" {
207-
endpoint = filepath.Join(common.DefaultDataDir(), "geth.ipc")
208-
}
209-
210-
listener, err := rpc.CreateIPCListener(endpoint)
211-
if err != nil {
212-
return err
213-
}
214-
215-
server := rpc.NewServer()
216-
217-
// register package API's this node provides
218-
offered := stack.APIs()
219-
for _, api := range offered {
220-
server.RegisterName(api.Namespace, api.Service)
221-
glog.V(logger.Debug).Infof("Register %T under namespace '%s' for IPC service\n", api.Service, api.Namespace)
222-
}
223-
224-
//var ethereum *eth.Ethereum
225-
//if err := stack.Service(&ethereum); err != nil {
226-
// return err
227-
//}
228-
229-
go func() {
230-
glog.V(logger.Info).Infof("Start IPC server on %s\n", endpoint)
231-
for {
232-
conn, err := listener.Accept()
233-
if err != nil {
234-
glog.V(logger.Error).Infof("Unable to accept connection - %v\n", err)
235-
}
236-
237-
codec := rpc.NewJSONCodec(conn)
238-
go server.ServeCodec(codec)
239-
}
240-
}()
241-
242-
return nil
243-
}

‎cmd/utils/client.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ func NewRemoteRPCClient(ctx *cli.Context) (rpc.Client, error) {
150150
endpoint := ctx.Args().First()
151151
return NewRemoteRPCClientFromString(endpoint)
152152
}
153-
154153
// use IPC by default
155-
endpoint := IPCSocketPath(ctx)
156-
return rpc.NewIPCClient(endpoint)
154+
return rpc.NewIPCClient(node.DefaultIpcEndpoint())
157155
}
158156

159157
// NewRemoteRPCClientFromString returns a RPC client which connects to the given

‎cmd/utils/flags.go‎

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ var (
261261
}
262262
IPCPathFlag = DirectoryFlag{
263263
Name: "ipcpath",
264-
Usage: "Filename for IPC socket/pipe",
265-
Value: DirectoryString{common.DefaultIpcPath()},
264+
Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)",
265+
Value: DirectoryString{common.DefaultIpcSocket()},
266266
}
267267
WSEnabledFlag = cli.BoolFlag{
268268
Name: "ws",
@@ -394,6 +394,15 @@ func MustMakeDataDir(ctx *cli.Context) string {
394394
return ""
395395
}
396396

397+
// MakeIpcPath creates an IPC path configuration from the set command line flags,
398+
// returning an empty string if IPC was explicitly disabled, or the set path.
399+
func MakeIpcPath(ctx *cli.Context) string {
400+
if ctx.GlobalBool(IPCDisabledFlag.Name) {
401+
return ""
402+
}
403+
return ctx.GlobalString(IPCPathFlag.Name)
404+
}
405+
397406
// MakeNodeKey creates a node key from set command line flags, either loading it
398407
// from a file or as a specified hex value. If neither flags were provided, this
399408
// method returns nil and an emphemeral key is to be generated.
@@ -582,6 +591,7 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
582591
// Configure the node's service container
583592
stackConf := &node.Config{
584593
DataDir: MustMakeDataDir(ctx),
594+
IpcPath: MakeIpcPath(ctx),
585595
PrivateKey: MakeNodeKey(ctx),
586596
Name: MakeNodeName(name, version, ctx),
587597
NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name),
@@ -734,63 +744,6 @@ func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database
734744
return chain, chainDb
735745
}
736746

737-
func IPCSocketPath(ctx *cli.Context) (ipcpath string) {
738-
if runtime.GOOS == "windows" {
739-
ipcpath = common.DefaultIpcPath()
740-
if ctx.GlobalIsSet(IPCPathFlag.Name) {
741-
ipcpath = ctx.GlobalString(IPCPathFlag.Name)
742-
}
743-
} else {
744-
ipcpath = common.DefaultIpcPath()
745-
if ctx.GlobalIsSet(DataDirFlag.Name) {
746-
ipcpath = filepath.Join(ctx.GlobalString(DataDirFlag.Name), "geth.ipc")
747-
}
748-
if ctx.GlobalIsSet(IPCPathFlag.Name) {
749-
ipcpath = ctx.GlobalString(IPCPathFlag.Name)
750-
}
751-
}
752-
753-
return
754-
}
755-
756-
func StartIPC(stack *node.Node, ctx *cli.Context) error {
757-
var ethereum *eth.Ethereum
758-
if err := stack.Service(&ethereum); err != nil {
759-
return err
760-
}
761-
762-
endpoint := IPCSocketPath(ctx)
763-
listener, err := rpc.CreateIPCListener(endpoint)
764-
if err != nil {
765-
return err
766-
}
767-
768-
server := rpc.NewServer()
769-
770-
// register package API's this node provides
771-
offered := stack.APIs()
772-
for _, api := range offered {
773-
server.RegisterName(api.Namespace, api.Service)
774-
glog.V(logger.Debug).Infof("Register %T under namespace '%s' for IPC service\n", api.Service, api.Namespace)
775-
}
776-
777-
go func() {
778-
glog.V(logger.Info).Infof("Start IPC server on %s\n", endpoint)
779-
for {
780-
conn, err := listener.Accept()
781-
if err != nil {
782-
glog.V(logger.Error).Infof("Unable to accept connection - %v\n", err)
783-
}
784-
785-
codec := rpc.NewJSONCodec(conn)
786-
go server.ServeCodec(codec)
787-
}
788-
}()
789-
790-
return nil
791-
792-
}
793-
794747
// StartRPC starts a HTTP JSON-RPC API server.
795748
func StartRPC(stack *node.Node, ctx *cli.Context) error {
796749
for _, api := range stack.APIs() {

‎common/path.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ func DefaultDataDir() string {
8989
return ""
9090
}
9191

92-
func DefaultIpcPath() string {
93-
if runtime.GOOS == "windows" {
94-
return `\\.\pipe\geth.ipc`
95-
}
96-
return filepath.Join(DefaultDataDir(), "geth.ipc")
92+
// DefaultIpcSocket returns the relative name of the default IPC socket. The path
93+
// resolution is done by a node with other contextual infos.
94+
func DefaultIpcSocket() string {
95+
return "geth.ipc"
9796
}

‎node/config.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import (
2323
"net"
2424
"os"
2525
"path/filepath"
26+
"runtime"
27+
"strings"
2628

29+
"github.com/ethereum/go-ethereum/common"
2730
"github.com/ethereum/go-ethereum/crypto"
2831
"github.com/ethereum/go-ethereum/logger"
2932
"github.com/ethereum/go-ethereum/logger/glog"
@@ -49,6 +52,12 @@ type Config struct {
4952
// in memory.
5053
DataDir string
5154

55+
// IpcPath is the requested location to place the IPC endpoint. If the path is
56+
// a simple file name, it is placed inside the data directory (or on the root
57+
// pipe path on Windows), whereas if it's a resolvable path name (absolute or
58+
// relative), then that specific path is enforced. An empty path disables IPC.
59+
IpcPath string
60+
5261
// This field should be a valid secp256k1 private key that will be used for both
5362
// remote peer identification as well as network traffic encryption. If no key
5463
// is configured, the preset one is loaded from the data dir, generating it if
@@ -90,6 +99,37 @@ type Config struct {
9099
MaxPendingPeers int
91100
}
92101

102+
// IpcEndpoint resolves an IPC endpoint based on a configured value, taking into
103+
// account the set data folders as well as the designated platform we're currently
104+
// running on.
105+
func (c *Config) IpcEndpoint() string {
106+
// Short circuit if IPC has not been enabled
107+
if c.IpcPath == "" {
108+
return ""
109+
}
110+
// On windows we can only use plain top-level pipes
111+
if runtime.GOOS == "windows" {
112+
if strings.HasPrefix(c.IpcPath, `\\.\pipe\`) {
113+
return c.IpcPath
114+
}
115+
return `\\.\pipe\` + c.IpcPath
116+
}
117+
// Resolve names into the data directory full paths otherwise
118+
if filepath.Base(c.IpcPath) == c.IpcPath {
119+
if c.DataDir == "" {
120+
return filepath.Join(os.TempDir(), c.IpcPath)
121+
}
122+
return filepath.Join(c.DataDir, c.IpcPath)
123+
}
124+
return c.IpcPath
125+
}
126+
127+
// DefaultIpcEndpoint returns the IPC path used by default.
128+
func DefaultIpcEndpoint() string {
129+
config := &Config{DataDir: common.DefaultDataDir(), IpcPath: common.DefaultIpcSocket()}
130+
return config.IpcEndpoint()
131+
}
132+
93133
// NodeKey retrieves the currently configured private key of the node, checking
94134
// first any manually set key, falling back to the one found in the configured
95135
// data folder. If no key can be found, a new one is generated.

‎node/config_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"runtime"
2425
"testing"
2526

2627
"github.com/ethereum/go-ethereum/crypto"
@@ -60,6 +61,37 @@ func TestDatadirCreation(t *testing.T) {
6061
}
6162
}
6263

64+
// Tests that IPC paths are correctly resolved to valid endpoints of different
65+
// platforms.
66+
func TestIpcPathResolution(t *testing.T) {
67+
var tests = []struct {
68+
DataDir string
69+
IpcPath string
70+
Windows bool
71+
Endpoint string
72+
}{
73+
{"", "", false, ""},
74+
{"data", "", false, ""},
75+
{"", "geth.ipc", false, filepath.Join(os.TempDir(), "geth.ipc")},
76+
{"data", "geth.ipc", false, "data/geth.ipc"},
77+
{"data", "./geth.ipc", false, "./geth.ipc"},
78+
{"data", "/geth.ipc", false, "/geth.ipc"},
79+
{"", "", true, ``},
80+
{"data", "", true, ``},
81+
{"", "geth.ipc", true, `\\.\pipe\geth.ipc`},
82+
{"data", "geth.ipc", true, `\\.\pipe\geth.ipc`},
83+
{"data", `\\.\pipe\geth.ipc`, true, `\\.\pipe\geth.ipc`},
84+
}
85+
for i, test := range tests {
86+
// Only run when platform/test match
87+
if (runtime.GOOS == "windows") == test.Windows {
88+
if endpoint := (&Config{DataDir: test.DataDir, IpcPath: test.IpcPath}).IpcEndpoint(); endpoint != test.Endpoint {
89+
t.Errorf("test %d: IPC endpoint mismatch: have %s, want %s", i, endpoint, test.Endpoint)
90+
}
91+
}
92+
}
93+
}
94+
6395
// Tests that node keys can be correctly created, persisted, loaded and/or made
6496
// ephemeral.
6597
func TestNodeKeyPersistency(t *testing.T) {

0 commit comments

Comments
 (0)