Geth-1.10.16 私链搭建
0、安裝環(huán)境
虛擬機(jī)中安裝的操作系統(tǒng):Centos7
參考官網(wǎng)文檔:Private Networks | Go Ethereum
1、創(chuàng)建賬戶
(1)創(chuàng)建目錄rungeth并進(jìn)入
[root@localhost local]# cd geth-alltools-1.10.16/ #進(jìn)入geth目錄 [root@localhost geth-alltools-1.10.16]# mkdir rungeth #創(chuàng)建目錄rungeth [root@localhost geth-alltools-1.10.16]# cd rungeth #進(jìn)入rungeth目錄(2)創(chuàng)建賬戶
[root@localhost rungeth]# geth account new --datadir data INFO [03-25|00:43:10.089] Maximum peer count ETH=50 LES=0 total=50 INFO [03-25|00:43:10.089] 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: #輸入密碼(我輸入的是abc) Repeat password: #確認(rèn)密碼Your new key was generatedPublic address of the key: 0x654FFcC87B54280d5BAA24CA7D366AD21E3A3C73 #生成賬戶地址 Path of the secret key file: data/keystore/UTC--2022-03-24T16-43-17.897096805Z--654ffcc87b54280d5baa24ca7d366ad21e3a3c73- 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!(3)查看生成目錄及文件
[root@localhost rungeth]# tree data data └── keystore└── UTC--2022-03-24T16-43-17.897096805Z--654ffcc87b54280d5baa24ca7d366ad21e3a3c731 directory, 1 file(4)查看賬戶信息
[root@localhost rungeth]# cat data/keystore/UTC--2022-03-24T16-43-17.897096805Z--654ffcc87b54280d5baa24ca7d366ad21e3a3c73 | jq . {"address": "654ffcc87b54280d5baa24ca7d366ad21e3a3c73","crypto": {"cipher": "aes-128-ctr","ciphertext": "3928f93d9aafef698c5ce1fe199a0437b0ac5efc853c94886a22bbff826f6282","cipherparams": {"iv": "8976121cedcef7ba44be2176ce58f569"},"kdf": "scrypt","kdfparams": {"dklen": 32,"n": 262144,"p": 1,"r": 8,"salt": "5981b38618d601c62befd9e40f38ba7de7cc82c83fcdef4e0900ab9318db59be"},"mac": "921527ffdc1ffc752157b114397f7d869e69840d80be58daec80d4217d272530"},"id": "b0a493e2-6d39-43b6-ae8e-6a3874e7e7d9","version": 3 }也可通過如下方式查看賬戶(此賬戶查看不是在同一環(huán)境下生成,因此賬戶信息與上面不同,只參考查詢方法即可)
[root@localhost rungeth]# geth account list --datadir data INFO [04-05|20:26:40.568] Maximum peer count ETH=50 LES=0 total=50 INFO [04-05|20:26:40.568] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory" INFO [04-05|20:26:40.569] Set global gas cap cap=50,000,000 Account #0: {b151a6dfa0f453402518bcd48adc876da4feb3f2} keystore:///usr/local/geth-alltools-1.10.16/rungeth/data/keystore/UTC--2022-04-05T12-22-54.238533327Z--b151a6dfa0f453402518bcd48adc876da4feb3f22、初始化創(chuàng)世塊
(1)生成創(chuàng)世塊
創(chuàng)世塊內(nèi)容如下:
{? "config": {
? ? "chainId": 108,
? ? "homesteadBlock": 0,
? ? "eip150Block": 0,
? ? "eip155Block": 0,
? ? "eip158Block": 0,
? ? "byzantiumBlock": 0,
? ? "constantinopleBlock": 0,
? ? "petersburgBlock": 0,
? ? "ethash": {}
? },
? "difficulty": "1",
? "gasLimit": "8000000",
? "alloc": {
? ? "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
? ? "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
? }
}
創(chuàng)世塊中對應(yīng)字段說明:?
| chainId | 網(wǎng)絡(luò)ID,區(qū)分不同的區(qū)塊鏈網(wǎng)絡(luò),值為0代表以太坊主網(wǎng) |
| difficulty | mine難度 |
| gasLimit | 創(chuàng)世塊能夠消耗gas的上限,即最多消耗的gas值;智能合約運(yùn)行在EVM上,運(yùn)行機(jī)器碼指令,每個(gè)指令都會對應(yīng)相應(yīng)的gas消耗,gas與以太不是等價(jià)的,它們之前有換算關(guān)系,gas * gasPrice = ether, gasPrice是gas單價(jià)(單位wei),可以上下浮動(感覺跟市場油價(jià)一樣會發(fā)生變動) |
?將創(chuàng)世塊內(nèi)容拷貝到文件,按ESC,輸入:wq保存退出。
(2)初始化創(chuàng)世塊
[root@localhost rungeth]# geth init --datadir data genesis.json INFO [03-25|01:15:46.766] Maximum peer count ETH=50 LES=0 total=50 INFO [03-25|01:15:46.766] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory" INFO [03-25|01:15:46.767] Set global gas cap cap=50,000,000 INFO [03-25|01:15:46.767] Allocated cache and file handles database=/usr/local/geth-alltools-1.10.16/rungeth/data/geth/chaindata cache=16.00MiB handles=16 INFO [03-25|01:15:46.771] Writing custom genesis block INFO [03-25|01:15:46.772] Persisted trie from memory database nodes=3 size=397.00B time="342.191μs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [03-25|01:15:46.772] Successfully wrote genesis state database=chaindata hash=c3638c..f97051 INFO [03-25|01:15:46.772] Allocated cache and file handles database=/usr/local/geth-alltools-1.10.16/rungeth/data/geth/lightchaindata cache=16.00MiB handles=16 INFO [03-25|01:15:46.774] Writing custom genesis block INFO [03-25|01:15:46.775] Persisted trie from memory database nodes=3 size=397.00B time="166.189μs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [03-25|01:15:46.775] Successfully wrote genesis state database=lightchaindata hash=c3638c..f970513、啟動Geth
(1)常用命令說明
可以通過geth -h幫助指令查看所以指令及對應(yīng)功能說明,以下常用指令說明
| --datadir | 指定之前初始化的數(shù)據(jù)目錄文件 |
| --networkid | 區(qū)分不同的區(qū)塊鏈網(wǎng)絡(luò),與創(chuàng)世塊chainId一樣,0為以太坊主網(wǎng),私鏈自己隨意編號 |
| --http | 開啟遠(yuǎn)程調(diào)用服務(wù),執(zhí)行智能合約時(shí)連接的節(jié)點(diǎn)是借助于http服務(wù) |
| --http.addr | 遠(yuǎn)程服務(wù)地址 |
| --http.port | 遠(yuǎn)程服務(wù)端囗,默認(rèn)是8545 |
| --http.api | 遠(yuǎn)程服務(wù)提供的遠(yuǎn)程服務(wù)調(diào)用函數(shù)集(db、net、eth、web3、personal等) |
| --http.corsdomain | 指定可以接收請求來源的域名列表(瀏覽器訪問時(shí)必須開啟),默認(rèn)為 “*” |
| --snapshot | 是否發(fā)現(xiàn)其它節(jié)點(diǎn) |
| --mine | 開啟出塊(miner.start()) |
| --miner.threads | 設(shè)置出塊的線程數(shù)量 |
| --allow-insecure-unlock | 允許在Geth命令窗囗解鎖賬戶(新版本1.9.0+增加的選項(xiàng)) |
| --dev | 開發(fā)者模式 |
| --dev.period | 在開發(fā)者模式中,產(chǎn)生交易后是否自動mine,1 是,0 否(默認(rèn)) |
| --console | 進(jìn)入管理后臺(如修改rpc端囗) |
| 2>1.log | 將Geth產(chǎn)生的日志輸出重定向到1.log文件中 0 標(biāo)準(zhǔn)輸入 1 標(biāo)準(zhǔn)輸出? 2 標(biāo)準(zhǔn)錯(cuò)誤 |
(2)啟動Geth
命令啟動
命令如下:
geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log
[root@localhost rungeth]# geth --datadir ./data --networkid 108 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --mine --miner.threads 1 --allow-insecure-unlock console 2> 1.log Welcome to the Geth JavaScript console!instance: Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5 coinbase: 0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73 at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))datadir: /usr/local/geth-alltools-1.10.16/rungeth/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.0To exit, press ctrl-d or type exit >以上Geth私鏈搭建成功。也可將以上命令生成一個(gè)腳本,見下腳本啟動。
腳本啟動
可將啟動命令生成一個(gè)腳本文件,以后每次執(zhí)行腳本文件即可,腳本內(nèi)容如下:
#!/bin/bash
geth --datadir ./data --networkid 108 --port 30303 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --allow-insecure-unlock console 2>1.log
[root@localhost rungeth]# vi rungeth.sh #生成腳本文件,將上面內(nèi)容拷貝到文件中 [root@localhost rungeth]# ls #查看當(dāng)前目錄是否生成了腳本文件 data genesis.json rungeth.sh [root@localhost rungeth]# ./rungeth.sh #執(zhí)行腳本 -bash: ./rungeth.sh: Permission denied #發(fā)現(xiàn)沒有權(quán)限 [root@localhost rungeth]# chmod 777 rungeth.sh #為腳本設(shè)置可執(zhí)行權(quán)限 [root@localhost rungeth]# ./rungeth.sh #再次執(zhí)行腳本,成功啟動Geth Welcome to the Geth JavaScript console!instance: Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5 coinbase: 0x865e7972b3b253cb9b39f5723b3c3f4115fca6f9 at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))datadir: /usr/local/geth-alltools-1.10.16/rungeth/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.0To exit, press ctrl-d or type exit >查看日志:打開一個(gè)新的命令窗囗
[root@localhost rungeth]# ls #查看日志文件 1.log data genesis.json rungeth.sh [root@localhost rungeth]# tail -f 1.log #查看日志內(nèi)容 INFO [04-04|17:12:25.107] Looking for peers peercount=0 tried=91 static=0 INFO [04-04|17:12:45.248] Looking for peers peercount=2 tried=99 static=0 INFO [04-04|17:12:55.334] Looking for peers peercount=0 tried=47 static=0 INFO [04-04|17:13:05.350] Looking for peers peercount=2 tried=86 static=0 INFO [04-04|17:13:15.408] Looking for peers peercount=0 tried=46 static=0 INFO [04-04|17:13:25.712] Looking for peers peercount=0 tried=86 static=0 INFO [04-04|17:13:55.958] Looking for peers peercount=0 tried=48 static=0 INFO [04-04|17:14:05.993] Looking for peers peercount=2 tried=59 static=0 ERROR[04-04|17:14:07.299] Snapshot extension registration failed peer=04dfa983 err="peer connected on snap without compatible eth support" INFO [04-04|17:14:16.163] Looking for peers peercount=0 tried=118 static=0開發(fā)者模式啟動
在啟動命令中加上開發(fā)者模式即可,即加上如下粗體字部分:?
geth --datadir ./data --networkid 108 --port 30303 --http --http.addr 0.0.0.0 --http.vhosts "*" --http.api "db,net,eth,web3,personal" --http.corsdomain "*" --snapshot=false --allow-insecure-unlock --dev --dev.period 1 console 2>1.log
如出現(xiàn)錯(cuò)誤:Fatal: Failed to unlock developer account: could not decrypt key with given password? 解決方案參見:Geth 使用dev模式啟動報(bào)錯(cuò):Fatal: Failed to unlock developer account: could not decrypt key with given pass_ling1998的博客-CSDN博客
當(dāng)然,也可以把開發(fā)者模式命令保存成一個(gè)腳本文件,每次直接執(zhí)行腳本文件即開啟GETH。?
進(jìn)入已啟動的Geth(通過IPC進(jìn)入)
若已啟動Geth,可通過如下命令進(jìn)入控制臺:
[root@localhost rungeth]# geth attach data/geth.ipc #加上geth.ipc所在路徑即可 Welcome to the Geth JavaScript console!instance: Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5 coinbase: 0xb151a6dfa0f453402518bcd48adc876da4feb3f2 at block: 140 (Wed Apr 06 2022 00:09:46 GMT+0800 (CST))datadir: /usr/local/geth-alltools-1.10.16/rungeth/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.0To exit, press ctrl-d or type exit >注:第一個(gè)區(qū)塊出塊時(shí)間較長,大概2~5分鐘左右,耐心等待,確保已開啟 miner.start()?
4、常用命令操作
(1)查看賬戶
> eth.accounts ["0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73"](2)賦值
> acc0=eth.accounts[0] "0x654ffcc87b54280d5baa24ca7d366ad21e3a3c73"(3)查詢賬戶余額
> eth.getBalance(acc0) 396000000000000000000 > eth.getBalance(acc0) 562000000000000000000一直在mining,所以余額會增加。
總結(jié)
以上是生活随笔為你收集整理的Geth-1.10.16 私链搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rtthread定时器的实现
- 下一篇: 微服务学习总结