diff options
author | Péter Szilágyi <peterke@gmail.com> | 2016-05-06 17:40:23 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2016-05-30 22:25:23 +0800 |
commit | ffaf58f0a98bd987bbe76e8669bb22c405dcd62a (patch) | |
tree | 4cd484504d4a7089cfd85d22a2a9fa62c79abb7d /console | |
parent | ab664c7e17009729d1083d8d4f7c37eb387012d3 (diff) | |
download | dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.gz dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.bz2 dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.lz dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.xz dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.tar.zst dexon-ffaf58f0a98bd987bbe76e8669bb22c405dcd62a.zip |
cmd, console: split off the console into a reusable package
Diffstat (limited to 'console')
-rw-r--r-- | console/bridge.go | 317 | ||||
-rw-r--r-- | console/console.go | 369 | ||||
-rw-r--r-- | console/console_test.go | 283 | ||||
-rw-r--r-- | console/prompter.go | 156 | ||||
-rw-r--r-- | console/testdata/exec.js | 1 | ||||
-rw-r--r-- | console/testdata/preload.js | 1 |
6 files changed, 1127 insertions, 0 deletions
diff --git a/console/bridge.go b/console/bridge.go new file mode 100644 index 000000000..b23e06837 --- /dev/null +++ b/console/bridge.go @@ -0,0 +1,317 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package console + +import ( + "encoding/json" + "fmt" + "io" + "time" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rpc" + "github.com/robertkrimen/otto" +) + +// bridge is a collection of JavaScript utility methods to bride the .js runtime +// environment and the Go RPC connection backing the remote method calls. +type bridge struct { + client rpc.Client // RPC client to execute Ethereum requests through + prompter UserPrompter // Input prompter to allow interactive user feedback + printer io.Writer // Output writer to serialize any display strings to +} + +// newBridge creates a new JavaScript wrapper around an RPC client. +func newBridge(client rpc.Client, prompter UserPrompter, printer io.Writer) *bridge { + return &bridge{ + client: client, + prompter: prompter, + printer: printer, + } +} + +// NewAccount is a wrapper around the personal.newAccount RPC method that uses a +// non-echoing password prompt to aquire the passphrase and executes the original +// RPC method (saved in jeth.newAccount) with it to actually execute the RPC call. +func (b *bridge) NewAccount(call otto.FunctionCall) (response otto.Value) { + var ( + password string + confirm string + err error + ) + switch { + // No password was specified, prompt the user for it + case len(call.ArgumentList) == 0: + if password, err = b.prompter.PromptPassword("Passphrase: "); err != nil { + throwJSException(err.Error()) + } + if confirm, err = b.prompter.PromptPassword("Repeat passphrase: "); err != nil { + throwJSException(err.Error()) + } + if password != confirm { + throwJSException("passphrases don't match!") + } + + // A single string password was specified, use that + case len(call.ArgumentList) == 1 && call.Argument(0).IsString(): + password, _ = call.Argument(0).ToString() + + // Otherwise fail with some error + default: + throwJSException("expected 0 or 1 string argument") + } + // Password aquired, execute the call and return + ret, err := call.Otto.Call("jeth.newAccount", nil, password) + if err != nil { + throwJSException(err.Error()) + } + return ret +} + +// UnlockAccount is a wrapper around the personal.unlockAccount RPC method that +// uses a non-echoing password prompt to aquire the passphrase and executes the +// original RPC method (saved in jeth.unlockAccount) with it to actually execute +// the RPC call. +func (b *bridge) UnlockAccount(call otto.FunctionCall) (response otto.Value) { + // Make sure we have an account specified to unlock + if !call.Argument(0).IsString() { + throwJSException("first argument must be the account to unlock") + } + account := call.Argument(0) + + // If password is not given or is the null value, prompt the user for it + var passwd otto.Value + + if call.Argument(1).IsUndefined() || call.Argument(1).IsNull() { + fmt.Fprintf(b.printer, "Unlock account %s\n", account) + if input, err := b.prompter.PromptPassword("Passphrase: "); err != nil { + throwJSException(err.Error()) + } else { + passwd, _ = otto.ToValue(input) + } + } else { + if !call.Argument(1).IsString() { + throwJSException("password must be a string") + } + passwd = call.Argument(1) + } + // Third argument is the duration how long the account must be unlocked. + duration := otto.NullValue() + if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() { + if !call.Argument(2).IsNumber() { + throwJSException("unlock duration must be a number") + } + duration = call.Argument(2) + } + // Send the request to the backend and return + val, err := call.Otto.Call("jeth.unlockAccount", nil, account, passwd, duration) + if err != nil { + throwJSException(err.Error()) + } + return val +} + +// Sleep will block the console for the specified number of seconds. +func (b *bridge) Sleep(call otto.FunctionCall) (response otto.Value) { + if call.Argument(0).IsNumber() { + sleep, _ := call.Argument(0).ToInteger() + time.Sleep(time.Duration(sleep) * time.Second) + return otto.TrueValue() + } + return throwJSException("usage: sleep(<number of seconds>)") +} + +// SleepBlocks will block the console for a specified number of new blocks optionally +// until the given timeout is reached. +func (b *bridge) SleepBlocks(call otto.FunctionCall) (response otto.Value) { + var ( + blocks = int64(0) + sleep = int64(9999999999999999) // indefinitely + ) + // Parse the input parameters for the sleep + nArgs := len(call.ArgumentList) + if nArgs == 0 { + throwJSException("usage: sleepBlocks(<n blocks>[, max sleep in seconds])") + } + if nArgs >= 1 { + if call.Argument(0).IsNumber() { + blocks, _ = call.Argument(0).ToInteger() + } else { + throwJSException("expected number as first argument") + } + } + if nArgs >= 2 { + if call.Argument(1).IsNumber() { + sleep, _ = call.Argument(1).ToInteger() + } else { + throwJSException("expected number as second argument") + } + } + // go through the console, this will allow web3 to call the appropriate + // callbacks if a delayed response or notification is received. + blockNumber := func() int64 { + result, err := call.Otto.Run("eth.blockNumber") + if err != nil { + throwJSException(err.Error()) + } + block, err := result.ToInteger() + if err != nil { + throwJSException(err.Error()) + } + return block + } + // Poll the current block number until either it ot a timeout is reached + targetBlockNr := blockNumber() + blocks + deadline := time.Now().Add(time.Duration(sleep) * time.Second) + + for time.Now().Before(deadline) { + if blockNumber() >= targetBlockNr { + return otto.TrueValue() + } + time.Sleep(time.Second) + } + return otto.FalseValue() +} + +// Send will serialize the first argument, send it to the node and returns the response. +func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) { + // Ensure that we've got a batch request (array) or a single request (object) + arg := call.Argument(0).Object() + if arg == nil || (arg.Class() != "Array" && arg.Class() != "Object") { + throwJSException("request must be an object or array") + } + // Convert the otto VM arguments to Go values + data, err := call.Otto.Call("JSON.stringify", nil, arg) + if err != nil { + throwJSException(err.Error()) + } + reqjson, err := data.ToString() + if err != nil { + throwJSException(err.Error()) + } + + var ( + reqs []rpc.JSONRequest + batch = true + ) + if err = json.Unmarshal([]byte(reqjson), &reqs); err != nil { + // single request? + reqs = make([]rpc.JSONRequest, 1) + if err = json.Unmarshal([]byte(reqjson), &reqs[0]); err != nil { + throwJSException("invalid request") + } + batch = false + } + // Iteratively execute the requests + call.Otto.Set("response_len", len(reqs)) + call.Otto.Run("var ret_response = new Array(response_len);") + + for i, req := range reqs { + // Execute the RPC request and parse the reply + if err = b.client.Send(&req); err != nil { + return newErrorResponse(call, -32603, err.Error(), req.Id) + } + result := make(map[string]interface{}) + if err = b.client.Recv(&result); err != nil { + return newErrorResponse(call, -32603, err.Error(), req.Id) + } + // Feed the reply back into the JavaScript runtime environment + id, _ := result["id"] + jsonver, _ := result["jsonrpc"] + + call.Otto.Set("ret_id", id) + call.Otto.Set("ret_jsonrpc", jsonver) + call.Otto.Set("response_idx", i) + + if res, ok := result["result"]; ok { + payload, _ := json.Marshal(res) + call.Otto.Set("ret_result", string(payload)) + response, err = call.Otto.Run(` + ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, result: JSON.parse(ret_result) }; + `) + continue + } + if res, ok := result["error"]; ok { + payload, _ := json.Marshal(res) + call.Otto.Set("ret_result", string(payload)) + response, err = call.Otto.Run(` + ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, error: JSON.parse(ret_result) }; + `) + continue + } + return newErrorResponse(call, -32603, fmt.Sprintf("Invalid response"), new(int64)) + } + // Convert single requests back from batch ones + if !batch { + call.Otto.Run("ret_response = ret_response[0];") + } + // Execute any registered callbacks + if call.Argument(1).IsObject() { + call.Otto.Set("callback", call.Argument(1)) + call.Otto.Run(` + if (Object.prototype.toString.call(callback) == '[object Function]') { + callback(null, ret_response); + } + `) + } + return +} + +// throwJSException panics on an otto.Value. The Otto VM will recover from the +// Go panic and throw msg as a JavaScript error. +func throwJSException(msg interface{}) otto.Value { + val, err := otto.ToValue(msg) + if err != nil { + glog.V(logger.Error).Infof("Failed to serialize JavaScript exception %v: %v", msg, err) + } + panic(val) +} + +// newErrorResponse creates a JSON RPC error response for a specific request id, +// containing the specified error code and error message. Beside returning the +// error to the caller, it also sets the ret_error and ret_response JavaScript +// variables. +func newErrorResponse(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) { + // Bundle the error into a JSON RPC call response + res := rpc.JSONErrResponse{ + Version: rpc.JSONRPCVersion, + Id: id, + Error: rpc.JSONError{ + Code: code, + Message: msg, + }, + } + // Serialize the error response into JavaScript variables + errObj, err := json.Marshal(res.Error) + if err != nil { + glog.V(logger.Error).Infof("Failed to serialize JSON RPC error: %v", err) + } + resObj, err := json.Marshal(res) + if err != nil { + glog.V(logger.Error).Infof("Failed to serialize JSON RPC error response: %v", err) + } + + if _, err = call.Otto.Run("ret_error = " + string(errObj)); err != nil { + glog.V(logger.Error).Infof("Failed to set `ret_error` to the occurred error: %v", err) + } + resVal, err := call.Otto.Run("ret_response = " + string(resObj)) + if err != nil { + glog.V(logger.Error).Infof("Failed to set `ret_response` to the JSON RPC response: %v", err) + } + return resVal +} diff --git a/console/console.go b/console/console.go new file mode 100644 index 000000000..37c9f0afa --- /dev/null +++ b/console/console.go @@ -0,0 +1,369 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package console + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "os/signal" + "path/filepath" + "regexp" + "sort" + "strings" + + "github.com/ethereum/go-ethereum/internal/jsre" + "github.com/ethereum/go-ethereum/internal/web3ext" + "github.com/ethereum/go-ethereum/rpc" + "github.com/peterh/liner" + "github.com/robertkrimen/otto" +) + +var ( + passwordRegexp = regexp.MustCompile("personal.[nus]") + onlyWhitespace = regexp.MustCompile("^\\s*$") + exit = regexp.MustCompile("^\\s*exit\\s*;*\\s*$") +) + +// HistoryFile is the file within the data directory to store input scrollback. +const HistoryFile = "history" + +// DefaultPrompt is the default prompt line prefix to use for user input querying. +const DefaultPrompt = "> " + +// Config is te collection of configurations to fine tune the behavior of the +// JavaScript console. +type Config struct { + DataDir string // Data directory to store the console history at + DocRoot string // Filesystem path from where to load JavaScript files from + Client rpc.Client // RPC client to execute Ethereum requests through + Prompt string // Input prompt prefix string (defaults to DefaultPrompt) + Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter) + Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout) + Preload []string // Absolute paths to JavaScript files to preload +} + +// Console is a JavaScript interpreted runtime environment. It is a fully fleged +// JavaScript console attached to a running node via an external or in-process RPC +// client. +type Console struct { + client rpc.Client // RPC client to execute Ethereum requests through + jsre *jsre.JSRE // JavaScript runtime environment running the interpreter + prompt string // Input prompt prefix string + prompter UserPrompter // Input prompter to allow interactive user feedback + histPath string // Absolute path to the console scrollback history + history []string // Scroll history maintained by the console + printer io.Writer // Output writer to serialize any display strings to +} + +func New(config Config) (*Console, error) { + // Handle unset config values gracefully + if config.Prompter == nil { + config.Prompter = TerminalPrompter + } + if config.Prompt == "" { + config.Prompt = DefaultPrompt + } + if config.Printer == nil { + config.Printer = os.Stdout + } + // Initialize the console and return + console := &Console{ + client: config.Client, + jsre: jsre.New(config.DocRoot, config.Printer), + prompt: config.Prompt, + prompter: config.Prompter, + printer: config.Printer, + histPath: filepath.Join(config.DataDir, HistoryFile), + } + if err := console.init(config.Preload); err != nil { + return nil, err + } + return console, nil +} + +// init retrieves the available APIs from the remote RPC provider and initializes +// the console's JavaScript namespaces based on the exposed modules. +func (c *Console) init(preload []string) error { + // Initialize the JavaScript <-> Go RPC bridge + bridge := newBridge(c.client, c.prompter, c.printer) + c.jsre.Set("jeth", struct{}{}) + + jethObj, _ := c.jsre.Get("jeth") + jethObj.Object().Set("send", bridge.Send) + jethObj.Object().Set("sendAsync", bridge.Send) + + consoleObj, _ := c.jsre.Get("console") + consoleObj.Object().Set("log", c.consoleOutput) + consoleObj.Object().Set("error", c.consoleOutput) + + // Load all the internal utility JavaScript libraries + if err := c.jsre.Compile("bignumber.js", jsre.BigNumber_JS); err != nil { + return fmt.Errorf("bignumber.js: %v", err) + } + if err := c.jsre.Compile("web3.js", jsre.Web3_JS); err != nil { + return fmt.Errorf("web3.js: %v", err) + } + if _, err := c.jsre.Run("var Web3 = require('web3');"); err != nil { + return fmt.Errorf("web3 require: %v", err) + } + if _, err := c.jsre.Run("var web3 = new Web3(jeth);"); err != nil { + return fmt.Errorf("web3 provider: %v", err) + } + // Load the supported APIs into the JavaScript runtime environment + apis, err := c.client.SupportedModules() + if err != nil { + return fmt.Errorf("api modules: %v", err) + } + flatten := "var eth = web3.eth; var personal = web3.personal; " + for api := range apis { + if api == "web3" { + continue // manually mapped or ignore + } + if file, ok := web3ext.Modules[api]; ok { + if err = c.jsre.Compile(fmt.Sprintf("%s.js", api), file); err != nil { + return fmt.Errorf("%s.js: %v", api, err) + } + flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) + } + } + if _, err = c.jsre.Run(flatten); err != nil { + return fmt.Errorf("namespace flattening: %v", err) + } + // Initialize the global name register (disabled for now) + //c.jsre.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`) + + // If the console is in interactive mode, instrument password related methods to query the user + if c.prompter != nil { + // Retrieve the account management object to instrument + personal, err := c.jsre.Get("personal") + if err != nil { + return err + } + // Override the unlockAccount and newAccount methods since these require user interaction. + // Assign the jeth.unlockAccount and jeth.newAccount in the Console the original web3 callbacks. + // These will be called by the jeth.* methods after they got the password from the user and send + // the original web3 request to the backend. + if obj := personal.Object(); obj != nil { // make sure the personal api is enabled over the interface + if _, err = c.jsre.Run(`jeth.unlockAccount = personal.unlockAccount;`); err != nil { + return fmt.Errorf("personal.unlockAccount: %v", err) + } + if _, err = c.jsre.Run(`jeth.newAccount = personal.newAccount;`); err != nil { + return fmt.Errorf("personal.newAccount: %v", err) + } + obj.Set("unlockAccount", bridge.UnlockAccount) + obj.Set("newAccount", bridge.NewAccount) + } + } + // The admin.sleep and admin.sleepBlocks are offered by the console and not by the RPC layer. + admin, err := c.jsre.Get("admin") + if err != nil { + return err + } + if obj := admin.Object(); obj != nil { // make sure the admin api is enabled over the interface + obj.Set("sleepBlocks", bridge.SleepBlocks) + obj.Set("sleep", bridge.Sleep) + } + // Preload any JavaScript files before starting the console + for _, path := range preload { + if err := c.jsre.Exec(path); err != nil { + return fmt.Errorf("%s: %v", path, jsErrorString(err)) + } + } + // Configure the console's input prompter for scrollback and tab completion + if c.prompter != nil { + if content, err := ioutil.ReadFile(c.histPath); err != nil { + c.prompter.SetScrollHistory(nil) + } else { + c.prompter.SetScrollHistory(strings.Split(string(content), "\n")) + } + c.prompter.SetWordCompleter(c.AutoCompleteInput) + } + return nil +} + +// consoleOutput is an override for the console.log and console.error methods to +// stream the output into the configured output stream instead of stdout. +func (c *Console) consoleOutput(call otto.FunctionCall) otto.Value { + output := []string{} + for _, argument := range call.ArgumentList { + output = append(output, fmt.Sprintf("%v", argument)) + } + fmt.Fprintln(c.printer, strings.Join(output, " ")) + return otto.Value{} +} + +// AutoCompleteInput is a pre-assembled word completer to be used by the user +// input prompter to provide hints to the user about the methods available. +func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, string) { + // No completions can be provided for empty inputs + if len(line) == 0 || pos == 0 { + return "", nil, "" + } + // Chunck data to relevant part for autocompletion + // E.g. in case of nested lines eth.getBalance(eth.coinb<tab><tab> + start := 0 + for start = pos - 1; start > 0; start-- { + // Skip all methods and namespaces (i.e. including te dot) + if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') { + continue + } + // Handle web3 in a special way (i.e. other numbers aren't auto completed) + if start >= 3 && line[start-3:start] == "web3" { + start -= 3 + continue + } + // We've hit an unexpected character, autocomplete form here + start++ + break + } + return line[:start], c.jsre.CompleteKeywords(line[start:pos]), line[pos:] +} + +// Welcome show summary of current Geth instance and some metadata about the +// console's available modules. +func (c *Console) Welcome() { + // Print some generic Geth metadata + c.jsre.Run(` + (function () { + console.log("Welcome to the Geth JavaScript console!\n"); + console.log("instance: " + web3.version.node); + console.log("coinbase: " + eth.coinbase); + console.log("at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")"); + console.log(" datadir: " + admin.datadir); + })(); + `) + // List all the supported modules for the user to call + if apis, err := c.client.SupportedModules(); err == nil { + modules := make([]string, 0, len(apis)) + for api, version := range apis { + modules = append(modules, fmt.Sprintf("%s:%s", api, version)) + } + sort.Strings(modules) + c.jsre.Run("(function () { console.log(' modules: " + strings.Join(modules, " ") + "'); })();") + } + c.jsre.Run("(function () { console.log(); })();") +} + +// Evaluate executes code and pretty prints the result to the specified output +// stream. +func (c *Console) Evaluate(statement string) error { + defer func() { + if r := recover(); r != nil { + fmt.Fprintf(c.printer, "[native] error: %v\n", r) + } + }() + if err := c.jsre.Evaluate(statement, c.printer); err != nil { + fmt.Fprintf(c.printer, "%v\n", jsErrorString(err)) + return err + } + return nil +} + +// Interactive starts an interactive user session, where input is propted from +// the configured user prompter. +func (c *Console) Interactive() { + var ( + prompt = c.prompt // Current prompt line (used for multi-line inputs) + indents = 0 // Current number of input indents (used for multi-line inputs) + input = "" // Current user input + scheduler = make(chan string) // Channel to send the next prompt on and receive the input + ) + // Start a goroutine to listen for promt requests and send back inputs + go func() { + for { + // Read the next user input + line, err := c.prompter.PromptInput(<-scheduler) + if err != nil { + // In case of an error, either clear the prompt or fail + if err == liner.ErrPromptAborted { // ctrl-C + prompt, indents, input = c.prompt, 0, "" + scheduler <- "" + continue + } + close(scheduler) + return + } + // User input retrieved, send for interpretation and loop + scheduler <- line + } + }() + // Monitor Ctrl-C too in case the input is empty and we need to bail + abort := make(chan os.Signal, 1) + signal.Notify(abort, os.Interrupt) + + // Start sending prompts to the user and reading back inputs + for { + // Send the next prompt, triggering an input read and process the result + scheduler <- prompt + select { + case <-abort: + // User forcefully quite the console + fmt.Fprintln(c.printer, "caught interrupt, exiting") + return + + case line, ok := <-scheduler: + // User input was returned by the prompter, handle special cases + if !ok || (indents <= 0 && exit.MatchString(input)) { + return + } + if onlyWhitespace.MatchString(line) { + continue + } + // Append the line to the input and check for multi-line interpretation + input += line + "\n" + + indents = strings.Count(input, "{") + strings.Count(input, "(") - strings.Count(input, "}") - strings.Count(input, ")") + if indents <= 0 { + prompt = c.prompt + } else { + prompt = strings.Repeat("..", indents*2) + " " + } + // If all the needed lines are present, save the command and run + if indents <= 0 { + if len(input) != 0 && input[0] != ' ' && !passwordRegexp.MatchString(input) { + c.history = append(c.history, input[:len(input)-1]) + } + c.Evaluate(input) + input = "" + } + } + } +} + +// Execute runs the JavaScript file specified as the argument. +func (c *Console) Execute(path string) error { + return c.jsre.Exec(path) +} + +// Stop cleans up the console and terminates the runtime envorinment. +func (c *Console) Stop(graceful bool) error { + if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), os.ModePerm); err != nil { + return err + } + c.jsre.Stop(graceful) + return nil +} + +// jsErrorString adds a backtrace to errors generated by otto. +func jsErrorString(err error) string { + if ottoErr, ok := err.(*otto.Error); ok { + return ottoErr.String() + } + return err.Error() +} diff --git a/console/console_test.go b/console/console_test.go new file mode 100644 index 000000000..5d38331e8 --- /dev/null +++ b/console/console_test.go @@ -0,0 +1,283 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package console + +import ( + "bytes" + "errors" + "fmt" + "io/ioutil" + "math/big" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/internal/jsre" + "github.com/ethereum/go-ethereum/node" +) + +const ( + testInstance = "console-tester" + testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" +) + +// hookedPrompter implements UserPrompter to simulate use input via channels. +type hookedPrompter struct { + scheduler chan string +} + +func (p *hookedPrompter) PromptInput(prompt string) (string, error) { + // Send the prompt to the tester + select { + case p.scheduler <- prompt: + case <-time.After(time.Second): + return "", errors.New("prompt timeout") + } + // Retrieve the response and feed to the console + select { + case input := <-p.scheduler: + return input, nil + case <-time.After(time.Second): + return "", errors.New("input timeout") + } +} + +func (p *hookedPrompter) PromptPassword(prompt string) (string, error) { + return "", errors.New("not implemented") +} +func (p *hookedPrompter) PromptConfirm(prompt string) (bool, error) { + return false, errors.New("not implemented") +} +func (p *hookedPrompter) SetScrollHistory(history []string) {} +func (p *hookedPrompter) SetWordCompleter(completer WordCompleter) {} + +// tester is a console test environment for the console tests to operate on. +type tester struct { + workspace string + stack *node.Node + ethereum *eth.Ethereum + console *Console + input *hookedPrompter + output *bytes.Buffer + + lastConfirm string +} + +// newTester creates a test environment based on which the console can operate. +// Please ensure you call Close() on the returned tester to avoid leaks. +func newTester(t *testing.T, confOverride func(*eth.Config)) *tester { + // Create a temporary storage for the node keys and initialize it + workspace, err := ioutil.TempDir("", "console-tester-") + if err != nil { + t.Fatalf("failed to create temporary keystore: %v", err) + } + accman := accounts.NewPlaintextManager(filepath.Join(workspace, "keystore")) + + // Create a networkless protocol stack and start an Ethereum service within + stack, err := node.New(&node.Config{DataDir: workspace, Name: testInstance, NoDiscovery: true}) + if err != nil { + t.Fatalf("failed to create node: %v", err) + } + ethConf := ð.Config{ + ChainConfig: &core.ChainConfig{HomesteadBlock: new(big.Int)}, + Etherbase: common.HexToAddress(testAddress), + AccountManager: accman, + PowTest: true, + } + if confOverride != nil { + confOverride(ethConf) + } + if err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil { + t.Fatalf("failed to register Ethereum protocol: %v", err) + } + // Start the node and assemble the JavaScript console around it + if err = stack.Start(); err != nil { + t.Fatalf("failed to start test stack: %v", err) + } + client, err := stack.Attach() + if err != nil { + t.Fatalf("failed to attach to node: %v", err) + } + prompter := &hookedPrompter{scheduler: make(chan string)} + printer := new(bytes.Buffer) + + console, err := New(Config{ + DataDir: stack.DataDir(), + DocRoot: "testdata", + Client: client, + Prompter: prompter, + Printer: printer, + Preload: []string{"preload.js"}, + }) + if err != nil { + t.Fatalf("failed to create JavaScript console: %v", err) + } + // Create the final tester and return + var ethereum *eth.Ethereum + stack.Service(ðereum) + + return &tester{ + workspace: workspace, + stack: stack, + ethereum: ethereum, + console: console, + input: prompter, + output: printer, + } +} + +// Close cleans up any temporary data folders and held resources. +func (env *tester) Close(t *testing.T) { + if err := env.console.Stop(false); err != nil { + t.Errorf("failed to stop embedded console: %v", err) + } + if err := env.stack.Stop(); err != nil { + t.Errorf("failed to stop embedded node: %v", err) + } + os.RemoveAll(env.workspace) +} + +// Tests that the node lists the correct welcome message, notably that it contains +// the instance name, coinbase account, block number, data directory and supported +// console modules. +func TestWelcome(t *testing.T) { + tester := newTester(t, nil) + defer tester.Close(t) + + tester.console.Welcome() + + output := string(tester.output.Bytes()) + if want := "Welcome"; !strings.Contains(output, want) { + t.Fatalf("console output missing welcome message: have\n%s\nwant also %s", output, want) + } + if want := fmt.Sprintf("instance: %s", testInstance); !strings.Contains(output, want) { + t.Fatalf("console output missing instance: have\n%s\nwant also %s", output, want) + } + if want := fmt.Sprintf("coinbase: %s", testAddress); !strings.Contains(output, want) { + t.Fatalf("console output missing coinbase: have\n%s\nwant also %s", output, want) + } + if want := "at block: 0"; !strings.Contains(output, want) { + t.Fatalf("console output missing sync status: have\n%s\nwant also %s", output, want) + } + if want := fmt.Sprintf("datadir: %s", tester.workspace); !strings.Contains(output, want) { + t.Fatalf("console output missing coinbase: have\n%s\nwant also %s", output, want) + } +} + +// Tests that JavaScript statement evaluation works as intended. +func TestEvaluate(t *testing.T) { + tester := newTester(t, nil) + defer tester.Close(t) + + tester.console.Evaluate("2 + 2") + if output := string(tester.output.Bytes()); !strings.Contains(output, "4") { + t.Fatalf("statement evaluation failed: have %s, want %s", output, "4") + } +} + +// Tests that the console can be used in interactive mode. +func TestInteractive(t *testing.T) { + // Create a tester and run an interactive console in the background + tester := newTester(t, nil) + defer tester.Close(t) + + go tester.console.Interactive() + + // Wait for a promt and send a statement back + select { + case <-tester.input.scheduler: + case <-time.After(time.Second): + t.Fatalf("initial prompt timeout") + } + select { + case tester.input.scheduler <- "2+2": + case <-time.After(time.Second): + t.Fatalf("input feedback timeout") + } + // Wait for the second promt and ensure first statement was evaluated + select { + case <-tester.input.scheduler: + case <-time.After(time.Second): + t.Fatalf("secondary prompt timeout") + } + if output := string(tester.output.Bytes()); !strings.Contains(output, "4") { + t.Fatalf("statement evaluation failed: have %s, want %s", output, "4") + } +} + +// Tests that preloaded JavaScript files have been executed before user is given +// input. +func TestPreload(t *testing.T) { + tester := newTester(t, nil) + defer tester.Close(t) + + tester.console.Evaluate("preloaded") + if output := string(tester.output.Bytes()); !strings.Contains(output, "some-preloaded-string") { + t.Fatalf("preloaded variable missing: have %s, want %s", output, "some-preloaded-string") + } +} + +// Tests that JavaScript scripts can be executes from the configured asset path. +func TestExecute(t *testing.T) { + tester := newTester(t, nil) + defer tester.Close(t) + + tester.console.Execute("exec.js") + + tester.console.Evaluate("execed") + if output := string(tester.output.Bytes()); !strings.Contains(output, "some-executed-string") { + t.Fatalf("execed variable missing: have %s, want %s", output, "some-executed-string") + } +} + +// Tests that the JavaScript objects returned by statement executions are properly +// pretty printed instead of just displaing "[object]". +func TestPrettyPrint(t *testing.T) { + tester := newTester(t, nil) + defer tester.Close(t) + + tester.console.Evaluate("obj = {int: 1, string: 'two', list: [3, 3, 3], obj: {null: null, func: function(){}}}") + + // Define some specially formatted fields + var ( + one = jsre.NumberColor("1") + two = jsre.StringColor("\"two\"") + three = jsre.NumberColor("3") + null = jsre.SpecialColor("null") + fun = jsre.FunctionColor("function()") + ) + // Assemble the actual output we're after and verify + want := `{ + int: ` + one + `, + list: [` + three + `, ` + three + `, ` + three + `], + obj: { + null: ` + null + `, + func: ` + fun + ` + }, + string: ` + two + ` +} +` + if output := string(tester.output.Bytes()); output != want { + t.Fatalf("pretty print mismatch: have %s, want %s", output, want) + } +} diff --git a/console/prompter.go b/console/prompter.go new file mode 100644 index 000000000..5039e8b1c --- /dev/null +++ b/console/prompter.go @@ -0,0 +1,156 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package console + +import ( + "fmt" + "strings" + + "github.com/peterh/liner" +) + +// TerminalPrompter holds the stdin line reader (also using stdout for printing +// prompts). Only this reader may be used for input because it keeps an internal +// buffer. +var TerminalPrompter = newTerminalPrompter() + +// UserPrompter defines the methods needed by the console to promt the user for +// various types of inputs. +type UserPrompter interface { + // PromptInput displays the given prompt to the user and requests some textual + // data to be entered, returning the input of the user. + PromptInput(prompt string) (string, error) + + // PromptPassword displays the given prompt to the user and requests some textual + // data to be entered, but one which must not be echoed out into the terminal. + // The method returns the input provided by the user. + PromptPassword(prompt string) (string, error) + + // PromptConfirm displays the given prompt to the user and requests a boolean + // choice to be made, returning that choice. + PromptConfirm(prompt string) (bool, error) + + // SetScrollHistory sets the the input scrollback history that the prompter will + // allow the user to scoll back to. + SetScrollHistory(history []string) + + // SetWordCompleter sets the completion function that the prompter will call to + // fetch completion candidates when the user presses tab. + SetWordCompleter(completer WordCompleter) +} + +// WordCompleter takes the currently edited line with the cursor position and +// returns the completion candidates for the partial word to be completed. If +// the line is "Hello, wo!!!" and the cursor is before the first '!', ("Hello, +// wo!!!", 9) is passed to the completer which may returns ("Hello, ", {"world", +// "Word"}, "!!!") to have "Hello, world!!!". +type WordCompleter func(line string, pos int) (string, []string, string) + +// terminalPrompter is a UserPrompter backed by the liner package. It supports +// prompting the user for various input, among others for non-echoing password +// input. +type terminalPrompter struct { + *liner.State + warned bool + supported bool + normalMode liner.ModeApplier + rawMode liner.ModeApplier +} + +// newTerminalPrompter creates a liner based user input prompter working off the +// standard input and output streams. +func newTerminalPrompter() *terminalPrompter { + r := new(terminalPrompter) + // Get the original mode before calling NewLiner. + // This is usually regular "cooked" mode where characters echo. + normalMode, _ := liner.TerminalMode() + // Turn on liner. It switches to raw mode. + r.State = liner.NewLiner() + rawMode, err := liner.TerminalMode() + if err != nil || !liner.TerminalSupported() { + r.supported = false + } else { + r.supported = true + r.normalMode = normalMode + r.rawMode = rawMode + // Switch back to normal mode while we're not prompting. + normalMode.ApplyMode() + } + r.SetCtrlCAborts(true) + r.SetTabCompletionStyle(liner.TabPrints) + + return r +} + +// PromptInput displays the given prompt to the user and requests some textual +// data to be entered, returning the input of the user. +func (r *terminalPrompter) PromptInput(prompt string) (string, error) { + if r.supported { + r.rawMode.ApplyMode() + defer r.normalMode.ApplyMode() + } else { + // liner tries to be smart about printing the prompt + // and doesn't print anything if input is redirected. + // Un-smart it by printing the prompt always. + fmt.Print(prompt) + prompt = "" + defer fmt.Println() + } + return r.State.Prompt(prompt) +} + +// PromptPassword displays the given prompt to the user and requests some textual +// data to be entered, but one which must not be echoed out into the terminal. +// The method returns the input provided by the user. +func (r *terminalPrompter) PromptPassword(prompt string) (passwd string, err error) { + if r.supported { + r.rawMode.ApplyMode() + defer r.normalMode.ApplyMode() + return r.State.PasswordPrompt(prompt) + } + if !r.warned { + fmt.Println("!! Unsupported terminal, password will be echoed.") + r.warned = true + } + // Just as in Prompt, handle printing the prompt here instead of relying on liner. + fmt.Print(prompt) + passwd, err = r.State.Prompt("") + fmt.Println() + return passwd, err +} + +// PromptConfirm displays the given prompt to the user and requests a boolean +// choice to be made, returning that choice. +func (r *terminalPrompter) PromptConfirm(prompt string) (bool, error) { + input, err := r.Prompt(prompt + " [y/N] ") + if len(input) > 0 && strings.ToUpper(input[:1]) == "Y" { + return true, nil + } + return false, err +} + +// SetScrollHistory sets the the input scrollback history that the prompter will +// allow the user to scoll back to. +func (r *terminalPrompter) SetScrollHistory(history []string) { + r.State.ReadHistory(strings.NewReader(strings.Join(history, "\n"))) +} + +// SetWordCompleter sets the completion function that the prompter will call to +// fetch completion candidates when the user presses tab. +func (r *terminalPrompter) SetWordCompleter(completer WordCompleter) { + r.State.SetWordCompleter(liner.WordCompleter(completer)) +} diff --git a/console/testdata/exec.js b/console/testdata/exec.js new file mode 100644 index 000000000..59e34d7c4 --- /dev/null +++ b/console/testdata/exec.js @@ -0,0 +1 @@ +var execed = "some-executed-string"; diff --git a/console/testdata/preload.js b/console/testdata/preload.js new file mode 100644 index 000000000..556793970 --- /dev/null +++ b/console/testdata/preload.js @@ -0,0 +1 @@ +var preloaded = "some-preloaded-string"; |