eric-ward-530909

Background

繼上次創立一個私鏈之後, 在網路上又找到另外一篇文章來介紹如何利用PoA來快速建立一個私鏈. 感覺之後應該用得到. 所以自己也根據那篇文章來實際Run一遍. 踩個雷, 為之後的礦池來做準備.

Installation

Basic

Update

$ sudo apt-get update
$ sudo apt-get dist-upgrade
$ sudo apt-get install build-essential make
$ sudo vim /etc/default/locale

/etc/default/locale

LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8

Time

$ sudo mkdir /etc/sysconfig/
$ sudo vim /etc/sysconfig/clock
$ sudo ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
$ sudo reboot

/etc/sysconfig/clock

ZONE="Asia/Taipei"
UTC=true

Golang

$ wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
$ sudo tar -xvf go1.9.2.linux-amd64.tar.gz
$ sudo mv go /usr/local
$ export GOROOT=/usr/local/go
$ export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
$ export GOPATH=$HOME/go
$ go version
go version go1.9.2 linux/amd64

go-ethereum

$ git clone https://github.com/ethereum/go-ethereum
$ cd go-ethereum
$ make geth
$ sudo cp build/bin/geth /usr/local/bin

puppeth

可以到這邊來下載 https://geth.ethereum.org/downloads/, 選Geth & Tools的來下載.

$ wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.7.3-4bb3c89d.tar.gz
$ tar -xvf geth-alltools-linux-amd64-1.7.3-4bb3c89d.tar.gz

Environment

首先我們需要先創四個資料夾. 分別代表之後的四個node. 接著我們要替這四個角色各建立一個 Ethereum 帳號.

$ mkdir node1 node2 signer1 signer2
$ ls
node1 node2 signer1 signer2
$ cd node1
$ geth --datadir ./data account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {1f9258c83b7b06979f578e445da6b98b10e304df}

分別進到那四個資料夾, 重複四次相同的動作, 就會得到四個 account address. 以下是我自己個人的四個 account address.

node1 => fcd0fb18a60ba6ed0d29d1934e56f893884408d2
node2 => 67179aecd0ccd9b0b945ff33e18a7ac84722afa3
signer1 => 6327c9ee039293617b3ad00f121d29b4e3561dcb
signer2 => 706b065493cc219ba94ec4b01382fde8a943f317

建立創世塊

$ cd geth-alltools-linux-amd64-1.7.3-4bb3c89d
$ puppeth
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+

Please specify a network name to administer (no spaces, please)
> poa-test
Sweet, you can set this via --network=poa next time!

INFO [01-30|16:53:12] Administering Ethereum network           name=poa
WARN [01-30|16:53:12] No previous configurations found         path=/home/ubuntu/.puppeth/poa

What would you like to do? (default = stats)
 1. Show network stats
 2. Configure new genesis
 3. Track new remote server
 4. Deploy network components
> 2

Which consensus engine to use? (default = clique)
 1. Ethash - proof-of-work
 2. Clique - proof-of-authority
> 2

How many seconds should blocks take? (default = 15)
> 15

Which accounts are allowed to seal? (mandatory at least one)
> 0x6327c9ee039293617b3ad00f121d29b4e3561dcb

Which accounts should be pre-funded? (advisable at least one)
> 0xfcd0fb18a60ba6ed0d29d1934e56f893884408d2
> 0x6327c9ee039293617b3ad00f121d29b4e3561dcb
> 0x


Specify your chain/network ID if you want an explicit one (default = random)
>

Anything fun to embed into the genesis block? (max 32 bytes)
>

What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
> 2

 1. Modify existing fork rules
 2. Export genesis configuration
> 2

Which file to save the genesis into? (default = poa-test.json)
>
INFO [01-30|17:01:14] Exported existing genesis block

What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components

ctrl+c 離開,會在當下目錄看到一個 paul-poa.json 檔案。

替 4 個節點初始化 Private chain

$ geth --datadir node1/data init poa-test.json
INFO [01-26|17:26:34] Allocated cache and file handles         database=/home/ubuntu/node1/data/geth/chaindata cache=16 handles=16
INFO [01-26|17:26:34] Writing custom genesis block
INFO [01-26|17:26:34] Successfully wrote genesis state         database=chaindata                              hash=3ff7db…89fc92
INFO [01-26|17:26:34] Allocated cache and file handles         database=/home/ubuntu/node1/data/geth/lightchaindata cache=16 handles=16
INFO [01-26|17:26:34] Writing custom genesis block
INFO [01-26|17:26:34] Successfully wrote genesis state         database=lightchaindata

對每個目錄做相同的事四次. 就完成了初始化的階段.

啟動 geth client 並設定 peers 間的連線

networkid => chainid

分別在 node1, node2 目錄使用指令啟動 geth

$ cd node1
$ geth --datadir ./data --networkid 63831 --port 2000 console
$ cd ..
$ cd node2 
$ geth --datadir ./data --networkid 63831 --port 2001 console

如果節點是授權打包 block 的節點,那你啟動時要先 unlock 你的 account,這樣才可以進行交易的打包。多帶一個 unlock 參數,以及你要解鎖的 account address。啟動後會要求輸入當時創 account 時的 passphrase。所以在這裡啟動 signer1 和 signer2 時都要用 unlock 參數帶入他們各自的 address 解鎖。

$ cd signer1
$ geth --datadir ./data --networkid 63831 --port 2002 --unlock 6327c9ee039293617b3ad00f121d29b4e3561dcb console
$ cd signer2
$ geth --datadir ./data --networkid 63831 --port 2003 --unlock 706b065493cc219ba94ec4b01382fde8a943f317 console
...
...
...
INFO [01-30|17:17:54] UDP listener up                          self=enode://7aa44eb3fbb2835b00ec9a99dce1097d6243b6db175da9ed1b98ce537723ee94d7937a6bb46d776ec4e899c700008bb6b4f59c016dcce863942b0fbcac314d09@[::]:2002
Unlocking account 6327c9ee039293617b3ad00f121d29b4e3561dcb | Attempt 1/3
Passphrase: INFO [01-30|17:17:54] RLPx listener up                         self=enode://7aa44eb3fbb2835b00ec9a99dce1097d6243b6db175da9ed1b98ce537723ee94d7937a6bb46d776ec4e899c700008bb6b4f59c016dcce863942b0fbcac314d09@[::]:2002
INFO [01-30|17:17:54] IPC endpoint opened: /home/ubuntu/signer1/data/geth.ipc

在 node2, signer1 的 geth console 頁面分別打入指令來加入節點 (geth 要連上對方的節點就必須好 enode://@:這格式, 複製剛剛啟動 node1 時出現的 enode 資訊將 [::] 換為 127.0.0.1 讓其他節點加入就可以連上了)

> admin.addPeer("enode://6a53a2def72bf8e81a21dc80ff008c57cd816f409bc41219498ad12f222a1d950f8396d262a1160dbcf5d254f3360447a4e92a601d18b02c986bbdc907abf72d@127.0.0.1:2000")
true

完成後回到 node1 的 geth console 打入 admin.peers 應該要看到三個節點資訊。

啟動 Miner

到 signer1 的 console 打入 miner.start() 這時候如果你本機之前沒有啟動過 miner,geth 會先產生 DAG 等 DAG 產生完後就會開始挖礦了。其他節點則會收到 import block 的訊息。

Reference