diff --git a/cmd/XDC/accountcmd_test.go b/cmd/XDC/accountcmd_test.go index 0fda6d8502..550ecdd33b 100644 --- a/cmd/XDC/accountcmd_test.go +++ b/cmd/XDC/accountcmd_test.go @@ -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() diff --git a/cmd/XDC/consolecmd.go b/cmd/XDC/consolecmd.go index 9a063a6cc8..b14b81601b 100644 --- a/cmd/XDC/consolecmd.go +++ b/cmd/XDC/consolecmd.go @@ -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...]", 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 -} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index dcfcf917f4..ed3b34d99d 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -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) diff --git a/console/console.go b/console/console.go index 633ac0393a..69ae6c3ffb 100644 --- a/console/console.go +++ b/console/console.go @@ -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() { diff --git a/console/console_test.go b/console/console_test.go index b59f41cdb9..23b1fd4839 100644 --- a/console/console_test.go +++ b/console/console_test.go @@ -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) { diff --git a/console/testdata/exec.js b/console/testdata/exec.js deleted file mode 100644 index 59e34d7c40..0000000000 --- a/console/testdata/exec.js +++ /dev/null @@ -1 +0,0 @@ -var execed = "some-executed-string";