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