快速學習-用 Geth 搭建以太坊私鏈
- 2020 年 4 月 10 日
- 筆記
用 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
二、源碼安裝
- 克隆 github 倉庫 我們的第一步是克隆 git 倉庫,以獲取源代碼的副本。
$ git clone https://github.com/ethereum/go-ethereum.git
- 從源碼構建 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"
恭喜!我們已經成功啟動了一條自己的私鏈。