github.com/bearnetworkchain/go-bearnetwork@v1.10.19-0.20220604150648-d63890c2e42b/cmd/geth/consolecmd.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "strings" 23 24 "github.com/bearnetworkchain/go-bearnetwork/cmd/utils" 25 "github.com/bearnetworkchain/go-bearnetwork/common" 26 "github.com/bearnetworkchain/go-bearnetwork/console" 27 "github.com/bearnetworkchain/go-bearnetwork/node" 28 "github.com/bearnetworkchain/go-bearnetwork/rpc" 29 "gopkg.in/urfave/cli.v1" 30 ) 31 32 var ( 33 consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag} 34 35 consoleCommand = cli.Command{ 36 Action: utils.MigrateFlags(localConsole), 37 Name: "console", 38 Usage: "Start an interactive JavaScript environment", 39 Flags: utils.GroupFlags(nodeFlags, rpcFlags, consoleFlags), 40 Category: "CONSOLE COMMANDS", 41 Description: ` 42 The Geth console is an interactive shell for the JavaScript runtime environment 43 which exposes a node admin interface as well as the Ðapp JavaScript API. 44 See https://geth.ethereum.org/docs/interface/javascript-console.`, 45 } 46 47 attachCommand = cli.Command{ 48 Action: utils.MigrateFlags(remoteConsole), 49 Name: "attach", 50 Usage: "Start an interactive JavaScript environment (connect to node)", 51 ArgsUsage: "[endpoint]", 52 Flags: utils.GroupFlags([]cli.Flag{utils.DataDirFlag}, consoleFlags), 53 Category: "CONSOLE COMMANDS", 54 Description: ` 55 The Geth console is an interactive shell for the JavaScript runtime environment 56 which exposes a node admin interface as well as the Ðapp JavaScript API. 57 See https://geth.ethereum.org/docs/interface/javascript-console. 58 This command allows to open a console on a running geth node.`, 59 } 60 61 javascriptCommand = cli.Command{ 62 Action: utils.MigrateFlags(ephemeralConsole), 63 Name: "js", 64 Usage: "Execute the specified JavaScript files", 65 ArgsUsage: "<jsfile> [jsfile...]", 66 Flags: utils.GroupFlags(nodeFlags, consoleFlags), 67 Category: "CONSOLE COMMANDS", 68 Description: ` 69 The JavaScript VM exposes a node admin interface as well as the Ðapp 70 JavaScript API. See https://geth.ethereum.org/docs/interface/javascript-console`, 71 } 72 ) 73 74 // localConsole starts a new geth node, attaching a JavaScript console to it at the 75 // same time. 76 func localConsole(ctx *cli.Context) error { 77 // Create and start the node based on the CLI flags 78 prepare(ctx) 79 stack, backend := makeFullNode(ctx) 80 startNode(ctx, stack, backend, true) 81 defer stack.Close() 82 83 // Attach to the newly started node and create the JavaScript console. 84 client, err := stack.Attach() 85 if err != nil { 86 return fmt.Errorf("Failed to attach to the inproc geth: %v", err) 87 } 88 config := console.Config{ 89 DataDir: utils.MakeDataDir(ctx), 90 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 91 Client: client, 92 Preload: utils.MakeConsolePreloads(ctx), 93 } 94 console, err := console.New(config) 95 if err != nil { 96 return fmt.Errorf("Failed to start the JavaScript console: %v", err) 97 } 98 defer console.Stop(false) 99 100 // If only a short execution was requested, evaluate and return. 101 if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { 102 console.Evaluate(script) 103 return nil 104 } 105 106 // Track node shutdown and stop the console when it goes down. 107 // This happens when SIGTERM is sent to the process. 108 go func() { 109 stack.Wait() 110 console.StopInteractive() 111 }() 112 113 // Print the welcome screen and enter interactive mode. 114 console.Welcome() 115 console.Interactive() 116 return nil 117 } 118 119 // remoteConsole will connect to a remote geth instance, attaching a JavaScript 120 // console to it. 121 func remoteConsole(ctx *cli.Context) error { 122 endpoint := ctx.Args().First() 123 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) 149 } 150 client, err := dialRPC(endpoint) 151 if err != nil { 152 utils.Fatalf("Unable to attach to remote geth: %v", err) 153 } 154 config := console.Config{ 155 DataDir: utils.MakeDataDir(ctx), 156 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 157 Client: client, 158 Preload: utils.MakeConsolePreloads(ctx), 159 } 160 console, err := console.New(config) 161 if err != nil { 162 utils.Fatalf("Failed to start the JavaScript console: %v", err) 163 } 164 defer console.Stop(false) 165 166 if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { 167 console.Evaluate(script) 168 return nil 169 } 170 171 // Otherwise print the welcome screen and enter interactive mode 172 console.Welcome() 173 console.Interactive() 174 return nil 175 } 176 177 // dialRPC returns a RPC client which connects to the given endpoint. 178 // The check for empty endpoint implements the defaulting logic 179 // for "geth attach" with no argument. 180 func dialRPC(endpoint string) (*rpc.Client, error) { 181 if endpoint == "" { 182 endpoint = node.DefaultIPCEndpoint(clientIdentifier) 183 } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") { 184 // Backwards compatibility with geth < 1.5 which required 185 // these prefixes. 186 endpoint = endpoint[4:] 187 } 188 return rpc.Dial(endpoint) 189 } 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 }