diff --git a/cmd/console/js.go b/cmd/console/js.go index 2a0602e518..f2f74f75bf 100644 --- a/cmd/console/js.go +++ b/cmd/console/js.go @@ -67,14 +67,14 @@ type jsre struct { prompter } -func newJSRE(datadir, libPath string) *jsre { +func newJSRE(datadir, libPath, ipcpath string) *jsre { js := &jsre{ps1: "> "} js.wait = make(chan *big.Int) // update state in separare forever blocks js.re = re.New(libPath) - js.apiBindings() - // js.adminBindings() + js.apiBindings(ipcpath) + // TODO js.adminBindings() if !liner.TerminalSupported() { js.prompter = dumbterm{bufio.NewReader(os.Stdin)} @@ -92,9 +92,9 @@ func newJSRE(datadir, libPath string) *jsre { return js } -func (js *jsre) apiBindings() { +func (js *jsre) apiBindings(ipcpath string) { ethApi := rpc.NewEthereumApi(nil) - jeth := rpc.NewJeth(ethApi, js.re) + jeth := rpc.NewJeth(ethApi, js.re, ipcpath) js.re.Set("jeth", struct{}{}) t, _ := js.re.Get("jeth") diff --git a/cmd/console/main.go b/cmd/console/main.go index a5a59778cd..836492738b 100644 --- a/cmd/console/main.go +++ b/cmd/console/main.go @@ -21,11 +21,15 @@ package main import ( + "fmt" "io" "os" "github.com/mattn/go-colorable" "github.com/mattn/go-isatty" + "github.com/codegangsta/cli" + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/logger" ) const ( @@ -33,6 +37,34 @@ const ( Version = "0.9.26" ) +var ( + gitCommit string // set via linker flag + nodeNameVersion string + app = utils.NewApp(Version, "the ether console") +) + +func init() { + if gitCommit == "" { + nodeNameVersion = Version + } else { + nodeNameVersion = Version + "-" + gitCommit[:8] + } + + app.Action = run + app.Flags = []cli.Flag{ + utils.DataDirFlag, + utils.IPCDisabledFlag, + utils.IPCPathFlag, + utils.VerbosityFlag, + utils.JSpathFlag, + } + + app.Before = func(ctx *cli.Context) error { + utils.SetupLogger(ctx) + return nil + } +} + func main() { // Wrap the standard output with a colorified stream (windows) if isatty.IsTerminal(os.Stdout.Fd()) { @@ -42,7 +74,32 @@ func main() { } } - // TODO, datadir + jspath - repl := newJSRE("/home/bas/.ethereum", ".") + var interrupted = false + utils.RegisterInterrupt(func(os.Signal) { + interrupted = true + }) + utils.HandleInterrupt() + + if err := app.Run(os.Args); err != nil { + fmt.Fprintln(os.Stderr, "Error: ", err) + } + + // we need to run the interrupt callbacks in case gui is closed + // this skips if we got here by actual interrupt stopping the GUI + if !interrupted { + utils.RunInterruptCallbacks(os.Interrupt) + } + logger.Flush() +} + +func run(ctx *cli.Context) { + //start := time.Now() + + datadir := ctx.GlobalString(utils.DataDirFlag.Name) + jspath := ctx.GlobalString(utils.JSpathFlag.Name) + ipcpath := ctx.GlobalString(utils.IPCPathFlag.Name) + + repl := newJSRE(datadir, jspath, ipcpath) + repl.interactive() } diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 706bc6554f..540f07be76 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -73,7 +73,7 @@ type jsre struct { prompter } -func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, interactive bool, f xeth.Frontend) *jsre { +func newJSRE(ethereum *eth.Ethereum, libPath, ipcpath, corsDomain string, interactive bool, f xeth.Frontend) *jsre { js := &jsre{ethereum: ethereum, ps1: "> "} // set default cors domain used by startRpc from CLI flag js.corsDomain = corsDomain @@ -84,7 +84,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, interactive boo js.wait = js.xeth.UpdateState() // update state in separare forever blocks js.re = re.New(libPath) - js.apiBindings(f) + js.apiBindings(f, ipcpath) js.adminBindings() if !liner.TerminalSupported() || !interactive { @@ -103,10 +103,10 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, interactive boo return js } -func (js *jsre) apiBindings(f xeth.Frontend) { +func (js *jsre) apiBindings(f xeth.Frontend, ipcpath string) { xe := xeth.New(js.ethereum, f) ethApi := rpc.NewEthereumApi(xe) - jeth := rpc.NewJeth(ethApi, js.re) + jeth := rpc.NewJeth(ethApi, js.re, ipcpath) js.re.Set("jeth", struct{}{}) t, _ := js.re.Get("jeth") diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 595ca51aa1..d092826bc0 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -238,7 +238,8 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso utils.RPCEnabledFlag, utils.RPCListenAddrFlag, utils.RPCPortFlag, - utils.IPCEnabledFlag, + utils.IPCDisabledFlag, + utils.IPCPathFlag, utils.WhisperEnabledFlag, utils.VMDebugFlag, utils.ProtocolVersionFlag, @@ -304,6 +305,7 @@ func console(ctx *cli.Context) { repl := newJSRE( ethereum, ctx.String(utils.JSpathFlag.Name), + ctx.String(utils.IPCPathFlag.Name), ctx.GlobalString(utils.RPCCORSDomainFlag.Name), true, nil, @@ -325,6 +327,7 @@ func execJSFiles(ctx *cli.Context) { repl := newJSRE( ethereum, ctx.String(utils.JSpathFlag.Name), + ctx.String(utils.IPCPathFlag.Name), ctx.GlobalString(utils.RPCCORSDomainFlag.Name), false, nil, @@ -387,7 +390,7 @@ func startEth(ctx *cli.Context, eth *eth.Ethereum) { utils.Fatalf("Error starting RPC: %v", err) } } - if ctx.GlobalBool(utils.IPCEnabledFlag.Name) { + if !ctx.GlobalBool(utils.IPCDisabledFlag.Name) { if err := utils.StartIPC(eth, ctx); err != nil { utils.Fatalf("Error starting IPC: %v", err) } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 3b73112256..5f177faef6 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -204,9 +204,14 @@ var ( Usage: "Domain on which to send Access-Control-Allow-Origin header", Value: "", } - IPCEnabledFlag = cli.BoolFlag{ - Name: "ipc", - Usage: "Enable the IPC-RPC server", + IPCDisabledFlag = cli.BoolFlag{ + Name: "ipcdisable", + Usage: "Disable the IPC-RPC server", + } + IPCPathFlag = cli.StringFlag{ + Name: "ipcpath", + Usage: "Filename for IPC socket/pipe", + Value: common.DefaultIpcPath(), } // Network Settings MaxPeersFlag = cli.IntFlag{ @@ -378,7 +383,7 @@ func StartRPC(eth *eth.Ethereum, ctx *cli.Context) error { func StartIPC(eth *eth.Ethereum, ctx *cli.Context) error { config := comms.IpcConfig{ - Endpoint: ctx.GlobalString(DataDirFlag.Name) + "/geth.sock", + Endpoint: ctx.GlobalString(IPCPathFlag.Name), } xeth := xeth.New(eth, nil) diff --git a/common/path.go b/common/path.go index 3468b3366f..63a23abcd1 100644 --- a/common/path.go +++ b/common/path.go @@ -94,6 +94,10 @@ func DefaultDataDir() string { } } +func DefaultIpcPath() string { + return filepath.Join(DefaultDataDir(), "geth.ipc") +} + func IsWindows() bool { return runtime.GOOS == "windows" } diff --git a/rpc/jeth.go b/rpc/jeth.go index c120c2583d..9c13998551 100644 --- a/rpc/jeth.go +++ b/rpc/jeth.go @@ -13,12 +13,13 @@ import ( ) type Jeth struct { - ethApi *EthereumApi - re *jsre.JSRE + ethApi *EthereumApi + re *jsre.JSRE + ipcpath string } -func NewJeth(ethApi *EthereumApi, re *jsre.JSRE) *Jeth { - return &Jeth{ethApi, re} +func NewJeth(ethApi *EthereumApi, re *jsre.JSRE, ipcpath string) *Jeth { + return &Jeth{ethApi, re, ipcpath} } func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) { @@ -39,9 +40,8 @@ func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { if err != nil { return self.err(call, -32700, err.Error(), nil) } - - // TODO - client, err := comms.NewIpcClient(comms.IpcConfig{"/home/bas/.ethereum/geth.sock"}, codec.JSON) + + client, err := comms.NewIpcClient(comms.IpcConfig{self.ipcpath}, codec.JSON) if err != nil { fmt.Println("Error response:", err) return self.err(call, -32603, err.Error(), -1)