consolecmd.go 7.1 KB

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