2016-05-06 09:40:23 +00: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 13:00:00 +00:00
|
|
|
"fmt"
|
2016-07-12 15:47:15 +00:00
|
|
|
"strings"
|
2016-05-06 09:40:23 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
|
|
"github.com/ethereum/go-ethereum/console"
|
2022-07-25 09:07:44 +00:00
|
|
|
"github.com/ethereum/go-ethereum/internal/flags"
|
2022-06-27 16:22:36 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2016-05-06 09:40:23 +00:00
|
|
|
)
|
|
|
|
|
2017-05-03 10:26:09 +00:00
|
|
|
var (
|
|
|
|
consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
|
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
consoleCommand = &cli.Command{
|
|
|
|
Action: localConsole,
|
|
|
|
Name: "console",
|
|
|
|
Usage: "Start an interactive JavaScript environment",
|
2022-07-25 09:07:44 +00:00
|
|
|
Flags: flags.Merge(nodeFlags, rpcFlags, consoleFlags),
|
2016-05-06 09:40:23 +00: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 21:01:28 +00:00
|
|
|
See https://geth.ethereum.org/docs/interface/javascript-console.`,
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2017-05-03 10:26:09 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
attachCommand = &cli.Command{
|
|
|
|
Action: remoteConsole,
|
2016-11-10 11:00:09 +00:00
|
|
|
Name: "attach",
|
|
|
|
Usage: "Start an interactive JavaScript environment (connect to node)",
|
2017-05-03 10:26:09 +00:00
|
|
|
ArgsUsage: "[endpoint]",
|
2022-09-30 11:50:25 +00:00
|
|
|
Flags: flags.Merge([]cli.Flag{utils.DataDirFlag, utils.HttpHeaderFlag}, consoleFlags),
|
2016-05-06 09:40:23 +00: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 21:01:28 +00:00
|
|
|
See https://geth.ethereum.org/docs/interface/javascript-console.
|
2017-05-03 10:26:09 +00:00
|
|
|
This command allows to open a console on a running geth node.`,
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2017-05-03 10:26:09 +00:00
|
|
|
|
2022-06-27 16:22:36 +00:00
|
|
|
javascriptCommand = &cli.Command{
|
|
|
|
Action: ephemeralConsole,
|
2016-11-10 11:00:09 +00:00
|
|
|
Name: "js",
|
2022-06-21 09:16:05 +00:00
|
|
|
Usage: "(DEPRECATED) Execute the specified JavaScript files",
|
2017-05-03 10:26:09 +00:00
|
|
|
ArgsUsage: "<jsfile> [jsfile...]",
|
2022-07-25 09:07:44 +00:00
|
|
|
Flags: flags.Merge(nodeFlags, consoleFlags),
|
2016-05-06 09:40:23 +00:00
|
|
|
Description: `
|
|
|
|
The JavaScript VM exposes a node admin interface as well as the Ðapp
|
2020-12-27 21:01:28 +00:00
|
|
|
JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console`,
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// localConsole starts a new geth node, attaching a JavaScript console to it at the
|
|
|
|
// same time.
|
2016-06-10 08:23:00 +00:00
|
|
|
func localConsole(ctx *cli.Context) error {
|
2016-05-06 09:40:23 +00:00
|
|
|
// Create and start the node based on the CLI flags
|
2019-08-15 12:54:16 +00:00
|
|
|
prepare(ctx)
|
2020-08-03 17:40:46 +00:00
|
|
|
stack, backend := makeFullNode(ctx)
|
2021-12-11 15:51:05 +00:00
|
|
|
startNode(ctx, stack, backend, true)
|
2020-08-03 17:40:46 +00:00
|
|
|
defer stack.Close()
|
2016-05-06 09:40:23 +00:00
|
|
|
|
2021-12-11 15:51:05 +00:00
|
|
|
// Attach to the newly started node and create the JavaScript console.
|
2020-08-03 17:40:46 +00:00
|
|
|
client, err := stack.Attach()
|
2016-05-06 09:40:23 +00:00
|
|
|
if err != nil {
|
2021-12-11 15:51:05 +00:00
|
|
|
return fmt.Errorf("Failed to attach to the inproc geth: %v", err)
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
config := console.Config{
|
2017-05-03 10:26:09 +00:00
|
|
|
DataDir: utils.MakeDataDir(ctx),
|
2022-06-27 16:22:36 +00:00
|
|
|
DocRoot: ctx.String(utils.JSpathFlag.Name),
|
2016-05-06 09:40:23 +00:00
|
|
|
Client: client,
|
|
|
|
Preload: utils.MakeConsolePreloads(ctx),
|
|
|
|
}
|
|
|
|
console, err := console.New(config)
|
|
|
|
if err != nil {
|
2021-12-11 15:51:05 +00:00
|
|
|
return fmt.Errorf("Failed to start the JavaScript console: %v", err)
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
defer console.Stop(false)
|
|
|
|
|
2021-12-11 15:51:05 +00:00
|
|
|
// If only a short execution was requested, evaluate and return.
|
2022-06-27 16:22:36 +00:00
|
|
|
if script := ctx.String(utils.ExecFlag.Name); script != "" {
|
2016-05-06 09:40:23 +00:00
|
|
|
console.Evaluate(script)
|
2016-06-10 08:23:00 +00:00
|
|
|
return nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2021-12-11 15:51:05 +00: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 09:40:23 +00:00
|
|
|
console.Welcome()
|
|
|
|
console.Interactive()
|
2016-06-10 08:23:00 +00:00
|
|
|
return nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remoteConsole will connect to a remote geth instance, attaching a JavaScript
|
|
|
|
// console to it.
|
2016-06-10 08:23:00 +00:00
|
|
|
func remoteConsole(ctx *cli.Context) error {
|
2022-07-04 17:52:19 +00:00
|
|
|
if ctx.Args().Len() > 1 {
|
|
|
|
utils.Fatalf("invalid command-line: too many arguments")
|
|
|
|
}
|
2017-11-28 13:00:00 +00:00
|
|
|
endpoint := ctx.Args().First()
|
2017-12-05 10:17:38 +00:00
|
|
|
if endpoint == "" {
|
2022-06-21 09:16:05 +00:00
|
|
|
cfg := defaultNodeConfig()
|
|
|
|
utils.SetDataDir(ctx, &cfg)
|
|
|
|
endpoint = cfg.IPCEndpoint()
|
2017-11-28 13:00:00 +00:00
|
|
|
}
|
2022-09-30 11:50:25 +00:00
|
|
|
client, err := utils.DialRPCWithHeaders(endpoint, ctx.StringSlice(utils.HttpHeaderFlag.Name))
|
2016-05-06 09:40:23 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Unable to attach to remote geth: %v", err)
|
|
|
|
}
|
|
|
|
config := console.Config{
|
2016-08-18 11:28:17 +00:00
|
|
|
DataDir: utils.MakeDataDir(ctx),
|
2022-06-27 16:22:36 +00:00
|
|
|
DocRoot: ctx.String(utils.JSpathFlag.Name),
|
2016-05-06 09:40:23 +00: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 16:22:36 +00:00
|
|
|
if script := ctx.String(utils.ExecFlag.Name); script != "" {
|
2016-05-06 09:40:23 +00:00
|
|
|
console.Evaluate(script)
|
2016-06-10 08:23:00 +00:00
|
|
|
return nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
2017-05-03 10:26:09 +00:00
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
// Otherwise print the welcome screen and enter interactive mode
|
|
|
|
console.Welcome()
|
|
|
|
console.Interactive()
|
2016-06-10 08:23:00 +00:00
|
|
|
return nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 09:16:05 +00: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 16:22:36 +00:00
|
|
|
for _, file := range ctx.Args().Slice() {
|
2022-06-21 09:16:05 +00: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
|
|
|
|
}
|