本文是区块链系列的第二篇,上一篇区块链系列教程——创建属于自己的区块链大概介绍了区块链的基本内容,并且使用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=