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