ETH搭建本地私有鏈(多節點互通)


本教程基於CentOS7.9 x64上部署以太坊的私鏈。

安裝go(可以自行安裝最新版本)

1、搭建Go軟件環境

    安裝方法,請參考這篇文章:https://blog.csdn.net/sanqima/article/details/113623784

2、設置Go訪問代理

go env -w GOPROXY=https://goproxy.cn,direct

3、設置CentOS的鏡像源

    將CentOS的鏡像源設置為阿里雲,具體請參考這篇文章:https://blog.csdn.net/sanqima/article/details/117634934

4、安裝工具軟件

yum install git wget bzip2 vim gcc-c++ ntp epel-release nodejs cmake -y
yum update

5、下載以太坊源碼(Go Ethereum)

可以自行在:https://github.com/ethereum/go-ethereum/ 查看最新tag的源碼編譯。

5.1 下載源碼並編譯

    這里選擇 Go Ethereum v1.9.25
    下載地址: https://github.com/ethereum/go-ethereum/archive/refs/tags/v1.9.25.tar.gz

    將go-ethereum-1.9.25.tar.gz存放到目錄:/opt

    依次使用如下命令:

cd /opt
sudo tar -zxvf go-ethereum-1.9.25.tar.gz cd go-ethereum-1.9.25 make all

5.2 設置geth環境

    a) 打開/etc/profile

nano /etc/profile

    b) 在/etc/profile里,添加如下語句:

export GOROOT=/usr/local/go
export GOPATH=/usr/local/gocode
export ETH_Base=/opt/go-ethereum-1.9.25
export PATH=$PATH:$GOROOT/bin:$GPPATH/bin:${ETH_Base}/build/bin

    c) 使能profile

source /etc/profile

    d) 查看geth的版本信息

geth version

    效果如下:

    若可以查看geth的版本信息,則說明geth環境配置成功。

6、升級cmake到3.x(可選)

//卸載舊版本的cmake

yum remove cmake

//安裝cmake 3.19.8

wget -c https://github.com/Kitware/CMake/releases/download/v3.19.8/cmake-3.19.8.tar.gz
sudo tar -zxvf cmake-3.19.8.tar.gz
cd cmake-3.19.8
./bootstrap && make && make install

7、啟動網絡時間同步

systemctl enable ntpd
systemctl start ntpd

8、關閉防火牆

    geth會用到8078和30303端口,放開防火牆端口 8078與30303

systemctl stop firewalld

9、設置ETH環境

在/opt 里新建一個文件夾名稱為ethdata

cd /opt
mkdir ethdata
cd ethdata
mkdir db
touch gensis.json 

其目錄結構如下:

ethdata的目錄結構

   

gensis.json的內容如下:

 

{
"config": {
"chainId": 666666,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0
},
"alloc": {"0x3Ecb2d7d3622d8FB030330D2c284dF77DE50d303":{"balance":"9000000000000000000000000000"}},
"coinbase": "0x3Ecb2d7d3622d8FB030330D2c284dF77DE50d303",
"difficulty": "0x400",
"extraData": "0x00",
"gasLimit": "0x80000000",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}

 

初始化

cd /opt/ethdata
geth --datadir "./db" init gensis.json

  初始化之后,會生成db/geth、db/keystore等文件夾; 其中,db/geth/chaindata中存放的是區塊數據,db/keystore中存放的是賬戶數據。

 

啟動私有鏈

cd /opt/ethdata

  geth --datadir "./db" console 2>>geth.log --nodiscover --http --http.addr 0.0.0.0 --http.port=12345 --http.api eth,net,web3

 

開始挖礦

控制台:指定coinbase用戶,啟動挖礦

miner.setEtherbase("0x3Ecb2d7d3622d8FB030330D2c284dF77DE50d303")

miner.start(2)     //啟動幾個線程挖礦

 

用戶的公鑰文件保存在 ethdata/db/keystore里

ls /home/levent/myProject/unit01/ethBase/ethdata/db/keystore

 

查看挖礦地址

eth.coinbase

停止挖礦

miner.stop()

 

如果關掉了geth console,可以通過下面命令進入控制台

geth attach console http://localhost:12345


本地多節點連接

說明:如果需要本地連接多節點,需要注意以下幾點

  1. 兩個節點客戶端的創世文件genesis.json需要一致(兩節點要加入同一條私鏈);
  2. 啟動節點的時候要使用同樣的networkid(即genesis.json中chanlid參數);
  3. 節點間的port和rpcport要不同

啟動

創建創世區塊:

geth --datadir "./db" init gensis.json

如若啟動多節點需要將上面私有鏈部署的啟動命令修改如下

節點1:

geth --datadir "./db" console 2>>geth.log --networkid 666666 --http --http.addr 172.22.0.55 --http.port=18340 --http.corsdomain "*" --http.api eth,net,web3,db --maxpeers=3 --allow-insecure-unlock --port 3000
View Code

節點2:

geth --datadir "./db" console 2>>geth.log --networkid 666666 --http --http.addr 172.22.0.52 --http.port=12345 --http.corsdomain "*" --http.api eth,net,web3,db --maxpeers=3 --allow-insecure-unlock --port 3001
View Code

連接

獲取節點2信息

admin.nodeInfo.enode
View Code

 結果:

"enode://eb72c7e321e68f07c4113668fab81a8b098fb0518c167a50e6c6e4f1f99f411359dc111064fa58869b4646635cbf588df3a39f176e27556c9246d2e88737cc81@169.254.128.131:3001"

注意:將紅色部分改為節點2的真實ip地址(查看網絡設置中的IPv4地址),因為admin.nodeInfo.enode輸出的不一定是正確的

在節點1中加入節點2:

admin.addPeer("enode://eb72c7e321e68f07c4113668fab81a8b098fb0518c167a50e6c6e4f1f99f411359dc111064fa58869b4646635cbf588df3a39f176e27556c9246d2e88737cc81@169.254.128.131:3001")
View Code

用net.peerCount和admin.peers命令查看是否連接成功,連接成功后兩個區塊間會相互同步。

驗證成功的簡單方法:在節點1跟節點2的geth控制台查看區塊高度,如果一樣則連接成功。

eth.blockNumber

 

如果以上多節點搭建有問題,可以參考這篇博文:https://blog.csdn.net/Khazking/article/details/122083702

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM