区块链系列教程——geth命令详解

本文是区块链系列的第二篇,上一篇区块链系列教程——创建属于自己的区块链大概介绍了区块链的基本内容,并且使用geth客户端创建了私链,给了各位读者较为直观的感受。本文简单介绍geth命令,这对后面的教程来说是一个基础。

geth用法简介

在安装有geth的环境下,使用命令即可查看geth的所有操作参数:

geth -help

其用法为:

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

geth命令简介

其中,command部分主要有:

   account           Manage accounts
   attach            Start an interactive JavaScript environment (connect to node)
   bug               opens a window to report a bug on the geth repo
   console           Start an interactive JavaScript environment
   copydb            Create a local chain from a target chaindata folder
   dump              Dump a specific block from storage
   dumpconfig        Show configuration values
   export            Export blockchain into file
   export-preimages  Export the preimage database into an RLP stream
   import            Import a blockchain file
   import-preimages  Import the preimage database from an RLP stream
   init              Bootstrap and initialize a new genesis block
   js                Execute the specified JavaScript files
   license           Display license information
   makecache         Generate ethash verification cache (for testing)
   makedag           Generate ethash mining DAG (for testing)
   monitor           Monitor and visualize node metrics
   removedb          Remove blockchain and state databases
   version           Print version numbers
   wallet            Manage Ethereum presale wallets
   help, h           Shows a list of commands or help for one command

此处主要讲解部分高频使用的命令。

1. account

该命令用于对节点中的账户进行管理,比如使用命令创建新的账户:

hadoop@tomcat:~/blockchain$ geth account new
INFO [05-02|22:34:35.822] Maximum peer count                       ETH=25 LES=0 total=25
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat passphrase: 
Address: {0661ae6f3586b5d06f72631d40b576572df58cf8}

比如列出所创建的所有账户信息:

hadoop@tomcat:~/blockchain$ geth account list
INFO [05-02|22:36:56.617] Maximum peer count                       ETH=25 LES=0 total=25
Account #0: {39270dca851f877e25de0b3dd2bc362a56c93d14} keystore:///home/hadoop/.ethereum/keystore/UTC--2018-11-08T07-51-54.537198312Z--39270dca851f877e25de0b3dd2bc362a56c93d14
Account #1: {3ad29039e3a403bec76f72f49df9c695132b7413} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T03-01-21.222439496Z--3ad29039e3a403bec76f72f49df9c695132b7413
Account #2: {76522ed9030ecdbc2adcf9db101ee351841a026d} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T03-02-18.142740162Z--76522ed9030ecdbc2adcf9db101ee351841a026d
Account #3: {0eb26f508cb4366cca769f5b42a2db53ac3411bc} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T03-06-41.861044859Z--0eb26f508cb4366cca769f5b42a2db53ac3411bc
Account #4: {03b753d082b2c65054b5ac0a624c3c206a8384ce} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T13-43-43.058685461Z--03b753d082b2c65054b5ac0a624c3c206a8384ce
Account #5: {69e5860b1b7b2fbbb5e99c856ccf1daa3d99cceb} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T14-36-59.127976359Z--69e5860b1b7b2fbbb5e99c856ccf1daa3d99cceb
Account #6: {49fc0574f7e149a5e772ca0144df1177af56a544} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T14-42-36.761539434Z--49fc0574f7e149a5e772ca0144df1177af56a544
Account #7: {36606041ff9727c15337aa68493c62a9f4fe721c} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T14-45-17.989230302Z--36606041ff9727c15337aa68493c62a9f4fe721c
Account #8: {c20302e3423fdc2d451c1f989978c82ddcb3d25d} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-08T14-49-35.136771402Z--c20302e3423fdc2d451c1f989978c82ddcb3d25d
Account #9: {0ee10994abc4ad567ae660c38dece6b66316e8f2} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-09T03-02-59.629189896Z--0ee10994abc4ad567ae660c38dece6b66316e8f2
Account #10: {dfad42736734d1e943cb3390ebab0f3f9b6cd956} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-09T03-08-56.185678319Z--dfad42736734d1e943cb3390ebab0f3f9b6cd956
Account #11: {b1d16776a90ab4acfa12c253821d39f4e72c8ede} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-03-09T03-10-18.698606526Z--b1d16776a90ab4acfa12c253821d39f4e72c8ede
Account #12: {0661ae6f3586b5d06f72631d40b576572df58cf8} keystore:///home/hadoop/.ethereum/keystore/UTC--2019-05-02T14-35-39.394844103Z--0661ae6f3586b5d06f72631d40b576572df58cf8
2. attach

attach命令主要用于连接到一个正在运行的节点,并且会打开一个控制台用于交互:

hadoop@tomcat:~/blockchain/test$ ls
geth  geth.ipc  history  keystore
hadoop@tomcat:~/blockchain/test$ geth attach geth.ipc 
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10
coinbase: 0x6393bb737b95465ccd9e4597df0647cdcd09775f
at block: 11 (Wed, 01 May 2019 21:40:53 CST)
 datadir: /home/hadoop/blockchain/test
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> 
3. console

console命令用于打开控制台用于交互,与attach有所不同,console一般是在区块链运行时打开,而attach是在节点运行的前提下连接到节点进行交互,两者可以并存。该命令具体用法在上一篇已经展示:

hadoop@tomcat:~/blockchain$ geth --datadir test/ --networkid 10 console
INFO [05-02|22:38:40.954] Maximum peer count                       ETH=25 LES=0 total=25
INFO [05-02|22:38:40.955] Starting peer-to-peer node               instance=Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10
INFO [05-02|22:38:40.955] Allocated cache and file handles         database=/home/hadoop/blockchain/test/geth/chaindata cache=768 handles=512
INFO [05-02|22:38:41.045] Initialised chain configuration          config="{ChainID: 10 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [05-02|22:38:41.045] Disk storage enabled for ethash caches   dir=/home/hadoop/blockchain/test/geth/ethash count=3
INFO [05-02|22:38:41.045] Disk storage enabled for ethash DAGs     dir=/home/hadoop/.ethash                     count=2
INFO [05-02|22:38:41.045] Initialising Ethereum protocol           versions="[63 62]" network=10
INFO [05-02|22:38:41.047] Loaded most recent local header          number=11 hash=4c92d8…336e91 td=382991890 age=1d57m48s
INFO [05-02|22:38:41.047] Loaded most recent local full block      number=11 hash=4c92d8…336e91 td=382991890 age=1d57m48s
INFO [05-02|22:38:41.047] Loaded most recent local fast block      number=11 hash=4c92d8…336e91 td=382991890 age=1d57m48s
INFO [05-02|22:38:41.048] Loaded local transaction journal         transactions=1 dropped=1
INFO [05-02|22:38:41.048] Regenerated local transaction journal    transactions=0 accounts=0
WARN [05-02|22:38:41.048] Blockchain not empty, fast sync disabled 
INFO [05-02|22:38:41.049] Starting P2P networking 
INFO [05-02|22:38:43.272] Mapped network port                      proto=udp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
INFO [05-02|22:38:43.383] UDP listener up                          self=enode://21d2a8209dc9aeadd15d94c2df30128dd7bd4b772f34e61e8f76afb893a2f7735a77c9061b64076ca61b5635b5d434fa32600af0e56f833fd7e582bff94e679d@222.201.145.179:30303
INFO [05-02|22:38:43.383] RLPx listener up                         self=enode://21d2a8209dc9aeadd15d94c2df30128dd7bd4b772f34e61e8f76afb893a2f7735a77c9061b64076ca61b5635b5d434fa32600af0e56f833fd7e582bff94e679d@222.201.145.179:30303
INFO [05-02|22:38:43.386] IPC endpoint opened                      url=/home/hadoop/blockchain/test/geth.ipc
INFO [05-02|22:38:43.418] Mapped network port                      proto=tcp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10
INFO [05-02|22:38:43.485] Etherbase automatically configured       address=0x6393Bb737b95465cCD9E4597dF0647CdcD09775f
coinbase: 0x6393bb737b95465ccd9e4597df0647cdcd09775f
at block: 11 (Wed, 01 May 2019 21:40:53 CST)
 datadir: /home/hadoop/blockchain/test
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> 
4. init

init命令是用于初始化一个区块链的,通常将genesis.json文件中的初始区块信息加载到区块链中:

hadoop@tomcat:~/blockchain$ mkdir test2
hadoop@tomcat:~/blockchain$ geth --datadir test2  init genesis.json 
INFO [05-02|22:45:45.306] Maximum peer count                       ETH=25 LES=0 total=25
INFO [05-02|22:45:45.306] Allocated cache and file handles         database=/home/hadoop/blockchain/test2/geth/chaindata cache=16 handles=16
INFO [05-02|22:45:45.389] Writing custom genesis block 
INFO [05-02|22:45:45.389] Persisted trie from memory database      nodes=0 size=0.00B time=5.821µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [05-02|22:45:45.390] Successfully wrote genesis state         database=chaindata                                    hash=b123b8…4077eb
INFO [05-02|22:45:45.390] Allocated cache and file handles         database=/home/hadoop/blockchain/test2/geth/lightchaindata cache=16 handles=16
INFO [05-02|22:45:45.394] Writing custom genesis block 
INFO [05-02|22:45:45.394] Persisted trie from memory database      nodes=0 size=0.00B time=3.963µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [05-02|22:45:45.395] Successfully wrote genesis state         database=lightchaindata                                    hash=b123b8…4077eb
5. js

js命令用于加载一个js文件:

hadoop@tomcat:~/blockchain$ geth js t.js
INFO [05-02|22:51:21.166] Maximum peer count                       ETH=25 LES=0 total=25
INFO [05-02|22:51:21.167] Starting peer-to-peer node               instance=Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10
INFO [05-02|22:51:21.167] Allocated cache and file handles         database=/home/hadoop/.ethereum/geth/chaindata cache=768 handles=512
INFO [05-02|22:51:21.180] Initialised chain configuration          config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Byzantium: 4370000 Constantinople: <nil> Engine: ethash}"
INFO [05-02|22:51:21.180] Disk storage enabled for ethash caches   dir=/home/hadoop/.ethereum/geth/ethash count=3
INFO [05-02|22:51:21.180] Disk storage enabled for ethash DAGs     dir=/home/hadoop/.ethash               count=2
INFO [05-02|22:51:21.180] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [05-02|22:51:21.181] Loaded most recent local header          number=192 hash=723899…123390 td=3453055867795 age=3y9mo3w
INFO [05-02|22:51:21.181] Loaded most recent local full block      number=0   hash=
### 在 VMware 上安装和配置 Geth 以搭建区块链网络 #### 准备工作 确保 VMware 网络配置已完成,能够上网并 ping 通物理机[^1]。 #### 安装 CentOS 7 选择合适的镜像文件,在 VMware 中创建新的虚拟机,并按照向导提示完成 CentOS 7 的安装过程。 #### 更新系统软件包 为了保证系统的稳定性和安全性,建议先更新所有的软件包至最新版本: ```bash sudo yum update -y ``` #### 安装必要的依赖库 Geth 需要一些额外的依赖项才能正常运行。执行如下命令来安装这些必需组件: ```bash sudo yum install epel-release -y sudo yum install git make gcc g++ openssl-devel libusb-devel jq wget curl vim python3-pip -y ``` #### 编译安装 Go 语言环境 由于 Geth 是由 Go 语言编写的程序,因此需要预先设置好 Go 开发环境。这里采用官方推荐的方式下载指定版本的 Go 并将其解压到 `/usr/local` 目录下: ```bash cd /tmp/ wget https://golang.org/dl/go1.20.linux-amd64.tar.gz tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc && source ~/.bashrc go version ``` #### 下载源码并构建 Geth 可执行文件 获取最新的 Geth 源代码仓库副本,并切换分支到稳定的发布标签页;接着利用 Makefile 工具链来进行项目的初始化与编译链接操作: ```bash git clone https://github.com/ethereum/go-ethereum.git $HOME/go/src/github.com/ethereum/go-ethereum cd !$ make geth ./build/bin/geth version ``` 此时应该能看到当前所使用的具体版本号信息输出显示出来,则说明已经成功完成了 Geth 的本地化部署流程[^2]。 #### 初始化创世区块 编辑 JSON 文件定义初始参数集作为整个网络的基础规则依据之一,比如难度系数、奖励机制等细节设定均在此处体现。保存命名为 `genesis.json` 后放置于合适路径位置以便后续调用加载之需: ```json { "config": { "chainId": 15, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "istanbulBlock": 0, "muirGlacierBlock": 0, "berlinBlock": 0, "londonBlock": 0 }, "difficulty": "0x200", "gasLimit": "0x2fefd8", "alloc": {} } ``` 随后通过下面这条指令实现对自定义 genesis block 数据结构体对象实例化的处理动作: ```bash ./build/bin/geth init ~/path/to/your/genesis.json --datadir=/home/user/.ethereum/private-chain ``` #### 启动私有链节点服务端口监听进程 最后一步就是正式开启一个全新的独立运作模式下的 Ethereum 节点服务器啦!可以通过附加不同的选项来自由定制更多高级特性功能哦~ ```bash nohup ./build/bin/geth \ --networkid 1234 \ --nodiscover \ --maxpeers 0 \ --nat none \ --http \ --http.addr 0.0.0.0 \ --http.port 8545 \ --http.api eth,net,web3,personal \ --ws \ --ws.addr 0.0.0.0 \ --ws.port 8546 \ --ws.origins "*" \ --mine \ --minerthreads=1 \ --etherbase 0xYourAddressHere \ --unlock 0xYourAddressHere \ --password <(echo -n YourPasswordHere) & ``` 上述命令将会以后台守护线程的形式持续不断地提供对外访问接口支持,同时也会自动触发挖矿作业任务直至遇到任何异常情况为止才会停止下来等待人工介入排查解决故障原因所在之处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值