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