您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 投融资/租赁 > 兄弟连区Go语言+区块链技术培训以太坊源码分析(10)CMD深入分析
1/40兄弟连区块链技术培训以太坊源码分析(10)CMD深入分析###cmd包分析####cmd下面总共有13个子包,除了util包之外,每个子包都有一个主函数,每个主函数的init方法中都定义了该主函数支持的命令,如#####geth包下面的:```funcinit(){//InitializetheCLIappandstartGethapp.Action=gethapp.HideVersion=true//wehaveacommandtoprinttheversionapp.Copyright=Copyright2013-2017Thego-ethereumAuthorsapp.Commands=[]cli.Command{//Seechaincmd.go:initCommand,importCommand,exportCommand,2/40copydbCommand,removedbCommand,dumpCommand,//Seemonitorcmd.go:monitorCommand,//Seeaccountcmd.go:accountCommand,walletCommand,//Seeconsolecmd.go:consoleCommand,attachCommand,javascriptCommand,//Seemisccmd.go:makecacheCommand,makedagCommand,versionCommand,bugCommand,licenseCommand,//Seeconfig.go3/40dumpConfigCommand,}sort.Sort(cli.CommandsByName(app.Commands))}```######再单独分析initCommand:```initCommand=cli.Command{Action:utils.MigrateFlags(initGenesis),Name:init,Usage:Bootstrapandinitializeanewgenesisblock,ArgsUsage:genesisPath,Flags:[]cli.Flag{utils.DataDirFlag,utils.LightModeFlag,},Category:BLOCKCHAINCOMMANDS,Description:`4/40Theinitcommandinitializesanewgenesisblockanddefinitionforthenetwork.Thisisadestructiveactionandchangesthenetworkinwhichyouwillbeparticipating.```######其中Name是对应命令的指令,action是调用该指令去完成的动作,usage表示用途,arguUsage显示该命令后面跟的参数个数以及每个参数的意义,######该init方法其实就是去初始化创世块,flags代表的是这个子命令额外可以执行的命令,如改init命令可以携带两个参数,点进去utils.DataDirFlag可以看到:```//GeneralsettingsDataDirFlag=DirectoryFlag{Name:datadir,Usage:Datadirectoryforthedatabasesandkeystore,Value:DirectoryString{node.DefaultDataDir()},}```*__可以用--datadir[dir]来指定数据库的路径,如果没有指定由于该参数有value所以会启用默认的路径,也是home目录下面的.ethereum.__5/40*__/cmd/wnode/main.go通过连接其他节点启动__*__/cmd/geth/cmd/swarm都是定义了很多命令__###eth下cmd的rlpdump子包,该包的主要作用从给定文件中转储RLP数据以可读的形式.如果文件名被省略,数据将从stdin中读取/rlpdump#####解码rlp的数据######rlpdump的command的help```Usage:/tmp/___cmd_rlpdump_test[-noascii][-hexdata][filename]-hexstringdumpgivenhexdata-noasciidon'tprintASCIIstringsreadably-singleprintonlythefirstelement,discardtherestDumpsRLPdatafromthegivenfileinreadableform.Ifthefilenameisomitted,dataisreadfromstdin.```######example1:6/40```democommand:--hexf872f870845609a1ba64c0b8660480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee8580success_result_demo:[[5609a1ba,d,[],0480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee85,,],]```######example2:```7/40democommand:--noascii--hexCE0183FFFFFFC4C304050583616263success_result_demo:[01,ffffff,[[04,05,05,],],616263,]```###cmd包下的p2psim子包的分析,p2psimisacommandlineclientfortheHTTPAPI#####首先我们启动对应的main函数,对应的启动参数是`--help`,来查看该包下所有命令的使用,结果如下:8/40```NAME:___go_build_main_go__1_-devp2psimulationcommand-lineclientUSAGE:___go_build_main_go__1_[globaloptions]command[commandoptions][arguments...]VERSION:0.0.0COMMANDS:showshownetworkinformationeventsstreamnetworkeventssnapshotcreateanetworksnapshottostdoutloadloadanetworksnapshotfromstdinnodemanagesimulationnodeshelp,hShowsalistofcommandsorhelpforonecommandGLOBALOPTIONS:--apivaluesimulationAPIURL(default:)[$P2PSIM_API_URL]--help,-hshowhelp9/40--version,-vprinttheversion```#####该子包提供如何下的命令:```p2psimshowp2psimevents[--current][--filter=FILTER]p2psimsnapshotp2psimloadp2psimnodecreate[--name=NAME][--services=SERVICES][--key=KEY]p2psimnodelistp2psimnodeshownodep2psimnodestartnodep2psimnodestopnodep2psimnodeconnectnodepeerp2psimnodedisconnectnodepeerp2psimnoderpcnodemethod[args][--subscribe]```#####要正常使用该子包下的命令,我们需要运行`/p2p/simulations/examples/ping-pong.go`的主函数来启动一个包含运行简单的节点的仿真网络.10/40正常启动后,你将看到:```INFO[01-23|11:17:10]usingsimadapterINFO[01-23|11:17:10]startingsimulationserveron0.0.0.0:8888...```#####该服务启动后,提供如下的API,其作用等同于上面的命令,命令调用的实现其实就是调用API,访问的路径前缀就是`0.0.0.0:8888`:```GET/GetnetworkinformationPOST/startStartallnodesinthenetworkPOST/stopStopallnodesinthenetworkGET/eventsStreamnetworkeventsGET/snapshotTakeanetworksnapshotPOST/snapshotLoadanetworksnapshotPOST/nodesCreateanodeGET/nodesGetallnodesinthenetworkGET/nodes/:nodeidGetnodeinformationPOST/nodes/:nodeid/startStartanodePOST/nodes/:nodeid/stopStopanode11/40POST/nodes/:nodeid/conn/:peeridConnecttwonodesDELETE/nodes/:nodeid/conn/:peeridDisconnecttwonodesGET/nodes/:nodeid/rpcMakeRPCrequeststoanodeviaWebSocket```#####此处不深究API,仿真网络的服务已经起来了,下面开始p2psim包下命令的使用:__/p2psim__*show```function:显示当前仿真网络的状态args:demo:shownotice:success_result_demo:NODES0CONNS0```*snapshot```12/40function:导出当前仿真网络的节点信息args:demo:shapshotnotice:success_result_demo:{nodes:[{node:{config:{id:085416957c3a0afef6aabe6c0d6b27b7cf8a61f28a3a5439010fcc9e49945a1818ea38946dda8c82004b231ab771450ee0d87886163b65eaa48ecfbcb85e871d,private_key:3480d230f453e7c207bbd3b770bf774dc8a17e599394f9283147a35c3ead561c,name:node1,services:[ping-pong]},up:true}},{node:{config:{id:cedbaecccfe42d04b742d1be6e924e0654a7eb1aa584d497f98d24951b156ada84b
本文标题:兄弟连区Go语言+区块链技术培训以太坊源码分析(10)CMD深入分析
链接地址:https://www.777doc.com/doc-5447084 .html