以太坊開發(二十八)在Windows伺服器上使用Geth同步以太坊主網區塊
1.前言
使用Geth同步以太坊主網區塊的文章已經很多了,這篇文章不會講的太詳細了。
我們將在Windows Server 2012 R2安裝Geth,啟動節點同步區塊,並且使用JSON RPC方式檢視節點和同步情況。
2. 安裝Geth
2.1 安裝Go
首先需要安裝Go語言環境。下載地址:ofollow,noindex">https://golang.org/dl/ 。
下載windows
版本然後安裝,我在D
盤根目錄下新建了一個資料夾go
,然後指定安裝在此資料夾中。系統會預設設定好環境變數。
檢查是否成功安裝:
PS C:\Users\Administrator> go version go version go1.11 windows/amd64
2.2 安裝Geth
下載地址:https://geth.ethereum.org/downloads/
。下載windows
版本的Geth然後安裝,我在D
盤根目錄下新建了一個資料夾geth
,然後指定安裝在此資料夾中。
檢查是否成功安裝:
PS C:\Users\Administrator> geth version Geth Version: 1.8.17-stable Git Commit: 8bbe72075e4e16442c4e28d999edee12e294329e Architecture: amd64 Protocol Versions: [63 62] Network Id: 1 Go Version: go1.11.1 Operating System: windows GOPATH=C:\Users\Administrator\go GOROOT=D:\go\
3. 啟動Geth
安裝完成後,在D
盤的geth
資料夾中有一個geth.exe
程式。如果直接雙擊,那麼geth會直接啟動並開始同步區塊,並且會將區塊資料儲存在C:\Users\Administrator\AppData\Roaming\Ethereum\geth\chaindata
中。區塊資料很大,會佔用系統盤,所以我們需要手動輸入指令啟動Geth,並指定區塊資料的儲存位置。
PS D:\geth> geth --identity "mainnet etherum" --datadir "./chain" --rpc --rpcapi "admin,web3,eth,personal,miner,net" --rpcadd r 0.0.0.0 --rpcport 8545 --cache=1024console
引數解釋:
-
--identity "mainnet etherum"
給節點起名 -
--datadir "./chain"
將區塊資料和keystore
檔案儲存在當前路徑的chain
檔案下 -
--rpc
開啟RPC服務 -
--rpcapi "admin,web3,eth,personal,miner,net"
註明要開啟哪些RPC服務 -
--rpcadd r 0.0.0.0 --rpcport 8545
指定RPC服務的IP和埠 -
--cache=1024
分配給Geth的記憶體,理論上越大同步速度越快,根據自己的配置來 -
console
開啟控制檯
如果一切正常,不出一會,命令列將顯示類似下面的輸出,表示正在同步區塊:
INFO [10-18|12:18:42.057] Imported new block headerscount=0 elapsed=142.004ms number=6296034 hash=0894db… 442bf0 age=1mo1w2dignored=192 INFO [10-18|12:18:42.426] Imported new block headerscount=0 elapsed=17.999msnumber=6296226 hash=49de5c… 9fa22f age=1mo1w2dignored=192 INFO [10-18|12:18:42.730] Imported new block headerscount=0 elapsed=31.249msnumber=6296418 hash=5025a3… 5a063f age=1mo1w2dignored=192 INFO [10-18|12:18:43.070] Imported new block receiptscount=2 elapsed=514.800ms number=6295844 hash=2b2caa… 80d5f8 age=1mo1w2dsize=271.18kB INFO [10-18|12:18:43.214] Imported new block receiptscount=8 elapsed=30.999msnumber=6295852 hash=80df4d… b6c7ee age=1mo1w2dsize=685.60kB INFO [10-18|12:18:43.591] Imported new block headerscount=0 elapsed=28.001msnumber=6296610 hash=b704f8… 5f6dd8 age=1mo1w2dignored=192 INFO [10-18|12:18:43.974] Imported new block receiptscount=46 elapsed=208.011ms number=6295898 hash=c952be …098c9a age=1mo1w2dsize=3.51mB INFO [10-18|12:18:44.163] Imported new block headerscount=0elapsed=250.013ms number=6296802 hash=42eea2 …2bc9c2 age=1mo1w2dignored=192 INFO [10-18|12:18:44.690] Imported new block receiptscount=6elapsed=15.000msnumber=6295904 hash=44c418 …cc1c93 age=1mo1w2dsize=388.41kB INFO [10-18|12:18:45.058] Imported new block receiptscount=46 elapsed=172.009ms number=6295950 hash=76f6c7 …7c0269 age=1mo1w2dsize=4.00mB INFO [10-18|12:18:44.870] Imported new block headerscount=0elapsed=30.000msnumber=6296994 hash=ecc2e9 …77e65b age=1mo1w2dignored=192
4. 新增節點
可能由於網路原因,啟動後會遲遲連線不上其他節點,導致無法同步區塊。可以新增星火節點。有兩種方式可以新增節點。
-
https://ethfans.org/wikis/
中找到
星火節點計劃超級節點列表
的頁面,下載static-nodes.json ,儲存在D:\geth\chain
中。如果已經啟動了Geth,需要重啟下
-
在Geth命令列中輸入指令
admin.addPeer(節點資訊)
,或者呼叫API請求(後面會講到)。返回true
表示新增成功
不過不管是上面哪種方式新增的節點,最終都沒有連線上。一般能連上1-4個節點。不過經過檢視,都不是匯入的節點。有人說在Linux上就可以,windows不行,這個有待驗證
5. 通過RPC API檢視節點和同步狀態
5.1web3_clientVersion
獲取當前客戶端版本資訊
post http://0.0.0.0:8545 method: web3_clientVersion params: {"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": "Geth/mainnet etherum/v1.8.17-stable-8bbe7207/windows-amd64/go1.11.1" } result:當前客戶端版本資訊
5.2web3_clientVersion
獲取同步狀態
post http://0.0.0.0:8545 method: eth_syncing params: {"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": { "currentBlock": "0x566683", "highestBlock": "0x63a229", "knownStates": "0x6c4ea8", "pulledStates": "0x6c3fc9", "startingBlock": "0x2f3add" } } currentBlock:已同步區塊號十六進位制 highestBlock:最新區塊號十六進位制
5.3net_peerCount
獲取當前連線節點數
post http://0.0.0.0:8545 method: net_peerCount params: {"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": "0x4" } result:當前已連線節點數
5.4net_version
獲取當前已連線網路的ID
post http://0.0.0.0:8545 method: net_version params: {"jsonrpc":"2.0","method":"net_version","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": "1" } result:當前已連線網路的ID。1為主網ID。
5.5net_version
獲取本地節點資訊
post http://0.0.0.0:8545 method: admin_nodeInfo params: {"jsonrpc":"2.0","method":"admin_nodeInfo","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": { "id": "64984e1c278b77993da1b08207837ba11d670c7e6d93aa6055890a94b62df5er", "name": "Geth/mainnet etherum/v1.8.17-stable-8bbe7207/windows-amd64/go1.11.1", "enode": "enode://d5529482408109611a2b9816d18cd39645b6361ecc69ed6cb84193bf87a2b4911e3cb78dc2968eac714007c9160835d096eab8f5c5ba238e6116b4bc365f09a2@181.223.111.118:30303", "ip": "183.222.121.113", "ports": { "discovery": 30303, "listener": 30303 }, "listenAddr": "[::]:30303", "protocols": { "eth": { "network": 1, "difficulty": 17179869184, "genesis": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "config": { "chainId": 1, "homesteadBlock": 1150000, "daoForkBlock": 1920000, "daoForkSupport": true, "eip150Block": 2463000, "eip150Hash": "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0", "eip155Block": 2675000, "eip158Block": 2675000, "byzantiumBlock": 4370000, "ethash": {} }, "head": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" } } } } result:當前已連線網路的ID。1為主網ID。
5.6admin_peers
獲取已連線節點資訊
post http://0.0.0.0:8545 method: admin_peers params: {"jsonrpc":"2.0","method":"admin_peers","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": [ { "enode": "enode://8a5f77665507bf6991537f08f0cac248a9a0edf2c7276920ee781741947039e1ff4223a188f5643d077353865d30fb8da4bf94606122edb4ed9ba9e0037d9cd1@200.108.64.94:30303", "id": "2eacb087ef9fea6d7945df6f6aedd608403c25fa725ac040d8f572f2e04e825c", "name": "Geth/v1.8.16-stable-477eb093/linux-amd64/go1.10", "caps": [ "eth/62", "eth/63" ], "network": { "localAddress": "10.0.1.110:59556", "remoteAddress": "200.108.64.94:30303", "inbound": false, "trusted": false, "static": false }, "protocols": { "eth": { "version": 63, "difficulty": 102970212347, "head": "0xf37c632d361e0a93f08ba29b1a2c708d9caa3ee19d1ee8d2a02612bffe49f0a9" } } }, { "enode": "enode://33c163eb46d4105606e275b2b867a49b6538e3681cf95b9990e7459293d4daa128bcb8f6fd0463b7fcacab55f36e987a06401d2999553eed93900ad640d818d0@124.248.48.105:35555", "id": "64a4f1ca60aa7fd9b94a488501eb8cd19617c850a5946489ae16a05ea0017897", "name": "Geth/v1.8.14-unstable-46d47215/linux-amd64/go1.9.4", "caps": [ "eth/63" ], "network": { "localAddress": "10.0.1.110:59807", "remoteAddress": "124.248.48.105:35555", "inbound": false, "trusted": false, "static": false }, "protocols": { "eth": { "version": 63, "difficulty": 7.252307770791149416925e+21, "head": "0xeb2182027c770464bcf9294ce6fc6c89f463fc527093215b3657f8ad8ad34159" } } }, { "enode": "enode://f6bc8c6c8bd6509b0e420222d318b561e84ec4585c9fc1e9a703d120f57e7502da3d940cb0044e1429db8898d75d49d22d69abe5aafb2b11043d8b55dd41ed61@34.239.162.121:30303", "id": "80d75f668d9202865a5eef26c052161e9dec539d63b5901844bf2ae6aaeb2f25", "name": "Geth/v1.8.15-omnibus-255989da/linux-amd64/go1.10.1", "caps": [ "eth/62", "eth/63" ], "network": { "localAddress": "10.0.1.110:59181", "remoteAddress": "34.239.162.121:30303", "inbound": false, "trusted": false, "static": false }, "protocols": { "eth": { "version": 63, "difficulty": 7.252333281014280363969e+21, "head": "0x517d365fc194212befd49b050c0438a522498e7c6a594a37612155f5e30ee3e2" } } }, { "enode": "enode://bcdce0388737748e94758373b3f1316b320d7846c663ac8cc413aca0e2b3072db8b4cc91543261f29e9f6be479df2caf4d01e2c528aa4615d79caaddd256cffe@47.100.213.201:30303", "id": "80fc3de11cfb64fd3e1f3fd811ec50edf4efc278658175c8cd3895d354600c1b", "name": "Geth/v1.8.13-unstable-526abe27/linux-amd64/go1.10.3", "caps": [ "eth/62", "eth/63" ], "network": { "localAddress": "10.0.1.110:59173", "remoteAddress": "47.100.213.201:30303", "inbound": false, "trusted": false, "static": false }, "protocols": { "eth": { "version": 63, "difficulty": 7.252253534510701838913e+21, "head": "0x17af0a63587777f7cc9d3053a3433d7135cad21f65f0ef0132e073efed686d17" } } } ] } result:已連線節點資訊
5.7admin_datadir
獲取區塊資料的儲存路徑
post http://0.0.0.0:8545 method: admin_datadir params: {"jsonrpc":"2.0","method":"admin_datadir","params":[],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": "D:\\geth\\chain" } result:區塊資料儲存路徑
5.8admin_addPeer
新增連線節點
post http://0.0.0.0:8545 method: admin_addPeer params: {"jsonrpc":"2.0","method":"admin_addPeer","params":["enode://6427b7e7446bb05f22fe7ce9ea175ec05858953d75a5a6e4f99a6aec0779a8bd6276f1959a42fe5948acbe14bcd0652082dc546d3b37ae8f2aea41eba4eca43b@121.202.15.182:30303"],"id":1} returns: { "jsonrpc": "2.0", "id": 1, "result": true } result:true表示新增成功