使用Geth搭建以太坊私有鏈

作者:李鵬 2019/11/25 02:19

因為 Geth 安裝有很多種方式,我這裡主要就 Linux 環境給出兩種:系統包管理器(apt-get)安裝和源碼安裝。推薦大家用源碼安裝,因為在整個安裝過程中可以看到 Geth 各組件的構建步驟。

使用Geth搭建以太坊私有鏈

一、apt-get方式

$ sudo apt-get install software-properties-common 
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update $ sudo apt-get install ethereum

二、源碼安裝

1、克隆 github 倉庫,取源代碼

$ git clone https://github.com/ethereum/go-ethereum.git

2、構建 Geth,切換到下載源代碼的目錄並使用 make 命令:

$ cd go-ethereum 
$ make geth

3、我們將看到 Go 編譯器構建每個組件的構建信息(部分信息我這裡已省略),直到它生成 geth 可執行文件

build/env.sh go run build/ci.go install ./cmd/geth
go: downloading github.com/cespare/cp v0.1.0
go: downloading golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
go: downloading github.com/Azure/azure-storage-blob-go v0.7.0
go: extracting github.com/cespare/cp v0.1.0
go: extracting github.com/Azure/azure-storage-blob-go v0.7.0
go: downloading github.com/Azure/azure-pipeline-go v0.2.2
go: extracting github.com/Azure/azure-pipeline-go v0.2.2
go: downloading github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d
go: extracting golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
go: extracting github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d
go: finding github.com/cespare/cp v0.1.0
go: downloading github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa
............................
github.com/naoina/go-stringutil
github.com/naoina/toml/ast
github.com/naoina/toml
github.com/ethereum/go-ethereum/eth/tracers

github.com/ethereum/go-ethereum/eth
github.com/ethereum/go-ethereum/les
github.com/ethereum/go-ethereum/ethstats
github.com/ethereum/go-ethereum/cmd/utils
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "./build/bin/geth" to launch geth.

4、geth version,確保在真正運行之前安裝正常

$ ./build/bin/geth version
Geth
Version: 1.8.0-unstable
Git Commit: e37f7be97e47a032d723db16d8b195998547805a Architecture: amd64
Protocol Versions: [63 62] Network Id: 1
Go Version: go1.9
Operating System: linux
GOPATH=/home/ubuntu/project
GOROOT=/usr/local/go

三、啟動節點同步

1、安裝好了 Geth,現在我們可以嘗試運行一下它。執行下面的命令,geth 就會開始同步區塊,並存儲在當前目錄下。這裡的 --syncmode fast 參數表示我們會以“快速”模式同步區塊。默認值為full,如過什麼不加就是下載全節點。在這種模式下,我 們只會下載每個區塊頭和區塊體,但不會執行驗證所有的交易,直到所有區塊同步完畢再去獲取一個系統當前的狀態。這樣就節省了很多交易驗證的時間。

$ geth --datadir . --syncmode fast
  1. 通常,在同步以太坊區塊鏈時,客戶端會一開始就下載並驗證每個塊和每個交易,也就是說從創世區塊開始。 毫無疑問,如果我們不加 --syncmode fast 參數,同步將花費很長時間並且具有很高的資源要求(它將需要更多的 RAM,如果你沒有快速存儲,則需要很長時間)。
  2. 有些文章會把這個參數寫成 --fast,這是以前快速同步模式的參數寫法,現在已經被 –syncmode fast取代。

2、默認是同步主網絡的區塊,如果我們想同步測試網絡的區塊,可以用下面的命令:

$ geth --testnet --datadir . --syncmode fast

--testnet 這個參數會告訴 geth 啟動並連接到最新的測試網絡,也就是 Ropsten。測試網絡的區塊和交易數量會明顯少於主網,所以會更快一點。但即使是用快速模式同步測試網絡,也會需要幾個小時的時間。

四、搭建自己的私有鏈

1、因為公共網絡的區塊數量太多,同步耗時太長,我們為了方便快速瞭解 Geth,可以試著用它來搭一個只屬於自己的私鏈。

首先,我們需要創建網絡的“創世”(genesis)狀態,這寫在一個小小的 JSON 文件裡(例如,我們將其命名為 genesis.json):

{
\t"config": {
\t\t"chainId": 15
\t},
\t"difficulty": "2000",
\t"gasLimit": "2100000",
\t"alloc": {

\t\t"0x4CB6940fEDD0EaBf33288DaB4C28E45796FD7D7f": {
\t\t\t"balance": "300000000000000000"
\t\t},
\t\t"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": {
\t\t\t"balance": "400000"
\t\t}
\t}
}

config:整個區塊的配置

difficulty:難度係數(根據當前需求,難度係數越大,挖礦難度越大)

gasLimit:一個區塊要求多少個gas

alloc:key:地址 value:賬戶餘額 。

2、要創建一條以它作為創世塊的區塊鏈,我們可以使用下面的命令:

./build/bin/geth --datadir /usr/local/alexlp/mychain/data init /usr/local/alexlp/mychain/genesis.json

3、在當前目錄下運行 geth,就會啟動這條私鏈,注意要將 networked 設置為與創世塊配置裡的chainId一致。後面如果帶console會出現交互控制檯。

./build/bin/geth --datadir /usr/local/alexlp/mychain/data --networkid 15 [console]

到這裡私有鏈已經搭建完成,感謝支持!


分享到:


相關文章: