github.com/bhs-gq/quorum-hotstuff@v21.1.0+incompatible/console/console.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package console 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "io/ioutil" 24 "os" 25 "os/signal" 26 "path/filepath" 27 "regexp" 28 "sort" 29 "strings" 30 "syscall" 31 32 "github.com/ethereum/go-ethereum/internal/jsre" 33 "github.com/ethereum/go-ethereum/internal/web3ext" 34 "github.com/ethereum/go-ethereum/rpc" 35 "github.com/mattn/go-colorable" 36 "github.com/peterh/liner" 37 "github.com/robertkrimen/otto" 38 ) 39 40 var ( 41 // u: unlock, s: signXX, sendXX, n: newAccount, i: importXX 42 passwordRegexp = regexp.MustCompile(`personal.[nusi]`) 43 onlyWhitespace = regexp.MustCompile(`^\s*$`) 44 exit = regexp.MustCompile(`^\s*exit\s*;*\s*$`) 45 ) 46 47 // HistoryFile is the file within the data directory to store input scrollback. 48 const HistoryFile = "history" 49 50 // DefaultPrompt is the default prompt line prefix to use for user input querying. 51 const DefaultPrompt = "> " 52 53 // Config is the collection of configurations to fine tune the behavior of the 54 // JavaScript console. 55 type Config struct { 56 DataDir string // Data directory to store the console history at 57 DocRoot string // Filesystem path from where to load JavaScript files from 58 Client *rpc.Client // RPC client to execute Ethereum requests through 59 Prompt string // Input prompt prefix string (defaults to DefaultPrompt) 60 Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter) 61 Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout) 62 Preload []string // Absolute paths to JavaScript files to preload 63 } 64 65 // Console is a JavaScript interpreted runtime environment. It is a fully fledged 66 // JavaScript console attached to a running node via an external or in-process RPC 67 // client. 68 type Console struct { 69 client *rpc.Client // RPC client to execute Ethereum requests through 70 jsre *jsre.JSRE // JavaScript runtime environment running the interpreter 71 prompt string // Input prompt prefix string 72 prompter UserPrompter // Input prompter to allow interactive user feedback 73 histPath string // Absolute path to the console scrollback history 74 history []string // Scroll history maintained by the console 75 printer io.Writer // Output writer to serialize any display strings to 76 } 77 78 // New initializes a JavaScript interpreted runtime environment and sets defaults 79 // with the config struct. 80 func New(config Config) (*Console, error) { 81 // Handle unset config values gracefully 82 if config.Prompter == nil { 83 config.Prompter = Stdin 84 } 85 if config.Prompt == "" { 86 config.Prompt = DefaultPrompt 87 } 88 if config.Printer == nil { 89 config.Printer = colorable.NewColorableStdout() 90 } 91 // Initialize the console and return 92 console := &Console{ 93 client: config.Client, 94 jsre: jsre.New(config.DocRoot, config.Printer), 95 prompt: config.Prompt, 96 prompter: config.Prompter, 97 printer: config.Printer, 98 histPath: filepath.Join(config.DataDir, HistoryFile), 99 } 100 if err := os.MkdirAll(config.DataDir, 0700); err != nil { 101 return nil, err 102 } 103 if err := console.init(config.Preload); err != nil { 104 return nil, err 105 } 106 return console, nil 107 } 108 109 // init retrieves the available APIs from the remote RPC provider and initializes 110 // the console's JavaScript namespaces based on the exposed modules. 111 func (c *Console) init(preload []string) error { 112 // Initialize the JavaScript <-> Go RPC bridge 113 bridge := newBridge(c.client, c.prompter, c.printer) 114 c.jsre.Set("jeth", struct{}{}) 115 116 jethObj, _ := c.jsre.Get("jeth") 117 jethObj.Object().Set("send", bridge.Send) 118 jethObj.Object().Set("sendAsync", bridge.Send) 119 120 consoleObj, _ := c.jsre.Get("console") 121 consoleObj.Object().Set("log", c.consoleOutput) 122 consoleObj.Object().Set("error", c.consoleOutput) 123 124 // Load all the internal utility JavaScript libraries 125 if err := c.jsre.Compile("bignumber.js", jsre.BignumberJs); err != nil { 126 return fmt.Errorf("bignumber.js: %v", err) 127 } 128 if err := c.jsre.Compile("web3.js", jsre.Web3Js); err != nil { 129 return fmt.Errorf("web3.js: %v", err) 130 } 131 if _, err := c.jsre.Run("var Web3 = require('web3');"); err != nil { 132 return fmt.Errorf("web3 require: %v", err) 133 } 134 if _, err := c.jsre.Run("var web3 = new Web3(jeth);"); err != nil { 135 return fmt.Errorf("web3 provider: %v", err) 136 } 137 // Load the supported APIs into the JavaScript runtime environment 138 apis, err := c.client.SupportedModules() 139 if err != nil { 140 return fmt.Errorf("api modules: %v", err) 141 } 142 flatten := "var eth = web3.eth; var personal = web3.personal; " 143 for api := range apis { 144 if api == "web3" { 145 continue // manually mapped or ignore 146 } 147 //quorum 148 // the @ symbol results in errors that prevent the extension from being added to the web3 object 149 api = strings.Replace(api, "plugin@", "plugin_", 1) 150 //!quorum 151 152 if file, ok := web3ext.Modules[api]; ok { 153 // Load our extension for the module. 154 if err = c.jsre.Compile(fmt.Sprintf("%s.js", api), file); err != nil { 155 return fmt.Errorf("%s.js: %v", api, err) 156 } 157 flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) 158 } else if obj, err := c.jsre.Run("web3." + api); err == nil && obj.IsObject() { 159 // Enable web3.js built-in extension if available. 160 flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) 161 } 162 } 163 if _, err = c.jsre.Run(flatten); err != nil { 164 return fmt.Errorf("namespace flattening: %v", err) 165 } 166 // Initialize the global name register (disabled for now) 167 //c.jsre.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`) 168 169 // If the console is in interactive mode, instrument password related methods to query the user 170 if c.prompter != nil { 171 // Retrieve the account management object to instrument 172 personal, err := c.jsre.Get("personal") 173 if err != nil { 174 return err 175 } 176 // Override the openWallet, unlockAccount, newAccount and sign methods since 177 // these require user interaction. Assign these method in the Console the 178 // original web3 callbacks. These will be called by the jeth.* methods after 179 // they got the password from the user and send the original web3 request to 180 // the backend. 181 if obj := personal.Object(); obj != nil { // make sure the personal api is enabled over the interface 182 if _, err = c.jsre.Run(`jeth.openWallet = personal.openWallet;`); err != nil { 183 return fmt.Errorf("personal.openWallet: %v", err) 184 } 185 if _, err = c.jsre.Run(`jeth.unlockAccount = personal.unlockAccount;`); err != nil { 186 return fmt.Errorf("personal.unlockAccount: %v", err) 187 } 188 if _, err = c.jsre.Run(`jeth.newAccount = personal.newAccount;`); err != nil { 189 return fmt.Errorf("personal.newAccount: %v", err) 190 } 191 if _, err = c.jsre.Run(`jeth.sign = personal.sign;`); err != nil { 192 return fmt.Errorf("personal.sign: %v", err) 193 } 194 obj.Set("openWallet", bridge.OpenWallet) 195 obj.Set("unlockAccount", bridge.UnlockAccount) 196 obj.Set("newAccount", bridge.NewAccount) 197 obj.Set("sign", bridge.Sign) 198 } 199 } 200 // The admin.sleep and admin.sleepBlocks are offered by the console and not by the RPC layer. 201 admin, err := c.jsre.Get("admin") 202 if err != nil { 203 return err 204 } 205 if obj := admin.Object(); obj != nil { // make sure the admin api is enabled over the interface 206 obj.Set("sleepBlocks", bridge.SleepBlocks) 207 obj.Set("sleep", bridge.Sleep) 208 obj.Set("clearHistory", c.clearHistory) 209 } 210 // Preload any JavaScript files before starting the console 211 for _, path := range preload { 212 if err := c.jsre.Exec(path); err != nil { 213 failure := err.Error() 214 if ottoErr, ok := err.(*otto.Error); ok { 215 failure = ottoErr.String() 216 } 217 return fmt.Errorf("%s: %v", path, failure) 218 } 219 } 220 // Configure the console's input prompter for scrollback and tab completion 221 if c.prompter != nil { 222 if content, err := ioutil.ReadFile(c.histPath); err != nil { 223 c.prompter.SetHistory(nil) 224 } else { 225 c.history = strings.Split(string(content), "\n") 226 c.prompter.SetHistory(c.history) 227 } 228 c.prompter.SetWordCompleter(c.AutoCompleteInput) 229 } 230 return nil 231 } 232 233 func (c *Console) clearHistory() { 234 c.history = nil 235 c.prompter.ClearHistory() 236 if err := os.Remove(c.histPath); err != nil { 237 fmt.Fprintln(c.printer, "can't delete history file:", err) 238 } else { 239 fmt.Fprintln(c.printer, "history file deleted") 240 } 241 } 242 243 // consoleOutput is an override for the console.log and console.error methods to 244 // stream the output into the configured output stream instead of stdout. 245 func (c *Console) consoleOutput(call otto.FunctionCall) otto.Value { 246 var output []string 247 for _, argument := range call.ArgumentList { 248 output = append(output, fmt.Sprintf("%v", argument)) 249 } 250 fmt.Fprintln(c.printer, strings.Join(output, " ")) 251 return otto.Value{} 252 } 253 254 // AutoCompleteInput is a pre-assembled word completer to be used by the user 255 // input prompter to provide hints to the user about the methods available. 256 func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, string) { 257 // No completions can be provided for empty inputs 258 if len(line) == 0 || pos == 0 { 259 return "", nil, "" 260 } 261 // Chunck data to relevant part for autocompletion 262 // E.g. in case of nested lines eth.getBalance(eth.coinb<tab><tab> 263 start := pos - 1 264 for ; start > 0; start-- { 265 // Skip all methods and namespaces (i.e. including the dot) 266 if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') { 267 continue 268 } 269 // Handle web3 in a special way (i.e. other numbers aren't auto completed) 270 if start >= 3 && line[start-3:start] == "web3" { 271 start -= 3 272 continue 273 } 274 // We've hit an unexpected character, autocomplete form here 275 start++ 276 break 277 } 278 return line[:start], c.jsre.CompleteKeywords(line[start:pos]), line[pos:] 279 } 280 281 // Welcome show summary of current Geth instance and some metadata about the 282 // console's available modules. 283 func (c *Console) Welcome() { 284 message := "Welcome to the Geth JavaScript console!\n\n" 285 286 // Quorum: Block timestamp for Raft is in nanoseconds, so convert accordingly 287 consensus := c.getConsensus() 288 if consensus == "raft" { 289 // Print some generic Geth metadata 290 if res, err := c.jsre.Run(` 291 var message = "instance: " + web3.version.node + "\n"; 292 try { 293 message += "coinbase: " + eth.coinbase + "\n"; 294 } catch (err) {} 295 message += "at block: " + eth.blockNumber + " (" + new Date(eth.getBlock(eth.blockNumber).timestamp / 1000000) + ")\n"; 296 try { 297 message += " datadir: " + admin.datadir + "\n"; 298 } catch (err) {} 299 message 300 `); err == nil { 301 message += res.String() 302 } 303 } else { 304 // Print some generic Geth metadata 305 if res, err := c.jsre.Run(` 306 var message = "instance: " + web3.version.node + "\n"; 307 try { 308 message += "coinbase: " + eth.coinbase + "\n"; 309 } catch (err) {} 310 message += "at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")\n"; 311 try { 312 message += " datadir: " + admin.datadir + "\n"; 313 } catch (err) {} 314 message 315 `); err == nil { 316 message += res.String() 317 } 318 } 319 // List all the supported modules for the user to call 320 if apis, err := c.client.SupportedModules(); err == nil { 321 modules := make([]string, 0, len(apis)) 322 for api, version := range apis { 323 modules = append(modules, fmt.Sprintf("%s:%s", api, version)) 324 } 325 sort.Strings(modules) 326 message += " modules: " + strings.Join(modules, " ") + "\n" 327 } 328 fmt.Fprintln(c.printer, message) 329 } 330 331 // Get the consensus mechanism that is in use 332 func (c *Console) getConsensus() string { 333 334 var nodeInfo struct { 335 Protocols struct { 336 Eth struct { // only partial of eth/handler.go#NodeInfo 337 Consensus string 338 } 339 Istanbul struct { // a bit different from others 340 Consensus string 341 } 342 } 343 } 344 345 if err := c.client.CallContext(context.Background(), &nodeInfo, "admin_nodeInfo"); err != nil { 346 _, _ = fmt.Fprintf(c.printer, "WARNING: call to admin.getNodeInfo() failed, unable to determine consensus mechanism\n") 347 return "unknown" 348 } 349 if nodeInfo.Protocols.Istanbul.Consensus != "" { 350 return nodeInfo.Protocols.Istanbul.Consensus 351 } 352 return nodeInfo.Protocols.Eth.Consensus 353 } 354 355 // Evaluate executes code and pretty prints the result to the specified output 356 // stream. 357 func (c *Console) Evaluate(statement string) error { 358 defer func() { 359 if r := recover(); r != nil { 360 fmt.Fprintf(c.printer, "[native] error: %v\n", r) 361 } 362 }() 363 return c.jsre.Evaluate(statement, c.printer) 364 } 365 366 // Interactive starts an interactive user session, where input is propted from 367 // the configured user prompter. 368 func (c *Console) Interactive() { 369 var ( 370 prompt = c.prompt // Current prompt line (used for multi-line inputs) 371 indents = 0 // Current number of input indents (used for multi-line inputs) 372 input = "" // Current user input 373 scheduler = make(chan string) // Channel to send the next prompt on and receive the input 374 ) 375 // Start a goroutine to listen for prompt requests and send back inputs 376 go func() { 377 for { 378 // Read the next user input 379 line, err := c.prompter.PromptInput(<-scheduler) 380 if err != nil { 381 // In case of an error, either clear the prompt or fail 382 if err == liner.ErrPromptAborted { // ctrl-C 383 prompt, indents, input = c.prompt, 0, "" 384 scheduler <- "" 385 continue 386 } 387 close(scheduler) 388 return 389 } 390 // User input retrieved, send for interpretation and loop 391 scheduler <- line 392 } 393 }() 394 // Monitor Ctrl-C too in case the input is empty and we need to bail 395 abort := make(chan os.Signal, 1) 396 signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM) 397 398 // Start sending prompts to the user and reading back inputs 399 for { 400 // Send the next prompt, triggering an input read and process the result 401 scheduler <- prompt 402 select { 403 case <-abort: 404 // User forcefully quite the console 405 fmt.Fprintln(c.printer, "caught interrupt, exiting") 406 return 407 408 case line, ok := <-scheduler: 409 // User input was returned by the prompter, handle special cases 410 if !ok || (indents <= 0 && exit.MatchString(line)) { 411 return 412 } 413 if onlyWhitespace.MatchString(line) { 414 continue 415 } 416 // Append the line to the input and check for multi-line interpretation 417 input += line + "\n" 418 419 indents = countIndents(input) 420 if indents <= 0 { 421 prompt = c.prompt 422 } else { 423 prompt = strings.Repeat(".", indents*3) + " " 424 } 425 // If all the needed lines are present, save the command and run 426 if indents <= 0 { 427 if len(input) > 0 && input[0] != ' ' && !passwordRegexp.MatchString(input) { 428 if command := strings.TrimSpace(input); len(c.history) == 0 || command != c.history[len(c.history)-1] { 429 c.history = append(c.history, command) 430 if c.prompter != nil { 431 c.prompter.AppendHistory(command) 432 } 433 } 434 } 435 c.Evaluate(input) 436 input = "" 437 } 438 } 439 } 440 } 441 442 // countIndents returns the number of identations for the given input. 443 // In case of invalid input such as var a = } the result can be negative. 444 func countIndents(input string) int { 445 var ( 446 indents = 0 447 inString = false 448 strOpenChar = ' ' // keep track of the string open char to allow var str = "I'm ...."; 449 charEscaped = false // keep track if the previous char was the '\' char, allow var str = "abc\"def"; 450 ) 451 452 for _, c := range input { 453 switch c { 454 case '\\': 455 // indicate next char as escaped when in string and previous char isn't escaping this backslash 456 if !charEscaped && inString { 457 charEscaped = true 458 } 459 case '\'', '"': 460 if inString && !charEscaped && strOpenChar == c { // end string 461 inString = false 462 } else if !inString && !charEscaped { // begin string 463 inString = true 464 strOpenChar = c 465 } 466 charEscaped = false 467 case '{', '(': 468 if !inString { // ignore brackets when in string, allow var str = "a{"; without indenting 469 indents++ 470 } 471 charEscaped = false 472 case '}', ')': 473 if !inString { 474 indents-- 475 } 476 charEscaped = false 477 default: 478 charEscaped = false 479 } 480 } 481 482 return indents 483 } 484 485 // Execute runs the JavaScript file specified as the argument. 486 func (c *Console) Execute(path string) error { 487 return c.jsre.Exec(path) 488 } 489 490 // Stop cleans up the console and terminates the runtime environment. 491 func (c *Console) Stop(graceful bool) error { 492 if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil { 493 return err 494 } 495 if err := os.Chmod(c.histPath, 0600); err != nil { // Force 0600, even if it was different previously 496 return err 497 } 498 c.jsre.Stop(graceful) 499 return nil 500 }