consolecmd.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. package main
  17. import (
  18. "fmt"
  19. "os"
  20. "os/signal"
  21. "path/filepath"
  22. "strings"
  23. "syscall"
  24. "github.com/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/console"
  26. "github.com/ethereum/go-ethereum/node"
  27. "github.com/ethereum/go-ethereum/rpc"
  28. "gopkg.in/urfave/cli.v1"
  29. )
  30. var (
  31. consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
  32. consoleCommand = cli.Command{
  33. Action: utils.MigrateFlags(localConsole),
  34. Name: "console",
  35. Usage: "Start an interactive JavaScript environment",
  36. Flags: append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...),
  37. Category: "CONSOLE COMMANDS",
  38. Description: `
  39. The Geth console is an interactive shell for the JavaScript runtime environment
  40. which exposes a node admin interface as well as the Ðapp JavaScript API.
  41. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console.`,
  42. }
  43. attachCommand = cli.Command{
  44. Action: utils.MigrateFlags(remoteConsole),
  45. Name: "attach",
  46. Usage: "Start an interactive JavaScript environment (connect to node)",
  47. ArgsUsage: "[endpoint]",
  48. Flags: append(consoleFlags, utils.DataDirFlag),
  49. Category: "CONSOLE COMMANDS",
  50. Description: `
  51. The Geth console is an interactive shell for the JavaScript runtime environment
  52. which exposes a node admin interface as well as the Ðapp JavaScript API.
  53. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console.
  54. This command allows to open a console on a running geth node.`,
  55. }
  56. javascriptCommand = cli.Command{
  57. Action: utils.MigrateFlags(ephemeralConsole),
  58. Name: "js",
  59. Usage: "Execute the specified JavaScript files",
  60. ArgsUsage: "<jsfile> [jsfile...]",
  61. Flags: append(nodeFlags, consoleFlags...),
  62. Category: "CONSOLE COMMANDS",
  63. Description: `
  64. The JavaScript VM exposes a node admin interface as well as the Ðapp
  65. JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console`,
  66. }
  67. )
  68. // localConsole starts a new geth node, attaching a JavaScript console to it at the
  69. // same time.
  70. func localConsole(ctx *cli.Context) error {
  71. // Create and start the node based on the CLI flags
  72. prepare(ctx)
  73. stack, backend := makeFullNode(ctx)
  74. startNode(ctx, stack, backend)
  75. defer stack.Close()
  76. // Attach to the newly started node and start the JavaScript console
  77. client, err := stack.Attach()
  78. if err != nil {
  79. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  80. }
  81. config := console.Config{
  82. DataDir: utils.MakeDataDir(ctx),
  83. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  84. Client: client,
  85. Preload: utils.MakeConsolePreloads(ctx),
  86. }
  87. console, err := console.New(config)
  88. if err != nil {
  89. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  90. }
  91. defer console.Stop(false)
  92. // If only a short execution was requested, evaluate and return
  93. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  94. console.Evaluate(script)
  95. return nil
  96. }
  97. // Otherwise print the welcome screen and enter interactive mode
  98. console.Welcome()
  99. console.Interactive()
  100. return nil
  101. }
  102. // remoteConsole will connect to a remote geth instance, attaching a JavaScript
  103. // console to it.
  104. func remoteConsole(ctx *cli.Context) error {
  105. // Attach to a remotely running geth instance and start the JavaScript console
  106. endpoint := ctx.Args().First()
  107. if endpoint == "" {
  108. path := node.DefaultDataDir()
  109. if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
  110. path = ctx.GlobalString(utils.DataDirFlag.Name)
  111. }
  112. if path != "" {
  113. if ctx.GlobalBool(utils.LegacyTestnetFlag.Name) || ctx.GlobalBool(utils.RopstenFlag.Name) {
  114. // Maintain compatibility with older Geth configurations storing the
  115. // Ropsten database in `testnet` instead of `ropsten`.
  116. legacyPath := filepath.Join(path, "testnet")
  117. if _, err := os.Stat(legacyPath); !os.IsNotExist(err) {
  118. path = legacyPath
  119. } else {
  120. path = filepath.Join(path, "ropsten")
  121. }
  122. } else if ctx.GlobalBool(utils.RinkebyFlag.Name) {
  123. path = filepath.Join(path, "rinkeby")
  124. } else if ctx.GlobalBool(utils.GoerliFlag.Name) {
  125. path = filepath.Join(path, "goerli")
  126. } else if ctx.GlobalBool(utils.YoloV1Flag.Name) {
  127. path = filepath.Join(path, "yolo-v1")
  128. }
  129. }
  130. endpoint = fmt.Sprintf("%s/geth.ipc", path)
  131. }
  132. client, err := dialRPC(endpoint)
  133. if err != nil {
  134. utils.Fatalf("Unable to attach to remote geth: %v", err)
  135. }
  136. config := console.Config{
  137. DataDir: utils.MakeDataDir(ctx),
  138. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  139. Client: client,
  140. Preload: utils.MakeConsolePreloads(ctx),
  141. }
  142. console, err := console.New(config)
  143. if err != nil {
  144. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  145. }
  146. defer console.Stop(false)
  147. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  148. console.Evaluate(script)
  149. return nil
  150. }
  151. // Otherwise print the welcome screen and enter interactive mode
  152. console.Welcome()
  153. console.Interactive()
  154. return nil
  155. }
  156. // dialRPC returns a RPC client which connects to the given endpoint.
  157. // The check for empty endpoint implements the defaulting logic
  158. // for "geth attach" and "geth monitor" with no argument.
  159. func dialRPC(endpoint string) (*rpc.Client, error) {
  160. if endpoint == "" {
  161. endpoint = node.DefaultIPCEndpoint(clientIdentifier)
  162. } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  163. // Backwards compatibility with geth < 1.5 which required
  164. // these prefixes.
  165. endpoint = endpoint[4:]
  166. }
  167. return rpc.Dial(endpoint)
  168. }
  169. // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
  170. // console to it, executes each of the files specified as arguments and tears
  171. // everything down.
  172. func ephemeralConsole(ctx *cli.Context) error {
  173. // Create and start the node based on the CLI flags
  174. stack, backend := makeFullNode(ctx)
  175. startNode(ctx, stack, backend)
  176. defer stack.Close()
  177. // Attach to the newly started node and start the JavaScript console
  178. client, err := stack.Attach()
  179. if err != nil {
  180. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  181. }
  182. config := console.Config{
  183. DataDir: utils.MakeDataDir(ctx),
  184. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  185. Client: client,
  186. Preload: utils.MakeConsolePreloads(ctx),
  187. }
  188. console, err := console.New(config)
  189. if err != nil {
  190. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  191. }
  192. defer console.Stop(false)
  193. // Evaluate each of the specified JavaScript files
  194. for _, file := range ctx.Args() {
  195. if err = console.Execute(file); err != nil {
  196. utils.Fatalf("Failed to execute %s: %v", file, err)
  197. }
  198. }
  199. // Wait for pending callbacks, but stop for Ctrl-C.
  200. abort := make(chan os.Signal, 1)
  201. signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
  202. go func() {
  203. <-abort
  204. os.Exit(0)
  205. }()
  206. console.Stop(true)
  207. return nil
  208. }