From 8049c64ef5205892145da41b3581e0c06994eab1 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 31 May 2022 17:32:53 +0200 Subject: [PATCH 1/7] cmd/geth: drop js command --- cmd/geth/consolecmd.go | 57 ----------------------------------------- cmd/geth/main.go | 1 - console/console.go | 5 ---- console/console_test.go | 13 ---------- 4 files changed, 76 deletions(-) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 5167f8536a27..25943de04f15 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -57,18 +57,6 @@ which exposes a node admin interface as well as the Ðapp JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console. This command allows to open a console on a running geth node.`, } - - javascriptCommand = cli.Command{ - Action: utils.MigrateFlags(ephemeralConsole), - Name: "js", - Usage: "Execute the specified JavaScript files", - ArgsUsage: " [jsfile...]", - Flags: utils.GroupFlags(nodeFlags, consoleFlags), - Category: "CONSOLE COMMANDS", - Description: ` -The JavaScript VM exposes a node admin interface as well as the Ðapp -JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console`, - } ) // localConsole starts a new geth node, attaching a JavaScript console to it at the @@ -187,48 +175,3 @@ func dialRPC(endpoint string) (*rpc.Client, error) { } return rpc.Dial(endpoint) } - -// 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 { - // Create and start the node based on the CLI flags - stack, backend := makeFullNode(ctx) - startNode(ctx, stack, backend, false) - defer stack.Close() - - // Attach to the newly started node and start the JavaScript console - client, err := stack.Attach() - if err != nil { - return fmt.Errorf("Failed to attach to the inproc geth: %v", err) - } - config := console.Config{ - DataDir: utils.MakeDataDir(ctx), - DocRoot: ctx.GlobalString(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() { - 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/geth/main.go b/cmd/geth/main.go index 1e2770ae808e..232ad98a5fe4 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -224,7 +224,6 @@ func init() { // See consolecmd.go: consoleCommand, attachCommand, - javascriptCommand, // See misccmd.go: makecacheCommand, makedagCommand, diff --git a/console/console.go b/console/console.go index 2f61c1d7a4cf..c8f6c9cfeec5 100644 --- a/console/console.go +++ b/console/console.go @@ -540,11 +540,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 04ba91d1576a..35341fcba0b5 100644 --- a/console/console_test.go +++ b/console/console_test.go @@ -234,19 +234,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 displaying "[object]". func TestPrettyPrint(t *testing.T) { From 9a1bd6a1d71068f38cf9ccd4f8d88e4a67db4151 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 31 May 2022 18:05:07 +0200 Subject: [PATCH 2/7] cmd: simplify ipc path determination for attach --- cmd/geth/consolecmd.go | 30 +++--------------------------- cmd/utils/flags.go | 4 ++-- 2 files changed, 5 insertions(+), 29 deletions(-) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 25943de04f15..938bcd0bf5c0 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -18,11 +18,9 @@ package main import ( "fmt" - "path/filepath" "strings" "github.com/ethereum/go-ethereum/cmd/utils" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc" @@ -109,31 +107,9 @@ func localConsole(ctx *cli.Context) error { func remoteConsole(ctx *cli.Context) error { endpoint := ctx.Args().First() if endpoint == "" { - path := node.DefaultDataDir() - if ctx.GlobalIsSet(utils.DataDirFlag.Name) { - path = ctx.GlobalString(utils.DataDirFlag.Name) - } - if path != "" { - if ctx.GlobalBool(utils.RopstenFlag.Name) { - // Maintain compatibility with older Geth configurations storing the - // Ropsten database in `testnet` instead of `ropsten`. - legacyPath := filepath.Join(path, "testnet") - if common.FileExist(legacyPath) { - path = legacyPath - } else { - path = filepath.Join(path, "ropsten") - } - } else if ctx.GlobalBool(utils.RinkebyFlag.Name) { - path = filepath.Join(path, "rinkeby") - } else if ctx.GlobalBool(utils.GoerliFlag.Name) { - path = filepath.Join(path, "goerli") - } else if ctx.GlobalBool(utils.SepoliaFlag.Name) { - path = filepath.Join(path, "sepolia") - } else if ctx.GlobalBool(utils.KilnFlag.Name) { - path = filepath.Join(path, "kiln") - } - } - endpoint = fmt.Sprintf("%s/geth.ipc", path) + cfg := defaultNodeConfig() + utils.SetDataDir(ctx, &cfg) + endpoint = cfg.IPCEndpoint() } client, err := dialRPC(endpoint) if err != nil { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 0b28cd09f141..d869f21cc4e3 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1315,7 +1315,7 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { setGraphQL(ctx, cfg) setWS(ctx, cfg) setNodeUserIdent(ctx, cfg) - setDataDir(ctx, cfg) + SetDataDir(ctx, cfg) setSmartCard(ctx, cfg) if ctx.GlobalIsSet(JWTSecretFlag.Name) { @@ -1366,7 +1366,7 @@ func setSmartCard(ctx *cli.Context, cfg *node.Config) { cfg.SmartCardDaemonPath = path } -func setDataDir(ctx *cli.Context, cfg *node.Config) { +func SetDataDir(ctx *cli.Context, cfg *node.Config) { switch { case ctx.GlobalIsSet(DataDirFlag.Name): cfg.DataDir = ctx.GlobalString(DataDirFlag.Name) From 70ca5d715e363802646ee23f076f4014d423d5c4 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 1 Jun 2022 13:59:39 +0200 Subject: [PATCH 3/7] Add deprecation warning for js --- cmd/geth/consolecmd.go | 21 +++++++++++++++++++++ cmd/geth/main.go | 1 + 2 files changed, 22 insertions(+) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 938bcd0bf5c0..245c1d3697b6 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -55,6 +55,18 @@ which exposes a node admin interface as well as the Ðapp JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console. This command allows to open a console on a running geth node.`, } + + javascriptCommand = cli.Command{ + Action: utils.MigrateFlags(ephemeralConsole), + Name: "js", + Usage: "(DEPRECATED) Execute the specified JavaScript files", + ArgsUsage: " [jsfile...]", + Flags: utils.GroupFlags(nodeFlags, consoleFlags), + Category: "CONSOLE COMMANDS", + Description: ` +The JavaScript VM exposes a node admin interface as well as the Ðapp +JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console`, + } ) // localConsole starts a new geth node, attaching a JavaScript console to it at the @@ -138,6 +150,15 @@ 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 { + utils.Fatalf(`The "js" command is deprecated. Please use the following instead: +geth --exec "loadScript('file.js')" console`) + return nil +} + // dialRPC returns a RPC client which connects to the given endpoint. // The check for empty endpoint implements the defaulting logic // for "geth attach" with no argument. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 232ad98a5fe4..1e2770ae808e 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -224,6 +224,7 @@ func init() { // See consolecmd.go: consoleCommand, attachCommand, + javascriptCommand, // See misccmd.go: makecacheCommand, makedagCommand, From e5c33e83cebcc5743ee34c994fcda3e0311fbcdd Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 1 Jun 2022 14:02:27 +0200 Subject: [PATCH 4/7] rm testdata for exec --- console/testdata/exec.js | 1 - 1 file changed, 1 deletion(-) delete mode 100644 console/testdata/exec.js diff --git a/console/testdata/exec.js b/console/testdata/exec.js deleted file mode 100644 index 59e34d7c4033..000000000000 --- a/console/testdata/exec.js +++ /dev/null @@ -1 +0,0 @@ -var execed = "some-executed-string"; From 15025407f5ebd60c61a2d94c47ca5fc730510490 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 1 Jun 2022 17:06:47 +0200 Subject: [PATCH 5/7] fix account unlock test cases --- cmd/geth/accountcmd_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/geth/accountcmd_test.go b/cmd/geth/accountcmd_test.go index 0c22e8c9bf57..bbde27c3785a 100644 --- a/cmd/geth/accountcmd_test.go +++ b/cmd/geth/accountcmd_test.go @@ -180,11 +180,12 @@ Fatal: could not decrypt key with given password func TestUnlockFlag(t *testing.T) { geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t), - "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js") + "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "console", "--exec", "loadScript('testdata/empty.js')") geth.Expect(` Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3 !! Unsupported terminal, password will be echoed. Password: {{.InputLine "foobar"}} +undefined `) geth.ExpectExit() @@ -201,7 +202,7 @@ Password: {{.InputLine "foobar"}} func TestUnlockFlagWrongPassword(t *testing.T) { geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t), - "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js") + "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "console", "--exec", "loadScript('testdata/empty.js')") defer geth.ExpectExit() geth.Expect(` @@ -219,7 +220,7 @@ Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could // https://github.com/ethereum/go-ethereum/issues/1785 func TestUnlockFlagMultiIndex(t *testing.T) { geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t), - "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "js", "testdata/empty.js") + "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "console", "--exec", "loadScript('testdata/empty.js')") geth.Expect(` Unlocking account 0 | Attempt 1/3 @@ -227,6 +228,7 @@ Unlocking account 0 | Attempt 1/3 Password: {{.InputLine "foobar"}} Unlocking account 2 | Attempt 1/3 Password: {{.InputLine "foobar"}} +undefined `) geth.ExpectExit() @@ -244,8 +246,11 @@ Password: {{.InputLine "foobar"}} func TestUnlockFlagPasswordFile(t *testing.T) { geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t), - "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "js", "testdata/empty.js") + "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "console", "--exec", "loadScript('testdata/empty.js')") + geth.Expect(` +undefined +`) geth.ExpectExit() wantMessages := []string{ @@ -275,7 +280,7 @@ func TestUnlockFlagAmbiguous(t *testing.T) { geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t), "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore", store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a", - "js", "testdata/empty.js") + "console", "--exec", "loadScript('testdata/empty.js')") defer geth.ExpectExit() // Helper for the expect template, returns absolute keystore path. @@ -294,6 +299,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 `) geth.ExpectExit() From 0e56ab33ba087ae59e499905432fbadfdb3d27ae Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Date: Tue, 7 Jun 2022 12:23:43 +0200 Subject: [PATCH 6/7] Update cmd/geth/consolecmd.go Co-authored-by: Martin Holst Swende --- cmd/geth/consolecmd.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 245c1d3697b6..c3c97547ff08 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -154,7 +154,12 @@ func remoteConsole(ctx *cli.Context) error { // 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() { + 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()) geth --exec "loadScript('file.js')" console`) return nil } From e1d262e75d984ee21c304b5f355d46229dfe443f Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 7 Jun 2022 12:27:32 +0200 Subject: [PATCH 7/7] fix --- cmd/geth/consolecmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index c3c97547ff08..4f538f96e3f5 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -160,7 +160,6 @@ func ephemeralConsole(ctx *cli.Context) error { } utils.Fatalf(`The "js" command is deprecated. Please use the following instead: geth --exec "%s" console`, b.String()) -geth --exec "loadScript('file.js')" console`) return nil }