github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/geth/consolecmd.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package main
    26  
    27  import (
    28  	"fmt"
    29  	"os"
    30  	"os/signal"
    31  	"path/filepath"
    32  	"strings"
    33  	"syscall"
    34  
    35  	"github.com/ethereum/go-ethereum/cmd/utils"
    36  	"github.com/ethereum/go-ethereum/console"
    37  	"github.com/ethereum/go-ethereum/node"
    38  	"github.com/ethereum/go-ethereum/rpc"
    39  	"gopkg.in/urfave/cli.v1"
    40  )
    41  
    42  var (
    43  	consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
    44  
    45  	consoleCommand = cli.Command{
    46  		Action:   utils.MigrateFlags(localConsole),
    47  		Name:     "console",
    48  		Usage:    "Start an interactive JavaScript environment",
    49  		Flags:    append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...),
    50  		Category: "CONSOLE COMMANDS",
    51  		Description: `
    52  The Geth console is an interactive shell for the JavaScript runtime environment
    53  which exposes a node admin interface as well as the Ðapp JavaScript API.
    54  See https://github.com/ethereum/go-ethereum/wiki/javascript控制台。
    55  	}
    56  
    57  	attachCommand = cli.Command{
    58  		Action:    utils.MigrateFlags(remoteConsole),
    59  		Name:      "attach",
    60  		Usage:     "Start an interactive JavaScript environment (connect to node)",
    61  		ArgsUsage: "[endpoint]",
    62  		Flags:     append(consoleFlags, utils.DataDirFlag),
    63  		Category:  "CONSOLE COMMANDS",
    64  		Description: `
    65  The Geth console is an interactive shell for the JavaScript runtime environment
    66  which exposes a node admin interface as well as the Ðapp JavaScript API.
    67  See https://github.com/ethereum/go-ethereum/wiki/javascript-console。
    68  This command allows to open a console on a running geth node.`,
    69  	}
    70  
    71  	javascriptCommand = cli.Command{
    72  		Action:    utils.MigrateFlags(ephemeralConsole),
    73  		Name:      "js",
    74  		Usage:     "Execute the specified JavaScript files",
    75  		ArgsUsage: "<jsfile> [jsfile...]",
    76  		Flags:     append(nodeFlags, consoleFlags...),
    77  		Category:  "CONSOLE COMMANDS",
    78  		Description: `
    79  The JavaScript VM exposes a node admin interface as well as the Ðapp
    80  JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/javascript控制台`,
    81  	}
    82  )
    83  
    84  //localconsole启动一个新的geth节点,在
    85  //同时。
    86  func localConsole(ctx *cli.Context) error {
    87  //根据CLI标志创建和启动节点
    88  	node := makeFullNode(ctx)
    89  	startNode(ctx, node)
    90  	defer node.Stop()
    91  
    92  //附加到新启动的节点并启动javascript控制台
    93  	client, err := node.Attach()
    94  	if err != nil {
    95  		utils.Fatalf("Failed to attach to the inproc geth: %v", err)
    96  	}
    97  	config := console.Config{
    98  		DataDir: utils.MakeDataDir(ctx),
    99  		DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
   100  		Client:  client,
   101  		Preload: utils.MakeConsolePreloads(ctx),
   102  	}
   103  
   104  	console, err := console.New(config)
   105  	if err != nil {
   106  		utils.Fatalf("Failed to start the JavaScript console: %v", err)
   107  	}
   108  	defer console.Stop(false)
   109  
   110  //如果只请求短时间执行,则计算并返回
   111  	if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
   112  		console.Evaluate(script)
   113  		return nil
   114  	}
   115  //否则,打印欢迎屏幕并进入交互模式
   116  	console.Welcome()
   117  	console.Interactive()
   118  
   119  	return nil
   120  }
   121  
   122  //远程控制台将连接到远程geth实例,附加一个javascript
   123  //控制台。
   124  func remoteConsole(ctx *cli.Context) error {
   125  //连接到远程运行的geth实例并启动javascript控制台
   126  	endpoint := ctx.Args().First()
   127  	if endpoint == "" {
   128  		path := node.DefaultDataDir()
   129  		if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
   130  			path = ctx.GlobalString(utils.DataDirFlag.Name)
   131  		}
   132  		if path != "" {
   133  			if ctx.GlobalBool(utils.TestnetFlag.Name) {
   134  				path = filepath.Join(path, "testnet")
   135  			} else if ctx.GlobalBool(utils.RinkebyFlag.Name) {
   136  				path = filepath.Join(path, "rinkeby")
   137  			}
   138  		}
   139  		endpoint = fmt.Sprintf("%s/geth.ipc", path)
   140  	}
   141  	client, err := dialRPC(endpoint)
   142  	if err != nil {
   143  		utils.Fatalf("Unable to attach to remote geth: %v", err)
   144  	}
   145  	config := console.Config{
   146  		DataDir: utils.MakeDataDir(ctx),
   147  		DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
   148  		Client:  client,
   149  		Preload: utils.MakeConsolePreloads(ctx),
   150  	}
   151  
   152  	console, err := console.New(config)
   153  	if err != nil {
   154  		utils.Fatalf("Failed to start the JavaScript console: %v", err)
   155  	}
   156  	defer console.Stop(false)
   157  
   158  	if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
   159  		console.Evaluate(script)
   160  		return nil
   161  	}
   162  
   163  //否则,打印欢迎屏幕并进入交互模式
   164  	console.Welcome()
   165  	console.Interactive()
   166  
   167  	return nil
   168  }
   169  
   170  //dialrpc返回连接到给定端点的RPC客户端。
   171  //空端点检查实现了默认逻辑
   172  //对于没有参数的“geth attach”和“geth monitor”。
   173  func dialRPC(endpoint string) (*rpc.Client, error) {
   174  	if endpoint == "" {
   175  		endpoint = node.DefaultIPCEndpoint(clientIdentifier)
   176  	} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
   177  //与geth的向后兼容性<1.5,这需要
   178  //这些前缀。
   179  		endpoint = endpoint[4:]
   180  	}
   181  	return rpc.Dial(endpoint)
   182  }
   183  
   184  //ephemeralconsole启动一个新的geth节点,附加一个ephemeral javascript
   185  //控制台,执行指定为参数和眼泪的每个文件
   186  //一切都结束了。
   187  func ephemeralConsole(ctx *cli.Context) error {
   188  //根据CLI标志创建和启动节点
   189  	node := makeFullNode(ctx)
   190  	startNode(ctx, node)
   191  	defer node.Stop()
   192  
   193  //附加到新启动的节点并启动javascript控制台
   194  	client, err := node.Attach()
   195  	if err != nil {
   196  		utils.Fatalf("Failed to attach to the inproc geth: %v", err)
   197  	}
   198  	config := console.Config{
   199  		DataDir: utils.MakeDataDir(ctx),
   200  		DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
   201  		Client:  client,
   202  		Preload: utils.MakeConsolePreloads(ctx),
   203  	}
   204  
   205  	console, err := console.New(config)
   206  	if err != nil {
   207  		utils.Fatalf("Failed to start the JavaScript console: %v", err)
   208  	}
   209  	defer console.Stop(false)
   210  
   211  //评估每个指定的javascript文件
   212  	for _, file := range ctx.Args() {
   213  		if err = console.Execute(file); err != nil {
   214  			utils.Fatalf("Failed to execute %s: %v", file, err)
   215  		}
   216  	}
   217  //等待挂起的回调,但停止使用ctrl-c。
   218  	abort := make(chan os.Signal, 1)
   219  	signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
   220  
   221  	go func() {
   222  		<-abort
   223  		os.Exit(0)
   224  	}()
   225  	console.Stop(true)
   226  
   227  	return nil
   228  }