以太坊源码分析(9)CMD实际操作分析

本文详细分析了以太坊的CMD实际操作,包括相关技术要点和实践过程。通过阅读,读者可以深入了解以太坊的底层运作,并获取造币技术和虚拟机资料。
摘要由CSDN通过智能技术生成
### cmd包分析
#### cmd下面总共有13个子包,除了util包之外,每个子包都有一个主函数,每个主函数的init方法中都定义了该主函数支持的命令,如

##### geth包下面的:

```
func init() {
    // Initialize the CLI app and start Geth
    app.Action = geth
    app.HideVersion = true // we have a command to print the version
    app.Copyright = "Copyright 2013-2017 The go-ethereum Authors"
    app.Commands = []cli.Command{
        // See chaincmd.go:
        initCommand,
        importCommand,
        exportCommand,
        copydbCommand,
        removedbCommand,
        dumpCommand,
        // See monitorcmd.go:
        monitorCommand,
        // See accountcmd.go:
        accountCommand,
        walletCommand,
        // See consolecmd.go:
        consoleCommand,
        attachCommand,
        javascriptCommand,
        // See misccmd.go:
        makecacheCommand,
        makedagCommand,
        versionCommand,
        bugCommand,
        licenseCommand,
        // See config.go
        dumpConfigCommand,
    }
    sort.Sort(cli.CommandsByName(app.Commands))
}
```


###### 再单独分析initCommand:

```
initCommand = cli.Command{
Action: utils.MigrateFlags(initGenesis),
Name: "init",
Usage: "Bootstrap and initialize a new genesis block",
ArgsUsage: "<genesisPath>",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.LightModeFlag,
},
Category: "BLOCKCHAIN COMMANDS",
Description: `
The init command initializes a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
participating.

```

###### 其中Name是对应命令的指令,action是调用该指令去完成的动作,usage表示用途,arguUsage显示该命令后面跟的参数个数以及每个参数的意义,
###### 该init方法其实就是去初始化创世块,flags代表的是这个子命令额外可以执行的命令,如改init命令可以携带两个参数,点进去utils.DataDirFlag可以看到:

```
// General settings
DataDirFlag = DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the databases and keystore",
Value: DirectoryString{node.DefaultDataDir()},
}
```


* __可以用 --datadir [dir]来指定数据库的路径,如果没有指定由于该参数有value所以会启用默认的路径,也是home目录下面的.ethereum.__

* __/cmd/wnode/main.go 通过连接其他节点启动__


* __/cmd/geth /cmd/swarm 都是定义了很多命令__





### eth下cmd的rlpdump子包,该包的主要作用从给定文件中转储RLP数据以可读的形式.如果文件名被省略,数据将从stdin中读取

  /rlpdump

##### 解码rlp的数据


###### rlpdump的command的help

```
Usage: /tmp/___cmd_rlpdump_test [-noascii] [-hex <data>] [filename]
-hex string
  dump given hex data
-noascii
  don't print ASCII strings readably
-single
  print only the first element, discard the rest

Dumps RLP data from the given file in readable form.
If the filename is omitted, data is read from stdin.

```


###### example1:
```
demo command: --hex f872f870845609a1ba64c0b8660480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee8580
success_result_demo:
[
[
5609a1ba,
"d",
[],
0480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee85,
"",
],
]
```

###### example2:

```
demo command: --noascii --hex CE0183FFFFFFC4C304050583616263
success_result_demo:
[
01,
ffffff,
[
[
04,
05,
05,
],
],
616263,
]



```
### cmd包下的p2psim子包的分析, p2psim is a command line client for the HTTP API

##### 首先我们启动对应的main函数,对应的启动参数是 `--help` ,来查看该包下所有命令的使用,结果如下:

```
NAME:
___go_build_main_go__1_ - devp2p simulation command-line client

USAGE:
___go_build_main_go__1_ [global options] command [command options] [arguments...]

VERSION:
0.0.0

COMMANDS:
show show network information
events stream network events
snapshot create a network snapshot to stdout
load load a network snapshot from stdin
node manage simulation nodes
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--api value simulation API U
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值