【Get 以太坊技能】CentOS 7 Geth 搭建私链
前言
- CentOS Linux release 7.4.1708 (Core)
- geth Version: 1.8.17-stable (【Get 以太坊技能】CentOS 7 Geth安裝)
為啥搭建私鏈
在以太坊的共有鏈上部署智能合約、發起交易需要花費以太幣。而通過修改配置,可以在本機搭建一套以太坊私有鏈,因為與公有鏈沒關系,既不用同步公有鏈龐大的數據,也不用花錢購買以太幣,很好地滿足了智能合約開發和測試的要求,開發好的智能合約也可以很容易地切換接口部署到以太坊公有鏈上。
搭建說明
- 官方的搭建說明:https://github.com/ethereum/go-ethereum#operating-a-private-network
- 非官方的搭建說明:自行搜索,很多!
- 我的搭建說明:往下看。
Step1:選好位置
- 工作目錄設定為:/data/sde/geth_priv
- 數據目錄設定為:/data/sde/geth_priv/data
Step2:創建第1個 Account 作為 coinbase
shell> cd /data/sde/geth_priv && geth --datadir ./data account new INFO [11-27|16:30:11.007] Maximum peer count ETH=50 LES=0 total=50 INFO [11-27|16:30:11.007] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory" Your new account is locked with a password. Please give a password. Do not forget this password. Password: Repeat password:Your new key was generatedPublic address of the key: 0xf6ce09760a136833A1574aea1C6Bc75EaC514652 Path of the secret key file: data/keystore/UTC--2019-11-27T08-30-18.297342351Z--f6ce09760a136833a1574aea1c6bc75eac514652- You can share your public address with anyone. Others need it to interact with you. - You must NEVER share the secret key with anyone! The key controls access to your funds! - You must BACKUP your key file! Without the key, it's impossible to access account funds! - You must REMEMBER your password! Without the password, it's impossible to decrypt the key!得到第1個 Account : 0xf6ce09760a136833A1574aea1C6Bc75EaC514652
Step3:再創建2個 Account ,在初始化私鏈時分配Ether
創建方法參考Step2。
得到第2個 Account : 0x95f86CeaE604A969005B18b8A2e7542Ea8043ded
得到第3個 Account : 0x92857592a2D96894a45F400D86fe9cbE0d72F199
Step4:編輯genesis.json
- genesis.json文件放置位置:/data/sde/geth_priv/genesis.json
- genesis.json文件內容。一定要去官網拷貝該文件的內容。geth是不停在升級的,genesis.json的內容也會有變化。血的教訓:我在網上找了個文章,就跟著做了,結果genesis.json的內容不匹配geth版本,浪費了好久。
- genesis.json文件內容舉例(內置了兩個地址,并分配了Ether):
說明參考這里:https://blog.csdn.net/scau_lth/article/details/79570886
Step5:初始化
shell> cd /data/sde/geth_priv && geth init ./genesis.json --datadir ./data INFO [11-27|16:37:29.844] Maximum peer count ETH=50 LES=0 total=50 INFO [11-27|16:37:29.844] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory" INFO [11-27|16:37:29.847] Allocated cache and file handles database=/data/sde/geth_priv/data/geth/chaindata cache=16.00MiB handles=16 INFO [11-27|16:37:29.855] Writing custom genesis block INFO [11-27|16:37:29.856] Persisted trie from memory database nodes=3 size=416.00B time=109.651μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [11-27|16:37:29.856] Successfully wrote genesis state database=chaindata hash=188825…e50e76 INFO [11-27|16:37:29.856] Allocated cache and file handles database=/data/sde/geth_priv/data/geth/lightchaindata cache=16.00MiB handles=16 INFO [11-27|16:37:29.865] Writing custom genesis block INFO [11-27|16:37:29.866] Persisted trie from memory database nodes=3 size=416.00B time=684.975μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [11-27|16:37:29.866] Successfully wrote genesis state database=lightchaindata hash=188825…e50e76看到 Successfully 就OK了。
此時,目錄結構如下:
Step6 : 第一次啟動
第一次啟動geth時,按照下面的命令進行輸入,體驗geth。
# 啟動 geth shell> cd /data/sde/geth_priv && geth --nodiscover --datadir ./data --identity "gethPriv" --networkid 12345 --port 30305 console # 查看 geth 的節點信息 geth shell> admin.nodeInfo # 退出 geth geth shell> exit體驗過程:
shell> cd /data/sde/geth_priv && geth --nodiscover --datadir ./data --identity "gethPriv" --networkid 12345 --port 30305 console INFO [11-27|16:38:55.218] Maximum peer count ETH=50 LES=0 total=50 INFO [11-27|16:38:55.218] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory" INFO [11-27|16:38:55.221] Starting peer-to-peer node instance=Geth/gethPriv/v1.9.8-stable-d62e9b28/linux-amd64/go1.13.4 INFO [11-27|16:38:55.221] Allocated trie memory caches clean=256.00MiB dirty=256.00MiB INFO [11-27|16:38:55.221] Allocated cache and file handles database=/data/sde/geth_priv/data/geth/chaindata cache=512.00MiB handles=32767 INFO [11-27|16:38:55.253] Opened ancient database database=/data/sde/geth_priv/data/geth/chaindata/ancient INFO [11-27|16:38:55.253] Initialised chain configuration config="{ChainID: 12345 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: <nil> Engine: unknown}" INFO [11-27|16:38:55.253] Disk storage enabled for ethash caches dir=/data/sde/geth_priv/data/geth/ethash count=3 INFO [11-27|16:38:55.253] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2 INFO [11-27|16:38:55.253] Initialising Ethereum protocol versions="[64 63]" network=12345 dbversion=<nil> WARN [11-27|16:38:55.253] Upgrade blockchain database version from=<nil> to=7 INFO [11-27|16:38:55.255] Loaded most recent local header number=0 hash=188825…e50e76 td=65536 age=50y7mo2w INFO [11-27|16:38:55.255] Loaded most recent local full block number=0 hash=188825…e50e76 td=65536 age=50y7mo2w INFO [11-27|16:38:55.255] Loaded most recent local fast block number=0 hash=188825…e50e76 td=65536 age=50y7mo2w INFO [11-27|16:38:55.256] Regenerated local transaction journal transactions=0 accounts=0 INFO [11-27|16:38:55.265] Allocated fast sync bloom size=512.00MiB INFO [11-27|16:38:55.272] Initialized fast sync bloom items=3 errorrate=0.000 elapsed=5.464ms INFO [11-27|16:38:55.279] New local node record seq=1 id=019ce63e78996e1c ip=127.0.0.1 udp=0 tcp=30305 INFO [11-27|16:38:55.279] Started P2P networking self="enode://09c25d97f8d5ef391838469cf3558f4bf8aea3332c933b48a5aa0263fabc8746e20328d26fcfa8d372ddfbad0af5c069408a10fbf0538cad10bd20ca110bb92e@127.0.0.1:30305?discport=0" INFO [11-27|16:38:55.279] IPC endpoint opened url=/data/sde/geth_priv/data/geth.ipc INFO [11-27|16:38:55.356] Etherbase automatically configured address=0xf6ce09760a136833A1574aea1C6Bc75EaC514652 Welcome to the Geth JavaScript console!instance: Geth/gethPriv/v1.9.8-stable-d62e9b28/linux-amd64/go1.13.4 coinbase: 0xf6ce09760a136833a1574aea1c6bc75eac514652 at block: 0 (Thu, 01 Jan 1970 08:00:00 CST)datadir: /data/sde/geth_priv/datamodules: 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.0geth shell> admin.nodeInfo {enode: "enode://09c25d97f8d5ef391838469cf3558f4bf8aea3332c933b48a5aa0263fabc8746e20328d26fcfa8d372ddfbad0af5c069408a10fbf0538cad10bd20ca110bb92e@127.0.0.1:30305?discport=0",enr: "enr:-JC4QKN7CPyL0M4lvJzUnSreIr7hbHPQ8y7j6oai4cKVUsjhMjNl3L6NQNLDDXb36hInOC2fv2ZhwtPBDSrOOztGhM4Bg2V0aMfGhEKv3-mAgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQIJwl2X-NXvORg4RpzzVY9L-K6jMyyTO0ilqgJj-ryHRoN0Y3CCdmE",id: "019ce63e78996e1c6eb0137bcb9d49018660522805d8f9e16412a937deb6f7ca",ip: "127.0.0.1",listenAddr: "[::]:30305",name: "Geth/gethPriv/v1.9.8-stable-d62e9b28/linux-amd64/go1.13.4",ports: {discovery: 0,listener: 30305},protocols: {eth: {config: {byzantiumBlock: 0,chainId: 12345,constantinopleBlock: 0,eip150Block: 0,eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",eip155Block: 0,eip158Block: 0,homesteadBlock: 0,petersburgBlock: 0},difficulty: 65536,genesis: "0x18882519e8a38ec1ab9516c1cef14f064827ec8160cb0b76821300dd9ce50e76",head: "0x18882519e8a38ec1ab9516c1cef14f064827ec8160cb0b76821300dd9ce50e76",network: 12345}} } geth shell> exit INFO [11-27|16:40:18.780] IPC endpoint closed url=/data/sde/geth_priv/data/geth.ipc INFO [11-27|16:40:18.780] Blockchain manager stopped INFO [11-27|16:40:18.780] Stopping Ethereum protocol INFO [11-27|16:40:18.780] Ethereum protocol stopped INFO [11-27|16:40:18.780] Transaction pool stoppedStep7:查看賬號
啟動方式參考Step6.
# 查看當前節點下有多少account geth shell> eth.accounts # 查看coinbase。挖礦得到的Ether會進入coinbase account geth shell> eth.coinbase # 查看第一個賬號的余額,單位:Wei。賬號下標從0開始。 geth shell> eth.getBalance(eth.accounts[0]) geth shell> eth.getBalance(eth.accounts[1]) geth shell> eth.getBalance(eth.accounts[2]) geth shell> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether"); geth shell> web3.fromWei(eth.getBalance(eth.accounts[1]), "ether"); geth shell> web3.fromWei(eth.getBalance(eth.accounts[2]), "ether");geth 執行結果:
# 查看當前節點下有多少account。下標從0開始。 geth shell> eth.accounts ["0xf6ce09760a136833a1574aea1c6bc75eac514652", "0x95f86ceae604a969005b18b8a2e7542ea8043ded", "0x92857592a2d96894a45f400d86fe9cbe0d72f199"] # 查看coinbase。挖礦得到的Ether會進入coinbase account geth shell> eth.coinbase "0xf6ce09760a136833a1574aea1c6bc75eac514652" # 查看第1個賬號的余額,單位:Wei。賬號下標從0開始。 geth shell> eth.getBalance(eth.accounts[0]) 0 # 查看第2個賬號的余額,單位:Wei。賬號下標從0開始。 geth shell> eth.getBalance(eth.accounts[1]) 1e+28 # 查看第3個賬號的余額,單位:Wei。賬號下標從0開始。 geth shell> eth.getBalance(eth.accounts[2]) 1e+25 # 查看第0個賬號的余額,單位:Ether。賬號下標從0開始。 geth shell> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether"); 0 # 查看第1個賬號的余額,單位:Ether。賬號下標從0開始。 geth shell> web3.fromWei(eth.getBalance(eth.accounts[1]), "ether"); 10000000000 # 查看第2個賬號的余額,單位:Ether。賬號下標從0開始。 geth shell> web3.fromWei(eth.getBalance(eth.accounts[2]), "ether"); 10000000Step8:退出 Geth Console
geth shell> exit最后
判斷 Geth 私鏈是否搭好:依據 Step6 中的方法查看賬戶及余額。能夠查到賬戶和余額,就表示搭建好了。
Geth 私鏈搭建好后,總結一下。通過 Geth 私鏈搭建,get 到了以下東西:
- 一個可以啟動/關閉的 Geth 私鏈
- 新建了3個賬號
- 2個賬號很富有
接下來要做:
- 挖礦。給新建的賬號一些創業資本。
- 轉賬。各種花式轉賬。
- 操作智能合約。
分享個心得體會:一次搭不好,多搭建幾次就行了。
說明
經過測試,發現geth需要至少512M內存。如果操作系統內存不足512M時,會提示fatal error: runtime: out of memory
參考
https://github.com/ethereum/go-ethereum
https://ethereum.gitbooks.io/frontier-guide/content/index.html
總結
以上是生活随笔為你收集整理的【Get 以太坊技能】CentOS 7 Geth 搭建私链的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拎一台联想小新平板电脑Pro足矣联想小新
- 下一篇: 【Get 以太坊技能】遇到错误 issu