cmd, console: drop geth js command #25000 (#1534)

This commit is contained in:
Daniel Liu 2025-09-24 07:47:21 +08:00 committed by GitHub
parent db0cd1581f
commit 6c73723f47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 53 additions and 90 deletions

View file

@ -204,11 +204,12 @@ func TestUnlockFlag(t *testing.T) {
XDC := runXDC(t,
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "256", "--ipcdisable",
"--datadir", datadir, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
"js", "testdata/empty.js")
"console", "--exec", "loadScript('testdata/empty.js')")
XDC.Expect(`
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
!! Unsupported terminal, password will be echoed.
Password: {{.InputLine "foobar"}}
undefined
`)
XDC.ExpectExit()
@ -248,13 +249,14 @@ func TestUnlockFlagMultiIndex(t *testing.T) {
defer os.RemoveAll(datadir)
XDC := runXDC(t,
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
"--datadir", datadir, "--unlock", "0,2", "js", "testdata/empty.js")
"--datadir", datadir, "--unlock", "0,2", "console", "--exec", "loadScript('testdata/empty.js')")
XDC.Expect(`
Unlocking account 0 | Attempt 1/3
!! Unsupported terminal, password will be echoed.
Password: {{.InputLine "foobar"}}
Unlocking account 2 | Attempt 1/3
Password: {{.InputLine "foobar"}}
undefined
`)
XDC.ExpectExit()
@ -276,7 +278,10 @@ func TestUnlockFlagPasswordFile(t *testing.T) {
XDC := runXDC(t,
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
"--datadir", datadir, "--password", "testdata/passwords.txt", "--unlock", "0,2",
"js", "testdata/empty.js")
"console", "--exec", "loadScript('testdata/empty.js')")
XDC.Expect(`
undefined
`)
XDC.ExpectExit()
wantMessages := []string{
@ -308,7 +313,7 @@ func TestUnlockFlagAmbiguous(t *testing.T) {
XDC := runXDC(t,
"--nat", "none", "--nodiscover", "--maxpeers", "0", "--port", "0", "--nousb", "--cache", "128", "--ipcdisable",
"--keystore", store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
"js", "testdata/empty.js")
"console", "--exec", "loadScript('testdata/empty.js')")
defer XDC.ExpectExit()
// Helper for the expect template, returns absolute keystore path.
@ -327,6 +332,7 @@ Testing your password against all of them...
Your password unlocked keystore://{{keypath "1"}}
In order to avoid this warning, you need to remove the following duplicate key files:
keystore://{{keypath "2"}}
undefined
`)
XDC.ExpectExit()

View file

@ -18,7 +18,6 @@ package main
import (
"fmt"
"path/filepath"
"slices"
"strings"
@ -59,7 +58,7 @@ This command allows to open a console on a running XDC node.`,
javascriptCommand = &cli.Command{
Action: ephemeralConsole,
Name: "js",
Usage: "Execute the specified JavaScript files",
Usage: "(DEPRECATED) Execute the specified JavaScript files",
ArgsUsage: "<jsfile> [jsfile...]",
Flags: slices.Concat(nodeFlags, consoleFlags),
Description: `
@ -118,18 +117,21 @@ func remoteConsole(ctx *cli.Context) error {
endpoint := ctx.Args().First()
if endpoint == "" {
path := node.DefaultDataDir()
if ctx.IsSet(utils.DataDirFlag.Name) {
path = ctx.String(utils.DataDirFlag.Name)
}
if path != "" {
if ctx.Bool(utils.TestnetFlag.Name) {
path = filepath.Join(path, "testnet")
} else if ctx.Bool(utils.DevnetFlag.Name) {
path = filepath.Join(path, "devnet")
}
}
endpoint = fmt.Sprintf("%s/XDC.ipc", path)
// path := node.DefaultDataDir()
// if ctx.IsSet(utils.DataDirFlag.Name) {
// path = ctx.String(utils.DataDirFlag.Name)
// }
// if path != "" {
// if ctx.Bool(utils.TestnetFlag.Name) {
// path = filepath.Join(path, "testnet")
// } else if ctx.Bool(utils.DevnetFlag.Name) {
// path = filepath.Join(path, "devnet")
// }
// }
// endpoint = fmt.Sprintf("%s/XDC.ipc", path)
cfg := defaultNodeConfig()
utils.SetDataDir(ctx, &cfg)
endpoint = cfg.IPCEndpoint()
}
client, err := dialRPC(endpoint)
@ -159,6 +161,19 @@ func remoteConsole(ctx *cli.Context) error {
return nil
}
// 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
for _, file := range ctx.Args().Slice() {
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
}
// dialRPC returns a RPC client which connects to the given endpoint.
// The check for empty endpoint implements the defaulting logic
// for "XDC attach" and "XDC monitor" with no argument.
@ -172,45 +187,3 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
}
return rpc.Dial(endpoint)
}
// ephemeralConsole starts a new XDC 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 {
// Create and start the node based on the CLI flags
stack, backend, cfg := makeFullNode(ctx)
startNode(ctx, stack, backend, cfg, false)
defer stack.Close()
// Attach to the newly started node and start the JavaScript console
client := stack.Attach()
config := console.Config{
DataDir: utils.MakeDataDir(ctx),
DocRoot: ctx.String(utils.JSpathFlag.Name),
Client: client,
Preload: utils.MakeConsolePreloads(ctx),
}
console, err := console.New(config)
if err != nil {
return fmt.Errorf("failed to start the JavaScript console: %v", err)
}
defer console.Stop(false)
// Interrupt the JS interpreter when node is stopped.
go func() {
stack.Wait()
console.Stop(false)
}()
// Evaluate each of the specified JavaScript files.
for _, file := range ctx.Args().Slice() {
if err = console.Execute(file); err != nil {
return fmt.Errorf("failed to execute %s: %v", file, err)
}
}
// The main script is now done, but keep running timers/callbacks.
console.Stop(true)
return nil
}

View file

@ -1346,6 +1346,7 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
setHTTP(ctx, cfg)
setWS(ctx, cfg)
setNodeUserIdent(ctx, cfg)
SetDataDir(ctx, cfg)
setSmartCard(ctx, cfg)
if ctx.IsSet(JWTSecretFlag.Name) {
@ -1356,17 +1357,6 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
log.Warn(fmt.Sprintf("Option --%s is deprecated. The 'personal' RPC namespace has been removed.", EnablePersonal.Name))
}
switch {
case ctx.IsSet(DataDirFlag.Name):
cfg.DataDir = ctx.String(DataDirFlag.Name)
case ctx.Bool(DeveloperFlag.Name):
cfg.DataDir = "" // unless explicitly requested, use memory databases
case ctx.Bool(TestnetFlag.Name):
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "testnet")
case ctx.Bool(DevnetFlag.Name):
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "devnet")
}
if ctx.IsSet(KeyStoreDirFlag.Name) {
cfg.KeyStoreDir = ctx.String(KeyStoreDirFlag.Name)
}
@ -1412,6 +1402,19 @@ func setSmartCard(ctx *cli.Context, cfg *node.Config) {
cfg.SmartCardDaemonPath = path
}
func SetDataDir(ctx *cli.Context, cfg *node.Config) {
switch {
case ctx.IsSet(DataDirFlag.Name):
cfg.DataDir = ctx.String(DataDirFlag.Name)
case ctx.Bool(DeveloperFlag.Name):
cfg.DataDir = "" // unless explicitly requested, use memory databases
case ctx.Bool(TestnetFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "testnet")
case ctx.Bool(DevnetFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "devnet")
}
}
func setGPO(ctx *cli.Context, cfg *gasprice.Config) {
if ctx.IsSet(GpoBlocksFlag.Name) {
cfg.Blocks = ctx.Int(GpoBlocksFlag.Name)

View file

@ -522,11 +522,6 @@ func countIndents(input string) int {
return indents
}
// Execute runs the JavaScript file specified as the argument.
func (c *Console) Execute(path string) error {
return c.jsre.Exec(path)
}
// Stop cleans up the console and terminates the runtime environment.
func (c *Console) Stop(graceful bool) error {
c.stopOnce.Do(func() {

View file

@ -232,19 +232,6 @@ func TestPreload(t *testing.T) {
}
}
// Tests that JavaScript scripts can be executes from the configured asset path.
func TestExecute(t *testing.T) {
tester := newTester(t, nil)
defer tester.Close(t)
tester.console.Execute("exec.js")
tester.console.Evaluate("execed")
if output := tester.output.String(); !strings.Contains(output, "some-executed-string") {
t.Fatalf("execed variable missing: have %s, want %s", output, "some-executed-string")
}
}
// Tests that the JavaScript objects returned by statement executions are properly
// pretty printed instead of just displaing "[object]".
func TestPrettyPrint(t *testing.T) {

View file

@ -1 +0,0 @@
var execed = "some-executed-string";