Brothers Link Block Chain Course Etaifang Source Code Analysis CMD in-depth analysis.
cmd packet analysis
There are 13 subpackages under cmd. In addition to util packages, each subpackage has a main function. The init method of each main function defines the commands supported by the main function, such as
Under the get package:
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)) }
Then initCommand is analyzed separately:
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 is the command corresponding to the command, action is the action that calls the command to complete, usage represents the purpose, arguUsage shows the number of parameters followed by the command and the meaning of each parameter.
The init method is actually to initialize the creation block. flags represent the additional commands that can be executed by this subcommand. If you change the init command, you can take two parameters and click in utils.DataDirFlag to see that:
// General settings DataDirFlag = DirectoryFlag{ Name: "datadir", Usage: "Data directory for the databases and keystore", Value: DirectoryString{node.DefaultDataDir()}, }
- You can use -- datadir [dir] to specify the path to the database, and if you don't specify the default path because the parameter has value, it's also. ethereum under the home directory.
- /cmd/wnode/main.go starts by connecting to other nodes
- / cmd/geth /cmd/swarm defines many commands
The rlpdump subpackage of cmd under eth, whose main function is to dump RLP data from a given file in readable form. If the file name is omitted, the data will be read from stdin
/rlpdump
Decoding rlp data
help of command of rlpdump
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, ]
Analysis of p2psim subpackage under cmd package, p2psim is a command line client for the HTTP API
First, we start the corresponding main function, and the corresponding startup parameter is -- help, to see the use of all commands under the package, the results are as follows:
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
This subpackage provides commands on how to issue:
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]
To use the commands under this subpackage properly, we need to run the main function of / p2p/simulations/examples/ping-pong.go to start a simulation network with simple running nodes.
After normal start-up, you will see:
INFO [01-23|11:17:10] using sim adapter INFO [01-23|11:17:10] starting simulation server on 0.0.0.0:8888...
After the service starts, it provides the following API, which is equivalent to the above command. The implementation of command invocation is actually to invoke API, and the path prefix of access is 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
Without going into API, the service of emulation network is up. Now we begin to use the command under p2psim package:
/p2psim
- show
function: Displays the status of the current simulation network args:"" demo: show notice: success_result_demo: NODES 0 CONNS 0
- snapshot
function:Export node information of current simulation network 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:Create a node args:[--name=NAME] [--services=SERVICES] [--key=KEY] demo: node create --name node1 notice: success_result_demo: Created node1
- list
function:List the node information of the current simulation network args:"" demo: node list notice: success_result_demo: NAME PROTOCOLS ID node1 085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d node2 cedbaecccfe42d04b742d1be6e924e0654a7eb1aa584d497f98d24951b156ada84bcfc6455ff37ba1fc81179d0a7c3da1ba34945be19d1fe5cd4c8a32a659a7b
- show
function:View the specific information of a node in the simulation network 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:Start a node args:<node> demo: node start node1 notice: success_result_demo: Started node1
- connect
function: Connect one node to another args:<node> <peer> demo: node connect node2 node1 notice: success_result_demo: Connected node2 to node1
- disconnect
function:Node disconnection args:<node> <peer> demo: node disconnect node2 node1 notice: success_result_demo: Disconnected node2 from node1
- stop
function:Stop a node args:<node> demo: node stop node2 notice: success_result_demo: Stopped node2
- rpc
function:call rpc Interface args:<node> <method> [<args>] [--subscribe] demo: node rpc node1 admin notice: success_result_demo: // TODO