CentOS 7.5 搭建以太坊私聯(聯盟鏈)及區塊鏈瀏覽器

環境:

操作系統爲win10,虛擬化2個centos7.5 系統,cpu 4核,內存8G。本文將以node1,node2代表虛擬機節點1 和虛擬機節點2。

此篇將搭建2個節點,node1 會搭建區塊鏈瀏覽器。

環境準備

node1 搭建

  • 關閉防火牆,確保後續節點之間能相互通訊,數據同步。
su root

關閉防火牆

systemctl disable firewalld

修改/etc/selinux/config 文件
將SELINUX=enforcing改爲SELINUX=disabled

reboot
  • 安裝git
yum install git
  • 安裝golang
wget https://golang.org/dl/go1.15.linux-amd64.tar.gz
tar -zxvf go1.15.linux-amd64.tar.gz
mv go /usr/local/
  • 設置go環境變量
vi /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

source /etc/profile
  • 安裝node
curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
yum install nodejs

編譯以太坊源碼

  • 下載以太坊源碼(go)
wget https://github.com/ethereum/go-ethereum/archive/refs/tags/v1.9.24.tar.gz

解壓
tar -zxvf v1.9.24.tar.gz
  • 編譯以太坊
cd go-ethereum-1.9.24
make all

設置環境變量

echo 'export PATH=$PATH:/root/go-ethereum-1.9.24/build/bin' >> /etc/profile
source /etc/profile

創建初始化創世區塊的文件
cd ~ 

vi genesis.json

輸入以下配置

{
  "config": {
    "chainId": 1234,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5ddf8f3e",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x400",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {
    "此處填需要初始eth的錢包地址" : {"balance" : "此處爲你想初始多少wei的eth 餘額給前面的地址"}
  },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

退出並保存文件

初始化
geth  --datadir "/root/ethereumData" init /root/genesis.json

創世文件解釋
chainID 該鏈的ID。在用geth 啓動區塊鏈時,還需要指定一個network 參數。只有當network、chainID、創世區塊配置都相同時,纔是同一條鏈。

alloc 代表初始資產配置,在該區塊鏈產生時,就預先賦予這些賬戶一定數額的WEI(不是ETH)。

nonce 預定一個隨機數,這是一個與PoW 機制有關的值。

difficulty 定義了每次挖礦時,最終確定nonce 的難度,私鏈建議難度低一點,否則需要gpu才能挖礦。

mixhash 一個與PoW 機制有關的值。

coinbase 每挖出一個區塊,都會獲得獎勵。該值指定默認情況下把獎勵給到哪個賬戶。實際上,我們每次挖礦開始之前,都會自己指定miner.setEtherbase(UserAddress),一般都會把獎勵給自己timestamp 時間戳,規定創世區塊開始的時間。

parentHash 在區塊鏈中,區塊是相連的,parentHash 指定了本區塊的上一個區塊Hash。對於創世區塊來說,parentHash 爲0。

extraData 在Clique 機制下,新區塊只能被簽名人(singers)挖掘,區塊鏈生長過程中,可以通過投票來選舉或者免除簽名人。在區塊鏈開始運行時,需要定義一個初始singer。

gasLimit 規定該區塊鏈中,gas 的上限,私鏈建議多一點,可以在交易中打包更大的數據。
  • 啓動geth
geth --networkid 1234 --nodiscover --datadir /root/ethereumData --rpccorsdomain "*" --rpc --rpcapi "admin,eth,debug,miner,net,txpool,personal,web3" --rpcaddr 0.0.0.0 --rpcport 8545 --port 30001  console 2 --dev.period 1 --allow-insecure-unlock

--nodiscover 關閉節點發現。

--allow-insecure-unlock  啓用賬戶解鎖,不開啓賬戶將無法解鎖進行轉賬。

創建賬號(參與挖礦)
personal.newAccount('password')

賬號可以多創建幾個,第一個默認爲挖礦賬戶。

 獲取節點信息
geth 啓動成功後會輸出節點信息,也可以通過admin.nodeInfo 查看,內容如下:

enode://73f68dbed3206fa341919a057cd583cdceeaee9855de31bebc7c9cd347c99a30ec58ccaf882b559645e6690da3b4581258d9ce40b3e37088c99de2d851b6c84c@127.0.0.1:30001?discport=0

把127.0.0.1改爲節點一IP,並記錄節點信息。

開啓挖礦
miner.start() 

搭建以太坊區塊鏈瀏覽器  

  • 下載編譯 explorer
cd ~

git clone https://github.com/etherparty/explorer
cd explorer

用 npm 安裝 bower
npm install -g bower -y

初始化 bower
bower init

bower 安裝
bower install --allow-root

bower install angular --save-dev  --allow-root

修改配置並啓動瀏覽器
修改配置,讓瀏覽器其他機器可以訪問
vi app/app.js

修改 var eth_node_url = 'http://localhost:8545';     // TODO: remote URL 爲 
var eth_node_url = 'http://節點一IP:8545';               // TODO: remote URL

vi package.json
修改 "start": "http-server ./app -a localhost -p 8000 -c-1", 爲
"start": "http-server ./app -a 節點一IP -p 8000 -c-1",

 

  • 啓動瀏覽器
npm start

訪問瀏覽器
http://節點一IP:8000
  • GETH 常用命令

查詢賬戶餘額,單位WEI
eth.getBalance("address1")


解鎖賬戶(300秒),只有解鎖後才能轉賬到其它賬戶,
web3.personal.unlockAccount("address1") 

輸入密碼


轉賬ETH到其他賬戶,單位WEI
eth.sendTransaction({from: "address1",to: "address2", value: "10000"})

節點二同步

node2重複node1的步驟,在成功啓動geth後可以通過以下命令加入節點一,形成集羣。

admin.addPeer("enode://73f68dbed3206fa341919a057cd583cdceeaee9855de31bebc7c9cd347c99a30ec58ccaf882b559645e6690da3b4581258d9ce40b3e37088c99de2d851b6c84c@nnode1IP:3301?discport=0")

此節點信息爲node1 geth啓動後輸出,也可以在node1 geth命令行中通過admin.nodeInfo 查看

最終可以在node1和node2 geth命令行中 通過eth 命令查看兩邊數據同步狀態。

查看節點信息命令:

admin.peers()

當加入節點信息成功後就能在node2中看到node1的節點信息,node1中也能看到node2的節點信息。

開啓node2 挖礦

miner.start()

至此,以太坊私鏈的搭建就已經完成。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章