github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/console/console.go (about)

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