在寫這篇教程之前,我是連 cmd 都不知道使用的技術小白,在官方文檔、技術博客、公司技術同事的幫助下,硬磕了1、2天才從0到1構建了一個ETH網絡。以下是我嘔心瀝血整理的教程,應該是網上最小白的教程了。
1. 下載并安裝geth,支持Windows、Mac、Linux、源碼
安裝包中包含Geth和開發者工具包
? Geth = 以太坊節點進程 + 與區塊鏈交互的命令行客戶端 + 密鑰管理與簽名工具;
? 開發者工具包,包含以下:
安裝成功后,在本地指定目錄新增以下程序:
安裝成功后,geth會自動加入到系統環境變量中,因此可以直接打開Windows CMD輸入 geth version,查看版本號,以此證明安裝成功。
C:\Users\user>geth version
Geth
Version: 1.9.12-stable
Git Commit: b6f1c8dcc058a936955eb8e5766e2962218924bc
Git Commit Date: 20200316
Architecture: amd64
Protocol Versions: [65 64 63]
Go Version: go1.13.8
Operating System: windows
GOPATH=
GOROOT=C:\go
2. 在啟動geth程序之前,先創建以太坊公私鑰對,打開Windows cmd, 輸入 geth account new , 輸入密碼,為了安全,輸入的密碼是不會在cmd上顯示出來的, 成功后,錢包地址會顯示出來,私鑰文件自動存在本地默認目錄下,并且再次文字強調要保管好私鑰。
C:\Users\user>geth account new
INFO [04-10|17:58:44.410] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:
Your new key was generated
Public address of the key:? 0xAb4a26Ca4cD52a84d6fc9fb2D7472D2C228742F7
Path of the secret key file: C:\Users\user\AppData\Local\Ethereum\keystore\UTC--2020-04-10T09-58-59.166758700Z--ab4a26ca4cd52a84d6fc9fb2d7472d2c228742f7
- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
可以創建多個公私鑰對,私鑰都是保存在同一個文件夾中。輸入 geth account list,可以將這個文件夾中所有的公私鑰對顯示出來。
C:\Users\user>geth account list
INFO [04-10|18:27:17.007] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50
Account #0: {ab4a26ca4cd52a84d6fc9fb2d7472d2c228742f7} keystore://C:\Users\user\AppData\Local\Ethereum\keystore\UTC--2020-04-10T09-58-59.166758700Z--ab4a26ca4cd52a84d6fc9fb2d7472d2c228742f7
Account #1: {820118cd4a04278c773ad30606798d84ce3578e9} keystore://C:\Users\user\AppData\Local\Ethereum\keystore\UTC--2020-04-10T10-25-07.390348400Z--820118cd4a04278c773ad30606798d84ce3578e9
3. 如果需要部署的是私鏈,可以手動創建創世的json文件,也可以用開發者工具中的puppeth,命令行交互式的完成配置,然后在同級目錄下生成一份js文件。支持配置的選項有:鏈的名字、鏈ID、POA算法還是POW算法、預充值賬戶、允許挖礦的賬戶等,如下:
+-----------------------------------------------------------+
| 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, hyphens or capital letters please)
> myethtestnet
Sweet, you can set this via --network=myethtestnet next time!
?[32mINFO ?[0m[04-11|17:01:54.956] Administering Ethereum network? ? ? ? ? ?[32mname?[0m=myethtestnet
?[33mWARN ?[0m[04-11|17:01:55.141] No previous configurations found? ? ? ? ?[33mpath?[0m=.puppeth\\myethtestnet
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
What would you like to do? (default = create)
1. Create new genesis from scratch
2. Import already existing genesis
> 1
Which consensus engine to use? (default = clique)
1. Ethash - proof-of-work
2. Clique - proof-of-authority
> 2
//最好選擇POA算法,出塊時間固定,ETH最大測試網rinkeby用的就是POA算法
How many seconds should blocks take? (default = 15)
> 5
//設置5秒出個塊,對應json文件clique:{"period"}
Which accounts are allowed to seal? (mandatory at least one)
> 0xAb4a26Ca4cD52a84d6fc9fb2D7472D2C228742F7
> 0x820118Cd4a04278c773aD30606798D84cE3578e9
> 0xaFdd64dc5A421cB35ceC78eC991e153045Ca2aB8
> 0x
//一個機器也可以部署多個節點,3個節點基本上可以覆蓋全部測試場景
Which accounts should be pre-funded? (advisable at least one)
> 0xAb4a26Ca4cD52a84d6fc9fb2D7472D2C228742F7
> 0x820118Cd4a04278c773aD30606798D84cE3578e9
> 0xaFdd64dc5A421cB35ceC78eC991e153045Ca2aB8
> 0x
Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)
> no
// 如果選 yes,那么從 0x0000000000000000000000000000000000000000 到
// 0x00000000000000000000000000000000000000ff 這些地址都會有 1wei 的以太幣。
// 這個看情況選擇,用得著就選 yes
Specify your chain/network ID if you want an explicit one (default = random)
>
?[32mINFO ?[0m[04-11|17:12:53.755] Configured new 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
>
但是 geth 在讀取這個配置文件時,只會取到「“genesis”」這個字段,后面的字段不會取到,所以要將 "genesis" 字段的內容作為這個 json 文件的內容(相當于刪除{ "genesis":這段字符和文件末尾最后一個})。
3. 創建節點數據存儲的文件夾,并用創世文件初始化節點。創建文件夾可以通過CMD自帶的 mkdir 實現,也可以直接在GUI的文件夾下創建。指定節點數據存儲位置用 datadir 選項,讀取創世配置文件初始化節點用 init 命令。
C:\Users\user>e:
E:\>cd myethtestnet
//我希望節點數據存儲在E盤,所以先指定根目錄
E:\myethtestnet>geth --datadir ./node1? init ./mygenesis
INFO [04-11|17:39:37.240] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50
INFO [04-11|17:39:37.687] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node1\\geth\\chaindata cache=16.00MiB handles=16
INFO [04-11|17:39:37.807] Writing custom genesis block
INFO [04-11|17:39:37.820] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=998.4μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|17:39:37.841] Successfully wrote genesis state? ? ? ? database=chaindata? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd
INFO [04-11|17:39:37.854] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node1\\geth\\lightchaindata cache=16.00MiB handles=16
INFO [04-11|17:39:38.080] Writing custom genesis block
INFO [04-11|17:39:38.088] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=1.0142ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|17:39:38.110] Successfully wrote genesis state? ? ? ? database=lightchaindata? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd
//初始化第一個節點node1
E:\myethtestnet>geth --datadir ./node2? init ./mygenesis
INFO [04-11|17:40:50.190] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50
INFO [04-11|17:40:50.627] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node2\\geth\\chaindata cache=16.00MiB handles=16
INFO [04-11|17:40:50.746] Writing custom genesis block
INFO [04-11|17:40:50.753] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=999.8μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|17:40:50.765] Successfully wrote genesis state? ? ? ? database=chaindata? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd
INFO [04-11|17:40:50.770] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node2\\geth\\lightchaindata cache=16.00MiB handles=16
INFO [04-11|17:40:50.879] Writing custom genesis block
INFO [04-11|17:40:50.887] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=997.8μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|17:40:50.910] Successfully wrote genesis state? ? ? ? database=lightchaindata? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd
//初始化第二個節點node2
E:\myethtestnet>geth --datadir ./node3? init ./mygenesis
INFO [04-11|17:41:08.357] Maximum peer count? ? ? ? ? ? ? ? ? ? ? ETH=50 LES=0 total=50
INFO [04-11|17:41:08.781] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node3\\geth\\chaindata cache=16.00MiB handles=16
INFO [04-11|17:41:08.937] Writing custom genesis block
INFO [04-11|17:41:08.946] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=1.0007ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|17:41:08.974] Successfully wrote genesis state? ? ? ? database=chaindata? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd
INFO [04-11|17:41:08.989] Allocated cache and file handles? ? ? ? database=E:\\myethtestnet\\node3\\geth\\lightchaindata cache=16.00MiB handles=16
INFO [04-11|17:41:09.112] Writing custom genesis block
INFO [04-11|17:41:09.118] Persisted trie from memory database? ? ? nodes=4 size=657.00B time=0s? ? ? gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-11|17:41:09.146] Successfully wrote genesis state? ? ? ? database=lightchaindata? ? ? ? ? ? ? ? ? ? ? ? ? hash=838c17…ced2bd
//初始化第三個節點node3
初始化節點成功后,會在指定目錄下增加兩個文件夾:
其中 geth/chaindata 中存放的是區塊數據,keystore 中存放的是賬戶數據
4. 配置節點P2P發現網絡。新節點要接入P2P網絡,至少要發現網絡中的一個節點。加入公共網絡,直接啟動節點即可,因為節點代碼中已經寫死了boot節點;創建私網,需要用 --networkid="12345" 連接上已知節點;或者將靜態節點的地址寫到 static-nodes.json 文件中,始終通過靜態節點發現網絡。我們采用靜態節點發現網絡的方式,將3個節點的地址全部加到文件中。
先需要知道節點的P2P地址,查詢方法,運行geth(記得用 --datadir 指定數據存儲位置),運行后按 Ctrl+C 停止運行:
E:\myethtestnet>geth --datadir ./node2
……
……
INFO [04-11|18:20:59.174] Started P2P networking? ? ? ? ? ? ? ? ? self=enode://c1f667baf27075879900ef48b90c55842c7dfcf2fc63710fdd36955438131087fe259156c0b4595a4fb7ad293b982609ee81edab321f08f61617b02d554dec71@127.0.0.1:30303
在運行過程中輸出的log中,有這么一行:enode:……@127.0.0.1:30303。就是該節點的P2P地址,注意P2P地址不是IP地址。
接著創建static-nodes.json 文件,文件名一定要一致,按照以下格式輸入三個節點的地址:
[
? "enode://f4642fa65af50cfdea8fa7414a5def7bb7991478b768e296f5e4a54e8b995de102e0ceae2e826f293c481b5325f89be6d207b003382e18a8ecba66fbaf6416c0@33.4.2.1:30303",
? "enode://pubkey@ip:port"
]
注意,3個節點的端口(port)號不能一樣。
注意,一定要將json文件放在節點目錄下,如:E:\myethtestnet\node3
5. 啟動節點,使用以下命令:
geth --datadir ./node1 --unlock 0 --mine --rpc
geth --datadir ./node2 --unlock 0 --mine --port 30304
geth --datadir ./node3 --unlock 0 --mine --port 30305
--mine,有該選項表示該節點支持挖礦;
--rpc,有該選項表示該節點啟動rpc功能,除了第一個節點,其它節點都沒有啟動 rpc 功能。一是因為 rpc 功能有一個節點啟動就夠用了;二是因為多個節點啟動 rpc 功能,rpc 端口被第一個啟動的節點占用,后面的啟動就會報失?。?/p>
--port,指定了P2P連接的端口,需要與 static-nodes.json 文件中的端口一致,且不同節點不能重復;
節點啟動,默認是按照fast節點模式運行。補充說明一下ETH共有三種節點模式:
? Full: Downloads all blocks (including headers, transactions and receipts) and generates the state of the blockchain incrementally by executing every block.
? Fast (Default): Downloads all blocks (including headers, transactions and receipts), verifies all headers, and downloads the state and verifies it against the headers.
? Light: Downloads all block headers, block data, and verifies some randomly.