区块链教程

兄弟连区块链入门教程以太坊源码分析CMD深入分析(一)

2018-10-18  本文已影响9人  ab6973df9221

  兄弟连区块链教程之以太坊源码分析CMD深入分析,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。

### 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 URL (default: "http://localhost:8888") [$P2PSIM_API_URL]

--help, -h show help

--version, -v print the version

```

#####该子包提供如何下的命令:

```

p2psim show

p2psim events [--current] [--filter=FILTER]

p2psim snapshot

p2psim load

p2psim node create [--name=NAME] [--services=SERVICES] [--key=KEY]

p2psim node list

p2psim node show <node>

p2psim node start <node>

p2psim node stop <node>

p2psim node connect <node> <peer>

p2psim node disconnect <node> <peer>

p2psim node rpc <node> <method> [<args>] [--subscribe]

```

#####要正常使用该子包下的命令,我们需要运行`/p2p/simulations/examples/ping-pong.go`的主函数来启动一个包含运行简单的节点的仿真网络.

正常启动后,你将看到:

```

INFO [01-23|11:17:10] using sim adapter

INFO [01-23|11:17:10] starting simulation server on 0.0.0.0:8888...

```

#####该服务启动后,提供如下的API,其作用等同于上面的命令,命令调用的实现其实就是调用API,访问的路径前缀就是`0.0.0.0:8888`:

```

GET / Get network information

POST /start Start all nodes in the network

POST /stop Stop all nodes in the network

GET /events Stream network events

GET /snapshot Take a network snapshot

POST /snapshot Load a network snapshot

POST /nodes Create a node

GET /nodes Get all nodes in the network

GET /nodes/:nodeid Get node information

POST /nodes/:nodeid/start Start a node

POST /nodes/:nodeid/stop Stop a node

POST /nodes/:nodeid/conn/:peerid Connect two nodes

DELETE /nodes/:nodeid/conn/:peerid Disconnect two nodes

GET /nodes/:nodeid/rpc Make RPC requests to a node via WebSocket

```

#####此处不深究API,仿真网络的服务已经起来了,下面开始p2psim包下命令的使用:

__/p2psim__

* show

```

function:显示当前仿真网络的状态

args:""

demo: show

notice:

success_result_demo:

NODES 0

CONNS 0

```

* snapshot

```

function:导出当前仿真网络的节点信息

args:""

demo: shapshot

notice:

success_result_demo:

{"nodes":[{"node":{"config":{"id":"085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d","private_key":"3480d230f453e7c207bbd3b770bf774dc8a17e599394f9283147a35c3ead561c","name":"node1","services":["ping-pong"]},"up":true}},{"node":{"config":{"id":"cedbaecccfe42d04b742d1be6e924e0654a7eb1aa584d497f98d24951b156ada84bcfc6455ff37ba1fc81179d0a7c3da1ba34945be19d1fe5cd4c8a32a659a7b","private_key":"b7592cdeee6195c4486fcdd8007e1aedfd3a49e6c9f53e0845bf977d4ad043cc","name":"node2","services":["ping-pong"]},"up":false}}],"conns":[{"one":"cedbaecccfe42d04b742d1be6e924e0654a7eb1aa584d497f98d24951b156ada84bcfc6455ff37ba1fc81179d0a7c3da1ba34945be19d1fe5cd4c8a32a659a7b","other":"085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d","up":false}]}

```

* node

> * create

```

function:创建一个节点

args:[--name=NAME] [--services=SERVICES] [--key=KEY]

demo: node create --name node1

notice:

success_result_demo:

Created node1

```

> * list

```

function:列出当前仿真网络的节点信息

args:""

demo: node list

notice:

success_result_demo:

NAME PROTOCOLS ID

node1 085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d

node2 cedbaecccfe42d04b742d1be6e924e0654a7eb1aa584d497f98d24951b156ada84bcfc6455ff37ba1fc81179d0a7c3da1ba34945be19d1fe5cd4c8a32a659a7b

```

> * show

```

function:查看仿真网络中某个节点的具体信息

args:<node>

demo: node show node1

notice:

success_result_demo:

NAME node1

PROTOCOLS

ID 085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d

ENODE enode://085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d@127.0.0.1:30303

```

> * start

```

function:启动一个节点

args:<node>

demo: node start node1

notice:

success_result_demo:

Started node1

```

> * connect

```

function:将一个节点连接到另外一个节点

args:<node> <peer>

demo: node connect node2 node1

notice:

success_result_demo:

Connected node2 to node1

```

> * disconnect

```

function:节点断开连接

args:<node> <peer>

demo: node disconnect node2 node1

notice:

success_result_demo:

Disconnected node2 from node1

```

> * stop

```

function:停止一个节点

args:<node>

demo: node stop node2

notice:

success_result_demo:

Stopped node2

```

> * rpc

```

function:调用rpc接口

args:<node> <method> [<args>] [--subscribe]

demo: node rpc node1 admin

notice:

success_result_demo:

 // TODO

上一篇下一篇

猜你喜欢

热点阅读