天天看點

快速學習-用 Geth 搭建以太坊私鍊用 Geth 搭建以太坊私鍊

用 Geth 搭建以太坊私鍊

這節課讓我們來用 Geth 來搭建一個屬于自己的以太坊私鍊。

安裝 Geth

安裝 Geth 有很多種方式,這裡主要就 Linux 環境給出兩種:系統包管理器(apt-get)安裝和源碼安裝。更加推薦大家用源碼安裝,在整個過程中可以看到 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 倉庫,以擷取源代碼的副本。

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

複制

  1. 從源碼建構 Geth

    要建構 Geth,切換到下載下傳源代碼的目錄并使用 make 指令:

$ cd go-ethereum
$ make geth           

複制

如果一切順利,我們将看到 Go 編譯器建構每個元件,直到它生成 geth 可執行檔案:

build/env.sh go run build/ci.go install ./cmd/geth 
>>> /usr/local/go/bin/go install -ldflags -X 
main.gitCommit=58a1e13e6dd7f52a1d5e67bee47d23fd6cfdee5c -v ./cmd/geth 
github.com/ethereum/go-ethereum/common/hexutil 
github.com/ethereum/go-ethereum/common/math 
github.com/ethereum/go-ethereum/crypto/sha3 github.com/ethereum/go-ethereum/rlp 
github.com/ethereum/go-ethereum/crypto/secp256k1 
github.com/ethereum/go-ethereum/common [...] 
github.com/ethereum/go-ethereum/cmd/utils
github.com/ethereum/go-ethereum/cmd/geth Done building. Run "build/bin/geth" to 
launch geth.           

複制

檢視 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           

複制

啟動節點同步

安裝好了 Geth,現在我們可以嘗試運作一下它。執行下面的指令,geth 就會開始同步區塊,并存儲在目前目錄下。這裡的 --syncmode fast 參數表示我們會以“快速”模式同步區塊。在這種模式下,我們隻會下載下傳每個區塊頭和區塊體,但不會執行驗證所有的交易,直到所有區塊同步完畢再去擷取一個系統目前的狀态。這樣就節省了很多交易驗證的時間。

$ geth –datadir . --syncmode fast           

複制

通常,在同步以太坊區塊鍊時,用戶端會一開始就下載下傳并驗證每個塊和每個交易,也就是說從創世區塊開始。 毫無疑問,如果我們不加 --syncmode fast 參數,同步将花費很長時間并且具有很高的資源要求(它将需要更多的 RAM,如果你沒有快速存儲,則需要很長時間)。

有些文章會把這個參數寫成 --fast,這是以前快速同步模式的參數寫法,現在已經被 –syncmode fast取代。

如果我們想同步測試網絡的區塊,可以用下面的指令:

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

複制

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

搭建自己的私有鍊

因為公共網絡的區塊數量太多,同步耗時太長,我們為了友善快速了解 Geth,可以試着用它來搭一個隻屬于自己的私鍊。

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

{
	"config": {
		"chainId": 15
	},
	"difficulty": "2000",
	"gasLimit": "2100000",
	"alloc": {
		"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": {
			"balance": "300000"
		},
		"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": {
			"balance": "400000"
		}
	}
}           

複制

要建立一條以它作為創世塊的區塊鍊,我們可以使用下面的指令:

geth --datadir path/to/custom/data/folder init genesis.json           

複制

在目前目錄下運作 geth,就會啟動這條私鍊,注意要将 networked 設定為與創世塊配置裡的chainId 一緻。

geth --datadir path/to/custom/data/folder --networkid 15           

複制

我們可以看到節點正常啟動:

WARN [10-23|02:38:19] No etherbase set and no accounts found as default
INFO [10-23|02:38:19] Starting peer-to-peer node 
instance=Geth/v1.8.0-unstable-e37f7be9/linux-amd64/go1.9
…
INFO [10-23|02:38:21] IPC endpoint opened: 
/home/ubuntu/project/go_ethereum_test/geth.ipc
INFO [10-23|02:38:21] Mapped network port proto=tcp 
extport=30303 intport=30303 interface="UPNP IGDv1-IP1"           

複制

恭喜!我們已經成功啟動了一條自己的私鍊。