Skip to content

Commit f20a569

Browse files
s1naholiman
andauthored
cmd/geth: drop geth js command (#25000)
* cmd/geth: drop js command * cmd: simplify ipc path determination for attach * Add deprecation warning for js * rm testdata for exec * fix account unlock test cases * Update cmd/geth/consolecmd.go Co-authored-by: Martin Holst Swende <[email protected]> * fix Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 10dc5dc commit f20a569

File tree

6 files changed

+30
-99
lines changed

6 files changed

+30
-99
lines changed

cmd/geth/accountcmd_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ Fatal: could not decrypt key with given password
180180

181181
func TestUnlockFlag(t *testing.T) {
182182
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
183-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
183+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "console", "--exec", "loadScript('testdata/empty.js')")
184184
geth.Expect(`
185185
Unlocking account f466859ead1932d743d622cb74fc058882e8648a | Attempt 1/3
186186
!! Unsupported terminal, password will be echoed.
187187
Password: {{.InputLine "foobar"}}
188+
undefined
188189
`)
189190
geth.ExpectExit()
190191

@@ -201,7 +202,7 @@ Password: {{.InputLine "foobar"}}
201202

202203
func TestUnlockFlagWrongPassword(t *testing.T) {
203204
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
204-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "js", "testdata/empty.js")
205+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "console", "--exec", "loadScript('testdata/empty.js')")
205206

206207
defer geth.ExpectExit()
207208
geth.Expect(`
@@ -219,14 +220,15 @@ Fatal: Failed to unlock account f466859ead1932d743d622cb74fc058882e8648a (could
219220
// https://github.com/ethereum/go-ethereum/issues/1785
220221
func TestUnlockFlagMultiIndex(t *testing.T) {
221222
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
222-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "js", "testdata/empty.js")
223+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--unlock", "0,2", "console", "--exec", "loadScript('testdata/empty.js')")
223224

224225
geth.Expect(`
225226
Unlocking account 0 | Attempt 1/3
226227
!! Unsupported terminal, password will be echoed.
227228
Password: {{.InputLine "foobar"}}
228229
Unlocking account 2 | Attempt 1/3
229230
Password: {{.InputLine "foobar"}}
231+
undefined
230232
`)
231233
geth.ExpectExit()
232234

@@ -244,8 +246,11 @@ Password: {{.InputLine "foobar"}}
244246

245247
func TestUnlockFlagPasswordFile(t *testing.T) {
246248
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
247-
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "js", "testdata/empty.js")
249+
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--password", "testdata/passwords.txt", "--unlock", "0,2", "console", "--exec", "loadScript('testdata/empty.js')")
248250

251+
geth.Expect(`
252+
undefined
253+
`)
249254
geth.ExpectExit()
250255

251256
wantMessages := []string{
@@ -275,7 +280,7 @@ func TestUnlockFlagAmbiguous(t *testing.T) {
275280
geth := runMinimalGeth(t, "--port", "0", "--ipcdisable", "--datadir", tmpDatadirWithKeystore(t),
276281
"--unlock", "f466859ead1932d743d622cb74fc058882e8648a", "--keystore",
277282
store, "--unlock", "f466859ead1932d743d622cb74fc058882e8648a",
278-
"js", "testdata/empty.js")
283+
"console", "--exec", "loadScript('testdata/empty.js')")
279284
defer geth.ExpectExit()
280285

281286
// Helper for the expect template, returns absolute keystore path.
@@ -294,6 +299,7 @@ Testing your password against all of them...
294299
Your password unlocked keystore://{{keypath "1"}}
295300
In order to avoid this warning, you need to remove the following duplicate key files:
296301
keystore://{{keypath "2"}}
302+
undefined
297303
`)
298304
geth.ExpectExit()
299305

cmd/geth/consolecmd.go

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ package main
1818

1919
import (
2020
"fmt"
21-
"path/filepath"
2221
"strings"
2322

2423
"github.com/ethereum/go-ethereum/cmd/utils"
25-
"github.com/ethereum/go-ethereum/common"
2624
"github.com/ethereum/go-ethereum/console"
2725
"github.com/ethereum/go-ethereum/node"
2826
"github.com/ethereum/go-ethereum/rpc"
@@ -61,7 +59,7 @@ This command allows to open a console on a running geth node.`,
6159
javascriptCommand = cli.Command{
6260
Action: utils.MigrateFlags(ephemeralConsole),
6361
Name: "js",
64-
Usage: "Execute the specified JavaScript files",
62+
Usage: "(DEPRECATED) Execute the specified JavaScript files",
6563
ArgsUsage: "<jsfile> [jsfile...]",
6664
Flags: utils.GroupFlags(nodeFlags, consoleFlags),
6765
Category: "CONSOLE COMMANDS",
@@ -121,31 +119,9 @@ func localConsole(ctx *cli.Context) error {
121119
func remoteConsole(ctx *cli.Context) error {
122120
endpoint := ctx.Args().First()
123121
if endpoint == "" {
124-
path := node.DefaultDataDir()
125-
if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
126-
path = ctx.GlobalString(utils.DataDirFlag.Name)
127-
}
128-
if path != "" {
129-
if ctx.GlobalBool(utils.RopstenFlag.Name) {
130-
// Maintain compatibility with older Geth configurations storing the
131-
// Ropsten database in `testnet` instead of `ropsten`.
132-
legacyPath := filepath.Join(path, "testnet")
133-
if common.FileExist(legacyPath) {
134-
path = legacyPath
135-
} else {
136-
path = filepath.Join(path, "ropsten")
137-
}
138-
} else if ctx.GlobalBool(utils.RinkebyFlag.Name) {
139-
path = filepath.Join(path, "rinkeby")
140-
} else if ctx.GlobalBool(utils.GoerliFlag.Name) {
141-
path = filepath.Join(path, "goerli")
142-
} else if ctx.GlobalBool(utils.SepoliaFlag.Name) {
143-
path = filepath.Join(path, "sepolia")
144-
} else if ctx.GlobalBool(utils.KilnFlag.Name) {
145-
path = filepath.Join(path, "kiln")
146-
}
147-
}
148-
endpoint = fmt.Sprintf("%s/geth.ipc", path)
122+
cfg := defaultNodeConfig()
123+
utils.SetDataDir(ctx, &cfg)
124+
endpoint = cfg.IPCEndpoint()
149125
}
150126
client, err := dialRPC(endpoint)
151127
if err != nil {
@@ -174,6 +150,19 @@ func remoteConsole(ctx *cli.Context) error {
174150
return nil
175151
}
176152

153+
// ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
154+
// console to it, executes each of the files specified as arguments and tears
155+
// everything down.
156+
func ephemeralConsole(ctx *cli.Context) error {
157+
var b strings.Builder
158+
for _, file := range ctx.Args() {
159+
b.Write([]byte(fmt.Sprintf("loadScript('%s');", file)))
160+
}
161+
utils.Fatalf(`The "js" command is deprecated. Please use the following instead:
162+
geth --exec "%s" console`, b.String())
163+
return nil
164+
}
165+
177166
// dialRPC returns a RPC client which connects to the given endpoint.
178167
// The check for empty endpoint implements the defaulting logic
179168
// for "geth attach" with no argument.
@@ -187,48 +176,3 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
187176
}
188177
return rpc.Dial(endpoint)
189178
}
190-
191-
// ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
192-
// console to it, executes each of the files specified as arguments and tears
193-
// everything down.
194-
func ephemeralConsole(ctx *cli.Context) error {
195-
// Create and start the node based on the CLI flags
196-
stack, backend := makeFullNode(ctx)
197-
startNode(ctx, stack, backend, false)
198-
defer stack.Close()
199-
200-
// Attach to the newly started node and start the JavaScript console
201-
client, err := stack.Attach()
202-
if err != nil {
203-
return fmt.Errorf("Failed to attach to the inproc geth: %v", err)
204-
}
205-
config := console.Config{
206-
DataDir: utils.MakeDataDir(ctx),
207-
DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
208-
Client: client,
209-
Preload: utils.MakeConsolePreloads(ctx),
210-
}
211-
212-
console, err := console.New(config)
213-
if err != nil {
214-
return fmt.Errorf("Failed to start the JavaScript console: %v", err)
215-
}
216-
defer console.Stop(false)
217-
218-
// Interrupt the JS interpreter when node is stopped.
219-
go func() {
220-
stack.Wait()
221-
console.Stop(false)
222-
}()
223-
224-
// Evaluate each of the specified JavaScript files.
225-
for _, file := range ctx.Args() {
226-
if err = console.Execute(file); err != nil {
227-
return fmt.Errorf("Failed to execute %s: %v", file, err)
228-
}
229-
}
230-
231-
// The main script is now done, but keep running timers/callbacks.
232-
console.Stop(true)
233-
return nil
234-
}

cmd/utils/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
13151315
setGraphQL(ctx, cfg)
13161316
setWS(ctx, cfg)
13171317
setNodeUserIdent(ctx, cfg)
1318-
setDataDir(ctx, cfg)
1318+
SetDataDir(ctx, cfg)
13191319
setSmartCard(ctx, cfg)
13201320

13211321
if ctx.GlobalIsSet(JWTSecretFlag.Name) {
@@ -1366,7 +1366,7 @@ func setSmartCard(ctx *cli.Context, cfg *node.Config) {
13661366
cfg.SmartCardDaemonPath = path
13671367
}
13681368

1369-
func setDataDir(ctx *cli.Context, cfg *node.Config) {
1369+
func SetDataDir(ctx *cli.Context, cfg *node.Config) {
13701370
switch {
13711371
case ctx.GlobalIsSet(DataDirFlag.Name):
13721372
cfg.DataDir = ctx.GlobalString(DataDirFlag.Name)

console/console.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,6 @@ func countIndents(input string) int {
540540
return indents
541541
}
542542

543-
// Execute runs the JavaScript file specified as the argument.
544-
func (c *Console) Execute(path string) error {
545-
return c.jsre.Exec(path)
546-
}
547-
548543
// Stop cleans up the console and terminates the runtime environment.
549544
func (c *Console) Stop(graceful bool) error {
550545
c.stopOnce.Do(func() {

console/console_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,6 @@ func TestPreload(t *testing.T) {
234234
}
235235
}
236236

237-
// Tests that JavaScript scripts can be executes from the configured asset path.
238-
func TestExecute(t *testing.T) {
239-
tester := newTester(t, nil)
240-
defer tester.Close(t)
241-
242-
tester.console.Execute("exec.js")
243-
244-
tester.console.Evaluate("execed")
245-
if output := tester.output.String(); !strings.Contains(output, "some-executed-string") {
246-
t.Fatalf("execed variable missing: have %s, want %s", output, "some-executed-string")
247-
}
248-
}
249-
250237
// Tests that the JavaScript objects returned by statement executions are properly
251238
// pretty printed instead of just displaying "[object]".
252239
func TestPrettyPrint(t *testing.T) {

console/testdata/exec.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)