2016-05-06 04:40:23 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-11-28 07:00:00 -06:00
|
|
|
"fmt"
|
2016-07-12 10:47:15 -05:00
|
|
|
"strings"
|
2016-05-06 04:40:23 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
|
|
"github.com/ethereum/go-ethereum/console"
|
2016-07-12 10:47:15 -05:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-06-27 11:22:36 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2016-05-06 04:40:23 -05:00
|
|
|
)
|
|
|
|
|
2017-05-03 05:26:09 -05:00
|
|
|
var (
|
|
|
|
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
|
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
consoleCommand = &cli.Command{
|
|
|
|
Action: localConsole,
|
|
|
|
Name: "console",
|
|
|
|
Usage: "Start an interactive JavaScript environment",
|
|
|
|
Flags: utils.GroupFlags(nodeFlags, rpcFlags, consoleFlags),
|
2016-05-06 04:40:23 -05:00
|
|
|
Description: `
|
|
|
|
The Geth console is an interactive shell for the JavaScript runtime environment
|
|
|
|
which exposes a node admin interface as well as the Ðapp JavaScript API.
|
2020-12-27 15:01:28 -06:00
|
|
|
See https://geth.ethereum.org/docs/interface/javascript-console.`,
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2017-05-03 05:26:09 -05:00
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
attachCommand = &cli.Command{
|
|
|
|
Action: remoteConsole,
|
2016-11-10 05:00:09 -06:00
|
|
|
Name: "attach",
|
|
|
|
Usage: "Start an interactive JavaScript environment (connect to node)",
|
2017-05-03 05:26:09 -05:00
|
|
|
ArgsUsage: "[endpoint]",
|
2022-05-03 01:46:17 -05:00
|
|
|
Flags: utils.GroupFlags([]cli.Flag{utils.DataDirFlag}, consoleFlags),
|
2016-05-06 04:40:23 -05:00
|
|
|
Description: `
|
|
|
|
The Geth console is an interactive shell for the JavaScript runtime environment
|
|
|
|
which exposes a node admin interface as well as the Ðapp JavaScript API.
|
2020-12-27 15:01:28 -06:00
|
|
|
See https://geth.ethereum.org/docs/interface/javascript-console.
|
2017-05-03 05:26:09 -05:00
|
|
|
This command allows to open a console on a running geth node.`,
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2017-05-03 05:26:09 -05:00
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
javascriptCommand = &cli.Command{
|
|
|
|
Action: ephemeralConsole,
|
2016-11-10 05:00:09 -06:00
|
|
|
Name: "js",
|
2022-06-21 04:16:05 -05:00
|
|
|
Usage: "(DEPRECATED) Execute the specified JavaScript files",
|
2017-05-03 05:26:09 -05:00
|
|
|
ArgsUsage: "<jsfile> [jsfile...]",
|
2022-05-03 01:46:17 -05:00
|
|
|
Flags: utils.GroupFlags(nodeFlags, consoleFlags),
|
2016-05-06 04:40:23 -05:00
|
|
|
Description: `
|
|
|
|
The JavaScript VM exposes a node admin interface as well as the Ðapp
|
2020-12-27 15:01:28 -06:00
|
|
|
JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console`,
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// localConsole starts a new geth node, attaching a JavaScript console to it at the
|
|
|
|
// same time.
|
2016-06-10 03:23:00 -05:00
|
|
|
func localConsole(ctx *cli.Context) error {
|
2016-05-06 04:40:23 -05:00
|
|
|
// Create and start the node based on the CLI flags
|
2019-08-15 07:54:16 -05:00
|
|
|
prepare(ctx)
|
2020-08-03 12:40:46 -05:00
|
|
|
stack, backend := makeFullNode(ctx)
|
2021-12-11 09:51:05 -06:00
|
|
|
startNode(ctx, stack, backend, true)
|
2020-08-03 12:40:46 -05:00
|
|
|
defer stack.Close()
|
2016-05-06 04:40:23 -05:00
|
|
|
|
2021-12-11 09:51:05 -06:00
|
|
|
// Attach to the newly started node and create the JavaScript console.
|
2020-08-03 12:40:46 -05:00
|
|
|
client, err := stack.Attach()
|
2016-05-06 04:40:23 -05:00
|
|
|
if err != nil {
|
2021-12-11 09:51:05 -06:00
|
|
|
return fmt.Errorf("Failed to attach to the inproc geth: %v", err)
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
config := console.Config{
|
2017-05-03 05:26:09 -05:00
|
|
|
DataDir: utils.MakeDataDir(ctx),
|
2022-06-27 11:22:36 -05:00
|
|
|
DocRoot: ctx.String(utils.JSpathFlag.Name),
|
2016-05-06 04:40:23 -05:00
|
|
|
Client: client,
|
|
|
|
Preload: utils.MakeConsolePreloads(ctx),
|
|
|
|
}
|
|
|
|
console, err := console.New(config)
|
|
|
|
if err != nil {
|
2021-12-11 09:51:05 -06:00
|
|
|
return fmt.Errorf("Failed to start the JavaScript console: %v", err)
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
defer console.Stop(false)
|
|
|
|
|
2021-12-11 09:51:05 -06:00
|
|
|
// If only a short execution was requested, evaluate and return.
|
2022-06-27 11:22:36 -05:00
|
|
|
if script := ctx.String(utils.ExecFlag.Name); script != "" {
|
2016-05-06 04:40:23 -05:00
|
|
|
console.Evaluate(script)
|
2016-06-10 03:23:00 -05:00
|
|
|
return nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2021-12-11 09:51:05 -06:00
|
|
|
|
|
|
|
// Track node shutdown and stop the console when it goes down.
|
|
|
|
// This happens when SIGTERM is sent to the process.
|
|
|
|
go func() {
|
|
|
|
stack.Wait()
|
|
|
|
console.StopInteractive()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Print the welcome screen and enter interactive mode.
|
2016-05-06 04:40:23 -05:00
|
|
|
console.Welcome()
|
|
|
|
console.Interactive()
|
2016-06-10 03:23:00 -05:00
|
|
|
return nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// remoteConsole will connect to a remote geth instance, attaching a JavaScript
|
|
|
|
// console to it.
|
2016-06-10 03:23:00 -05:00
|
|
|
func remoteConsole(ctx *cli.Context) error {
|
2022-07-04 12:52:19 -05:00
|
|
|
if ctx.Args().Len() > 1 {
|
|
|
|
utils.Fatalf("invalid command-line: too many arguments")
|
|
|
|
}
|
|
|
|
|
2017-11-28 07:00:00 -06:00
|
|
|
endpoint := ctx.Args().First()
|
2017-12-05 04:17:38 -06:00
|
|
|
if endpoint == "" {
|
2022-06-21 04:16:05 -05:00
|
|
|
cfg := defaultNodeConfig()
|
|
|
|
utils.SetDataDir(ctx, &cfg)
|
|
|
|
endpoint = cfg.IPCEndpoint()
|
2017-11-28 07:00:00 -06:00
|
|
|
}
|
|
|
|
client, err := dialRPC(endpoint)
|
2016-05-06 04:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Unable to attach to remote geth: %v", err)
|
|
|
|
}
|
|
|
|
config := console.Config{
|
2016-08-18 06:28:17 -05:00
|
|
|
DataDir: utils.MakeDataDir(ctx),
|
2022-06-27 11:22:36 -05:00
|
|
|
DocRoot: ctx.String(utils.JSpathFlag.Name),
|
2016-05-06 04:40:23 -05:00
|
|
|
Client: client,
|
|
|
|
Preload: utils.MakeConsolePreloads(ctx),
|
|
|
|
}
|
|
|
|
console, err := console.New(config)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to start the JavaScript console: %v", err)
|
|
|
|
}
|
|
|
|
defer console.Stop(false)
|
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
if script := ctx.String(utils.ExecFlag.Name); script != "" {
|
2016-05-06 04:40:23 -05:00
|
|
|
console.Evaluate(script)
|
2016-06-10 03:23:00 -05:00
|
|
|
return nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
2017-05-03 05:26:09 -05:00
|
|
|
|
2016-05-06 04:40:23 -05:00
|
|
|
// Otherwise print the welcome screen and enter interactive mode
|
|
|
|
console.Welcome()
|
|
|
|
console.Interactive()
|
2016-06-10 03:23:00 -05:00
|
|
|
return nil
|
2016-05-06 04:40:23 -05:00
|
|
|
}
|
|
|
|
|
2022-06-21 04:16:05 -05:00
|
|
|
// ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
|
|
|
|
// console to it, executes each of the files specified as arguments and tears
|
|
|
|
// everything down.
|
|
|
|
func ephemeralConsole(ctx *cli.Context) error {
|
|
|
|
var b strings.Builder
|
2022-06-27 11:22:36 -05:00
|
|
|
for _, file := range ctx.Args().Slice() {
|
2022-06-21 04:16:05 -05:00
|
|
|
b.Write([]byte(fmt.Sprintf("loadScript('%s');", file)))
|
|
|
|
}
|
|
|
|
utils.Fatalf(`The "js" command is deprecated. Please use the following instead:
|
|
|
|
geth --exec "%s" console`, b.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:47:15 -05:00
|
|
|
// dialRPC returns a RPC client which connects to the given endpoint.
|
|
|
|
// The check for empty endpoint implements the defaulting logic
|
2021-05-10 05:19:32 -05:00
|
|
|
// for "geth attach" with no argument.
|
2016-07-12 10:47:15 -05:00
|
|
|
func dialRPC(endpoint string) (*rpc.Client, error) {
|
|
|
|
if endpoint == "" {
|
2016-08-18 06:28:17 -05:00
|
|
|
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
|
2016-07-12 10:47:15 -05:00
|
|
|
} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
|
|
|
|
// Backwards compatibility with geth < 1.5 which required
|
|
|
|
// these prefixes.
|
|
|
|
endpoint = endpoint[4:]
|
|
|
|
}
|
|
|
|
return rpc.Dial(endpoint)
|
|
|
|
}
|