github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/geth/consolecmd.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:32</date> 10 //</624450068426002432> 11 12 13 package main 14 15 import ( 16 "fmt" 17 "os" 18 "os/signal" 19 "path/filepath" 20 "strings" 21 "syscall" 22 23 "github.com/ethereum/go-ethereum/cmd/utils" 24 "github.com/ethereum/go-ethereum/console" 25 "github.com/ethereum/go-ethereum/node" 26 "github.com/ethereum/go-ethereum/rpc" 27 "gopkg.in/urfave/cli.v1" 28 ) 29 30 var ( 31 consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag} 32 33 consoleCommand = cli.Command{ 34 Action: utils.MigrateFlags(localConsole), 35 Name: "console", 36 Usage: "Start an interactive JavaScript environment", 37 Flags: append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...), 38 Category: "CONSOLE COMMANDS", 39 Description: ` 40 The Geth console is an interactive shell for the JavaScript runtime environment 41 which exposes a node admin interface as well as the Ðapp JavaScript API. 42 See https://github.com/ethereum/go-ethereum/wiki/javascript控制台。 43 } 44 45 attachCommand = cli.Command{ 46 Action: utils.MigrateFlags(remoteConsole), 47 Name: "attach", 48 Usage: "Start an interactive JavaScript environment (connect to node)", 49 ArgsUsage: "[endpoint]", 50 Flags: append(consoleFlags, utils.DataDirFlag), 51 Category: "CONSOLE COMMANDS", 52 Description: ` 53 The Geth console is an interactive shell for the JavaScript runtime environment 54 which exposes a node admin interface as well as the Ðapp JavaScript API. 55 See https://github.com/ethereum/go-ethereum/wiki/javascript-console。 56 This command allows to open a console on a running geth node.`, 57 } 58 59 javascriptCommand = cli.Command{ 60 Action: utils.MigrateFlags(ephemeralConsole), 61 Name: "js", 62 Usage: "Execute the specified JavaScript files", 63 ArgsUsage: "<jsfile> [jsfile...]", 64 Flags: append(nodeFlags, consoleFlags...), 65 Category: "CONSOLE COMMANDS", 66 Description: ` 67 The JavaScript VM exposes a node admin interface as well as the Ðapp 68 JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/javascript控制台`, 69 } 70 ) 71 72 //localconsole启动一个新的geth节点,在 73 //同时。 74 func localConsole(ctx *cli.Context) error { 75 //根据CLI标志创建和启动节点 76 node := makeFullNode(ctx) 77 startNode(ctx, node) 78 defer node.Stop() 79 80 //附加到新启动的节点并启动javascript控制台 81 client, err := node.Attach() 82 if err != nil { 83 utils.Fatalf("Failed to attach to the inproc geth: %v", err) 84 } 85 config := console.Config{ 86 DataDir: utils.MakeDataDir(ctx), 87 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 88 Client: client, 89 Preload: utils.MakeConsolePreloads(ctx), 90 } 91 92 console, err := console.New(config) 93 if err != nil { 94 utils.Fatalf("Failed to start the JavaScript console: %v", err) 95 } 96 defer console.Stop(false) 97 98 //如果只请求短时间执行,则计算并返回 99 if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { 100 console.Evaluate(script) 101 return nil 102 } 103 //否则,打印欢迎屏幕并进入交互模式 104 console.Welcome() 105 console.Interactive() 106 107 return nil 108 } 109 110 //远程控制台将连接到远程geth实例,附加一个javascript 111 // 112 func remoteConsole(ctx *cli.Context) error { 113 //连接到远程运行的geth实例并启动javascript控制台 114 endpoint := ctx.Args().First() 115 if endpoint == "" { 116 path := node.DefaultDataDir() 117 if ctx.GlobalIsSet(utils.DataDirFlag.Name) { 118 path = ctx.GlobalString(utils.DataDirFlag.Name) 119 } 120 if path != "" { 121 if ctx.GlobalBool(utils.TestnetFlag.Name) { 122 path = filepath.Join(path, "testnet") 123 } else if ctx.GlobalBool(utils.RinkebyFlag.Name) { 124 path = filepath.Join(path, "rinkeby") 125 } 126 } 127 endpoint = fmt.Sprintf("%s/geth.ipc", path) 128 } 129 client, err := dialRPC(endpoint) 130 if err != nil { 131 utils.Fatalf("Unable to attach to remote geth: %v", err) 132 } 133 config := console.Config{ 134 DataDir: utils.MakeDataDir(ctx), 135 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 136 Client: client, 137 Preload: utils.MakeConsolePreloads(ctx), 138 } 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 146 if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" { 147 console.Evaluate(script) 148 return nil 149 } 150 151 //否则,打印欢迎屏幕并进入交互模式 152 console.Welcome() 153 console.Interactive() 154 155 return nil 156 } 157 158 //dialrpc返回连接到给定端点的RPC客户端。 159 //空端点检查实现了默认逻辑 160 //对于没有参数的“geth attach”和“geth monitor”。 161 func dialRPC(endpoint string) (*rpc.Client, error) { 162 if endpoint == "" { 163 endpoint = node.DefaultIPCEndpoint(clientIdentifier) 164 } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") { 165 //与geth的向后兼容性<1.5,这需要 166 //这些前缀。 167 endpoint = endpoint[4:] 168 } 169 return rpc.Dial(endpoint) 170 } 171 172 //ephemeralconsole启动一个新的geth节点,附加一个ephemeral javascript 173 //控制台,执行指定为参数和眼泪的每个文件 174 //一切都结束了。 175 func ephemeralConsole(ctx *cli.Context) error { 176 //根据CLI标志创建和启动节点 177 node := makeFullNode(ctx) 178 startNode(ctx, node) 179 defer node.Stop() 180 181 //附加到新启动的节点并启动javascript控制台 182 client, err := node.Attach() 183 if err != nil { 184 utils.Fatalf("Failed to attach to the inproc geth: %v", err) 185 } 186 config := console.Config{ 187 DataDir: utils.MakeDataDir(ctx), 188 DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), 189 Client: client, 190 Preload: utils.MakeConsolePreloads(ctx), 191 } 192 193 console, err := console.New(config) 194 if err != nil { 195 utils.Fatalf("Failed to start the JavaScript console: %v", err) 196 } 197 defer console.Stop(false) 198 199 //评估每个指定的javascript文件 200 for _, file := range ctx.Args() { 201 if err = console.Execute(file); err != nil { 202 utils.Fatalf("Failed to execute %s: %v", file, err) 203 } 204 } 205 //等待挂起的回调,但停止使用ctrl-c。 206 abort := make(chan os.Signal, 1) 207 signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM) 208 209 go func() { 210 <-abort 211 os.Exit(0) 212 }() 213 console.Stop(true) 214 215 return nil 216 } 217