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"
|
2017-12-05 10:17:38 +00:00
|
|
|
"path/filepath"
|
2016-07-12 15:47:15 +00:00
|
|
|
"strings"
|
2016-05-06 09:40:23 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2022-04-27 09:48:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-05-06 09:40:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/console"
|
2016-07-12 15:47:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-06-09 09:44:42 +00:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
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}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
consoleCommand = cli.Command{
|
2017-05-03 10:26:09 +00:00
|
|
|
Action: utils.MigrateFlags(localConsole),
|
|
|
|
Name: "console",
|
|
|
|
Usage: "Start an interactive JavaScript environment",
|
2022-05-03 06:46:17 +00:00
|
|
|
Flags: utils.GroupFlags(nodeFlags, rpcFlags, consoleFlags),
|
2017-05-03 10:26:09 +00:00
|
|
|
Category: "CONSOLE COMMANDS",
|
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
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
attachCommand = cli.Command{
|
2017-05-03 10:26:09 +00:00
|
|
|
Action: utils.MigrateFlags(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-05-03 06:46:17 +00:00
|
|
|
Flags: utils.GroupFlags([]cli.Flag{utils.DataDirFlag}, consoleFlags),
|
2016-11-10 11:00:09 +00:00
|
|
|
Category: "CONSOLE COMMANDS",
|
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
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
javascriptCommand = cli.Command{
|
2017-05-03 10:26:09 +00:00
|
|
|
Action: utils.MigrateFlags(ephemeralConsole),
|
2016-11-10 11:00:09 +00:00
|
|
|
Name: "js",
|
|
|
|
Usage: "Execute the specified JavaScript files",
|
2017-05-03 10:26:09 +00:00
|
|
|
ArgsUsage: "<jsfile> [jsfile...]",
|
2022-05-03 06:46:17 +00:00
|
|
|
Flags: utils.GroupFlags(nodeFlags, consoleFlags),
|
2016-11-10 11:00:09 +00:00
|
|
|
Category: "CONSOLE COMMANDS",
|
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),
|
2016-05-06 09:40:23 +00:00
|
|
|
DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
|
|
|
|
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.
|
2016-05-06 09:40:23 +00:00
|
|
|
if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
|
|
|
|
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 {
|
2017-11-28 13:00:00 +00:00
|
|
|
endpoint := ctx.Args().First()
|
2017-12-05 10:17:38 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
path := node.DefaultDataDir()
|
|
|
|
if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
|
|
|
|
path = ctx.GlobalString(utils.DataDirFlag.Name)
|
|
|
|
}
|
2017-12-15 21:31:10 +00:00
|
|
|
if path != "" {
|
2021-02-24 13:07:58 +00:00
|
|
|
if ctx.GlobalBool(utils.RopstenFlag.Name) {
|
2020-04-09 09:09:58 +00:00
|
|
|
// Maintain compatibility with older Geth configurations storing the
|
|
|
|
// Ropsten database in `testnet` instead of `ropsten`.
|
|
|
|
legacyPath := filepath.Join(path, "testnet")
|
2022-04-27 09:48:02 +00:00
|
|
|
if common.FileExist(legacyPath) {
|
2020-04-09 09:09:58 +00:00
|
|
|
path = legacyPath
|
|
|
|
} else {
|
|
|
|
path = filepath.Join(path, "ropsten")
|
|
|
|
}
|
2017-12-15 21:31:10 +00:00
|
|
|
} else if ctx.GlobalBool(utils.RinkebyFlag.Name) {
|
|
|
|
path = filepath.Join(path, "rinkeby")
|
2020-04-09 09:09:58 +00:00
|
|
|
} else if ctx.GlobalBool(utils.GoerliFlag.Name) {
|
|
|
|
path = filepath.Join(path, "goerli")
|
2021-11-08 11:06:01 +00:00
|
|
|
} else if ctx.GlobalBool(utils.SepoliaFlag.Name) {
|
|
|
|
path = filepath.Join(path, "sepolia")
|
2022-05-03 06:46:17 +00:00
|
|
|
} else if ctx.GlobalBool(utils.KilnFlag.Name) {
|
|
|
|
path = filepath.Join(path, "kiln")
|
2017-12-15 21:31:10 +00:00
|
|
|
}
|
2017-12-05 10:17:38 +00:00
|
|
|
}
|
|
|
|
endpoint = fmt.Sprintf("%s/geth.ipc", path)
|
2017-11-28 13:00:00 +00:00
|
|
|
}
|
|
|
|
client, err := dialRPC(endpoint)
|
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),
|
2016-05-06 09:40:23 +00:00
|
|
|
DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
|
|
|
|
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)
|
|
|
|
|
|
|
|
if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-12 15:47:15 +00:00
|
|
|
// dialRPC returns a RPC client which connects to the given endpoint.
|
|
|
|
// The check for empty endpoint implements the defaulting logic
|
2021-05-10 10:19:32 +00:00
|
|
|
// for "geth attach" with no argument.
|
2016-07-12 15:47:15 +00:00
|
|
|
func dialRPC(endpoint string) (*rpc.Client, error) {
|
|
|
|
if endpoint == "" {
|
2016-08-18 11:28:17 +00:00
|
|
|
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
|
2016-07-12 15:47:15 +00: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)
|
|
|
|
}
|
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
// ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
|
2017-05-03 10:26:09 +00:00
|
|
|
// console to it, executes each of the files specified as arguments and tears
|
2016-05-06 09:40:23 +00:00
|
|
|
// everything down.
|
2016-06-10 08:23:00 +00:00
|
|
|
func ephemeralConsole(ctx *cli.Context) error {
|
2016-05-06 09:40:23 +00:00
|
|
|
// Create and start the node based on the CLI flags
|
2020-08-03 17:40:46 +00:00
|
|
|
stack, backend := makeFullNode(ctx)
|
2021-12-11 15:51:05 +00:00
|
|
|
startNode(ctx, stack, backend, false)
|
2020-08-03 17:40:46 +00:00
|
|
|
defer stack.Close()
|
2016-05-06 09:40:23 +00:00
|
|
|
|
|
|
|
// Attach to the newly started node and start 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),
|
2016-05-06 09:40:23 +00:00
|
|
|
DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
|
|
|
|
Client: client,
|
|
|
|
Preload: utils.MakeConsolePreloads(ctx),
|
|
|
|
}
|
2017-05-03 10:26:09 +00:00
|
|
|
|
2016-05-06 09:40:23 +00:00
|
|
|
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
|
|
|
// Interrupt the JS interpreter when node is stopped.
|
|
|
|
go func() {
|
|
|
|
stack.Wait()
|
|
|
|
console.Stop(false)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Evaluate each of the specified JavaScript files.
|
2016-05-06 09:40:23 +00:00
|
|
|
for _, file := range ctx.Args() {
|
|
|
|
if err = console.Execute(file); err != nil {
|
2021-12-11 15:51:05 +00:00
|
|
|
return fmt.Errorf("Failed to execute %s: %v", file, err)
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 15:51:05 +00:00
|
|
|
// The main script is now done, but keep running timers/callbacks.
|
2016-05-06 09:40:23 +00:00
|
|
|
console.Stop(true)
|
2016-06-10 08:23:00 +00:00
|
|
|
return nil
|
2016-05-06 09:40:23 +00:00
|
|
|
}
|