4.1 命令行接口和选项

优质
小牛编辑
124浏览
2023-12-01

命令行选项

geth [global options] command [command options] [arguments...]

VERSION:
   1.0.0

COMMANDS:
   recover      通过设置新的数量或者哈希块来恢复受损的数据库,见help recover
   blocktest    加载一个块测试文件
   import       导入一个blockchain文件
   export       导出blockchain至文件
   upgradedb    升级链中块的数据库
   removedb     移除blockchain和状态数据库
   dump         从存储中转存特定的块
   monitor      Geth Monitor: 节点指标监控和可视化
   makedag      生成ethash dag(测试用)
   version      打印ethereum 版本值
   wallet       ethereum 预售钱包
   account      管理账户
   console      Geth Console: 交互式JavaScript环境
   attach       Geth Console: 交互式JavaScript环境 (连接节点)
   js           在Geth JavaScript VM中执行给定的JavaScript文件
   help         命令列表或者一个命令的帮助项

GLOBAL OPTIONS:
   --identity                                                           自定义节点名称
   --unlock                                                             解锁给定账户直到程序退出(需要密码),‘--unlock n’解锁第n个账户,或者创建
   --password                                                           使用选项和子命令所需要的密码的密码文件路径
   --genesis                                                            插入或者重写创世块 (json 格式)
   --bootnodes                                                          空格分隔的enode url,用于p2p发现引导
   --datadir "/Users/tron/Library/Ethereum"                             使用的数据文件夹
   --blockchainversion "3"                                              Blockchain 版本(integer)
   --jspath "."                                                          控制台和js子命令使用的JS库路径
   --port "30303"                                                       网络监听端口
   --maxpeers "25"                                                      网络peer的最大数量 (设置为0,网络不可用)
   --maxpendpeers "0"                                                   等待连接尝试的最大数量 (设置为0,默认使用)
   --etherbase "0"                                                      挖矿回报的公共地址. 默认使用第一个被创建的地址
   --gasprice "1000000000000"                                           设置采矿交易的最小的gasprice
   --minerthreads "8"                                                   挖矿线程数
   --mine                                                               开启挖矿
   --autodag                                                            开启自动DAG预生成
   --nat "any"                                                          NAT端口映射机制 (any|none|upnp|pmp|extip:<IP>)
   --natspec                                                            开启NatSpec确认通知
   --nodiscover                                                         关闭peer发现机制 (手动添加除外)
   --nodekey                                                            P2P节点密钥文件
   --nodekeyhex                                                         十六进制的P2P节点秘钥(测试)
   --rpc                                                                开启JSON-RPC服务器
   --rpcaddr "127.0.0.1"                                                监听json-rpc服务器的地址
   --rpcport "8545"                                                     JSON-RPC服务器监听的地址
   --rpcapi "db,eth,net,web3"                                           指定HTTP RPC接口提供的API
   --ipcdisable                                                         关闭IPC-RPC服务器
   --ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3"     指定RPC接口提供的API
   --ipcpath "/Users/tron/Library/Ethereum/geth.ipc"                    IPC socket/pipe的文件名
   --exec                                                               执行javascript命令(只有结合 console/attach)
   --shh                                                                开启 whisper
   --vmdebug                                                            Virtual Machine debug 输出
   --networkid "1"                                                      Network Id (integer)
   --rpccorsdomain                                                      发送Access-Control-Allow-Origin header上的Domain
   --verbosity "3"                                                      日志类型: 0-6 (0=silent, 1=error, 2=warn, 3=info, 4=core, 5=debug, 6=debug detail)
   --backtrace_at ":0"                                                  如果设置为持有一个日志语句的文件和行号(如。“block.go:271”),将记录一个堆栈跟踪
   --logtostderr                                                        日志写入标准错误而不是文件。
   --vmodule ""                                                         The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the ".go" suffix) or "glob" pattern and N is a log verbosity level.
   --logfile                                                            输出log至文件
   --logjson                                                            发送json结构日志输出到一个文件或为标准输出“-” (默认无json输出)
   --pprof                                                              启用本地主机上配置服务器
   --pprofport "6060"                                                   profiler应该监听的端口
   --metrics                                                            开启指标收集和汇报
   --solc "solc"                                                        使用的solidity编辑器
   --gpomin "1000000000000"                                             建议的最小gas price
   --gpomax "100000000000000"                                           建议的最大gas price
   --gpofull "80"                                                       gas price计算的完整块阈值(%)
   --gpobasedown "10"                                                   建议的gas price基础下调比率(1/1000)
   --gpobaseup "100"                                                    建议的gas price基础上调比率(1/1000)
   --gpobasecf "110"                                                    建议的gas price基础校正因子(%)
   --help, -h                                                           显示帮助

请注意,datadir的默认值是特定于平台的。有关详细信息,请参阅备份和还原

例子

账户

参见账户管理

把ether的预售钱包导入你的节点(需要密码)

geth wallet import /path/to/my/etherwallet.json

把一个EC私钥导入到一个ethereum账户(需要密码)

geth account import /path/to/key.prv

Geth的JavaScript运行环境

参见Geth JavaScript 控制台

打开geth javascript控制台:

geth --verbosity 5 --jspath /mydapp/js console 2>> /path/to/logfile

执行使用js API的test.js脚本,并把Debug-level记录至/path/to/logfile

geth --verbosity 6 js test.js 2>> /path/to/logfile

导入/导出链并转存块

从文件中导入blockchain

geth import blockchain.bin

升级blockchain数据库

当认证算法改变时,blockchain中的块也必须使用新算法重新导入,Geth会通知用户并提供说明,在需要的时候,何时以及如何处理这个信息

geth upgradedb

挖矿和网络

启动两个分别监听30303和30304端口,并且使用不同数据文件目录的挖矿节点:

geth --mine --minerthreads 4 --datadir /usr/local/share/ethereum/30303 --port 30303 geth --mine --minerthreads 4 --datadir /usr/local/share/ethereum/30304 --port 30304

开启一个在8000端口上的rpc客户端

geth --rpc=true --rpcport 8000 --rpccorsdomain '"*"'

开启无网络的客户端:

geth --maxpeers 0 --nodiscover --networdid 3301 js justwannarunthis.js

重新设置Blockchain

在数据文件目录中,删除Blockchain目录,例如: rm -rf /usr/local/share/ethereum/30303/blockchain

测试网络中同样的用法

下面的代码仅在非交互式脚本使用的测试网络和安全环境中有效

geth --datadir /tmp/eth/42 --password <(echo -n notsosecret) account new 2>> /tmp/eth/42.log
geth --datadir /tmp/eth/42 --port 30342  js <(echo 'console.log(admin.nodeInfo().NodeUrl)') > enode 2>> /tmp/eth/42.log
geth --datadir /tmp/eth/42 --port 30342 --password <(echo -n notsosecret) --unlock primary --minerthreads 4 --mine 2>> /tmp/eth/42.log

Attach

将控制台附加到一个正在运行的geth实例上,默认情况下,这通过IPC在默认IPC端点上发生,但必要时可以指定自定义端点:

geth attach ipc:/some/path
geth attach rpc:http://host:8545

设置标记的可选方法

警告 :这不是适应于最新的Frontier

可以通过配置文件(默认情况下<datadir>/conf.ini)以及环境变量来设置相同的标志。

优先级:default < config file < environment variables < command line